package com.zcloud.controller.hiddenDangerCheckStandard;

import com.alibaba.fastjson.JSON;
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.*;
import com.zcloud.service.corp.CorpInfoService;
import com.zcloud.service.hiddenDangerCheckStandard.*;
import com.zcloud.service.offduty.OffDutyService;
import com.zcloud.service.sms.SMSLogService;
import com.zcloud.service.sms.SMSManagementService;
import com.zcloud.service.system.DepartmentService;
import com.zcloud.service.system.ImgFilesService;
import com.zcloud.service.system.UsersService;
import com.zcloud.util.*;
import com.zcloud.util.message.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 说明：清单检查记录 作者：luoxiaobao 时间：2021-01-04 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/customCheckrecord")
public class CustomCheckRecordController extends BaseController {
	@Autowired
	private CorpInfoService corpinfoService;
	@Autowired
	private CustomCheckRecordService customCheckRecordService;
	@Autowired
	private CustomCheckRecordItemService customCheckRecordItemService;
	@Autowired
	private CustomItemService customItemService;
	@Autowired
	private CustomHiddenService hiddenService;
	@Autowired
	private CustomCheckUserService checkuserService;
	@Autowired
	private CustomStatisticsService listStatisticsService;
	@Autowired
	private CustomService customService;
	@Autowired
	private UsersService usersService;
	@Autowired
	private SMSManagementService smsmanagementService;
	@Autowired
	private SMSLogService smSlogService;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private ImgFilesService imgFilesService;
	@Autowired
	private MessageService messageService;
	@Autowired
	private OffDutyService offDutyService;
	@Autowired
	private SysDateService sysDateService;
	@Autowired
	private CustomDisableTimeService customDisableTimeService;
	@Autowired
	private SendMessageUtil sendMessageUtil;
	/**
	 * 新增
	 *
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/add")
	// @RequiresPermissions("checkrecord:add")
	@ResponseBody
	@Transactional
	@LogAnno(menuType= "双重预防",menuServer= "隐患排查",instructionsOperate = "清单排查",instructionsType = "新增记录")
	public Object add() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		PageData sysdate = new PageData();
		sysdate.put("DATE", pd.get("CHECK_TIME"));
		sysdate = sysDateService.findByDate(sysdate);
		pd.put("CHECKRECORD_ID", this.get32UUID()); // 主键
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID()); // 企业
		pd.put("ISDELETE", "0"); // 是否删除
		pd.put("CREATOR", Jurisdiction.getName()); // 添加人
		pd.put("CREATTIME", DateUtil.date2Str(new Date())); // 创建时间
		pd.put("OPERATOR", Jurisdiction.getName()); // 修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));
		pd.put("FINISHED", '1');
		pd.put("ISSTATISTICS", '1');
		pd.put("TYPE", '1');
		pd.put("CUSTOM_ID", pd.get("ID"));
		PageData list = customService.findById(pd);
		String periodstart = "";
		String periodend = "";
		if(!list.get("TYPE").toString().equals("listType0005")) {
			switch (list.get("PERIOD").toString()) {
			case "checkPeriod0001":
				periodstart = sysdate.getString("DAY_START");
				periodend = sysdate.getString("DAY_END");
				break;
			case "checkPeriod0002":
				periodstart = sysdate.getString("WEEK_START");
				periodend = sysdate.getString("WEEK_END");
				break;
			case "checkPeriod0003":
				periodstart = sysdate.getString("XUN_START");
				periodend = sysdate.getString("XUN_END");
				break;
			case "checkPeriod0004":
				periodstart = sysdate.getString("MONTH_START");
				periodend = sysdate.getString("MONTH_END");
				break;
			case "checkPeriod0005":
				periodstart = sysdate.getString("QUARTER_START");
				periodend = sysdate.getString("QUARTER_END");
				break;
			case "checkPeriod0006":
				periodstart = sysdate.getString("YEAR_START");
				periodend = sysdate.getString("YEAR_END");
				break;
			case "checkPeriod0007":
				periodstart = sysdate.getString("HALFYEAR_START");
				periodend = sysdate.getString("HALFYEAR_END");
				break;
			}
			pd.put("PERIODSTART", periodstart);
			pd.put("PERIODEND", periodend);
		} else {
			pd.put("PERIODSTART", list.get("START_DATE"));
			pd.put("PERIODEND", list.get("END_DATE"));
		}
		customCheckRecordService.save(pd);
		checkuserService.delete(pd);
		PageData checkUser = new PageData();
		checkUser.put("CHECKUSER_ID", this.get32UUID()); // 主键
		checkUser.put("CHECKRECORD_ID", pd.get("CHECKRECORD_ID"));
		checkUser.put("DEPARTMENT_ID", pd.get("CHECKDEPT"));
		checkUser.put("USER_ID", pd.get("CHECKOR"));
		PageData u = usersService.findUserDept(checkUser);

		checkUser.put("USER_NAME", u.get("USER_NAME"));
		checkUser.put("DEPARTMENT_NAME", u.get("DEPARTMENT_NAME"));

		checkuserService.save(checkUser);
		if (pd.get("OTHER") != null && !pd.get("OTHER").equals("")) {
			List<JSONObject> otherList = (List<JSONObject>) JSON.parse(pd.get("OTHER").toString());
			for (JSONObject json : otherList) {
				if (json.get("DEPARTMENT_ID") != null && json.get("DEPARTMENT_ID") != "" && json.get("USER_ID") != null
						&& json.get("USER_ID") != "") {
					PageData other = new PageData();
					other.put("CHECKUSER_ID", this.get32UUID()); // 主键
					other.put("CHECKRECORD_ID", pd.get("CHECKRECORD_ID"));
					other.put("DEPARTMENT_ID", json.get("DEPARTMENT_ID"));
					other.put("USER_ID", json.get("USER_ID"));
					PageData ou = usersService.findUserDept(other);

					other.put("USER_NAME", ou.get("USER_NAME"));
					other.put("DEPARTMENT_NAME", ou.get("DEPARTMENT_NAME"));

					checkuserService.save(other);
				}
			}
		}
		if (pd.get("ITEMS") != null && !pd.get("ITEMS").equals("")) {
			List<JSONObject> itemsList = (List<JSONObject>) JSON.parse(pd.get("ITEMS").toString());
			for (JSONObject json : itemsList) {
				PageData item = new PageData();
				//item.put("RECORDITEM_ID", this.get32UUID());  主键
				item.put("RECORDITEM_ID", json.get("RECORDITEM_ID"));
				item.put("CUSTOM_ITEM_ID", json.get("CUSTOM_ITEM_ID"));
				PageData chei = customItemService.findById(item);
				item.put("ISNORMAL", json.get("ISNORMAL"));
				item.put("CHECKRECORD_ID", pd.get("CHECKRECORD_ID"));
				item.put("CUSTOM_ID", pd.get("CUSTOM_ID"));
				item.put("CHECK_CATEGORY", chei.get("CHECK_CATEGORY"));
				item.put("CHECK_CATEGORY_NAME", chei.get("CHECK_CATEGORY_NAME"));
				item.put("CHECK_ITEM", chei.get("CHECK_ITEM"));
				item.put("CHECK_ITEM_NAME", chei.get("CHECK_ITEM_NAME"));
				item.put("CHECK_CONTENT", chei.get("CHECK_CONTENT"));
				item.put("CHECK_STANDARD", chei.get("CHECK_STANDARD"));
				item.put("REFERENCE_BASIS", chei.get("REFERENCE_BASIS"));
				item.put("CHECK_RESULT", json.get("CHECK_RESULT"));
				item.put("COMMON_ITEM_SORT", chei.get("COMMON_ITEM_SORT"));
				item.put("LONGITUDE", pd.get("LONGITUDE"));
				item.put("LATITUDE", pd.get("LATITUDE"));
				item.put("CORPINFO_ID", pd.get("CORPINFO_ID"));
				item.put("ISDELETE", "0");
				item.put("CREATOR_ID", Jurisdiction.getUSER_ID());
				item.put("CREATOR_NAME", Jurisdiction.getUsername());
				item.put("CREATE_TIME", DateUtil.date2Str(new Date()));    //删除时间
				customCheckRecordItemService.save(item);
			}
		}
		if (pd.get("IDS") != null && !pd.get("IDS").equals("")) {
			String ids = pd.get("IDS").toString();
			String ArrayDATA_IDS[] = ids.split(",");
			PageData hiddens = new PageData();
			hiddens.put("CHECKRECORD_ID", pd.get("CHECKRECORD_ID"));
			hiddens.put("IDS", ArrayDATA_IDS);
			hiddenService.updateAll(hiddens);
		}
		hiddenService.submit(pd);

		hiddenService.submitList(pd);// 其他隐患提交

		List<PageData> hList = hiddenService.getHiddenByRecord(pd);
		if (!hList.isEmpty()) {
			for (PageData h : hList) {
				if (Tools.notEmpty(h.getString("RECTIFICATIONOR")) && !h.getString("RECTIFICATIONTYPE").equals("1")) {
					messageService.sendMessageByParameter("HIDDEN_MAIL_002", "友情提示：有一条隐患需要整改",
							h.getString("CORPINFO_ID"), h.getString("RECTIFICATIONOR"), h.getString("HIDDEN_ID"),
							h.getString("HIDDENDESCR"), "隐患整改", "整改", "", "");
				}
				if ("2".equals(h.get("RECTIFICATIONTYPE").toString())) {
					// 发短信
					PageData mes = new PageData();
					mes.put("RECEIVER_ID", h.get("RECTIFICATIONOR"));// 收信人userid
					mes.put("templateCode", "SMS_215820677");// 短信模板编码
					mes.put("CORPINFO_ID", pd.get("CORPINFO_ID"));// 企业id

					// 参数集合
					List<PageData> paramsList = new ArrayList<PageData>();
					// 第1个参数
					PageData params1 = new PageData();
					params1.put("name", "time");// 存入短信模板中的参数名称
					params1.put("value", h.get("RECTIFICATIONDEADLINE"));// 存入上面参数名称所传递的值
					paramsList.add(params1);

					sendMessageUtil.sendMessages(mes, paramsList);
				}
			}
		}
		map.put("result", errInfo);
		return map;
	}

	/**
	 * 删除
	 *
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/delete")
	// @RequiresPermissions("checkrecord:del")
	@ResponseBody
	@Transactional
	@LogAnno(menuType= "双重预防",menuServer= "隐患排查",instructionsOperate = "清单检查情况",instructionsType = "删除检查记录")
	public Object delete() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		customCheckRecordService.delete(pd);
		customCheckRecordItemService.delete(pd);
		map.put("result", errInfo); // 返回结果
		return map;
	}

	/**
	 * 修改
	 *
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/edit")
	// @RequiresPermissions("checkrecord:edit")
	@ResponseBody
	@LogAnno(menuType= "双重预防",menuServer= "隐患排查",instructionsOperate = "清单检查情况",instructionsType = "修改")
	public Object edit() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		customCheckRecordService.edit(pd);
		map.put("result", errInfo);
		return map;
	}

	/**
	 * 获取隐患数据
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/findHidden")
	@ResponseBody
	public Object findHidden() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		map.put("pd",customItemService.findHidden(pd));
		map.put("result", errInfo);
		return map;
	}

	/**
	 * 列表
	 *
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value = "/list")
	// @RequiresPermissions("checkrecord:list")
	@ResponseBody
	@LogAnno(menuType= "双重预防",menuServer= "隐患排查",instructionsOperate = "清单检查情况",instructionsType = "检查记录列表")
	public Object list(Page page) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		String KEYWORDS = pd.getString("KEYWORDS"); // 关键词检索条件
		if (Tools.notEmpty(KEYWORDS))
			pd.put("KEYWORDS", KEYWORDS.trim());
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
		/*PageData ls = listmanagerService.findById(pd);
		// 拼接sql语句
		StringBuffer sql = new StringBuffer();
		sql.append("WHERE 1 = 1 ");
		// 排除当前周期
		if(ls.get("TYPE") != null && Tools.notEmpty(ls.get("TYPE").toString())) {
			if(!"listType0005".equals(ls.get("TYPE").toString())) {
				sql.append(" AND ( CONCAT(DATE,'00:00:00')  ");
				if(ls.get("PERIOD") != null && Tools.notEmpty(ls.get("PERIOD").toString())) {
					String period = ls.get("PERIOD").toString();
					String todayDate = "";
					switch (period) {
					case "checkPeriod0001"://每日
						todayDate = DateUtil.getDay()+" 00:00:00";
						break;

					case "checkPeriod0002"://每周
						todayDate = DateUtil.getWeekStart();
						break;

					case "checkPeriod0003"://每旬
						String year = DateUtil.getYear().toString();
						String month = DateUtil.getMonth().toString();
						Integer day = Integer.parseInt(DateUtil.getOnlyDay().toString());
						String startTime = year + "-" + month + "-";
						String endTime = year + "-" + month + "-";
						if(day <= 10) {
							startTime = startTime+"01 00:00:00";
							endTime = endTime+"10 23:59:59";
						} else if(day > 10 && day <= 20) {
							startTime = startTime+"11 00:00:00";
							endTime = endTime+"20 23:59:59";
						} else {
							startTime = startTime+"21 00:00:00";
							endTime = DateUtil.getMonthEndDay()+" 23:59:59";
						}

						todayDate = startTime;
						break;

					case "checkPeriod0004"://每月
						todayDate = DateUtil.getMonthFirstDay()+" 00:00:00";
						break;

					case "checkPeriod0005"://每季
						todayDate = DateUtil.quarterStart()+" 00:00:00";
						break;

					case "checkPeriod0006"://每年
						todayDate = DateUtil.getCurrYearFirst()+" 00:00:00";
						break;

					case "checkPeriod0007"://半年
						String byear = DateUtil.getYear().toString();
						Integer bmonth = Integer.parseInt(DateUtil.getMonth().toString());
						if(bmonth <= 6) {
							todayDate = byear+"-01-01 00:00:00";
						} else if(bmonth > 6) {
							todayDate = byear+"-07-01 00:00:00";
						}
						break;

					default:
						break;
					}
					sql.append(" < '"+todayDate+"' )");
				}
			}
		}
		// 用户休班列表
		PageData timePd = new PageData();
		timePd.put("USER_ID", ls.get("USER_ID"));
		timePd.put("CORPINFO_ID", ls.get("CORPINFO_ID"));
		List<PageData> userRestList = offDutyService.listAll(timePd);
		if(userRestList != null && userRestList.size() > 0) {
			for(PageData userRest : userRestList) {
				sql.append(" AND (DAY_START < '"+userRest.getString("STARTTIME")+" 00:00:00' or DAY_END > '"+userRest.getString("ENDTIME")+" 23:59:59' ) ");
			}
		}
		// 清单禁用列表
		PageData disPd = new PageData();
		disPd.put("LISTMANAGER_ID", ls.get("LISTMANAGER_ID"));
		disPd.put("CORPINFO_ID", ls.get("CORPINFO_ID"));
		List<PageData> listDisList = listDisableTimeService.listByFull(disPd);
		if(listDisList != null && listDisList.size() > 0) {
			for(PageData listDis : listDisList) {
				sql.append(" AND (DAY_START < '"+listDis.getString("STARTTIME")+"' or DAY_END > '"+listDis.getString("ENDTIME")+"' ) ");
			}
		}
		List<PageData> varList = new ArrayList<PageData>();
		if(ls.getString("TYPE").equals("listType0005")) {
			PageData type = new PageData();
			type.put("LISTMANAGER_ID", ls.get("LISTMANAGER_ID"));
			type.put("IS_XUN", 1);
			type.put("FINISHED", '1');
			type.put("BEGINTIME", ls.getString("START_DATE")+" 00:00:00");
			type.put("ENDTIME", ls.getString("END_DATE")+" 23:59:59");
			page.setPd(type);
			varList = customCheckRecordService.listAllList(page);
			// 查看节假日清单期间，是否有请假或者停用
			PageData sqlStr = new PageData();
			sqlStr.put("SQL", sql.toString());
			List<PageData> jjrDate = sysDateService.listAllByJjr(sqlStr);
			if(jjrDate != null && jjrDate.size() > 0) {// 节假日期间，若存在非请假活停用日期，则补全
				if(DateUtil.timeCompare(DateUtil.fomatDateTime(ls.getString("END_DATE")+" 23:59:59"),new Date())) {
					PageData jjr = new PageData();
					jjr.put("DATESTART", ls.get("START_DATE")+" 00:00:00");
					jjr.put("DATEEND", ls.get("END_DATE")+" 23:59:59");
					if(varList.size() < 2) {
						for(int i = varList.size();i <2;i++) {
							varList.add(jjr);
						}
					}
				}
			}
			page.setCurrentPage(1);
			page.setShowCount(10);
			page.setTotalPage(1);
			page.setTotalResult(varList.size());
		} else {
			pd.put("LISTDATE", ls.get("CREATTIME"));
			pd.put("LISTTYPE", ls.get("PERIOD"));
			if(ls.getString("TYPE").equals("listType0005")) {
				pd.put("STATUS", "1");
			}
			pd.put("ISDELETE", ls.get("ISDELETE"));
			pd.put("DELETEDATE", ls.get("OPERATTIME"));
			pd.put("SQL", sql.toString());
			page.setPd(pd);
			varList = customCheckRecordService.list(page); // 列出CheckRecord列表
		}*/
		page.setPd(pd);
		List<PageData> varList = customCheckRecordService.list(page);
		PageData ls = customService.findById(pd);
		map.put("varList", varList);
		map.put("page", page);
		map.put("ls", ls);
		map.put("result", errInfo);
		return map;
	}

	/**
	 * 清单统计
	 *
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/getListConut")
	@ResponseBody
	public Object getListConut() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		List<PageData> lists = listStatisticsService.getCountList(pd);

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

	/**
	 * 去修改页面获取数据
	 *
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/goEdit")
	@ResponseBody
	@LogAnno(menuType= "双重预防",menuServer= "隐患排查",instructionsOperate = "清单检查情况",instructionsType = "去修改页面获取数据")
	public Object goEdit() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd = customCheckRecordService.findById(pd); // 根据ID读取
		PageData condition = new PageData();
		condition.put("CUSTOM_ID",pd.getString("CUSTOM_ID"));
		PageData listManager = customService.findById(condition);
		pd.put("BAO_BAO_TYPE",listManager.getString("BAO_BAO_TYPE"));
		PageData users = checkuserService.getCheckUser(pd);
		List<PageData> varList = customCheckRecordItemService.listAll(pd);
		if(users != null) {
			pd.put("USERS", users.get("USERS"));
		}
		PageData pd2 = new PageData();
		pd2.put("FOREIGN_KEY",pd.get("CHECKRECORD_ID"));
		pd2.put("TYPE","13");
		List<PageData> qianming = imgFilesService.listAll(pd2);


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

	@RequestMapping(value = "/goMap")
	@ResponseBody
	@LogAnno(menuType= "双重预防",menuServer= "隐患排查",instructionsOperate = "清单检查情况",instructionsType = "获取地图信息")
	public Object goMap() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		PageData checkrecord = customCheckRecordService.findById(pd);
		List<PageData> varList = customCheckRecordItemService.listAllForMap(checkrecord);
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
		PageData cinfo = corpinfoService.findById(pd);
		checkrecord.put("FIND_CHECKRECORD_ID", checkrecord.get("CHECKRECORD_ID"));
		List<PageData> hdList = hiddenService.listAll(checkrecord);
		map.put("varList", varList);
		map.put("hdList", hdList);
		map.put("pd", pd);
		map.put("cinfo", cinfo);
		map.put("checkrecord", checkrecord);
		map.put("result", errInfo);
		return map;
	}

	/**
	 * 批量删除
	 *
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteAll")
	// @RequiresPermissions("checkrecord:del")
	@ResponseBody
	public Object deleteAll() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		String DATA_IDS = pd.getString("DATA_IDS");
		if (Tools.notEmpty(DATA_IDS)) {
			String ArrayDATA_IDS[] = DATA_IDS.split(",");
			customCheckRecordService.deleteAll(ArrayDATA_IDS);
			errInfo = "success";
		} else {
			errInfo = "error";
		}
		map.put("result", errInfo); // 返回结果
		return map;
	}

	/**
	 * 导出到excel
	 *
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/excel")
	public ModelAndView exportExcel() throws Exception {
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		Map<String, Object> dataMap = new HashMap<String, Object>();
		List<String> titles = new ArrayList<String>();
		titles.add("清单ID"); // 1
		titles.add("清单名称"); // 2
		titles.add("企业"); // 3
		titles.add("添加人"); // 4
		titles.add("创建时间"); // 5
		titles.add("修改人"); // 6
		titles.add("修改时间"); // 7
		titles.add("是否删除"); // 8
		dataMap.put("titles", titles);
		List<PageData> varOList = customCheckRecordService.listAll(pd);
		List<PageData> varList = new ArrayList<PageData>();
		for (int i = 0; i < varOList.size(); i++) {
			PageData vpd = new PageData();
			vpd.put("var1", varOList.get(i).getString("LISTMANAGER_ID")); // 1
			vpd.put("var2", varOList.get(i).getString("LIST_NAME")); // 2
			vpd.put("var3", varOList.get(i).getString("CORPINFO_ID")); // 3
			vpd.put("var4", varOList.get(i).getString("CREATOR")); // 4
			vpd.put("var5", varOList.get(i).getString("CREATTIME")); // 5
			vpd.put("var6", varOList.get(i).getString("OPERATOR")); // 6
			vpd.put("var7", varOList.get(i).getString("OPERATTIME")); // 7
			vpd.put("var8", varOList.get(i).get("ISDELETE").toString()); // 8
			varList.add(vpd);
		}
		dataMap.put("varList", varList);
		ObjectExcelView erv = new ObjectExcelView();
		mv = new ModelAndView(erv, dataMap);
		return mv;
	}

	public static String getLastDayOfMonth(int year, int month) {
		Calendar cal = Calendar.getInstance();
		// 设置年份
		cal.set(Calendar.YEAR, year);
		// 设置月份
		cal.set(Calendar.MONTH, month - 1);
		// 获取某月最大天数
		int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		// 设置日历中月份的最大天数
		cal.set(Calendar.DAY_OF_MONTH, lastDay);
		// 格式化日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String lastDayOfMonth = sdf.format(cal.getTime());

		return lastDayOfMonth;
	}

	/**
	 * main
	 */
	public static void main(String[] args) {
		Double aDouble = 0d / 1d;
		DecimalFormat df = new DecimalFormat("#0.00");
		System.out.println(df.format(aDouble));
	}

	/**
	 * 去修改页面获取数据
	 *
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/goBiListCount")
	@ResponseBody
	public Object goBiListCount() 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()); // 企业

		switch (pd.get("COUNTTYPE").toString()) {
		case "day":// 每日
			pd.put("STARTTIME", DateUtil.getDay() + " 00:00:00");
			pd.put("ENDTIME", DateUtil.getDay() + " 23:59:59");
			break;

		case "week":// 每周
			pd.put("STARTTIME", DateUtil.getWeekStart());
			pd.put("ENDTIME", DateUtil.getWeekEnd());
			break;

		case "xun":// 每旬
			String year = DateUtil.getYear().toString();
			String month = DateUtil.getMonth().toString();
			Integer day = Integer.parseInt(DateUtil.getOnlyDay().toString());
			String startTime = year + "-" + month + "-";
			String endTime = year + "-" + month + "-";
			if (day <= 10) {
				startTime = startTime + "01 00:00:00";
				endTime = endTime + "10 23:59:59";
			} else if (day > 10 && day <= 20) {
				startTime = startTime + "11 00:00:00";
				endTime = endTime + "20 23:59:59";
			} else {
				startTime = startTime + "21 00:00:00";
				endTime = DateUtil.getMonthEndDay() + " 23:59:59";
			}
			pd.put("STARTTIME", startTime);
			pd.put("ENDTIME", endTime);
			break;

		case "month":// 每月
			pd.put("STARTTIME", DateUtil.getMonthFirstDay() + " 00:00:00");
			pd.put("ENDTIME", DateUtil.getMonthEndDay() + " 23:59:59");
			break;

		case "quarter":// 每季
			pd.put("STARTTIME", DateUtil.quarterStart() + " 00:00:00");
			pd.put("ENDTIME", DateUtil.quarterEnd() + " 23:59:59");
			break;

		case "year":// 每年
			pd.put("STARTTIME", DateUtil.getCurrYearFirst() + " 00:00:00");
			pd.put("ENDTIME", DateUtil.getCurrYearLast() + " 23:59:59");
			break;

		case "halfyear":// 半年
			String byear = DateUtil.getYear().toString();
			Integer bmonth = Integer.parseInt(DateUtil.getMonth().toString());
			if (bmonth <= 6) {
				pd.put("STARTTIME", byear + "-01-01 00:00:00");
				pd.put("ENDTIME", byear + "-06-30 23:59:59");
			} else if (bmonth > 6) {
				pd.put("STARTTIME", byear + "-07-01 00:00:00");
				pd.put("ENDTIME", byear + "-12-31 23:59:59");
			}
			break;

		default:
			break;
		}
//			switch (pd.get("COUNTTYPE").toString()) {
//			case "month":
//				pd.put("STARTTIME", DateUtil.getMonthFirstDay()+" 00:00:00");
//				pd.put("ENDTIME", DateUtil.getMonthEndDay()+" 23:59:59");
//				break;
//			case "quarter":
//				pd.put("STARTTIME", DateUtil.quarterStart()+" 00:00:00");
//				pd.put("ENDTIME", DateUtil.quarterEnd()+" 23:59:59");
//				break;
//			case "year":
//				pd.put("STARTTIME", DateUtil.getCurrYearFirst()+" 00:00:00");
//				pd.put("ENDTIME", DateUtil.getCurrYearLast()+" 23:59:59");
//				break;
//
//			default:
//				break;
//			}
		String STARTTIME = pd.getString("STARTTIME");
		String ENDTIME = pd.getString("ENDTIME");

		/*
		 * List<PageData> varList = new ArrayList<PageData>(); PageData listType0001 =
		 * new PageData(); PageData listType0002 = new PageData(); PageData listType0003
		 * = new PageData(); PageData listType0004 = new PageData(); PageData
		 * listType0005 = new PageData();
		 */
		PageData all = new PageData();
		all.put("type", "all");
		all.put("name", "合计");
		all.put("molecule", 0);
		all.put("denominator", 0);
		all.put("percentage", 0d);
		/*
		 * Map<String, PageData> varMap = new HashMap<String, PageData>();
		 *
		 * listType0001.put("type", "listType0001"); listType0001.put("name", "日常");
		 * listType0001.put("molecule", 0); listType0001.put("denominator", 0);
		 * listType0001.put("percentage", 0d); listType0002.put("type", "listType0002");
		 * listType0002.put("name", "综合"); listType0002.put("molecule", 0);
		 * listType0002.put("denominator", 0); listType0002.put("percentage", 0d);
		 *
		 *
		 * listType0003.put("type", "listType0003"); listType0003.put("name", "专业");
		 * listType0003.put("molecule", 0); listType0003.put("denominator", 0);
		 * listType0003.put("percentage", 0d);
		 *
		 *
		 * listType0004.put("type", "listType0004"); listType0004.put("name", "季节性");
		 * listType0004.put("molecule", 0); listType0004.put("denominator", 0);
		 * listType0004.put("percentage", 0d);
		 *
		 *
		 * listType0005.put("type", "listType0005"); listType0005.put("name", "节假日");
		 * listType0005.put("molecule", 0); listType0005.put("denominator", 0);
		 * listType0005.put("percentage", 0d);
		 *
		 *
		 *
		 *
		 * varMap.put("listType0001", listType0001); varMap.put("listType0002",
		 * listType0002); varMap.put("listType0003", listType0003);
		 * varMap.put("listType0004", listType0004); varMap.put("listType0005",
		 * listType0005); varMap.put("all", all);
		 */

		List<PageData> countList = listStatisticsService.getCountList(pd);// 统计分母

		if (countList != null && countList.size() > 0) {
			int sum = 0;
			List<PageData> dayList = customCheckRecordService.listCountDay(pd); // 统计--日
			if (dayList != null && dayList.size() > 0) {
				for (PageData count : dayList) {
					sum += Integer.parseInt(count.get("NUM").toString());
				}
			}
			List<PageData> weekList = customCheckRecordService.listCountWeek(pd); // 统计--周
			if (weekList != null && weekList.size() > 0) {
				for (PageData count : weekList) {
					sum += Integer.parseInt(count.get("NUM").toString());
				}
			}
			List<PageData> monthList = customCheckRecordService.listCountMonth(pd); // 统计--月

			if (monthList != null && monthList.size() > 0) {
				for (PageData count : monthList) {
					sum += Integer.parseInt(count.get("NUM").toString());
				}
			}
			List<PageData> quarterList = customCheckRecordService.listCountQuarter(pd); // 统计--季

			if (quarterList != null && quarterList.size() > 0) {
				for (PageData count : quarterList) {
					sum += Integer.parseInt(count.get("NUM").toString());
				}
			}
			List<PageData> halfYearList = customCheckRecordService.listCountHalfYear(pd); // 统计--半年

			if (halfYearList != null && halfYearList.size() > 0) {
				for (PageData count : halfYearList) {
					sum += Integer.parseInt(count.get("NUM").toString());
				}
			}
			List<PageData> yearList = customCheckRecordService.listCountYear(pd); // 统计--年
			if (yearList != null && yearList.size() > 0) {
				for (PageData count : yearList) {
					sum += Integer.parseInt(count.get("NUM").toString());
				}
			}
			// 处理旬时间
			StringBuffer stt = new StringBuffer();
			stt.append(pd.getString("STARTTIME"));
			stt.setCharAt(9, '1');
			pd.put("STARTTIME", stt.toString());

			String endDay = ENDTIME.substring(8, 10);
			Integer endTime = Integer.parseInt(endDay);
			StringBuffer et = new StringBuffer();
			et.append(pd.getString("ENDTIME"));

			if (endTime <= 10) {
				stt.setCharAt(8, '1');
				stt.setCharAt(9, '0');
				pd.put("ENDTIME", et.toString());
			} else if (endTime <= 20) {
				stt.setCharAt(8, '2');
				stt.setCharAt(9, '0');
				pd.put("ENDTIME", et.toString());
			} else {
				Integer endMonth = Integer.parseInt(ENDTIME.substring(5, 7));
				Integer endYear = Integer.parseInt(ENDTIME.substring(0, 4));
				pd.put("ENDTIME", getLastDayOfMonth(endYear, endMonth) + " 23:59:59");
			}
			List<PageData> tenDaysList = customCheckRecordService.listCountTenDays(pd); // 统计--旬
			if (tenDaysList != null && tenDaysList.size() > 0) {
				for (PageData count : tenDaysList) {
					sum += Integer.parseInt(count.get("NUM").toString());
				}
			}
			// 分母
			for (PageData count : countList) {

				Integer allDenominator = Integer.parseInt(count.get("NOTEXAMINEDNUM").toString())
						+ Integer.parseInt(all.get("denominator").toString());
				all.put("denominator", allDenominator);
			}

			Integer jjr = customCheckRecordService.listCountJjr(pd) == null ? 0 : customCheckRecordService.listCountJjr(pd);
			all.put("molecule", sum + jjr);
		}
		/*
		 * if(!varMap.get("listType0005").get("denominator").toString().equals("0")) {
		 *
		 * if(jjr == 0) { varMap.get("listType0005").put("percentage", 0d); } else {
		 * Double denominator=
		 * Double.parseDouble(varMap.get("listType0005").get("denominator").toString());
		 * Double molecule= Double.valueOf(jjr); Double percentage =
		 * molecule/denominator*100; DecimalFormat df = new DecimalFormat("#.00");
		 * varMap.get("listType0005").put("percentage", df.format(percentage)); } }
		 * Integer allMolecule =
		 * Integer.parseInt(varMap.get("listType0005").get("molecule").toString()) +
		 * Integer.parseInt(all.get("molecule").toString()); all.put("molecule",
		 * allMolecule); varList.add(varMap.get("listType0005"));
		 */
		if (!all.get("denominator").toString().equals("0")) {
			if (all.get("molecule").toString().equals("0")) {
				all.put("percentage", 0d);
			} else {
				Double denominator = Double.parseDouble(all.get("denominator").toString());
				Double molecule = Double.parseDouble(all.get("molecule").toString());
				Double percentage = molecule / denominator * 100;
				DecimalFormat df = new DecimalFormat("#0.00");
				all.put("percentage", df.format(percentage));
			}
		}

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

	/**
	 * 去修改页面获取数据
	 *
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/goIndexCount")
	@ResponseBody
	@LogAnno(menuType= "首页",menuServer= "首页",instructionsOperate = "首页",instructionsType = "去修改页面获取数据")
	public Object goIndexCount() 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()); // 企业
		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");

		List<PageData> varList = new ArrayList<PageData>();
		PageData listType0001 = new PageData();
		PageData listType0002 = new PageData();
		PageData listType0003 = new PageData();
		PageData listType0004 = new PageData();
		PageData listType0005 = new PageData();
//		PageData listType0006 = new PageData();
		PageData all = new PageData();

		Map<String, PageData> varMap = new HashMap<String, PageData>();

		listType0001.put("type", "listType0001");
		listType0001.put("name", "日常");
		listType0001.put("molecule", 0);
		listType0001.put("denominator", 0);
		listType0001.put("percentage", 0d);

		listType0002.put("type", "listType0002");
		listType0002.put("name", "综合");
		listType0002.put("molecule", 0);
		listType0002.put("denominator", 0);
		listType0002.put("percentage", 0d);

		listType0003.put("type", "listType0003");
		listType0003.put("name", "专业");
		listType0003.put("molecule", 0);
		listType0003.put("denominator", 0);
		listType0003.put("percentage", 0d);

		listType0004.put("type", "listType0004");
		listType0004.put("name", "季节性");
		listType0004.put("molecule", 0);
		listType0004.put("denominator", 0);
		listType0004.put("percentage", 0d);

		listType0005.put("type", "listType0005");
		listType0005.put("name", "节假日");
		listType0005.put("molecule", 0);
		listType0005.put("denominator", 0);
		listType0005.put("percentage", 0d);

//		listType0006.put("type", "listType0006");
//		listType0006.put("name", "临时");
//		listType0006.put("molecule", 0);
//		listType0006.put("denominator", 0);
//		listType0006.put("percentage", 0d);

		all.put("type", "all");
		all.put("name", "合计");
		all.put("molecule", 0);
		all.put("denominator", 0);
		all.put("percentage", 0d);

		varMap.put("listType0001", listType0001);
		varMap.put("listType0002", listType0002);
		varMap.put("listType0003", listType0003);
		varMap.put("listType0004", listType0004);
		varMap.put("listType0005", listType0005);
//		varMap.put("listType0006", listType0006);
		varMap.put("all", all);

		List<PageData> countList = listStatisticsService.getCountList(pd);// 统计分母

		if (countList != null && countList.size() > 0) {
			List<PageData> dayList = customCheckRecordService.listCountDay(pd); // 统计--日
			if (dayList != null && dayList.size() > 0) {
				for (PageData count : dayList) {
					Integer molecule = Integer.parseInt(count.get("NUM").toString())
							+ Integer.parseInt(varMap.get(count.get("TYPE")).get("molecule").toString());
					varMap.get(count.get("TYPE")).put("molecule", molecule);

					Integer allMolecule = Integer.parseInt(count.get("NUM").toString())
							+ Integer.parseInt(varMap.get("all").get("molecule").toString());
					varMap.get("all").put("molecule", allMolecule);
				}
			}
			List<PageData> weekList = customCheckRecordService.listCountWeek(pd); // 统计--周
			if (weekList != null && weekList.size() > 0) {
				for (PageData count : weekList) {
					Integer molecule = Integer.parseInt(count.get("NUM").toString())
							+ Integer.parseInt(varMap.get(count.get("TYPE")).get("molecule").toString());
					varMap.get(count.get("TYPE")).put("molecule", molecule);

					Integer allMolecule = Integer.parseInt(count.get("NUM").toString())
							+ Integer.parseInt(varMap.get("all").get("molecule").toString());
					varMap.get("all").put("molecule", allMolecule);
				}
			}
			List<PageData> monthList = customCheckRecordService.listCountMonth(pd); // 统计--月

			if (monthList != null && monthList.size() > 0) {
				for (PageData count : monthList) {
					Integer molecule = Integer.parseInt(count.get("NUM").toString())
							+ Integer.parseInt(varMap.get(count.get("TYPE")).get("molecule").toString());
					varMap.get(count.get("TYPE")).put("molecule", molecule);

					Integer allMolecule = Integer.parseInt(count.get("NUM").toString())
							+ Integer.parseInt(varMap.get("all").get("molecule").toString());
					varMap.get("all").put("molecule", allMolecule);
				}
			}
			List<PageData> quarterList = customCheckRecordService.listCountQuarter(pd); // 统计--季

			if (quarterList != null && quarterList.size() > 0) {
				for (PageData count : quarterList) {
					Integer molecule = Integer.parseInt(count.get("NUM").toString())
							+ Integer.parseInt(varMap.get(count.get("TYPE")).get("molecule").toString());
					varMap.get(count.get("TYPE")).put("molecule", molecule);

					Integer allMolecule = Integer.parseInt(count.get("NUM").toString())
							+ Integer.parseInt(varMap.get("all").get("molecule").toString());
					varMap.get("all").put("molecule", allMolecule);
				}
			}
			List<PageData> halfYearList = customCheckRecordService.listCountHalfYear(pd); // 统计--半年

			if (halfYearList != null && halfYearList.size() > 0) {
				for (PageData count : halfYearList) {
					Integer molecule = Integer.parseInt(count.get("NUM").toString())
							+ Integer.parseInt(varMap.get(count.get("TYPE")).get("molecule").toString());
					varMap.get(count.get("TYPE")).put("molecule", molecule);

					Integer allMolecule = Integer.parseInt(count.get("NUM").toString())
							+ Integer.parseInt(varMap.get("all").get("molecule").toString());
					varMap.get("all").put("molecule", allMolecule);
				}
			}
			List<PageData> yearList = customCheckRecordService.listCountYear(pd); // 统计--年
			if (yearList != null && yearList.size() > 0) {
				for (PageData count : yearList) {
					Integer molecule = Integer.parseInt(count.get("NUM").toString())
							+ Integer.parseInt(varMap.get(count.get("TYPE")).get("molecule").toString());
					varMap.get(count.get("TYPE")).put("molecule", molecule);

					Integer allMolecule = Integer.parseInt(count.get("NUM").toString())
							+ Integer.parseInt(varMap.get("all").get("molecule").toString());
					varMap.get("all").put("molecule", allMolecule);
				}
			}
			// 处理旬时间
			StringBuffer stt = new StringBuffer();
			stt.append(pd.getString("STARTTIME"));
			stt.setCharAt(9, '1');
			pd.put("STARTTIME", stt.toString());

			String endDay = ENDTIME.substring(8, 10);
			Integer endTime = Integer.parseInt(endDay);
			StringBuffer et = new StringBuffer();
			et.append(pd.getString("ENDTIME"));

			if (endTime <= 10) {
				stt.setCharAt(8, '1');
				stt.setCharAt(9, '0');
				pd.put("ENDTIME", et.toString());
			} else if (endTime <= 20) {
				stt.setCharAt(8, '2');
				stt.setCharAt(9, '0');
				pd.put("ENDTIME", et.toString());
			} else {
				Integer endMonth = Integer.parseInt(ENDTIME.substring(5, 7));
				Integer endYear = Integer.parseInt(ENDTIME.substring(0, 4));
				pd.put("ENDTIME", getLastDayOfMonth(endYear, endMonth) + " 23:59:59");
			}
			List<PageData> tenDaysList = customCheckRecordService.listCountTenDays(pd); // 统计--旬
			if (tenDaysList != null && tenDaysList.size() > 0) {
				for (PageData count : tenDaysList) {
					Integer molecule = Integer.parseInt(count.get("NUM").toString())
							+ Integer.parseInt(varMap.get(count.get("TYPE")).get("molecule").toString());
					varMap.get(count.get("TYPE")).put("molecule", molecule);

					Integer allMolecule = Integer.parseInt(count.get("NUM").toString())
							+ Integer.parseInt(varMap.get("all").get("molecule").toString());
					varMap.get("all").put("molecule", allMolecule);
				}
			}
			// 分母
			for (PageData count : countList) {
				Integer denominator = Integer.parseInt(count.get("NOTEXAMINEDNUM").toString())
						+ Integer.parseInt(varMap.get(count.get("TYPE")).get("denominator").toString());
				varMap.get(count.get("TYPE")).put("denominator", denominator);

				Integer allDenominator = Integer.parseInt(count.get("NOTEXAMINEDNUM").toString())
						+ Integer.parseInt(varMap.get("all").get("denominator").toString());
				varMap.get("all").put("denominator", allDenominator);
			}
		}
		for (int i = 1; i < 5; i++) {
			String type = "listType000" + i;
			if (!varMap.get(type).get("denominator").toString().equals("0")) {
				if (varMap.get(type).get("molecule").toString().equals("0")) {
					varMap.get(type).put("percentage", 0d);
				} else {
					Double denominator = Double.parseDouble(varMap.get(type).get("denominator").toString());
					Double molecule = Double.parseDouble(varMap.get(type).get("molecule").toString());
					Double percentage = molecule / denominator * 100;
					DecimalFormat df = new DecimalFormat("#0.00");
					varMap.get(type).put("percentage", df.format(percentage));
				}

			}
			varList.add(varMap.get(type));
		}
		if (!varMap.get("listType0005").get("denominator").toString().equals("0")) {
			Integer jjr = customCheckRecordService.listCountJjr(pd) == null ? 0 : customCheckRecordService.listCountJjr(pd);
			if (jjr == 0) {
				varMap.get("listType0005").put("percentage", 0d);
			} else {
				Double denominator = Double.parseDouble(varMap.get("listType0005").get("denominator").toString());
				Double molecule = Double.valueOf(jjr);
				Double percentage = molecule / denominator * 100;
				DecimalFormat df = new DecimalFormat("#0.00");
				varMap.get("listType0005").put("percentage", df.format(percentage));
			}
		}
		Integer allMolecule = Integer.parseInt(varMap.get("listType0005").get("molecule").toString())
				+ Integer.parseInt(varMap.get("all").get("molecule").toString());
		varMap.get("all").put("molecule", allMolecule);
		varList.add(varMap.get("listType0005"));
		if (!varMap.get("all").get("denominator").toString().equals("0")) {
			if (varMap.get("all").get("molecule").toString().equals("0")) {
				varMap.get("all").put("percentage", 0d);
			} else {
				Double denominator = Double.parseDouble(varMap.get("all").get("denominator").toString());
				Double molecule = Double.parseDouble(varMap.get("all").get("molecule").toString());
				Double percentage = molecule / denominator * 100;
				DecimalFormat df = new DecimalFormat("#0.00");
				varMap.get("all").put("percentage", df.format(percentage));
			}
		}
		varList.add(varMap.get("all"));
		map.put("varList", varList);
		map.put("varMap", varMap);
		map.put("result", errInfo);
		return map;
	}

	/**
	 * BI页辨识部位检查记录
	 *
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/listLogByIde")
	@ResponseBody
	public Object listLogByIde() 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()); // 企业
		List<PageData> varList = customCheckRecordService.listLogByIde(pd);
		map.put("varList", varList);
		map.put("result", errInfo);
		return map;
	}

	/**
	 * BI页隐患排查人员/部门/清单数据
	 *
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/countNumBi")
	@ResponseBody
	public Object countNumBi() 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()); // 企业
		int allUser = usersService.getUserCount(pd);
		int workUser = customCheckRecordService.getWorkedUserCount(pd);
		int allDep = departmentService.getDepCount(pd);
		int workDep = customCheckRecordService.getWorkedDepCount(pd);
		int allList = customService.getListCount(pd);
		int workList = customService.getWorkedListCount(pd);
		map.put("allUser", allUser);
		map.put("workUser", workUser);
		map.put("allDep", allDep);
		map.put("workDep", workDep);
		map.put("allList", allList);
		map.put("workList", workList);
		map.put("result", errInfo);
		return map;
	}

	/**
	 * BI页清单排查类型统计
	 *
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/listTypeBi")
	@ResponseBody
	public Object listTypeBi() 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("YEAR",new Date().getYear()+1900);
		List<PageData> typeList = customCheckRecordService.listTypeBi(pd);
		map.put("typeList", typeList);
		map.put("result", errInfo);
		return map;
	}

	/**
	 * 列表
	 *
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value = "/allList")
	// @RequiresPermissions("checkrecord:list")
	@ResponseBody
	@LogAnno(menuType= "双重预防",menuServer= "隐患排查",instructionsOperate = "检查记录管理",instructionsType = "检查记录列表")
	public Object allList(Page page) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		PageData npd = new PageData();
		pd = this.getPageData();
		String KEYWORDS = pd.getString("KEYWORDS"); // 关键词检索条件
		if (Tools.notEmpty(KEYWORDS))
			npd.put("KEYWORDS", KEYWORDS.trim());
		npd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
		npd.put("ISSUPERVISE", Jurisdiction.getISSUPERVISE());
		npd.put("SELF_DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());
		npd.put("ISMAIN",Jurisdiction.getIS_MAIN());
		pd.put("USER_ID",Jurisdiction.getUSER_ID());
		PageData cpd = new PageData();
		cpd = usersService.findById(pd);
		npd.put("ISLEADER", cpd.getString("ISLEADER"));
		if(cpd.getString("ISLEADER") != null && cpd.getString("ISLEADER").equals("1")){
			String DEPARTMENT_ID = npd.getString("SELF_DEPARTMENT_ID");
			String ids = departmentService.getDEPARTMENT_IDS(DEPARTMENT_ID);
			ids=npd.getString("SELF_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(npd.getString("ISMAIN").equals("0")){
				npd.put("USER_ID",Jurisdiction.getUSER_ID());
			}
		}
		npd.put("LISTMANAGER_ID",pd.getString("LISTMANAGER_ID"));
		npd.put("DEPARTMENT_ID",pd.getString("DEPARTMENT_ID"));
		npd.put("TYPE",pd.getString("TYPE"));
		npd.put("STARTTIME",pd.getString("STARTTIME"));
		npd.put("ENDTIME",pd.getString("ENDTIME"));
		page.setPd(npd);
		List<PageData> varList = customCheckRecordService.list(page);
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

	/**
	 * 补录
	 * TODO
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value = "/recording")
	// @RequiresPermissions("checkrecord:add")
	@ResponseBody
	@Transactional
	@LogAnno(menuType= "双重预防",menuServer= "隐患排查",instructionsOperate = "清单检查情况",instructionsType = "补录")
	public Object recording() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();

		pd.put("TYPE", "1");
		pd.put("ISSTATISTICS", "1");

		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID()); // 企业
		pd.put("OPERATOR", Jurisdiction.getName()); // 添加人
		customCheckRecordService.recording(pd);// 修改检查记录
		checkuserService.delete(pd);
		PageData checkUser = new PageData();
		checkUser.put("CHECKUSER_ID", this.get32UUID()); // 主键
		checkUser.put("CHECKRECORD_ID", pd.get("CHECKRECORD_ID"));
		checkUser.put("DEPARTMENT_ID", pd.get("CHECKDEPT"));
		checkUser.put("USER_ID", pd.get("CHECKOR"));
		PageData u = usersService.findUserDept(checkUser);

		checkUser.put("USER_NAME", u.get("USER_NAME"));
		checkUser.put("DEPARTMENT_NAME", u.get("DEPARTMENT_NAME"));

		checkuserService.save(checkUser);
		if (pd.get("OTHER") != null && !pd.get("OTHER").equals("")) {
			List<JSONObject> otherList = (List<JSONObject>) JSON.parse(pd.get("OTHER").toString());
			for (JSONObject json : otherList) {
				if (json.get("DEPARTMENT_ID") != null && json.get("DEPARTMENT_ID") != "" && json.get("USER_ID") != null
						&& json.get("USER_ID") != "") {
					PageData other = new PageData();
					other.put("CHECKUSER_ID", this.get32UUID()); // 主键
					other.put("CHECKRECORD_ID", pd.get("CHECKRECORD_ID"));
					other.put("DEPARTMENT_ID", json.get("DEPARTMENT_ID"));
					other.put("USER_ID", json.get("USER_ID"));
					PageData ou = usersService.findUserDept(other);

					other.put("USER_NAME", ou.get("USER_NAME"));
					other.put("DEPARTMENT_NAME", ou.get("DEPARTMENT_NAME"));

					checkuserService.save(other);
				}
			}
		}
		if (pd.get("ITEMS") != null && !pd.get("ITEMS").equals("")) {
			List<JSONObject> itemsList = (List<JSONObject>) JSON.parse(pd.get("ITEMS").toString());
			for (JSONObject json : itemsList) {
				PageData item = new PageData();
				item.put("RECORDITEM_ID", json.get("RECORDITEM_ID")); // 主键
				item.put("CUSTOM_ITEM_ID", json.get("CUSTOM_ITEM_ID"));
				PageData chei = customItemService.findById(item);
				item.put("ISNORMAL", json.get("ISNORMAL"));
				item.put("CHECKRECORD_ID", pd.get("CHECKRECORD_ID"));
				item.put("CUSTOM_ID", pd.get("CUSTOM_ID"));
				item.put("CHECK_CATEGORY", chei.get("CHECK_CATEGORY"));
				item.put("CHECK_CATEGORY_NAME", chei.get("CHECK_CATEGORY_NAME"));
				item.put("CHECK_ITEM", chei.get("CHECK_ITEM"));
				item.put("CHECK_ITEM_NAME", chei.get("CHECK_ITEM_NAME"));
				item.put("CHECK_CONTENT", chei.get("CHECK_CONTENT"));
				item.put("CHECK_STANDARD", chei.get("CHECK_STANDARD"));
				item.put("REFERENCE_BASIS", chei.get("REFERENCE_BASIS"));
				item.put("CHECK_RESULT", json.get("CHECK_RESULT"));
				item.put("COMMON_ITEM_SORT", chei.get("COMMON_ITEM_SORT"));
				item.put("LONGITUDE", pd.get("LONGITUDE"));
				item.put("LATITUDE", pd.get("LATITUDE"));
				item.put("CORPINFO_ID", pd.get("CORPINFO_ID"));
				item.put("ISDELETE", "0");
				item.put("CREATOR_ID", Jurisdiction.getUSER_ID());
				item.put("CREATOR_NAME", Jurisdiction.getUsername());
				item.put("CREATE_TIME", DateUtil.date2Str(new Date()));    //删除时间
				customCheckRecordItemService.save(item);

			}
		}
		if (pd.get("IDS") != null && !pd.get("IDS").equals("")) {
			String ids = pd.get("IDS").toString();
			String ArrayDATA_IDS[] = ids.split(",");
			PageData hiddens = new PageData();
			hiddens.put("CHECKRECORD_ID", pd.get("CHECKRECORD_ID"));
			hiddens.put("IDS", ArrayDATA_IDS);
			hiddenService.updateAll(hiddens);
		}
		hiddenService.submit(pd);

		hiddenService.submitList(pd);// 其他隐患提交
		List<PageData> hList = hiddenService.getHiddenByRecord(pd);
		if (hList.size() > 0) {
			for (PageData h : hList) {
				if ("2".equals(h.get("RECTIFICATIONTYPE").toString())) {
					// 发短信
					PageData mes = new PageData();
					mes.put("RECEIVER_ID", h.get("RECTIFICATIONOR"));// 收信人userid
					mes.put("templateCode", "SMS_215820677");// 短信模板编码
					mes.put("CORPINFO_ID", pd.get("CORPINFO_ID"));// 企业id

					// 参数集合
					List<PageData> paramsList = new ArrayList<PageData>();
					// 第1个参数
					PageData params1 = new PageData();
					params1.put("name", "time");// 存入短信模板中的参数名称
					params1.put("value", h.get("RECTIFICATIONDEADLINE"));// 存入上面参数名称所传递的值
					paramsList.add(params1);

					sendMessageUtil.sendMessages(mes, paramsList);
				}
			}
		}
		map.put("result", errInfo);
		return map;
	}

	/**
	 * 员工清单检查情况（月）
	 *
	 * @throws Exception
	 */
	@RequestMapping(value = "/monthStatistics")
	@ResponseBody
	@LogAnno(menuType= "双重预防",menuServer= "隐患排查",instructionsOperate = "员工清单检查情况",instructionsType = "员工清单检查情况（月）")
	public Object monthStatistics(Page page) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		PageData npd = new PageData();
		pd = this.getPageData();
		String month = pd.getString("MONTH");
		npd.put("CORPINFO_ID",Jurisdiction.getCORPINFO_ID());
		String DEPTIDS = pd.getString("DEPTIDS");
		if(Tools.notEmpty(DEPTIDS)) {
			String DEPT_IDS[] = DEPTIDS.split(",");
			npd.put("DEPT_IDS", DEPT_IDS);
		}
		npd.put("ISMAIN", Jurisdiction.getIS_MAIN());
		npd.put("ISSUPERVISE", Jurisdiction.getISSUPERVISE());
		npd.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());
		pd.put("USER_ID",Jurisdiction.getUSER_ID());
		PageData cpd = new PageData();
		cpd = usersService.findById(pd);
		npd.put("ISLEADER", cpd.getString("ISLEADER"));
		if(cpd.getString("ISLEADER") != null && cpd.getString("ISLEADER").equals("1")){
			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(npd.getString("ISMAIN").equals("0")){
				npd.put("USER_ID",Jurisdiction.getUSER_ID());
			}
		}
		String KEYWORDS = pd.getString("KEYWORDS"); // 关键词检索条件
		if (Tools.notEmpty(KEYWORDS))
			npd.put("KEYWORDS", KEYWORDS.trim());
		npd.put("USERNAME",pd.getString("USERNAME"));
		page.setPd(npd);
		List<PageData> lists = customService.getListByCorplistPage(page);

		if(lists!=null && lists.size()>0){
			Map<String,Map> resmap = new HashMap<>();
			List<String> listIds = new ArrayList<>();
			lists.forEach(list -> {
				resmap.put(list.getString("LISTMANAGER_ID"),new HashMap<String,Map>());
				listIds.add(list.getString("LISTMANAGER_ID"));
				if(DateSysUtil.isSameMonth(list.getString("CREATTIME"),month)){//如果清单创建时间是查询月
					List<String> datesInRange = DateSysUtil.getDatesInRange(month+"-01 00:00:00",list.getString("CREATTIME"));
					for (int i = 0; i < datesInRange.size()-1; i++) {
						Map<String, String> data = new HashMap<String, String>();
						data.put("TYPE", "3");
						data.put("HIDDEN_COUNT", "-");
						resmap.get(list.getString("LISTMANAGER_ID")).put(datesInRange.get(i), data);//将1号到创建日期期间得值设为3:未建立
					}
				}
			});

			if(listIds.size() > 0){
				pd.put("LIST_IDS",listIds);
				List<PageData> recordList = customCheckRecordService.listForIdsAndMonth(pd);//获取查询月在检查周期内的检查记录
				recordList.forEach(record -> {
					List<String> datesInRange = DateSysUtil.getDatesInRange(record.getString("PERIODSTART"),record.getString("PERIODEND"));
					for (int i = 0; i < datesInRange.size(); i++) {
						Map<String, String> data = new HashMap<String, String>();
						data.put("TYPE", record.getString("TYPE"));
						if ("1".equals(record.getString("TYPE"))) {
							if (datesInRange.get(i).equals(record.getString("CREATTIME").substring(0, 10))) {
								data.put("HIDDEN_COUNT", record.getString("HIDDEN_COUNT"));
							} else {
								data.put("HIDDEN_COUNT", "0");
							}
						}
						resmap.get(record.getString("LISTMANAGER_ID")).put(datesInRange.get(i), data); //2未检查
					}
				});
			}

			map.put("list",lists);
			map.put("resmap",resmap);
		}
		map.put("page",page);
		map.put("result", "success");
		return map;
	}
}
