package com.zcloud.controller.bus;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.api.EducationApiService;
import com.zcloud.service.api.OtherApiService;
import com.zcloud.service.bus.CorpInfoService;
import com.zcloud.service.api.PreventionApiService;
import com.zcloud.service.bus.QualificationsService;
import com.zcloud.service.system.CorpScoreService;
import com.zcloud.service.system.PostService;
import com.zcloud.service.system.UsersService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fangjiakai
 * @date 2023/07/28 15:42
 */
@Controller
@RequestMapping("/corpDigital")
public class CorpDigitalController extends BaseController {
    @Autowired
    private CorpInfoService corpinfoService;
    @Autowired
    private PreventionApiService preventionApiService;
    @Autowired
    private EducationApiService educationApiService;
    @Autowired
    private OtherApiService otherApiService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private PostService postService;
    @Autowired
    private QualificationsService qualificationsService;
    @Autowired
    private CorpScoreService corpScoreService;

    /**根据企业ID获取数据
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/getDiagnosis")
    @ResponseBody
    public Object getDiagnosis() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        PageData cpd = corpinfoService.findById(pd);	//根据ID读取
        PageData riskpd = preventionApiService.getDiagnosis(pd);
        List<PageData> lists = preventionApiService.listAllListsForDiagnosis(pd);
        List<PageData> thismonth = preventionApiService.listDaterangeListmanager(pd);
        List<PageData> hdlist = preventionApiService.listHiddenForDiagnosis(pd);
        PageData studypd = educationApiService.getDiagnosis(pd);

        map.put("cpd", cpd);
        map.put("monthlist",thismonth);
        map.put("riskpd", riskpd);
        map.put("lists", lists);
        map.put("hdlist", hdlist);
        map.put("studypd", studypd);
        map.put("result", errInfo);
        return map;
    }

    /**公司人员业绩
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/jobEvaluationlist")
    @ResponseBody
    public Object jobEvaluationlist() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData> userList = usersService.listUserDeptPostlist(pd);

        if(userList != null && userList.size() > 0) {
            List<String> uIdList = new ArrayList<String>();
            Map<String, String> listMap = new HashMap<String, String>();//查询负责清单数
            Map<String, PageData> countMap = new HashMap<String, PageData>();//查询负责清单数
            List<PageData> lists = preventionApiService.countListByUser(pd);
            if(lists != null && lists.size() > 0) {
                for(PageData list : lists) {
                    if(countMap.containsKey(list.get("USER_ID").toString())) {
                        countMap.get(list.get("USER_ID").toString()).put("listCount", list.get("NUM").toString());
                    } else {
                        PageData count = new PageData();
                        count.put("listCount", list.get("NUM").toString());
                        countMap.put(list.get("USER_ID").toString(), count);
                    }
                    listMap.put(list.get("USER_ID").toString(), list.get("NUM").toString());
                }
            }

            Map<String, String> staMap = new HashMap<String, String>();//查询负责清单数
            List<PageData> stas = preventionApiService.countStaByUser(pd);
            if(stas != null && stas.size() > 0) {
                for(PageData sta : stas) {
                    if(countMap.containsKey(sta.get("USER_ID").toString())) {
                        countMap.get(sta.get("USER_ID").toString()).put("staCount", sta.get("NUM").toString());
                    } else {
                        PageData count = new PageData();
                        count.put("staCount", sta.get("NUM").toString());
                        countMap.put(sta.get("USER_ID").toString(), count);
                    }
                    staMap.put(sta.get("USER_ID").toString(), sta.get("NUM").toString());
                }
            }
            Map<String, String> cheMap = new HashMap<String, String>();//查询检查清单数
            List<PageData> ches = preventionApiService.countCheByUser(pd);
            if(ches != null && ches.size() > 0) {
                for(PageData che : ches) {
                    if(countMap.containsKey(che.get("USER_ID").toString())) {
                        countMap.get(che.get("USER_ID").toString()).put("cheCount", che.get("NUM").toString());
                    } else {
                        PageData count = new PageData();
                        count.put("cheCount", che.get("NUM").toString());
                        countMap.put(che.get("USER_ID").toString(), count);
                    }
                    cheMap.put(che.get("USER_ID").toString(), che.get("NUM").toString());
                }
            }

            for(PageData u : userList) {
                if(countMap.containsKey(u.get("USER_ID").toString())) {
                    u.put("listCount", countMap.get(u.get("USER_ID").toString()).get("listCount")==null?0:countMap.get(u.get("USER_ID").toString()).get("listCount"));
                    u.put("staCount", countMap.get(u.get("USER_ID").toString()).get("staCount")==null?0:countMap.get(u.get("USER_ID").toString()).get("staCount"));
                    u.put("cheCount", countMap.get(u.get("USER_ID").toString()).get("cheCount")==null?0:countMap.get(u.get("USER_ID").toString()).get("cheCount"));
                } else {
                    u.put("listCount", 0);
                    u.put("staCount", 0);
                    u.put("cheCount", 0);
                }
                if("0".equals(u.get("staCount").toString())) {
                    u.put("percent", "0.00%");
                    u.put("percentNum", 0d);
                } else {
                    Double staCount = Double.valueOf(u.get("staCount").toString());
                    Double cheCount = Double.valueOf(u.get("cheCount").toString());
                    Double per = cheCount/staCount*100;
                    DecimalFormat df   = new DecimalFormat("######0.00");
                    u.put("percent", df.format(per)+"%");
                    u.put("percentNum", per);
                }
            }
        }
        userList.sort((y, x) -> Double.compare(Double.valueOf(x.get("percentNum").toString()), Double.valueOf(y.get("percentNum").toString())));
        map.put("varList", userList);
        map.put("pd", pd);
        map.put("result", errInfo);
        return map;
    }


    /**列表
     * @throws Exception
     */
    @RequestMapping(value="/studytaskList")
    @ResponseBody
    public Object studytaskList() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData>	varList = educationApiService.studytaskList(pd);	//列出StudyTask列表
        map.put("varList", varList);
        map.put("result", errInfo);
        return map;
    }

    /**根据企业ID获取数据
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/getEightWorks")
    @ResponseBody
    public Object getEightWorks() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData> eightlist = otherApiService.getEightWorks(pd);
        map.put("eightlist", eightlist);
        map.put("result", errInfo);
        return map;
    }

    /**根据企业ID获取风险点
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/getRiskSta")
    @ResponseBody
    public Object getRiskSta() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        PageData riskpd = preventionApiService.getDiagnosis(pd);
        map.put("riskpd", riskpd);
        map.put("result", errInfo);
        return map;
    }


    /**根据企业ID获取风险点
     * @param
     * @throws Exception
     */
    @RequestMapping(value="/goCorpInfo")
    @ResponseBody
    public Object goCorpInfo() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        PageData cpd = preventionApiService.goCorpInfoBasicInfo(pd);
        List<PageData> users = usersService.listAllUser(pd);
        List<PageData> posts = postService.listAll(pd);
        List<PageData> files = qualificationsService.listAll(pd);

        List<PageData> thismonth = preventionApiService.listDaterangeListmanager(pd);
        List<PageData> hdlist = preventionApiService.listHiddenForDiagnosis(pd);
        List<PageData> ches = preventionApiService.countCheByUser(pd);

        List<PageData> riskImg = preventionApiService.listAllRiskImg(pd);

        PageData scorepd = corpScoreService.findById(pd);

        map.put("cpd", cpd);
        map.put("users", users);
        map.put("posts", posts);
        map.put("files", files);
        map.put("riskImg", riskImg);

        map.put("thismonth", thismonth);
        map.put("hdlist", hdlist);
        map.put("ches", ches);

        map.put("scorepd", scorepd);

        map.put("result", errInfo);
        return map;
    }

    /**
     * 隐患半年统计
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/hiddenSta")
    @ResponseBody
    public Object hiddenSta() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<String> dates = DateUtil.getHalfYearMonth(true);
        Map<String, Object> dateMap = new HashMap<>();
        pd.put("TYPE", 1);
        pd.put("TIME", dates.get(0));
        List<PageData> allHidden = preventionApiService.getHiddenNum(pd);

        Map<String, PageData> allMap = new HashMap<>();
        for(int i = 0; i < allHidden.size(); i++) {
            allMap.put(allHidden.get(i).getString("TIME"), allHidden.get(i));
        }
        pd.put("TYPE", 2);
        List<PageData> wzgHidden = preventionApiService.getHiddenNum(pd);
        Map<String, PageData> wzgMap = new HashMap<>();
        for(int i = 0; i < wzgHidden.size(); i++) {
            wzgMap.put(wzgHidden.get(i).getString("TIME"), wzgHidden.get(i));
        }
        pd.put("TYPE", 3);
        List<PageData> yzgHidden = preventionApiService.getHiddenNum(pd);
        Map<String, PageData> yzgMap = new HashMap<>();
        for(int i = 0; i < yzgHidden.size(); i++) {
            yzgMap.put(yzgHidden.get(i).getString("TIME"), yzgHidden.get(i));
        }
        pd.put("TYPE", 4);
        List<PageData> yysHidden = preventionApiService.getHiddenNum(pd);
        Map<String, PageData> yysMap = new HashMap<>();
        for(int i = 0; i < yysHidden.size(); i++) {
            yysMap.put(yysHidden.get(i).getString("TIME"), yysHidden.get(i));
        }
        List<String> names = new ArrayList<String>();
        List<String> all = new ArrayList<String>();
        List<String> wzg = new ArrayList<String>();
        List<String> yzg = new ArrayList<String>();
        List<String> yys = new ArrayList<String>();
        for(int i = 0; i < dates.size(); i++) {
            names.add(dates.get(i).substring(dates.get(i).length()-2, dates.get(i).length())+"月");
            if(allMap.containsKey(dates.get(i))){
                PageData hidden = allMap.get(dates.get(i));
                all.add(hidden.get("NUMBER").toString());
            } else {
                all.add("0");
            }
            if(wzgMap.containsKey(dates.get(i))){
                PageData hidden = wzgMap.get(dates.get(i));
                wzg.add(hidden.get("NUMBER").toString());
            } else {
                wzg.add("0");
            }
            if(yzgMap.containsKey(dates.get(i))){
                PageData hidden = yzgMap.get(dates.get(i));
                yzg.add(hidden.get("NUMBER").toString());
            } else {
                yzg.add("0");
            }
            if(yysMap.containsKey(dates.get(i))){
                PageData hidden = yysMap.get(dates.get(i));
                yys.add(hidden.get("NUMBER").toString());
            } else {
                yys.add("0");
            }
        }
        map.put("result", errInfo);
        map.put("names", names);
        map.put("all", all);
        map.put("wzg", wzg);
        map.put("yzg", yzg);
        map.put("yys", yys);
        return map;
    }

    /**列表
     * @throws Exception
     */
    @RequestMapping(value="/performanceexamine_dept")
    @ResponseBody
    public Object performanceexamineDept() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData>	varList = preventionApiService.examineDeptList(pd);	//列出PerformanceExamine列表
        map.put("varList", varList);
        map.put("result", errInfo);
        return map;
    }

    /**列表
     * @throws Exception
     */
    @RequestMapping(value="/performanceexamine_user")
    @ResponseBody
    public Object performanceexamineUser() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData>	varList = preventionApiService.examineUserList(pd);	//列出PerformanceExamine列表
        map.put("varList", varList);
        map.put("result", errInfo);
        return map;
    }

    /**列表
     * @throws Exception
     */
    @RequestMapping(value="/eduMonthSta")
    @ResponseBody
    public Object eduMonthSta() throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        PageData	epd = educationApiService.eduMonthSta(pd);
        map.put("epd", epd);
        map.put("result", errInfo);
        return map;
    }
}
