package com.yanzhu.jh.bigscreen.web.controller;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.yanzhu.jh.project.domain.SurProject;
import com.yanzhu.jh.project.domain.SurProjectCostOutput;
import com.yanzhu.jh.project.service.ISurProjectCostOutputService;
import com.yanzhu.jh.project.service.ISurProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/bgscreen/costOut")
public class ProjectCostOutputController {
    @Autowired
    private ISurProjectCostOutputService surProjectCostOutputService;
    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISurProjectService projectService;

    @PostMapping("/selectYearAndMonth")
    public AjaxResult selectYearAndMonth(@RequestBody SurProjectCostOutput where){
        String key="bgscreen_costOut_selectYearAndMonth_"+where.getYear()+"-"+where.getMonth()+"-"+where.getProjectId();
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return AjaxResult.success(obj);
        }
        List<SurProjectCostOutput> list=surProjectCostOutputService.selectYearAndMonth(where);
        if(list.size()==0){
            surProjectCostOutputService.initProject(where.getProjectId());
        }
        list=surProjectCostOutputService.selectYearAndMonth(where);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return AjaxResult.success(list);
    }

    @PostMapping("/sumByDeptId")
    public AjaxResult sumByDeptId(@RequestBody SurProjectCostOutput where){
        String key="bgscreen_costOut_sumByDeptId_"+where.getYear()+"-"+where.getMonth()+"-"+where.getId()+"-"+where.getProjectId();
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return AjaxResult.success(obj);
        }
        List<SurProjectCostOutput> list=surProjectCostOutputService.sumByDeptId(where);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return AjaxResult.success(list);
    }


}
