package com.zcloud.controller.keyProjects.app;

import com.alibaba.fastjson.JSON;
import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.service.relatedparty.OutSourcedService;
import com.zcloud.service.relatedparty.UnitsService;
import com.zcloud.service.specialoperation.PersonnelManagementService;
import com.zcloud.service.system.DictionariesService;
import com.zcloud.service.video.VideoManagerService;
import com.zcloud.util.Tools;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.SimpleHash;
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
 * 时间：2022-06-07
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/app/keyProjects")
public class AppKeyProjectsController extends BaseController {

	@Autowired
	private UnitsService unitsService;
	@Autowired
	private OutSourcedService outsourcedService;
	@Autowired
	private DictionariesService dictionariesService;
	@Autowired
	private PersonnelManagementService personnelmanagementService;

	@Autowired
	private VideoManagerService videomanagerService;

	/**列表
	 * @throws Exception
	 */
	@RequestMapping(value="/listAppZdgcgl")
	@ResponseBody
	public Object listAppZdgcgl(Page page) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		String UserId = pd.getString("UserId");
		pd.put("UserId",UserId);
		if(!StringUtils.equals("1",UserId)){
			pd.put("DeptId",outsourcedService.getDeptId(UserId));
		}
		String KEYWORDS = pd.getString("KEYWORDS");						//关键词检索条件
		if(Tools.notEmpty(KEYWORDS))pd.put("KEYWORDS", KEYWORDS.trim());
		page.setPd(pd);
		List<PageData>	varList = outsourcedService.applylist(page);	//列出OutSourced列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

	/**
	 * 重点工程定位
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/editDingWei")
	@ResponseBody
	public Object editDingWei() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		outsourcedService.editDingWei(pd);	//根据ID读取
		String corpInfoId = outsourcedService.getCorpInfoByOutsourcesCreator(pd);
		pd.put("operatingCorpId",corpInfoId);
		map.put("dockData", JSON.toJSONString(pd));
		map.put("result", errInfo);
		return map;
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/videomanagerList")
	@ResponseBody
	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();
		page.setPd(pd);
		List<PageData>	varList = videomanagerService.listApp(page);	//列出VideoManager列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

	/**去修改页面获取数据
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/getOutsourced")
	@ResponseBody
	public Object getOutsourced() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd = outsourcedService.findById(pd);	//根据ID读取
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}

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

	/**列表
	 * @throws Exception
	 */
	@RequestMapping(value="/listOutsourced")
	@ResponseBody
	public Object listOutsourced(Page page) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		String UserId = pd.getString("UNITS_ID");
		pd.put("UserId",UserId);
		if(!StringUtils.equals("1",UserId)){
			pd.put("DeptId",outsourcedService.getDeptId(UserId));
		}
		String KEYWORDS = pd.getString("KEYWORDS");						//关键词检索条件
		if(Tools.notEmpty(KEYWORDS))pd.put("KEYWORDS", KEYWORDS.trim());
		page.setPd(pd);
		List<PageData>	varList = outsourcedService. list(page);	//列出OutSourced列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

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


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

