package org.jeecg.modules.stm.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.IgnoreAuth;
import org.jeecg.modules.stm.metric.entity.MetricMonitorSet;
import org.jeecg.modules.stm.metric.entity.MetricReport;
import org.jeecg.modules.stm.metric.service.IMetricMonitorSetService;
import org.jeecg.modules.stm.metric.service.IMetricReportService;
import org.jeecg.modules.stm.my.entity.MyTask;
import org.jeecg.modules.stm.my.service.IMyTaskService;
import org.jeecg.modules.stm.page.entity.PageMonitorchart;
import org.jeecg.modules.stm.page.entity.PageTitleconfig;
import org.jeecg.modules.stm.page.service.IPageMonitorchartService;
import org.jeecg.modules.stm.page.service.IPageTitleconfigService;
import org.jeecg.modules.stm.rectify.approve.entity.StRectifyApprove;
import org.jeecg.modules.stm.rectify.approve.service.IStRectifyApproveService;
import org.jeecg.modules.stm.rectify.assin.entity.StProblemTaskAssin;
import org.jeecg.modules.stm.rectify.assin.service.IStProblemTaskAssinService;
import org.jeecg.modules.stm.rectify.exec.service.IStRectifyExecService;
import org.jeecg.modules.stm.rectify.plan.entity.StRectifyPlan;
import org.jeecg.modules.stm.rectify.plan.service.IStRectifyPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


@RestController
@RequestMapping("/api/bigscreen/")

public class BigScreenAPIController {
    @Autowired
    private IPageTitleconfigService pageTitleconfigService;
    @Autowired
    private IMyTaskService myTaskService;
    @Autowired
    private IStProblemTaskAssinService stProblemTaskAssinService;
    @Autowired
    private IStRectifyApproveService stRectifyApproveService;
    @Autowired
    private IStRectifyExecService stRectifyExecService;
    @Autowired
    private IStRectifyPlanService iStRectifyPlanService;
    @Autowired
    private IPageMonitorchartService pageMonitorchartService;
    @Autowired
    private IMetricReportService metricReportService;
    @Autowired
    private IMetricMonitorSetService metricMonitorSetService;

    @IgnoreAuth
    @GetMapping(value = "/getBaseInfoList")
    public Result<Map<Integer,List<PageTitleconfig>>> getBaseInfoList() {
        QueryWrapper<PageTitleconfig> queryWrapperCfg = new QueryWrapper<>();
        queryWrapperCfg.orderByAsc("show_sort");
        List<PageTitleconfig> pageList = pageTitleconfigService.list(queryWrapperCfg);
        Map<Integer,List<PageTitleconfig>> retMap = new HashMap<>();
        List<PageTitleconfig> orgList = new ArrayList<>();
        List<PageTitleconfig> techList = new ArrayList<>();

        List<Map<String, String>> retList = new ArrayList<>();
        if(pageList!=null && pageList.size()!=0) {
            List<Map<String,Object>> listData =pageTitleconfigService.getFirstPageFxcStat();
            Map<Integer,String> tmpMap = new HashMap<>();
            for (Map<String,Object> cnt : listData) {
                Integer dtId = Integer.valueOf(cnt.get("itemid").toString());
                if (tmpMap.get(dtId) != null) {
                    continue;
                }
                if(cnt.get("content")!=null){
                    tmpMap.put(dtId, cnt.get("content").toString());
                }
            }
            for(PageTitleconfig cfg:pageList) {
                Integer dtId = cfg.getTplItemDtId();
                String val = tmpMap.get(dtId);
                if(val!=null&&!val.equals("null")) {
                    cfg.setDefaultVal(val);
                }
                if(cfg.getTp().intValue()==1) {
                    orgList.add(cfg);
                } else {
                    techList.add(cfg);
                }
            }
        }
        retMap.put(1,orgList);
        retMap.put(2,techList);
        return Result.OK(retMap);
    }

    @GetMapping(value = "/getMyTask")
    public Result<List> getMyTask() {
        //获取待办事项列表，待审批和待填报
        QueryWrapper<MyTask> queryWrapper = new QueryWrapper();
        queryWrapper.eq("sta",0).orderByDesc("priority");
        queryWrapper.last("limit 4");
        List<MyTask> list = myTaskService.list(queryWrapper);
        return Result.OK(list);
    }

    @RequestMapping(value = "/getPageStatProblemLevel")
    public  Result<?> getPageStatProblemLevel() {
        List<Map<String,Object>> list = stProblemTaskAssinService.statProblemLevel();
        return Result.ok(list);
    }

