package com.zcloud.controller.risk;

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.risk.IdentificationPartsService;
import com.zcloud.service.risk.RiskPointService;
import com.zcloud.service.risk.RiskUnitService;
import com.zcloud.service.system.DepartmentService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.ObjectExcelView;
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 org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * 说明：辨识部位
 * 作者：luoxiaobao
 * 时间：2020-12-30
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/app/identificationparts")
public class AppIdentificationPartsController extends BaseController {

	@Autowired
	private IdentificationPartsService identificationpartsService;
	@Autowired
	private RiskUnitService riskunitService;
	@Autowired
	private RiskPointService riskpointService;
	@Autowired
	private DepartmentService departmentService;
	/**新增
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/add")
	// @RequiresPermissions("identificationparts:add")
	@ResponseBody
	@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("IDENTIFICATIONPARTS_ID", this.get32UUID());	//主键

		pd.put("CREATOR", Jurisdiction.getUsername());	//添加人
		pd.put("CREATTIME", DateUtil.date2Str(new Date()));	//添加时间
		pd.put("OPERATOR", Jurisdiction.getUsername());	//修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
		pd.put("ISDELETE", "0");	//是否删除 1-是  0-否
		identificationpartsService.save(pd);
		map.put("result", errInfo);
		return map;
	}

	/**获取风险点下拉框选项
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/getSelect")
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "风险管控",instructionsOperate = "辨识部位",instructionsType = "获取风险点下拉框选项")
	public Object getPointSelect() 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> unitList = riskunitService.listAll(pd);
		map.put("unitList", unitList);
		map.put("result", errInfo);
		return map;
	}

	/**删除
	 * @param out
	 * @throws Exception
	 */
	@RequestMapping(value="/delete")
	// @RequiresPermissions("identificationparts:del")
	@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();
		pd.put("CHECK_IDENTIFICATION_ID", pd.get("IDENTIFICATIONPARTS_ID"));
		List<PageData> list = riskpointService.listAll(pd);
		if(list!=null && list.size()>0) {
			map.put("result", "fail");				//返回结果
			return map;
		}
		pd.put("OPERATOR", Jurisdiction.getUsername());	//修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		identificationpartsService.delete(pd);
		map.put("result", errInfo);				//返回结果
		return map;
	}

	/**修改
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/edit")
	// @RequiresPermissions("identificationparts: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();
		pd.put("OPERATOR", Jurisdiction.getUsername());	//修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		identificationpartsService.edit(pd);
		map.put("result", errInfo);
		return map;
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	// @RequiresPermissions("identificationparts: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());
		page.setPd(pd);
		List<PageData>	varList = identificationpartsService.list(page);	//列出IdentificationParts列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/listAll")
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "风险管控",instructionsOperate = "辨识部位",instructionsType = "列表")
	public Object listAll(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());	//企业
		List<PageData> partsList = identificationpartsService.listAll(pd);
		map.put("partsList", partsList);
		map.put("result", errInfo);
		return map;
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/listAllByIdens")
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "风险管控",instructionsOperate = "辨识部位",instructionsType = "列表")
	public Object listAllByIdens(Page page) 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("IDS");
		if(Tools.notEmpty(DATA_IDS)){
			String ArrayDATA_IDS[] = DATA_IDS.split(",");
			pd.put("ArrayDATA_IDS", ArrayDATA_IDS);
			List<PageData> varList = identificationpartsService.listAllByIdens(pd);
			errInfo = "success";
			map.put("varList", varList);
		}else{
			errInfo = "error";
		}
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业
		List<PageData> partsList = identificationpartsService.listAll(pd);
		map.put("partsList", partsList);
		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 = identificationpartsService.findById(pd);	//根据ID读取
		Set<String> accidentSet = new HashSet<String>();
		// 自定义风险因素列表
		pd.put("CHECK_IDENTIFICATION_ID", pd.get("IDENTIFICATIONPARTS_ID"));
		List<PageData> riskPointList = riskpointService.listAllToRes(pd);

		if(riskPointList != null & riskPointList.size() > 0) {
			for(PageData pData : riskPointList) {
				if(pData.get("ACCIDENTS_NAME") != null && Tools.notEmpty(pData.get("ACCIDENTS_NAME").toString())) {
					String[] split = pData.get("ACCIDENTS_NAME").toString().split(",");
					Set<String> accSet = new HashSet<String>(Arrays.asList(split));
					accidentSet.addAll(accSet);
				}
			}
		}
		String accidents_name = String.join(",", accidentSet);
		pd.put("ACCIDENTS_NAME", accidents_name);
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}

	 /**批量删除
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteAll")
	// @RequiresPermissions("identificationparts:del")
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "风险管控",instructionsOperate = "辨识部位",instructionsType = "批量删除")
	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(",");
			for (String id : ArrayDATA_IDS) {
				PageData iden = new PageData();
				iden.put("CHECK_IDENTIFICATION_ID", id);
				List<PageData> list = riskpointService.listAll(iden);
				if(list!=null && list.size()>0) {
					continue;
				}
				pd.put("IDENTIFICATIONPARTS_ID", id);
				pd.put("OPERATOR", Jurisdiction.getUsername());	//修改人
				pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
				identificationpartsService.delete(pd);
			}
//			identificationpartsService.deleteAll(ArrayDATA_IDS);
			errInfo = "success";
		}else{
			errInfo = "error";
		}
		map.put("result", errInfo);				//返回结果
		return map;
	}

	 /**导出到excel
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/excel")
	@LogAnno(menuType= "手机",menuServer= "风险管控",instructionsOperate = "辨识部位",instructionsType = "导出到excel")
	public ModelAndView exportExcel() throws Exception{
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());
		Map<String,Object> dataMap = new HashMap<String,Object>();
		List<String> titles = new ArrayList<String>();
		titles.add("所属企业");	//2
		titles.add("风险点（单元）");	//2
		titles.add("辨识部位");	//1
		dataMap.put("titles", titles);
		List<PageData> varOList = identificationpartsService.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("CORP_NAME"));	    //1
			vpd.put("var2", varOList.get(i).getString("RISKUNITNAME"));	    //2
			vpd.put("var3", varOList.get(i).getString("PARTSNAME"));	    //6
			varList.add(vpd);
		}
		dataMap.put("varList", varList);
		ObjectExcelView erv = new ObjectExcelView();
		mv = new ModelAndView(erv,dataMap);
		return mv;
	}

		 /**判断同一个风险单元辨识部位名称是否重复
		 * @param
		 * @throws Exception
		 */
		@RequestMapping(value="/hasName")
		@ResponseBody
		@LogAnno(menuType= "手机",menuServer= "风险管控",instructionsOperate = "辨识部位",instructionsType = "判断同一个风险单元辨识部位名称是否重复")
		public Object hasName() throws Exception{
			Map<String, Object> map = new HashMap<>();
			PageData pd = new PageData();
			pd = this.getPageData();
			String errInfo = "success";
			PageData identificationparts = new PageData();

			identificationparts.put("RISKUNIT_ID", pd.getString("RISK_UNIT_ID"));
			identificationparts.put("CHECK_NAME",pd.getString("name"));
			List<PageData> list = identificationpartsService.listAll(identificationparts);
			if(list.size()>0) {
				errInfo = "error";
			}
			map.put("result", errInfo);				//返回结果
			map.put("count", list.size());
			return map;
		}
}
