package com.zcloud.controller.risk;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.entity.Response;
import com.zcloud.logs.LogAnno;
import com.zcloud.service.risk.*;
import com.zcloud.service.system.DepartmentService;
import com.zcloud.service.system.UsersService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.Tools;
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 java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：风险点台账
 * 作者：luoxiaobao
 * 时间：2020-12-29
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/riskpointanalysis")
public class RiskPointAnalysisController extends BaseController {

	@Autowired
	private RiskPointAnalysisService riskpointAnalysisService;
	@Autowired
	private RiskPointService riskpointService;
	@Autowired
	private RiskCheckItemService riskcheckitemService;
	@Autowired
	private UsersService usersService;
	@Autowired
	private DepartmentService departmentService;

	/**新增
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/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();
		pd.put("RISKPOINT_ANALYSIS_ID", this.get32UUID());	//主键
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业
		pd.put("ISDELETE", "0");	//是否删除
		pd.put("CREATOR", Jurisdiction.getUSER_ID());	//添加人
		pd.put("CREATTIME", DateUtil.date2Str(new Date()));	//创建时间
		pd.put("OPERATOR", Jurisdiction.getUsername());	//修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		String riskPointCountString = riskpointAnalysisService.countByCorpInfo(pd);
		Integer count = Integer.parseInt(riskPointCountString);
		count ++;
		pd.put("SORT", count);
		riskpointAnalysisService.save(pd);

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

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/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());
		pd.put("ISMAIN", Jurisdiction.getIS_MAIN());
		pd.put("ISSUPERVISE", Jurisdiction.getISSUPERVISE());
		pd.put("DEPARTMENT_ID", Jurisdiction.getDEPARTMENT_ID());
		pd.put("USER_ID",Jurisdiction.getUSER_ID());
		PageData cpd = new PageData();
		cpd = usersService.findById(pd);
		pd.put("ISLEADER", cpd.getString("ISLEADER"));
		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);
			}
		}

		String DEPTIDS = pd.getString("DEPTIDS");
		if(Tools.notEmpty(DEPTIDS)) {
			String DEPT_IDS[] = DEPTIDS.split(",");
			pd.put("DEPT_IDS", DEPT_IDS);
		}
		String BIANMAS = pd.getString("BIANMA");
		if(Tools.notEmpty(BIANMAS)) {
			String BIANMA_S[] = BIANMAS.split(",");
			pd.put("BIANMA_S", BIANMA_S);
		}
		page.setPd(pd);
		List<PageData> varList = riskpointAnalysisService.list(page);	//列出RiskPoint列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

	/**通过
	 * @throws Exception
	 */
	@RequestMapping(value="/apply")
	@ResponseBody
	@LogAnno(menuType= "双重预控",menuServer= "持续改进",instructionsOperate = "隐患分析",instructionsType = "通过")
	public Object apply() throws Exception{
		Map<String,String> map = new HashMap<String,String>();
		String errInfo = "success";
		PageData pd = new PageData();
		PageData request = this.getPageData();
		pd.putAll(request);
		pd = riskpointAnalysisService.findById(pd);
		pd.put("STATUS",1);
		pd.put("OPERATOR", Jurisdiction.getUSER_ID());	//修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		PageData analysis = new PageData();
		analysis.putAll(pd);


		pd.put("RISK_CAUSES",request.getString("RISK_CAUSES"));
		if(pd.get("IS_NEW").toString().equals("1")) {
			pd.put("RISKPOINT_ID", this.get32UUID());    //主键
			pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());    //企业
			pd.put("ISDELETE", "0");    //是否删除
			pd.put("CREATOR", Jurisdiction.getUsername());    //添加人
			pd.put("CREATTIME", DateUtil.date2Str(new Date()));    //创建时间
			pd.put("OPERATOR", Jurisdiction.getUsername());    //修改人
			pd.put("OPERATTIME", DateUtil.date2Str(new Date()));    //修改时间
			String riskPointCountString = riskpointService.countByCorpInfo(pd);
			Integer count = Integer.parseInt(riskPointCountString);
			count++;
			pd.put("SORT", count);
			riskpointService.save(pd);

			PageData item = new PageData();
			item.put("RISKCHECKITEM_ID", this.get32UUID());    //主键
			item.put("RISKPOINT_ID", pd.get("RISKPOINT_ID"));    //风险点主键
			item.put("ISDELETE", "0");    //是否删除
			item.put("CREATOR", Jurisdiction.getUsername());    //添加人
			item.put("CREATTIME", DateUtil.date2Str(new Date()));    //创建时间
			item.put("OPERATOR", Jurisdiction.getUsername());    //修改人
			item.put("OPERATTIME", DateUtil.date2Str(new Date()));    //修改时间
			item.put("CHECK_CONTENT", pd.get("MEASURES"));    //检查内容
			riskcheckitemService.save(item);
		}else{
			pd.put("OPERATOR", Jurisdiction.getUsername());    //修改人
			pd.put("OPERATTIME", DateUtil.date2Str(new Date()));    //修改时间
			riskpointService.edit(pd);
			// 修改冗余字段检查内容
			PageData pd2 = new PageData();
			pd2.put("CHECK_CONTENT", pd.get("MEASURES"));
			pd2.put("RISKPOINT_ID", pd.get("RISKPOINT_ID"));
			riskcheckitemService.editByPoint(pd2);
		}
		analysis.put("RISKPOINT_ID",pd.getString("RISKPOINT_ID"));
		riskpointAnalysisService.edit(analysis);
		map.put("result", errInfo);				//返回结果
		return map;
	}

	/**删除
	 * @throws Exception
	 */
	@RequestMapping(value="/delete")
	@ResponseBody
	@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();
		riskpointAnalysisService.delete(pd);
		map.put("result", errInfo);				//返回结果
		return map;
	}

	/**列表
	 * @throws Exception
	 */
	@RequestMapping(value="/statisticsBI")
	@ResponseBody
	public Object statisticsBI() 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());	//企业ID
		List<PageData> varList = riskpointAnalysisService.statisticsBI(pd);	//列出StudyTask列表
		map.put("varList", varList);
		map.put("result", errInfo);
		return map;
	}

	@RequestMapping("/getLogs")
	@ResponseBody
	public Response getLogs(Page page) {
		Response response = Response.getResponse();
		try {
			PageData request = this.getPageData();
			response.putAll(riskpointAnalysisService.getLogs(request, page));
			return response.OK();
		} catch (Exception e) {
			e.printStackTrace();
			return response.errorMessage(e.getMessage());
		}
	}

	@RequestMapping("/getInfo")
	@ResponseBody
	public Response getInfo() {
		Response response = Response.getResponse();
		try {
			PageData request = this.getPageData();
			response.put("form",riskpointAnalysisService.getInfo(request));
			return response.OK();
		} catch (Exception e) {
			e.printStackTrace();
			return response.errorMessage(e.getMessage());
		}
	}

}