	/**列表
	 * @throws Exception
	 */
	@RequestMapping(value="/login")
	@ResponseBody
	public Object login() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		String pwd =  new SimpleHash("SHA-1", pd.get("USERNAME"), pd.get("PASSWORD")).toString();
		pd.put("PHONENUM", pd.get("USERNAME"));
		pd.put("PASSWORD",pwd);
		pd = personnelmanagementService.findForLogin(pd);	//列出PersonnelManagement列表
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}


	/**
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/editPwd")
	@ResponseBody
	public Object editPwd (Page page) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		PageData pageData = personnelmanagementService.findById(pd);
		String oldpwd = new SimpleHash("SHA-1", pageData.getString("PHONENUM"), pd.get("OLDPASSWORD")).toString();
		if(!pageData.getString("PASSWORD").equals(oldpwd)){
			errInfo = "error";
			map.put("msg","旧密码错误");
			return map;
		}
		String pwd = new SimpleHash("SHA-1", pageData.getString("PHONENUM"), pd.get("PASSWORD")).toString(); //新密码
		PageData updateUser = new PageData();
		updateUser.put("PASSWORD", pwd);
		updateUser.put("PERSONNELMANAGEMENT_ID", pageData.getString("PERSONNELMANAGEMENT_ID"));
		personnelmanagementService.editPassword(updateUser);

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

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

//	/**去修改页面获取数据
//	 * @param
//	 * @throws Exception
//	 */
//	@RequestMapping(value="/goEdit")
//	@ResponseBody
//	public Object goEdit() throws Exception{
//		Map<String,Object> map = new HashMap<String,Object>();
//		String errInfo = "success";
//		PageData pd = new PageData();
//		pd = this.getPageData();
//		pd = personnelmanagementService.findByOutsourced(pd);	//根据ID读取
//		map.put("pd", pd);
//		map.put("result", errInfo);
//		return map;
//	}

	/**列表
	 * @throws Exception
	 */
	@RequestMapping(value="/outSouceslistPage")
	@ResponseBody
	public Object outSouceslistPage(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());
		String UserId = pd.getString("UserId");
		if(!StringUtils.equals("1",UserId)){
			String CORPINFO_ID = pd.getString("CORPINFO_ID");
//			-- 河港机械 河港港工 河港检测
			if(!StringUtils.equals(CORPINFO_ID,"1e6dbbe16004402f8d2c0e52afd9a676")
					&&!StringUtils.equals(CORPINFO_ID,"3a854eefa7894e06aaa1a2611bca80f6")&&
					!StringUtils.equals(CORPINFO_ID,"020578a4c1f04bc692ee25145c2efbe5")
					&&!StringUtils.equals(CORPINFO_ID,"90966974de3c4b83aca6f8fd6432d5c2")){
				pd.put("DeptId",outsourcedService.getDeptId(UserId));
				pd.put("UserId",UserId);

			}
		}
		page.setPd(pd);
		List<PageData> varList = outsourcedService.outSouceslistPage(page);	//列出OutSourced列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

	/** 检验视频是否全部定位以及工程是否定位
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/getDingWeiSum")
	@ResponseBody
	public Object getDingWeiSum() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		Integer co = outsourcedService.getDingWeiSum(pd);	//获取未定位的工程和视频总量  >1 则不让申请开工
		if(co>0){
			map.put("code", "500");
		}else {
			map.put("code", "0");
		}
		map.put("result", errInfo);
		return map;
	}

	/** 修改工程状态
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/updateState")
	@ResponseBody
	public Object updateState() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		outsourcedService.updateState(pd);	//获取同时绑定的重点工程信息如果大于1表示还有其他重点工程
		map.put("result", errInfo);
		return map;
	}

	/**
	 * 校验当前重点工程是否可以结束
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/checkEnd")
	@ResponseBody
	public Object jie() throws Exception {
		Map<String, String> map = new HashMap<String, String>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		//校验当前重点工程是否存在未完成的隐患 （包含未整改，未处罚，ai报警未处置的）
		PageData pageData = outsourcedService.jieshuOutSourced(pd);
		if(!StringUtils.equals("0",pageData.getString("qr"))){
			map.put("result", errInfo);
			map.put("message","当前重点工程还有"+pageData.getString("qr")+"项待确认安全环保检查,请处理以后再结束");
			map.put("code","500");
			return map;
		}
		if(!StringUtils.equals("0",pageData.getString("zg"))){
			map.put("result", errInfo);
			map.put("message","当前重点工程还有"+pageData.getString("zg")+"项需整改的隐患,请处理以后再结束");
			map.put("code","500");
			return map;
		}
		if(!StringUtils.equals("0",pageData.getString("ys"))){
			map.put("result", errInfo);
			map.put("message","当前重点工程还有"+pageData.getString("ys")+"项待验收隐患,请处理以后再结束");
			map.put("code","500");
			return map;
		}
		if(!StringUtils.equals("0",pageData.getString("cf"))){
			map.put("result", errInfo);
			map.put("message","当前重点工程还有"+pageData.getString("cf")+"项待处罚隐患,请处理以后再结束");
			map.put("code","500");
			return map;
		}
		if(!StringUtils.equals("0",pageData.getString("fk"))){
			map.put("result", errInfo);
			map.put("message","当前重点工程还有"+pageData.getString("fk")+"项处罚待反馈隐患,请处理以后再结束");
			map.put("code","500");
			return map;
		}
		if(!StringUtils.equals("0",pageData.getString("ai"))){
			map.put("result", errInfo);
			map.put("message","当前重点工程还有"+pageData.getString("ai")+"项AI报警待处置,请处理以后再结束");
			map.put("code","500");
			return map;
		}
		map.put("code","0");
		map.put("result", errInfo);                //返回结果
		return map;
	}

	/**去修改页面获取数据
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/goEdit")
	@ResponseBody
	public Object goEdit() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd = personnelmanagementService.findByOutsourced(pd);	//根据ID读取
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}

	/**处罚管理列表  查询自己可以处罚的重点工程    角标计算逻辑 查询重点工程中所有处罚未选择是否处罚的隐患累加
	 * @throws Exception
	 */
	@RequestMapping(value="/getPUNISHlist")
	@ResponseBody
	public Object getPUNISHlist(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());
		page.setPd(pd);
		List<PageData>	varList = outsourcedService.getPUNISHlist(page);	//列出OutSourced列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

}