    @RequestMapping(value = "/problemStat")
    public Result<?> problemStat(HttpServletResponse response) {
        //@ApiModelProperty(value = "审核标志;0 未开始 1 待审核、2 已完成、3 已驳回")
        long total =  stProblemTaskAssinService.count();
        //接收即确认的问题数
        QueryWrapper<StProblemTaskAssin> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("receive_status","1").or().eq("assin_status","0").or().isNull("receive_status");
        //未分配的
        long noAssignNm =  stProblemTaskAssinService.count(queryWrapper);  //已经分配,但未接收

        QueryWrapper<StRectifyApprove> queryApproveWrapper = new QueryWrapper<>();
        queryApproveWrapper.eq("approve_flag","3");
        long finishNm =  stRectifyApproveService.count(queryApproveWrapper);

        long execNm = stRectifyExecService.count();

        QueryWrapper<StRectifyPlan> queryPlanWrapper = new QueryWrapper<>();
        queryPlanWrapper.ne("edit_flag","1");
        long planNm = iStRectifyPlanService.count(queryPlanWrapper);

        Map<String,Object> retMap = new LinkedHashMap<>();
        retMap.put("total",total);
        retMap.put("execNm",(execNm-finishNm));
        retMap.put("finishNm",finishNm);
        retMap.put("planNm",planNm);
        retMap.put("noAssignNm",noAssignNm);

        return Result.OK(retMap);
    }

    @RequestMapping(value = "/getPageChartData")
    public Result<?> getPageCharData(HttpServletRequest request, HttpServletResponse response) {
        List<PageMonitorchart> listData =  pageMonitorchartService.list();
        return Result.OK(listData);
    }


    @GetMapping(value = "/getMtricDataByMtrcNo")
    public Result<?> getMtricDataByMtrcNo(String mtrcNo) {

        QueryWrapper<MetricReport> queryWrapper = new QueryWrapper();
        queryWrapper.eq("mtrc_no",mtrcNo);
        //queryWrapper.ge("fill_time",mc.getYear());
        queryWrapper.orderByAsc("fill_time");
        List<MetricReport> metrcList  =  metricReportService.list(queryWrapper);
        Map<String,Object> retMap = new HashMap<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        List<Float> retList = new ArrayList<>();
        List<Object> retWarnList = new ArrayList<>();
        List<Object> retIntvList = new ArrayList<>();
        List<Object> retTolList = new ArrayList<>();
        List<String> category = new ArrayList<>();

        MetricMonitorSet metrcSet = getMetricMonitorSet(mtrcNo);
        Float warnVal = metrcSet.getWarnVal();
        Float intvVal = metrcSet.getIntvVal();

        if(warnVal!=null){
            for(MetricReport mr:metrcList){
                retWarnList.add(warnVal);
            }
            retMap.put("warn",retWarnList);
        }
        if(intvVal!=null){
            for(MetricReport mr:metrcList){
                retIntvList.add(intvVal);
            }
            retMap.put("intv",retIntvList);
        }
        for(MetricReport mr:metrcList){
            if(mr.getFillVals()==null)continue;
            category.add(sdf.format(mr.getFillTime()));
            retList.add(Float.parseFloat(mr.getFillVals()));
            retTolList.add(metrcSet.getTolVal());
        }
        retMap.put("mtrc",retList);
        retMap.put("tol",retTolList);
        retMap.put("category",category);
        retMap.put("unit",metrcSet.getUnit());

        if(retList.size()>0) {
            float max = Collections.max(retList);
            float min = Collections.min(retList);
            List<Float> tmpList = new ArrayList<>();
            tmpList.add(max);
            tmpList.add(min);
            if(warnVal!=null) {
                tmpList.add(warnVal);
            }
            if(intvVal!=null) {
                tmpList.add(intvVal);
            }
            max = Collections.max(tmpList);
            min = Collections.min(tmpList);
            retMap.put("min",min);
            retMap.put("max",max);
        }
        retMap.put("interval",metrcSet.getUnit());
        return Result.OK(retMap);
    }

    private MetricMonitorSet getMetricMonitorSet(String mtricNo) {
        QueryWrapper<MetricMonitorSet> queryWrapper = new QueryWrapper();
        queryWrapper.eq("mtrc_no",mtricNo);
        MetricMonitorSet metrcSet =metricMonitorSetService.getOne(queryWrapper);
        return metrcSet;
    }

    @GetMapping(value = "/getMtricDataMultMtrc")
    public Result<?> getMtricDataMultMtrc() {
        Map<String,List<Map<String,Object>>> retMap = new HashMap<>();
        List<Map<String,Object>> listData =  pageMonitorchartService.queryMulMetrcSta();

        Map<String, List<Map<String, Object>>> groupedByMtrcNo = listData.stream()
                .collect(Collectors.groupingBy(record -> (String) record.get("mtrcNo")));
        return Result.OK(groupedByMtrcNo);
    }

    @GetMapping(value = "/getBaosongJggm")
    public Map<String,String> getBaosongJggm() {
        Map<String,String> retMap = new HashMap<>();
        retMap.put("C23A001","888");
        retMap.put("C23A002","999");
        return retMap;
    }

    @GetMapping(value = "/getBaosongDsh")
    public List<Map<String,String>> getBaosongDsh() {

        List<Map<String,String>> retList = new ArrayList<>();
        Map<String,String> map1 = new HashMap<>();
        map1.put("code","C20A001");
        map1.put("type","number");
        map1.put("unit","次");
        map1.put("val","10");
        map1.put("name","本年度董事会召开涉及信息科技议题的会议数量");
        retList.add(map1);
        Map<String,String> map2 = new HashMap<>();
        map2.put("code","C20A003");
        map2.put("type","boolean");
        map2.put("val","是");
        map2.put("unit","");
        map2.put("name","本年度董事会（含授权的风险管理委员会）审阅信息科技风险管理报告");
        retList.add(map2);
        return retList;
    }

}




