package com.zcloud.controller.check;

import com.alibaba.fastjson.JSONObject;
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.CheckRecordService;
import com.zcloud.service.check.ListCheckItemService;
import com.zcloud.service.check.ListManagerService;
import com.zcloud.service.check.RecordItemService;
import com.zcloud.service.risk.RiskPointService;
import com.zcloud.service.system.DepartmentService;
import com.zcloud.service.system.UsersService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.PLSUtil;
import com.zcloud.util.ReturnMap;
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.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：清单管理
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/app/listmanager")
public class AppListManagerController extends BaseController {

    @Autowired
    private ListManagerService listmanagerService;
    @Autowired
    private ListCheckItemService listcheckitemService;
    @Autowired
    private CheckRecordService checkrecordService;
    @Autowired
    private RecordItemService recordItemService;
    @Autowired
    private RiskPointService riskpointService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private UsersService usersService;

    /**
     * 列表
     *
     * @throws Exception
     */
    @RequestMapping(value = "/checkList")
    @ResponseBody
    @LogAnno(menuType = "手机", menuServer = "风险排查", instructionsOperate = "清单管理", instructionsType = "列表")
    public Object checkList() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        String USER_NAME = pd.getString("USER_NAME");
        PageData cpd = new PageData();
        cpd = usersService.findById(pd);
        pd.put("ISLEADER", cpd.getString("ISLEADER"));
        PageData dept = new PageData();
        dept.put("DEPARTMENT_ID", pd.getString("CHECK_DEPARTMENT_ID"));
        dept = departmentService.findById(dept);
        if (dept != null && dept.getString("ISSUPERVISE").equals("1")) {
            pd.put("SUP_DEPARTMENT_ID", dept.getString("DEPARTMENT_ID"));
            pd.put("ISSUPERVISE", dept.getString("ISSUPERVISE"));
        } else {
            pd.put("DEPARTMENT_ID", pd.getString("CHECK_DEPARTMENT_ID"));
        }
        if (cpd.getString("ISLEADER") != null && cpd.getString("ISLEADER").equals("1")) {
            String DEPARTMENT_ID = pd.getString("CHECK_DEPARTMENT_ID");
            String ids = departmentService.getDEPARTMENT_IDS(DEPARTMENT_ID);
            ids = pd.getString("CHECK_DEPARTMENT_ID") + "," + ids;   //把自己部门插入进去
            if (ids != null && Tools.notEmpty(ids) && ids.lastIndexOf(",") > -1) {
                ids = ids.substring(0, ids.lastIndexOf(","));
                pd.put("DEPARTMENT_IDS", ids.split(","));
            } else {
                pd.put("DEPARTMENT_IDS", DEPARTMENT_ID);
            }
        }
        pd.put("orderUserId", pd.get("USER_ID"));
        List<PageData> varList = listmanagerService.listAll(pd);//列出ListManager列表
        for (PageData list : varList) {
            String cycle = list.getString("PERIODNAME");
            if (cycle.equals("每日")) {
                list.put("START_DATE", DateUtil.getDay());
                list.put("END_DATE", DateUtil.getDay());
            } else if (cycle.equals("每周")) {
                list.put("START_DATE", DateUtil.getWeekStart().substring(0, 10));
                list.put("END_DATE", DateUtil.getWeekEnd().substring(0, 10));
            } else if (cycle.equals("每旬")) {
                //1-10
                if (DateUtil.timeCalendar(new Date(), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "01 00:00:00"), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "10 23:59:59"))) {
                    list.put("START_DATE", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "01");
                    list.put("END_DATE", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "10");
                }
                //10-20
                if (DateUtil.timeCalendar(new Date(), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "11 00:00:00"), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "20 23:59:59"))) {
                    list.put("START_DATE", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "11");
                    list.put("END_DATE", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "20");
                }
                //20-月底
                if (DateUtil.timeCalendar(new Date(), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "21 00:00:00"), DateUtil.fomatDateTime(DateUtil.getMonthEndDay() + " 23:59:59"))) {
                    list.put("START_DATE", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "21");
                    list.put("END_DATE", DateUtil.getMonthEndDay());
                }

            } else if (cycle.equals("每月")) {
                list.put("START_DATE", DateUtil.getMonthFirstDay());
                list.put("END_DATE", DateUtil.getMonthEndDay());
            } else if (cycle.equals("每季")) {
                list.put("START_DATE", DateUtil.quarterStart());
                list.put("END_DATE", DateUtil.quarterEnd());
            } else if (cycle.equals("半年")) {
                Integer bmonth = Integer.parseInt(DateUtil.getMonth().toString());
                if (bmonth <= 6) {
                    list.put("START_DATE", DateUtil.getYear() + "-01-01");
                    list.put("END_DATE", DateUtil.getYear() + "-06-30");
                } else if (bmonth > 6) {
                    list.put("START_DATE", DateUtil.getYear() + "-07-01");
                    list.put("END_DATE", DateUtil.getYear() + "-12-31");
                }
            } else if (cycle.equals("每年")) {
                list.put("START_DATE", DateUtil.getCurrYearFirst());
                list.put("END_DATE", DateUtil.getCurrYearLast());
            }
        }
        map.put("varList", varList);
        map.put("result", errInfo);
        return map;
    }


    /**
     * 列表(监管部门权限)
     *
     * @throws Exception
     */
    @RequestMapping(value = "/checkListNew")
    @ResponseBody
    @LogAnno(menuType = "手机", menuServer = "隐患排查", instructionsOperate = "清单管理", instructionsType = "监管部门权限列表")
    public Object checkListNew() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        String USER_NAME = pd.getString("USER_NAME");

        String DEPARTMENT_ID = pd.getString("CHECK_DEPARTMENT_ID");
