package com.zcloud.controller.check;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.logs.LogAnno;
import com.zcloud.service.check.CheckUserService;
import com.zcloud.service.check.ListManagerService;
import com.zcloud.service.statistics.ListStatisticsService;
import com.zcloud.service.system.DepartmentService;
import com.zcloud.service.system.UsersService;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.Tools;
import org.apache.shiro.authz.annotation.RequiresPermissions;
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;

/**
 * 说明：人员工作业绩
 * 作者：luoxiaobao
 * 时间：2020-12-30
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/jobEvaluation")
public class JobEvaluationController extends BaseController {
    @Autowired
    private UsersService usersService;
    @Autowired
    private ListManagerService listManagerService;
    @Autowired
    private ListStatisticsService listStatisticsService;
    @Autowired
    private CheckUserService checkUserService;
    @Autowired
    private DepartmentService departmentService;

    /**
     * 公司人员业绩
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/count")
    // @RequiresPermissions("jobEvaluation:list")
    @ResponseBody
    public Object jobEvaluation() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        pd.put("ISMAIN", Jurisdiction.getIS_MAIN());
        pd.put("ISSUPERVISE", Jurisdiction.getISSUPERVISE());
        pd.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());
        String DEPTIDS = pd.getString("DEPTIDS");
        if (Tools.notEmpty(DEPTIDS)) {
            String DEPT_IDS[] = DEPTIDS.split(",");
            pd.put("DEPT_IDS", DEPT_IDS);
        }
        List<PageData> userList = usersService.listUserDepPos(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 = listManagerService.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 = listStatisticsService.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 = checkUserService.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;
    }

    /**
     * 公司人员业绩
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/counlistPaget")
    // @RequiresPermissions("jobEvaluation:list")
    @ResponseBody
    @LogAnno(menuType = "双重预控", menuServer = "持续改进", instructionsOperate = "企业报告", instructionsType = "公司人员业绩")
    public Object jobEvaluationlistPage(Page page) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = this.getPageData();
        PageData npd = new PageData();
        npd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
        npd.put("ISMAIN", Jurisdiction.getIS_MAIN());
        npd.put("ISSUPERVISE", Jurisdiction.getISSUPERVISE());
        npd.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());
        String DEPTIDS = pd.getString("DEPTIDS");
        if (Tools.notEmpty(DEPTIDS)) {
            String DEPT_IDS[] = DEPTIDS.split(",");
            npd.put("DEPT_IDS", DEPT_IDS);
        }
        pd.put("USER_ID", Jurisdiction.getUSER_ID());
        PageData cpd = usersService.findById(pd);
        npd.put("ISLEADER", cpd.getString("ISLEADER"));
        if (cpd.getString("ISLEADER") != null && "1".equals(cpd.getString("ISLEADER"))) {
            String DEPARTMENT_ID = npd.getString("DEPARTMENT_ID");
            String ids = departmentService.getDEPARTMENT_IDS(DEPARTMENT_ID);
            ids = npd.getString("DEPARTMENT_ID") + "," + ids;   //把自己部门插入进去
            if (ids != null && Tools.notEmpty(ids) && ids.lastIndexOf(",") > -1) {
                ids = ids.substring(0, ids.lastIndexOf(","));
                npd.put("DEPARTMENT_IDS", ids.split(","));
            } else {
                npd.put("DEPARTMENT_IDS", DEPARTMENT_ID);
            }
        } else {
            if ("0".equals(npd.getString("ISMAIN"))) {
                npd.put("USER_ID", Jurisdiction.getUSER_ID());
            }
        }
        String KEYWORDS = pd.getString("KEYWORDS"); // 关键词检索条件
        if (Tools.notEmpty(KEYWORDS)) npd.put("KEYWORDS", KEYWORDS.trim());
        page.setPd(npd);
        List<PageData> userList = usersService.listUserDepPoslist(page);

        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 = listManagerService.countListByUser(pd);
            if (lists != null && !lists.isEmpty()) {
                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 = listStatisticsService.countStaByUser(pd);
            if (stas != null && !stas.isEmpty()) {
                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 = checkUserService.countCheByUser(pd);
            if (ches != null && !ches.isEmpty()) {
                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("page", page);
        map.put("result", errInfo);
        return map;
    }

}
