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


import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.yanzhu.jh.trouble.domain.SmzSspProblemmodify;
import com.yanzhu.jh.trouble.domain.where.SmzSspProblemmodifyWhere;
import com.yanzhu.jh.trouble.service.ISmzSspProblemmodifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/bgscreen/problemmodify")
public class ProblemmodifyController extends BaseController {
    @Autowired
    private RedisCache redisCache;


    @Autowired
    private ISmzSspProblemmodifyService smzSspProblemmodifyService;

    /**
     * 质量整改-安全分类汇总(按分类)
     * @param deptId
     * @param roleType
     * @return
     */
    @PostMapping("/safetySummary")
    public List<SmzSspProblemmodify> safetySummary(@RequestBody SmzSspProblemmodifyWhere where)
    {
        //3集团//4子公司//5监理单位//6总包//7分包
        where.setInfoType(0l);
        String startDate=where.getStartDate()==null?"":DateUtil.format(where.getStartDate(),"yyyy-MM-dd");
        String endDate=where.getEndDate()==null?"":DateUtil.format(where.getEndDate(),"yyyy-MM-dd");
        String key="problemmodify_safetySummary-"+where.getDeptId()+"-"+where.getRoleType()+"_"+where.getProjectId()+"-"+startDate+"_"+endDate;
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return (List<SmzSspProblemmodify>) obj;
        }
        List<SmzSspProblemmodify> list = smzSspProblemmodifyService.selectSummary(where);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return list;
    }


    /**
     * 质量整改-质量分类汇总(按分类)
     * @param deptId
     * @param roleType
     * @return
     */
    @PostMapping("/qualitySummary")
    public List<SmzSspProblemmodify> qualitySummary(@RequestBody SmzSspProblemmodifyWhere where)
    {
        //3集团//4子公司//5监理单位//6总包//7分包
        where.setInfoType(1l);
        String startDate=where.getStartDate()==null?"":DateUtil.format(where.getStartDate(),"yyyy-MM-dd");
        String endDate=where.getEndDate()==null?"":DateUtil.format(where.getEndDate(),"yyyy-MM-dd");
        String key="problemmodify_qualitySummary-"+where.getDeptId()+"-"+where.getRoleType()+"_"+where.getProjectId()+"-"+startDate+"_"+endDate;
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return (List<SmzSspProblemmodify>) obj;
        }
        List<SmzSspProblemmodify> list = smzSspProblemmodifyService.selectSummary(where);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return list;
    }


    /**
     * 质量整改-安全分类汇总(按项目)
     * @param deptId
     * @param roleType
     * @return
     */
    @PostMapping("/safetySummaryByProject")
    public  List<SmzSspProblemmodify>  safetySummaryByProject(@RequestBody SmzSspProblemmodifyWhere where)
    {
        //3集团//4子公司//5监理单位//6总包//7分包
        where.setInfoType(0l);
        String startDate=where.getStartDate()==null?"":DateUtil.format(where.getStartDate(),"yyyy-MM-dd");
        String endDate=where.getEndDate()==null?"":DateUtil.format(where.getEndDate(),"yyyy-MM-dd");
        String key="problemmodify_safetySummaryByProject-"+where.getDeptId()+"-"+where.getRoleType()+"_"+where.getProjectId()+"-"+startDate+"_"+endDate;
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return (List<SmzSspProblemmodify>) obj;
        }
        List<SmzSspProblemmodify> list = smzSspProblemmodifyService.selectSummaryByProject(where);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return list;
    }


    /**
     * 质量整改-安全分类汇总(按项目)
     * @param deptId
     * @param roleType
     * @return
     */
    @PostMapping("/qualitySummaryByProject")
    public  List<SmzSspProblemmodify>  qualitySummaryByProject(@RequestBody SmzSspProblemmodifyWhere where)
    {
        //3集团//4子公司//5监理单位//6总包//7分包 int deptId,int roleType,int projectId
        where.setInfoType(1l);
        String startDate=where.getStartDate()==null?"":DateUtil.format(where.getStartDate(),"yyyy-MM-dd");
        String endDate=where.getEndDate()==null?"":DateUtil.format(where.getEndDate(),"yyyy-MM-dd");
        String key="problemmodify_qualitySummaryByProject-"+where.getDeptId()+"-"+where.getRoleType()+"_"+where.getProjectId()+"-"+startDate+"_"+endDate;

        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return (List<SmzSspProblemmodify>) obj;
        }
        List<SmzSspProblemmodify> list = smzSspProblemmodifyService.selectSummaryByProject(where);
        redisCache.setCacheObject(key, list, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return list;
    }

    @PostMapping("/countByDate")
    public AjaxResult countByDate(@RequestBody SmzSspProblemmodifyWhere where){
        List<SmzSspProblemmodify> list=smzSspProblemmodifyService.countByDate(where);
        return AjaxResult.success(list);
    }

    @PostMapping("/countByDateRange")
    public AjaxResult countByDateRange(@RequestBody SmzSspProblemmodifyWhere where){
        List<SmzSspProblemmodify> list=smzSspProblemmodifyService.countByDateRange(where);
        return AjaxResult.success(list);
    }
    @PostMapping("/groupByInfotypeCheckState")
    public AjaxResult groupByInfotypeCheckState(@RequestBody SmzSspProblemmodifyWhere where){
        List<SmzSspProblemmodify> list=smzSspProblemmodifyService.groupByInfotypeCheckState(where);
        SmzSspProblemmodify obj=new SmzSspProblemmodify();
        obj.setId(smzSspProblemmodifyService.countTimeout(where));
        obj.setCheckState(5l);
        obj.setInfoType(where.getInfoType());
        list.add(obj);
        return AjaxResult.success(list);
    }

    /**
     * 项目监测预警查询，用于大屏项目概况
     * @param deptId
     * @return
     */
    @GetMapping("/getMonitAndWarning")
    public AjaxResult getMonitAndWarning(long deptId,long projectId){
        String key="problemmodify_getMonitAndWarning-"+deptId+"_"+projectId;
        Object obj=redisCache.getCacheObject(key);
        if(obj!=null){
            return AjaxResult.success(obj);
        }
        SmzSspProblemmodifyWhere where=new SmzSspProblemmodifyWhere();
        where.setDeptId(deptId);
        where.setProjectId(projectId);
        where.setStartDate(new Date());
        Map<String,  List<SmzSspProblemmodify>> map=new HashMap<>();
        map.put("today",smzSspProblemmodifyService.countByDate(where));
        Date[] dts=DateUtils.getCurrentWeekDate();
        where.setStartDate(dts[0]);
        where.setEndDate(dts[1]);
        map.put("week",smzSspProblemmodifyService.countByDateRange(where));
        map.put("group",smzSspProblemmodifyService.groupByInfotypeCheckState(where));
        redisCache.setCacheObject(key, map, Constants.BIGSCREEN_QUERY_CACHE, TimeUnit.MINUTES);
        return AjaxResult.success(map);
    }

    @PostMapping("/listSspProblemmodify")
    public AjaxResult listSspProblemmodify(@RequestBody SmzSspProblemmodify where){
        return  AjaxResult.success(smzSspProblemmodifyService.selectSmzSspProblemmodifyList(where));
    }
}
