package com.zcloud.controller.statistics;

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.statistics.ListStatisticsService;
import com.zcloud.service.system.DepartmentService;
import com.zcloud.service.system.UsersService;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.Tools;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

	/**公司人员业绩
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/count")
	// @RequiresPermissions("periodStatistics:list")
	@ResponseBody
	@LogAnno(menuType= "双重预防",menuServer= "统计分析",instructionsOperate = "员工清单检查统计",instructionsType = "列表")
	public Object count(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();
		npd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
		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());
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_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 DEPTIDS = pd.getString("DEPTIDS");
		if(Tools.notEmpty(DEPTIDS)) {
			String DEPT_IDS[] = DEPTIDS.split(",");
			npd.put("DEPT_IDS", DEPT_IDS);
		}
		String KEYWORDS = pd.getString("KEYWORDS"); // 关键词检索条件
		if (Tools.notEmpty(KEYWORDS))
			npd.put("KEYWORDS", KEYWORDS.trim());
		npd.put("STATE",pd.getString("STATE"));
		page.setPd(npd);
		List<PageData> userList = usersService.listUserlistPage(page);
		List<PageData> stsList = listStatisticsService.countByPeriodUser(pd);
		List<PageData> checkList = checkRecordService.countByPeriodUser(pd);
		if(userList != null && userList.size() > 0) {
			Map<String, PageData> stsMap = new HashMap<String, PageData>();
			Map<String, PageData> checkMap = new HashMap<String, PageData>();
			if(stsList != null && stsList.size() > 0) {
				for(PageData sts : stsList) {
					stsMap.put(sts.getString("USER_ID"), sts);
				}
			}
			if(checkList != null && checkList.size() > 0) {
				for(PageData check : checkList) {
					checkMap.put(check.getString("USER_ID"), check);
				}
			}
			for(PageData user : userList) {
				user.put("all", stsMap.get(user.getString("USER_ID")));
				user.put("check", checkMap.get(user.getString("USER_ID")));
			}
		}
		map.put("varList", userList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

}
