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.SurProjectUserinfo;
import com.yanzhu.jh.project.service.ISurProjectService;
import com.yanzhu.jh.project.domain.SurProjectBuildNodeData;
import com.yanzhu.jh.project.service.ISurProjectBuildNodeDataService;
import com.yanzhu.jh.project.service.ISurProjectUserinfoService;
import org.aspectj.weaver.loadtime.Aj;
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/project")
public class ProjectController {

    @Autowired
    private ISurProjectService isurProjectService;

    @Autowired
    private ISurProjectBuildNodeDataService surProjectBuildNodeDataService;

    @Autowired
    private ISurProjectUserinfoService surProjectUserinfoService;

    @Autowired
    private RedisCache redisCache;


    @GetMapping("findProjectByDept")
    public AjaxResult findProjectByDept(Long deptId){
        SurProject surProject=new SurProject();
        if(deptId!=null &&  deptId.longValue()>0){
            surProject.setDeptId(deptId);
        }
        return AjaxResult.success(isurProjectService.selectSurProjectList(surProject));
    }

    @GetMapping("getProjectBuildNode")
    public AjaxResult getProjectBuildNode(Long projectId){
        SurProjectBuildNodeData nodeData=new SurProjectBuildNodeData();
        nodeData.setProjectId(projectId);
        List list=surProjectBuildNodeDataService.selectSurProjectBuildNodeDataList(nodeData);
        if(list.size()>0){
            return AjaxResult.success(list);
        }
        surProjectBuildNodeDataService.addProjectNodes(projectId);
        list=surProjectBuildNodeDataService.selectSurProjectBuildNodeDataList(nodeData);
        return AjaxResult.success(list);
    }

    /**
     * 获取进度显示的项目
     * @return
     */
    @GetMapping("getProgressProjects")
    public AjaxResult getProgressProjects(long deptId){
        String key="bgscreen_prj_getProgressProjects_"+deptId;
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return AjaxResult.success(obj);
        }

        List<SurProject> list= isurProjectService.selectProgressProjects(deptId);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return AjaxResult.success(list);
    }

    @GetMapping("groupByProjectCategory")
    public AjaxResult groupByProjectCategory(long deptId){
        String key="bgscreen_prj_groupByProjectCategory_"+deptId;
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return AjaxResult.success(obj);
        }
        List<SurProject> list= isurProjectService.groupByProjectCategory(deptId);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return AjaxResult.success(list);
    }

    @PostMapping("groupByCategory")
    public AjaxResult groupByCategory(@RequestBody SurProject surProject){
        String key="bgscreen_prj_groupByProjectCategory_"+surProject.getId()+"_"+surProject.getDeptId();
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return AjaxResult.success(obj);
        }
        List<SurProject> list=isurProjectService.groupByCategory(surProject);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return AjaxResult.success(list);
    }

    @PostMapping("groupByLevel")
    public AjaxResult groupByLevel(@RequestBody SurProject surProject){
        String key="bgscreen_prj_groupByLevel_"+surProject.getId()+"_"+surProject.getDeptId();
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return AjaxResult.success(obj);
        }
        List<SurProject> list=isurProjectService.groupByLevel(surProject);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return AjaxResult.success(list);
    }

    @GetMapping("/getProjectUser")
    public AjaxResult getProjectUser(Long projectId){
        SurProjectUserinfo where=new SurProjectUserinfo();
        where.setProjectId(projectId);
        return AjaxResult.success( surProjectUserinfoService.selectSurProjectUserinfoList(where));
    }
}
