package com.zcloud.controller.commitment;

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.commitment.CommitmentTeamService;
import com.zcloud.service.system.DepartmentService;
import com.zcloud.service.system.UsersService;
import com.zcloud.util.*;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * 说明：班组级承诺
 * 作者：luoxiaobao
 * 时间：2021-10-19
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/app/commitment/team")
public class AppCommitmentTeamController extends BaseController {

	@Autowired
	private CommitmentTeamService commitmentteamService;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private UsersService usersService;
	@Autowired
	private Smb smb;
	/**新增前检查
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/check")
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "班组级承诺",instructionsOperate = "班组级承诺",instructionsType = "新增前检查")
	public Object check() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		PageData dept = departmentService.findById(pd);
		if(!dept.get("LEVEL").equals("departmentLevel0003")){
			errInfo = "fail";
			map.put("msg", "无此权限");
		}
		pd.put("SIGN_TIME", DateUtil.date2Str(new Date())); // 创建时间
		if(commitmentteamService.listAll(pd).size()>0){
			errInfo = "fail";
			map.put("msg", "此班组本日安全承诺已完成，无需重复操作。");
		}
		map.put("result", errInfo);
		return map;
	}

	/**新增
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/add")
//	// @RequiresPermissions("commitmentteam:add")
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "班组级承诺",instructionsOperate = "班组级承诺",instructionsType = "新增")
	public Object add(@RequestParam(value="FFILE",required=false) MultipartFile[] files) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("COMMITMENTTEAM_ID", this.get32UUID());	//主键
		pd.put("SIGN_TIME", DateUtil.date2Str(new Date()));	//签字时间

		List<PageData>	userList = usersService.listUserBYids(pd.getString("SIGN_USER").split(","));
		if (userList != null && userList.size()>0) {
			pd.put("DEPARTMENTNAME", userList.get(0).getString("DEPARTMENT_NAME"));	//车间名称
			pd.put("POSTNAME", userList.get(0).getString("POST_NAME"));	//岗位名称
		}
		if (files != null && files.length > 0) {
			MultipartFile file = files[0];
			String  ffile = DateUtil.getDays();
			String fileName = this.get32UUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
			smb.sshSftp(file, fileName, Const.FILEPATHFILE + pd.getString("CORPINFO_ID") + "/" + ffile);

			pd.put("SIGN_PATH",  Const.FILEPATHFILE + pd.getString("CORPINFO_ID") + "/" + ffile + "/" + fileName);
		}
		commitmentteamService.save(pd);
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}

	/**删除
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/delete")
//	// @RequiresPermissions("commitmentteam: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("OPERATOR", Jurisdiction.getUsername());	//修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		commitmentteamService.delete(pd);
		map.put("result", errInfo);				//返回结果
		return map;
	}

	/**修改
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/edit")
//	// @RequiresPermissions("commitmentteam: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()));	//修改时间
		commitmentteamService.edit(pd);
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}

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

	 /**去修改页面获取数据
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/goEdit")
//	// @RequiresPermissions("commitmentteam:edit")
	@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 = commitmentteamService.findById(pd);	//根据ID读取
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}

	 /**批量删除
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteAll")
//	// @RequiresPermissions("commitmentteam: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();
		pd.put("OPERATOR", Jurisdiction.getUsername());	//修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		String DATA_IDS = pd.getString("DATA_IDS");
		if(Tools.notEmpty(DATA_IDS)){
			String[] ArrayDATA_IDS = DATA_IDS.split(",");
			pd.put("ArrayDATA_IDS", ArrayDATA_IDS);	//待删除ids
			commitmentteamService.deleteAll(pd);
			errInfo = "success";
		}else{
			errInfo = "error";
		}
		map.put("result", errInfo);				//返回结果
		return map;
	}

	 /**导出到excel
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/excel")
//	// @RequiresPermissions("toExcel")
	@LogAnno(menuType= "手机",menuServer= "班组级承诺",instructionsOperate = "班组级承诺",instructionsType = "导出到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("企业ID");	//2
		titles.add("签字时间");	//3
		titles.add("签字人");	//4
		titles.add("签字");	//5
		titles.add("车间名称");	//6
		titles.add("岗位名称");	//7
		titles.add("检维修作业");	//8
		titles.add("特殊作业");	//9
		titles.add("动火作业");	//10
		titles.add("特级");	//11
		titles.add("一级");	//12
		titles.add("二级");	//13
		titles.add("高处作业");	//14
		titles.add("受限空间作业");	//15
		titles.add("吊装作业");	//16
		titles.add("临时用电作业");	//17
		titles.add("盲板抽堵作业");	//18
		titles.add("动土作业");	//19
		titles.add("断路作业");	//20
		titles.add("风险辨识、作业许可及管控措施的落实情况(1.是 0.否)");	//21
		titles.add("处于试生产情况(1.是 0.否)");	//22
		titles.add("处于开停车情况(1.开 0.停)");	//23
		titles.add("承包商作业");	//24
		titles.add("管控情况(1.受控 0.非受控)");	//25
		titles.add("变更情况(1.有 0.没有)");	//26
		titles.add("落实审批程序情况(1.是 0.否)");	//27
		titles.add("罐区、仓库等危险源是否处于安全状态(1.是 0.否)");	//28
		titles.add("岗位主要设备（装置）名称、数量");	//29
		titles.add("运行");	//30
		titles.add("停产");	//31
		titles.add("检修");	//32
		titles.add("生产装置的温度、压力、组分液位、流量等主要工艺参数是否处于指标范围(1.是 0.否)");	//33
		titles.add("压力容器、压力管道等特种设备处于安全运行状态(1.是 0.否)");	//34
		titles.add("设备设施的静动密封完好无泄漏(1.是 0.否)");	//35
		titles.add("超限报警、紧急切断、联锁等各类安全设施配备完好投用，并可靠运行(1.是 0.否)");	//36
		titles.add("储罐、管道、机泵、阀门及仪表系统完好无泄漏(1.是 0.否)");	//37
		titles.add("储罐的液位、温度、压力、气柜钟罩无超限运行(1.是 0.否)");	//38
		titles.add("气柜钟罩超高、低运行(1.是 0.否)");	//39
		titles.add("手动放脱水、装卸车时确保人员在岗(1.是 0.否)");	//40
		titles.add("可燃及有毒气体报警和联锁处于可靠运行状态(1.是 0.否)");	//41
		titles.add("仓库是否按照国家标准分区分类储存危险化学品，是否超量、超品种储存，相互禁配物质是否混放混存");	//42
		titles.add("开始时间");	//43
		titles.add("结束时间");	//44
		dataMap.put("titles", titles);
		List<PageData> varOList = commitmentteamService.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("DEPARTMENT_ID"));	    //1
			vpd.put("var2", varOList.get(i).getString("CORPINFO_ID"));	    //2
			vpd.put("var3", varOList.get(i).getString("SIGN_TIME"));	    //3
			vpd.put("var4", varOList.get(i).getString("SIGN_USER"));	    //4
			vpd.put("var5", varOList.get(i).getString("SIGN_PATH"));	    //5
			vpd.put("var6", varOList.get(i).getString("DEPARTMENTNAME"));	    //6
			vpd.put("var7", varOList.get(i).getString("POSTNAME"));	    //7
			vpd.put("var8", varOList.get(i).get("INPUT1").toString());	//8
			vpd.put("var9", varOList.get(i).get("INPUT2").toString());	//9
			vpd.put("var10", varOList.get(i).get("INPUT3").toString());	//10
			vpd.put("var11", varOList.get(i).get("INPUT4").toString());	//11
			vpd.put("var12", varOList.get(i).get("INPUT5").toString());	//12
			vpd.put("var13", varOList.get(i).get("INPUT6").toString());	//13
			vpd.put("var14", varOList.get(i).get("INPUT7").toString());	//14
			vpd.put("var15", varOList.get(i).get("INPUT8").toString());	//15
			vpd.put("var16", varOList.get(i).get("INPUT9").toString());	//16
			vpd.put("var17", varOList.get(i).get("INPUT10").toString());	//17
			vpd.put("var18", varOList.get(i).get("INPUT11").toString());	//18
			vpd.put("var19", varOList.get(i).get("INPUT12").toString());	//19
			vpd.put("var20", varOList.get(i).get("INPUT13").toString());	//20
			vpd.put("var21", varOList.get(i).get("RADIO1").toString());	//21
			vpd.put("var22", varOList.get(i).get("RADIO2").toString());	//22
			vpd.put("var23", varOList.get(i).get("RADIO3").toString());	//23
			vpd.put("var24", varOList.get(i).get("INPUT14").toString());	//24
			vpd.put("var25", varOList.get(i).getString("RADIO4"));	    //25
			vpd.put("var26", varOList.get(i).get("RADIO5").toString());	//26
			vpd.put("var27", varOList.get(i).get("RADIO6").toString());	//27
			vpd.put("var28", varOList.get(i).get("RADIO7").toString());	//28
			vpd.put("var29", varOList.get(i).getString("INPUT15"));	    //29
			vpd.put("var30", varOList.get(i).get("INPUT16").toString());	//30
			vpd.put("var31", varOList.get(i).get("INPUT17").toString());	//31
			vpd.put("var32", varOList.get(i).get("INPUT18").toString());	//32
			vpd.put("var33", varOList.get(i).get("RADIO8").toString());	//33
			vpd.put("var34", varOList.get(i).get("RADIO9").toString());	//34
			vpd.put("var35", varOList.get(i).get("RADIO10").toString());	//35
			vpd.put("var36", varOList.get(i).get("RADIO11").toString());	//36
			vpd.put("var37", varOList.get(i).get("RADIO12").toString());	//37
			vpd.put("var38", varOList.get(i).get("RADIO13").toString());	//38
			vpd.put("var39", varOList.get(i).get("RADIO14").toString());	//39
			vpd.put("var40", varOList.get(i).get("RADIO15").toString());	//40
			vpd.put("var41", varOList.get(i).get("RADIO16").toString());	//41
			vpd.put("var42", varOList.get(i).getString("INPUT19"));	    //42
			vpd.put("var43", varOList.get(i).getString("START_TIME"));	    //43
			vpd.put("var44", varOList.get(i).getString("END_TIME"));	    //44
			varList.add(vpd);
		}
		dataMap.put("varList", varList);
		ObjectExcelView erv = new ObjectExcelView();
		mv = new ModelAndView(erv,dataMap);
		return mv;
	}

}
