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.SurProjectBuildNodeData;
import com.yanzhu.jh.project.service.ISurProjectBuildNodeDataService;
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/projectBuildNode")

public class ProjectBuildNodeController {

    @Autowired
    ISurProjectBuildNodeDataService surProjectBuildNodeDataService;
    @Autowired
    private RedisCache redisCache;
    /**
     * 节点计划预警
     * @param where
     * @return
     */
    @PostMapping("/selectScheduledAlerts")
    public AjaxResult selectScheduledAlerts(@RequestBody SurProjectBuildNodeData where){
        return AjaxResult.success(surProjectBuildNodeDataService.selectScheduledAlerts(where));
    }

    /**
     * 节点完成率统计-进度管理
     * @param where
     * @return
     */
    @PostMapping("/countCompletionRate")
    public AjaxResult countCompletionRate(@RequestBody SurProjectBuildNodeData where){
        return AjaxResult.success(surProjectBuildNodeDataService.countCompletionRate(where));
    }
    /**
     * 获取当前节点及叶子节点
     * @param where
     * @return
     */
    @PostMapping("/selectCurrent")
    public AjaxResult selectCurrent(@RequestBody SurProjectBuildNodeData where){
        return AjaxResult.success(surProjectBuildNodeDataService.selectCurrent(where));
    }

    /**
     * 获取项目的计划节点
     * @param projectId
     * @return
     */
    @GetMapping("/listByProject")
    public AjaxResult listByProject(long projectId){
        SurProjectBuildNodeData surProjectBuildNodeData=new SurProjectBuildNodeData();
        surProjectBuildNodeData.setProjectId(projectId);
        List list=surProjectBuildNodeDataService.selectSurProjectBuildNodeDataList(surProjectBuildNodeData);
        if(list.size()>0){
            return AjaxResult.success(list);
        }
        surProjectBuildNodeDataService.addProjectNodes(projectId);
        list=surProjectBuildNodeDataService.selectSurProjectBuildNodeDataList(surProjectBuildNodeData);
        return AjaxResult.success(list);
    }

    /**
     * 按项目类型查询计划节点数据
     * @param type
     * @return
     */
    @GetMapping("queryByProjectType")
    public AjaxResult queryByProjectType(long type){
        String key="bgscreen_projectBuildNode_queryByProjectType_"+type;
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return AjaxResult.success(obj);
        }
        List list=surProjectBuildNodeDataService.queryByProjectType(type);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return AjaxResult.success(list);

    }
}