//		String ids = departmentService.getDEPARTMENT_IDS(DEPARTMENT_ID);
//		if(pd.getString("IS_MAIN").equals("0")) {
//			if(ids!=null && Tools.notEmpty(ids)&& ids.lastIndexOf(",")>-1) {
//				ids = ids.substring(0,ids.lastIndexOf(","));
//				pd.put("DEPARTMENT_IDS", ids.split(","));
//			}else {
//				pd.put("DEPARTMENT_ID", DEPARTMENT_ID);
//			}
//		}
        pd.put("DEPARTMENT_ID", DEPARTMENT_ID);
        pd.put("orderUserId", pd.get("USER_ID"));
        List<PageData> varList = listmanagerService.appListAll(pd);//列出ListManager列表
        for (PageData list : varList) {
            String cycle = list.getString("PERIODNAME");
            if (cycle.equals("每日")) {
                list.put("START_DATE", DateUtil.getDay());
                list.put("END_DATE", DateUtil.getDay());
            } else if (cycle.equals("每周")) {
                list.put("START_DATE", DateUtil.getWeekStart().substring(0, 10));
                list.put("END_DATE", DateUtil.getWeekEnd().substring(0, 10));
            } else if (cycle.equals("每旬")) {
                //1-10
                if (DateUtil.timeCalendar(new Date(), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "01 00:00:00"), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "10 23:59:59"))) {
                    list.put("START_DATE", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "01");
                    list.put("END_DATE", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "10");
                }
                //10-20
                if (DateUtil.timeCalendar(new Date(), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "11 00:00:00"), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "20 23:59:59"))) {
                    list.put("START_DATE", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "11");
                    list.put("END_DATE", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "20");
                }
                //20-月底
                if (DateUtil.timeCalendar(new Date(), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "21 00:00:00"), DateUtil.fomatDateTime(DateUtil.getMonthEndDay() + " 23:59:59"))) {
                    list.put("START_DATE", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "21");
                    list.put("END_DATE", DateUtil.getMonthEndDay());
                }

            } else if (cycle.equals("每月")) {
                list.put("START_DATE", DateUtil.getMonthFirstDay());
                list.put("END_DATE", DateUtil.getMonthEndDay());
            } else if (cycle.equals("每季")) {
                list.put("START_DATE", DateUtil.quarterStart());
                list.put("END_DATE", DateUtil.quarterEnd());
            } else if (cycle.equals("半年")) {
                Integer bmonth = Integer.parseInt(DateUtil.getMonth().toString());
                if (bmonth <= 6) {
                    list.put("START_DATE", DateUtil.getYear() + "-01-01");
                    list.put("END_DATE", DateUtil.getYear() + "-06-30");
                } else if (bmonth > 6) {
                    list.put("START_DATE", DateUtil.getYear() + "-07-01");
                    list.put("END_DATE", DateUtil.getYear() + "-12-31");
                }
            } else if (cycle.equals("每年")) {
                list.put("START_DATE", DateUtil.getCurrYearFirst());
                list.put("END_DATE", DateUtil.getCurrYearLast());
            }
        }
        map.put("varList", varList);
        map.put("result", errInfo);
        return map;
    }

    /**
     * 列表
     *
     * @param page
     * @throws Exception
     */
    @RequestMapping(value = "/recordList")
    @ResponseBody
    @LogAnno(menuType = "手机", menuServer = "隐患排查", instructionsOperate = "清单管理", instructionsType = "列表")
    public Object recordList(Page page) 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("USER_ID",Jurisdiction.getUSER_ID());

        String KEYWORDS = pd.getString("KEYWORDS");                        //关键词检索条件
        if (Tools.notEmpty(KEYWORDS)) pd.put("KEYWORDS", KEYWORDS.trim());
        String STARTTIME = pd.getString("STARTTIME");                        //关键词检索条件
        if (Tools.notEmpty(STARTTIME)) pd.put("STARTTIME", STARTTIME.trim() + " 00:00:00");
        String ENDTIME = pd.getString("ENDTIME");                        //关键词检索条件
        if (Tools.notEmpty(ENDTIME)) pd.put("ENDTIME", ENDTIME.trim() + " 23:59:59");
        PageData dept = new PageData();
        dept.put("DEPARTMENT_ID", pd.get("DEPARTMENT_ID").toString());
        dept = departmentService.findById(dept);
        PageData cpd = new PageData();
        cpd = usersService.findById(pd);
        pd.put("ISLEADER", cpd.getString("ISLEADER"));
        if (dept.getString("ISSUPERVISE").equals("1")) {
            pd.put("SUP_DEPARTMENT_ID", dept.getString("DEPARTMENT_ID"));
            List<PageData> subdept = departmentService.findBySubId(pd);  //查询监管哪些部门
            String subIds = "";  //用于存储监管部门ID
            for (int i = 0; i < subdept.size(); i++) {
                subIds += subdept.get(i).getString("SUB_DEPARTMENT_ID") + ",";
            }
            pd.put("DEPARTMENT_IDS", subIds.split(","));
        } else {
            pd.put("DEPARTMENT_ID", pd.getString("DEPARTMENT_ID"));

        }
        pd.put("ISSUPERVISE", dept.getString("ISSUPERVISE"));
        if (cpd.getString("ISLEADER") != null && cpd.getString("ISLEADER").equals("1")) {
            String DEPARTMENT_ID = pd.getString("DEPARTMENT_ID");
            String ids = departmentService.getDEPARTMENT_IDS(DEPARTMENT_ID);
            ids = pd.getString("DEPARTMENT_ID") + "," + ids;   //把自己部门插入进去
            if (ids != null && Tools.notEmpty(ids) && ids.lastIndexOf(",") > -1) {
                ids = ids.substring(0, ids.lastIndexOf(","));
                pd.put("DEPARTMENT_IDS", ids.split(","));
            } else {
                pd.put("DEPARTMENT_IDS", DEPARTMENT_ID);
            }
        }
        page.setPd(pd);
        List<PageData> varList = listmanagerService.recordList(page);    //列出ListManager列表
        for (PageData list : varList) {
            String userId = list.getString("USER_ID");
            if (userId.equals(pd.get("USER_ID").toString())) {
                list.put("stateMy", "0");
            } else {
                list.put("stateMy", "1");
            }
        }
        map.put("varList", varList);
        map.put("page", page);
        map.put("result", errInfo);
        return map;
    }


    /**
     * 获取清单风险点
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/getRisk")
    @ResponseBody
    @LogAnno(menuType = "手机", menuServer = "隐患排查", instructionsOperate = "清单管理", instructionsType = "获取清单风险点")
    public Object getRisk() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        String USER_ID = pd.getString("USER_NAME");
        List<PageData> varList = listmanagerService.listAllRisk(pd);
        map.put("varList", varList);
        pd = listmanagerService.findById(pd);
        if (pd.getString("TYPE").equals("listType0005")) {
            if (DateUtil.timeCalendar(new Date(), DateUtil.fomatDateTime(pd.getString("START_DATE") + " 00:00:00"), DateUtil.fomatDateTime(pd.getString("END_DATE") + " 23:59:59"))) {
                PageData type = new PageData();
                type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
                type.put("IS_XUN", 1);//查询方法和旬一样,直接借用
                type.put("FINISHED", '0');
                type.put("USER_ID", USER_ID);
                type.put("BEGINTIME", pd.getString("START_DATE") + " 00:00:00");
                type.put("ENDTIME", pd.getString("END_DATE") + " 23:59:59");
                List<PageData> records = checkrecordService.listAllUnfinished(type);
                if (records != null && records.size() > 0) {
                    PageData record = new PageData();
                    record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                    List<PageData> checkedList = recordItemService.listAll(record);
                    Map<String, Integer> checkmap = new HashMap<String, Integer>();//记录所有风险点
                    for (PageData item : checkedList) {
                        if (!checkmap.containsKey(item.getString("IDENTIFICATION_ID"))) {
                            checkmap.put(item.getString("IDENTIFICATION_ID"), 1);
                        } else {
                            checkmap.put(item.getString("IDENTIFICATION_ID"), checkmap.get(item.getString("IDENTIFICATION_ID")) + 1);
                        }
                    }
                    map.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                    map.put("checkmap", checkmap);
                }
            }
            map.put("result", errInfo);
            return map;
        }
        String cycle = pd.getString("PERIODNAME");
        if (cycle.equals("每日")) {
            PageData type = new PageData();
            type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
            type.put("IS_DAY", 1);
            type.put("FINISHED", '0');
            type.put("USER_ID", USER_ID);
            List<PageData> records = checkrecordService.listAllUnfinished(type);
            pd.put("checkCount", records == null ? 0 : records.size());
            if (records != null && records.size() > 0) {
                PageData record = new PageData();
                record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                List<PageData> checkedList = recordItemService.listAll(record);
                Map<String, Integer> checkmap = new HashMap<String, Integer>();//记录所有风险点
                for (PageData item : checkedList) {
                    if (!checkmap.containsKey(item.getString("IDENTIFICATION_ID"))) {
                        checkmap.put(item.getString("IDENTIFICATION_ID"), 1);
                    } else {
                        checkmap.put(item.getString("IDENTIFICATION_ID"), checkmap.get(item.getString("IDENTIFICATION_ID")) + 1);
                    }
                }
                map.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                map.put("checkmap", checkmap);
            }
        } else if (cycle.equals("每周")) {
            PageData type = new PageData();
            type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
            type.put("IS_WEEK", 1);
            type.put("FINISHED", '0');
            type.put("USER_ID", USER_ID);
            List<PageData> records = checkrecordService.listAllUnfinished(type);
            pd.put("checkCount", records == null ? 0 : records.size());
            if (records != null && records.size() > 0) {
                PageData record = new PageData();
                record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                List<PageData> checkedList = recordItemService.listAll(record);
                Map<String, Integer> checkmap = new HashMap<String, Integer>();//记录所有风险点
                for (PageData item : checkedList) {
                    if (!checkmap.containsKey(item.getString("IDENTIFICATION_ID"))) {
                        checkmap.put(item.getString("IDENTIFICATION_ID"), 1);
                    } else {
                        checkmap.put(item.getString("IDENTIFICATION_ID"), checkmap.get(item.getString("IDENTIFICATION_ID")) + 1);
                    }
                }
                map.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                map.put("checkmap", checkmap);
            }
        } else if (cycle.equals("每旬")) {
            //1-10
            if (DateUtil.timeCalendar(new Date(), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "01 00:00:00"), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "10 23:59:59"))) {
                PageData type = new PageData();
                type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
                type.put("IS_XUN", 1);
                type.put("FINISHED", '0');
                type.put("USER_ID", USER_ID);
                type.put("BEGINTIME", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "01 00:00:00");
                type.put("ENDTIME", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "10 23:59:59");
                List<PageData> records = checkrecordService.listAllUnfinished(type);
                if (records != null && records.size() > 0) {
                    PageData record = new PageData();
                    record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                    List<PageData> checkedList = recordItemService.listAll(record);
                    Map<String, Integer> checkmap = new HashMap<String, Integer>();//记录所有风险点
                    for (PageData item : checkedList) {
                        if (!checkmap.containsKey(item.getString("IDENTIFICATION_ID"))) {
                            checkmap.put(item.getString("IDENTIFICATION_ID"), 1);
                        } else {
                            checkmap.put(item.getString("IDENTIFICATION_ID"), checkmap.get(item.getString("IDENTIFICATION_ID")) + 1);
                        }
                    }
                    map.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                    map.put("checkmap", checkmap);
                }
            }
            //10-20
            if (DateUtil.timeCalendar(new Date(), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "11 00:00:00"), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "20 23:59:59"))) {
                PageData type = new PageData();
                type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
                type.put("IS_XUN", 1);
                type.put("FINISHED", '0');
                type.put("USER_ID", USER_ID);
                type.put("BEGINTIME", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "11 00:00:00");
                type.put("ENDTIME", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "20 23:59:59");
                List<PageData> records = checkrecordService.listAllUnfinished(type);
                if (records != null && records.size() > 0) {
                    PageData record = new PageData();
                    record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                    List<PageData> checkedList = recordItemService.listAll(record);
                    Map<String, Integer> checkmap = new HashMap<String, Integer>();//记录所有风险点
                    for (PageData item : checkedList) {
                        if (!checkmap.containsKey(item.getString("IDENTIFICATION_ID"))) {
                            checkmap.put(item.getString("IDENTIFICATION_ID"), 1);
                        } else {
                            checkmap.put(item.getString("IDENTIFICATION_ID"), checkmap.get(item.getString("IDENTIFICATION_ID")) + 1);
                        }
                    }
                    map.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                    map.put("checkmap", checkmap);
                }
            }
            //20-月底
            if (DateUtil.timeCalendar(new Date(), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "21 00:00:00"), DateUtil.fomatDateTime(DateUtil.getMonthEndDay() + " 23:59:59"))) {
                PageData type = new PageData();
                type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
                type.put("IS_XUN", 1);
                type.put("FINISHED", '0');
                type.put("USER_ID", USER_ID);
                type.put("BEGINTIME", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "21 00:00:00");
                type.put("ENDTIME", DateUtil.getMonthEndDay() + " 23:59:59");
                List<PageData> records = checkrecordService.listAllUnfinished(type);
                if (records != null && records.size() > 0) {
                    PageData record = new PageData();
                    record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                    List<PageData> checkedList = recordItemService.listAll(record);
                    Map<String, Integer> checkmap = new HashMap<String, Integer>();//记录所有风险点
                    for (PageData item : checkedList) {
                        if (!checkmap.containsKey(item.getString("IDENTIFICATION_ID"))) {
                            checkmap.put(item.getString("IDENTIFICATION_ID"), 1);
                        } else {
                            checkmap.put(item.getString("IDENTIFICATION_ID"), checkmap.get(item.getString("IDENTIFICATION_ID")) + 1);
                        }
                    }
                    map.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                    map.put("checkmap", checkmap);
                }
            }

        } else if (cycle.equals("每月")) {
            PageData type = new PageData();
            type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
            type.put("IS_MONTH", 1);
            type.put("FINISHED", '0');
            type.put("USER_ID", USER_ID);
            List<PageData> records = checkrecordService.listAllUnfinished(type);
            pd.put("checkCount", records == null ? 0 : records.size());
            if (records != null && records.size() > 0) {
                PageData record = new PageData();
                record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                List<PageData> checkedList = recordItemService.listAll(record);
                Map<String, Integer> checkmap = new HashMap<String, Integer>();//记录所有风险点
                for (PageData item : checkedList) {
                    if (!checkmap.containsKey(item.getString("IDENTIFICATION_ID"))) {
                        checkmap.put(item.getString("IDENTIFICATION_ID"), 1);
                    } else {
                        checkmap.put(item.getString("IDENTIFICATION_ID"), checkmap.get(item.getString("IDENTIFICATION_ID")) + 1);
                    }
                }
                map.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                map.put("checkmap", checkmap);
            }
        } else if (cycle.equals("每季")) {
            PageData type = new PageData();
            type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
            type.put("IS_QUARTER", 1);
            type.put("FINISHED", '0');
            type.put("USER_ID", USER_ID);
            List<PageData> records = checkrecordService.listAllUnfinished(type);
            pd.put("checkCount", records == null ? 0 : records.size());
            if (records != null && records.size() > 0) {
                PageData record = new PageData();
                record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                List<PageData> checkedList = recordItemService.listAll(record);
                Map<String, Integer> checkmap = new HashMap<String, Integer>();//记录所有风险点
                for (PageData item : checkedList) {
                    if (!checkmap.containsKey(item.getString("IDENTIFICATION_ID"))) {
                        checkmap.put(item.getString("IDENTIFICATION_ID"), 1);
                    } else {
                        checkmap.put(item.getString("IDENTIFICATION_ID"), checkmap.get(item.getString("IDENTIFICATION_ID")) + 1);
                    }
                }
                map.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                map.put("checkmap", checkmap);
            }
        } else if (cycle.equals("半年")) {
            PageData type = new PageData();
            type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
            type.put("IS_HALFYEAR", 1);
            type.put("FINISHED", '0');
            type.put("USER_ID", USER_ID);
            List<PageData> records = checkrecordService.listAllUnfinished(type);
            pd.put("checkCount", records == null ? 0 : records.size());
            if (records != null && records.size() > 0) {
                PageData record = new PageData();
                record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                List<PageData> checkedList = recordItemService.listAll(record);
                Map<String, Integer> checkmap = new HashMap<String, Integer>();//记录所有风险点
                for (PageData item : checkedList) {
                    if (!checkmap.containsKey(item.getString("IDENTIFICATION_ID"))) {
                        checkmap.put(item.getString("IDENTIFICATION_ID"), 1);
                    } else {
                        checkmap.put(item.getString("IDENTIFICATION_ID"), checkmap.get(item.getString("IDENTIFICATION_ID")) + 1);
                    }
                }
                map.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                map.put("checkmap", checkmap);
            }
        } else if (cycle.equals("每年")) {
            PageData type = new PageData();
            type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
            type.put("IS_YEAR", 1);
            type.put("FINISHED", '0');
            type.put("USER_ID", USER_ID);
            List<PageData> records = checkrecordService.listAllUnfinished(type);
            pd.put("checkCount", records == null ? 0 : records.size());
            if (records != null && records.size() > 0) {
                PageData record = new PageData();
                record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                List<PageData> checkedList = recordItemService.listAll(record);
                Map<String, Integer> checkmap = new HashMap<String, Integer>();//记录所有风险点
                for (PageData item : checkedList) {
                    if (!checkmap.containsKey(item.getString("IDENTIFICATION_ID"))) {
                        checkmap.put(item.getString("IDENTIFICATION_ID"), 1);
                    } else {
                        checkmap.put(item.getString("IDENTIFICATION_ID"), checkmap.get(item.getString("IDENTIFICATION_ID")) + 1);
                    }
                }
                map.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                map.put("checkmap", checkmap);
            }
        }
        map.put("result", errInfo);
        return map;
    }


    /**
     * 辨别部位有电子围栏得话 判断是人员定位是否在电子围栏中
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/isInElectronicFence")
    @ResponseBody
    @LogAnno(menuType = "手机", menuServer = "隐患排查", instructionsOperate = "清单管理", instructionsType = "判断是人员是否在电子围栏中")
    public Object isInElectronicFence() throws Exception {
        PageData pd = this.getPageData();
        if (!Tools.notEmpty(pd.getString("CARDNO"))) {
            return ReturnMap.error("当前人员未绑定定位卡号");
        }
        try {
            boolean jsonObject = PLSUtil.determineWhetherThePersonIsInTheElectronicFence(pd.getString("CORPINFO_ID"), Integer.valueOf(pd.getString("CARDNO")), pd.getString("ELECTRONIC_FENCE_ID"));
            if (jsonObject) return ReturnMap.ok();
        } catch (Exception e) {
            return ReturnMap.error("当前人员不在电子围栏中禁止排查清单");
        }
        return ReturnMap.error("当前人员不在电子围栏中禁止排查清单");
    }

    /**
     * 获取检查项
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/goCheck")
    @ResponseBody
    @LogAnno(menuType = "手机", menuServer = "风险排查", instructionsOperate = "清单管理", instructionsType = "获取检查项")
    public Object goCheck() throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        String errInfo = "success";
        PageData pd = new PageData();
        pd = this.getPageData();
        List<PageData> varList = listcheckitemService.listAll(pd);
        for (PageData item : varList) {
            item.put("RECORDITEM_ID", this.get32UUID());    //主键
        }
        map.put("varList", varList);
        String USER_ID = pd.getString("USER_NAME");
        pd = listmanagerService.findById(pd);
        if (pd.getString("TYPE").equals("listType0005")) {
            if (DateUtil.timeCalendar(new Date(), DateUtil.fomatDateTime(pd.getString("START_DATE") + " 00:00:00"), DateUtil.fomatDateTime(pd.getString("END_DATE") + " 23:59:59"))) {
                PageData type = new PageData();
                type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
                type.put("IS_XUN", 1);//查询方法和旬一样,直接借用
                type.put("FINISHED", '0');
                type.put("USER_ID", USER_ID);
                type.put("BEGINTIME", pd.getString("START_DATE") + " 00:00:00");
                type.put("ENDTIME", pd.getString("END_DATE") + " 23:59:59");
                List<PageData> records = checkrecordService.listAllUnfinished(type);
                if (records != null && records.size() > 0) {
                    PageData record = new PageData();
                    record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                    List<PageData> checkedList = recordItemService.listAll(record);
                    map.put("records", checkedList);
                }
            }
            map.put("result", errInfo);
            return map;
        }
        String cycle = pd.getString("PERIODNAME");
        if (cycle.equals("每日")) {
            PageData type = new PageData();
            type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
            type.put("IS_DAY", 1);
            type.put("FINISHED", '0');
            type.put("USER_ID", USER_ID);
            List<PageData> records = checkrecordService.listAllUnfinished(type);
            if (records != null && records.size() > 0) {
                PageData record = new PageData();
                record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                List<PageData> checkedList = recordItemService.listAll(record);
                map.put("records", checkedList);
            }
        } else if (cycle.equals("每周")) {
            PageData type = new PageData();
            type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
            type.put("IS_WEEK", 1);
            type.put("FINISHED", '0');
            type.put("USER_ID", USER_ID);
            List<PageData> records = checkrecordService.listAllUnfinished(type);
            if (records != null && records.size() > 0) {
                PageData record = new PageData();
                record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                List<PageData> checkedList = recordItemService.listAll(record);
                map.put("records", checkedList);
            }
        } else if (cycle.equals("每旬")) {
            //1-10
            if (DateUtil.timeCalendar(new Date(), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "01 00:00:00"), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "10 23:59:59"))) {
                PageData type = new PageData();
                type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
                type.put("IS_XUN", 1);
                type.put("FINISHED", '0');
                type.put("USER_ID", USER_ID);
                type.put("BEGINTIME", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "01 00:00:00");
                type.put("ENDTIME", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "10 23:59:59");
                List<PageData> records = checkrecordService.listAllUnfinished(type);
                if (records != null && records.size() > 0) {
                    PageData record = new PageData();
                    record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                    List<PageData> checkedList = recordItemService.listAll(record);
                    map.put("records", checkedList);
                }
            }
            //10-20
            if (DateUtil.timeCalendar(new Date(), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "11 00:00:00"), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "20 23:59:59"))) {
                PageData type = new PageData();
                type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
                type.put("IS_XUN", 1);
                type.put("FINISHED", '0');
                type.put("USER_ID", USER_ID);
                type.put("BEGINTIME", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "11 00:00:00");
                type.put("ENDTIME", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "20 23:59:59");
                List<PageData> records = checkrecordService.listAllUnfinished(type);
                if (records != null && records.size() > 0) {
                    PageData record = new PageData();
                    record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                    List<PageData> checkedList = recordItemService.listAll(record);
                    map.put("records", checkedList);
                }
            }
            //20-月底
            if (DateUtil.timeCalendar(new Date(), DateUtil.fomatDateTime(DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "21 00:00:00"), DateUtil.fomatDateTime(DateUtil.getMonthEndDay() + " 23:59:59"))) {
                PageData type = new PageData();
                type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
                type.put("IS_XUN", 1);
                type.put("FINISHED", '0');
                type.put("USER_ID", USER_ID);
                type.put("BEGINTIME", DateUtil.getYear() + "-" + DateUtil.getMonth() + "-" + "21 00:00:00");
                type.put("ENDTIME", DateUtil.getMonthEndDay() + " 23:59:59");
                List<PageData> records = checkrecordService.listAllUnfinished(type);
                if (records != null && records.size() > 0) {
                    PageData record = new PageData();
                    record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                    List<PageData> checkedList = recordItemService.listAll(record);
                    map.put("records", checkedList);
                }
            }
        } else if (cycle.equals("每月")) {
            PageData type = new PageData();
            type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
            type.put("IS_MONTH", 1);
            type.put("FINISHED", '0');
            type.put("USER_ID", USER_ID);
            List<PageData> records = checkrecordService.listAllUnfinished(type);
            if (records != null && records.size() > 0) {
                PageData record = new PageData();
                record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                List<PageData> checkedList = recordItemService.listAll(record);
                map.put("records", checkedList);
            }
        } else if (cycle.equals("每季")) {
            PageData type = new PageData();
            type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
            type.put("IS_QUARTER", 1);
            type.put("FINISHED", '0');
            type.put("USER_ID", USER_ID);
            List<PageData> records = checkrecordService.listAllUnfinished(type);
            if (records != null && records.size() > 0) {
                PageData record = new PageData();
                record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                List<PageData> checkedList = recordItemService.listAll(record);
                map.put("records", checkedList);
            }
        } else if (cycle.equals("半年")) {
            PageData type = new PageData();
            type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
            type.put("IS_HALFYEAR", 1);
            type.put("FINISHED", '0');
            type.put("USER_ID", USER_ID);
            List<PageData> records = checkrecordService.listAllUnfinished(type);
            if (records != null && records.size() > 0) {
                PageData record = new PageData();
                record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                List<PageData> checkedList = recordItemService.listAll(record);
                map.put("records", checkedList);
            }
        } else if (cycle.equals("每年")) {
            PageData type = new PageData();
            type.put("LISTMANAGER_ID", pd.get("LISTMANAGER_ID"));
            type.put("IS_YEAR", 1);
            type.put("FINISHED", '0');
            type.put("USER_ID", USER_ID);
            List<PageData> records = checkrecordService.listAllUnfinished(type);
            if (records != null && records.size() > 0) {
                PageData record = new PageData();
                record.put("CHECKRECORD_ID", records.get(0).get("CHECKRECORD_ID"));
                List<PageData> checkedList = recordItemService.listAll(record);
                map.put("records", checkedList);
            }
        }
        map.put("result", errInfo);
        return map;
    }


    /**
     * 列表
     *
     * @param
     * @throws Exception
     */
    @RequestMapping(value = "/checkListIndex")
    @ResponseBody
    @LogAnno(menuType = "手机", menuServer = "隐患排查", instructionsOperate = "清单管理", instructionsType = "列表")
    public Object checkListIndex() 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("POST_ID", Jurisdiction.getPOST_ID());	//岗位
        //	pd.put("USER_ID",Jurisdiction.getUSER_ID());
        String DEPARTMENT_ID = pd.get("DEPARTMENT_ID").toString();
        String ids = departmentService.getDEPARTMENT_IDS(DEPARTMENT_ID);
        if (ids != null && Tools.notEmpty(ids) && ids.lastIndexOf(",") > -1) {
            ids = ids.substring(0, ids.lastIndexOf(","));
            pd.put("DEPARTMENT_IDS", ids.split(","));
        } else {
            pd.put("DEPARTMENT_ID", DEPARTMENT_ID);
        }
        List<PageData> varList = listmanagerService.queryHiddenDangerList(pd);    //列出ListManager列表
        int fxwjcNum = 0;
        int fxyjcNum = 0;
        int yhwjcNum = 0;
        int yhyjcNum = 0;
        for (PageData list : varList) {
            if (list.getString("list_type").equals("1")) {
                // 风险管控
                if(Integer.parseInt(list.get("checkCount").toString()) > 0) {
                    fxyjcNum++;
                } else {
                    fxwjcNum++;
                }
            }
            if (list.getString("list_type").equals("2")) {
                // 隐患
                if (Integer.parseInt(list.get("checkCount").toString()) > 0) {
                    yhyjcNum++;
                } else {
                    yhwjcNum++;
                }
            }
        }
        map.put("fxwjcNum", fxwjcNum);
        map.put("fxyjcNum", fxyjcNum);
        map.put("yhwjcNum", yhwjcNum);
        map.put("yhyjcNum", yhyjcNum);
        map.put("result", errInfo);
        return map;
    }
}
