package com.zcloud.controller.speEquip;

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.speEquip.SpecialEquipmentAqxjService;
import com.zcloud.service.speEquip.SpecialEquipmentService;
import com.zcloud.service.system.ImgFilesService;
import com.zcloud.util.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
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 org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * 说明：特种设备安全巡检管理
 * 作者：luoxiaobao
 * 时间：2021-06-19
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/app/specialequipmentaqxj")
public class AppSpecialEquipmentAqxjController extends BaseController {

	@Autowired
	private SpecialEquipmentAqxjService specialequipmentaqxjService;
	@Autowired
	private ImgFilesService imgfilesService;
	@Autowired
	private SpecialEquipmentService specialequipmentService;
	@Autowired
	private Smb smb;
	/**新增
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/add")
	@ResponseBody
	@Transactional
	@LogAnno(menuType= "手机",menuServer= "特种设备安全巡检管理",instructionsOperate = "特种设备安全巡检管理",instructionsType = "新增")
	public Object add(MultipartRequest request) throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("SPECIALEQUIPMENTAQXJ_ID", this.get32UUID());	//主键
		pd.put("CREATOR",  pd.getString("CREATOR"));	//添加人
		pd.put("CREATTIME", DateUtil.date2Str(new Date()));	//添加时间
		pd.put("OPERATOR",  pd.getString("CREATOR"));	//修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		pd.put("ISDELETE", 0);	//是否删除 1-是  0-否
		pd.put("CORPINFO_ID", pd.getString("CORPINFO_ID"));	//企业ID

		/*
		巡检完成后,更新到下次巡检周期
		 */
		PageData equip = this.specialequipmentService.findById(pd);
		pd.put("PERIOD_START_DATE",equip.get("PERIOD_START_DATE"));
		pd.put("PERIOD_END_DATE",equip.get("PERIOD_END_DATE"));
		specialequipmentaqxjService.save(pd);

		equip.put("PERIOD_START_DATE",DateSysUtil.getNexPeriod(pd.getString("PERIOD_START_DATE"),equip.getString("PERIOD")).get("PERIOD_START_DATE") + " 00:00:00");
		equip.put("PERIOD_END_DATE",DateSysUtil.getNexPeriod(pd.getString("PERIOD_END_DATE"),equip.getString("PERIOD")).get("PERIOD_END_DATE") + " 23:59:59");
		this.specialequipmentService.edit(equip);


		List<MultipartFile> fileList = new ArrayList<MultipartFile>();
		String type = "";
		String TYPE[] = pd.getString("TYPES").split(",");
		for (int i = 0; i < TYPE.length; i++) {
			type = TYPE[i];
			fileList.add(request.getFile("file"+i));
			MultipartFile[] fileArr = new MultipartFile[fileList.size()];
			saveImg(fileList.toArray(fileArr),type,pd.getString("SPECIALEQUIPMENTAQXJ_ID"),pd.getString("CORPINFO_ID"));
			fileList.clear();
		}

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

	private void saveImg(MultipartFile[] files,String TYPE, String FOREIGN_KEY,String CORPINFO_ID) throws Exception{
		String FILEPATH = Const.FILEPATHIMGSPECIAL;
		if (files != null && files.length > 0) {
			for (int i = 0; i < files.length; i++) {
				MultipartFile file = files[i];

				String  ffile = DateUtil.getDays();
				String fileName = this.get32UUID()+file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
				smb.sshSftp(file, fileName,FILEPATH + CORPINFO_ID + "/" + ffile);
				PageData pd = new PageData();
				pd.put("IMGFILES_ID",  this.get32UUID());
				pd.put("FILEPATH",  FILEPATH + CORPINFO_ID + "/" + ffile + "/" + fileName);
				pd.put("TYPE", TYPE);
				pd.put("FOREIGN_KEY",  FOREIGN_KEY);
				imgfilesService.save(pd);
			}
		}
	}

	/**删除
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/delete")
	// @RequiresPermissions("specialequipmentaqxj: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()));	//修改时间
		specialequipmentaqxjService.delete(pd);
		map.put("result", errInfo);				//返回结果
		return map;
	}

	/**修改
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/edit")
	// @RequiresPermissions("specialequipmentaqxj: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()));	//修改时间
		specialequipmentaqxjService.edit(pd);
		map.put("pd", 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());
		page.setPd(pd);
		List<PageData>	varList = specialequipmentaqxjService.list(page);	//列出SpecialEquipmentAqxj列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

	/**列表（通过特种设备ID获取数据）
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/listByEquipment")
	@ResponseBody
	@LogAnno(menuType= "手机",menuServer= "特种设备安全巡检管理",instructionsOperate = "特种设备安全巡检管理",instructionsType = "通过特种设备ID获取数据")
	public Object listByEquipment(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 = specialequipmentaqxjService.listByEquipmentlistPage(page);	//列出SpecialEquipmentAqxj列表
		map.put("varList", varList);
		map.put("page", page);
		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 = specialequipmentaqxjService.findById(pd);	//根据ID读取

		PageData pd2 = new PageData();
		pd2.put("FOREIGN_KEY", pd.getString("SPECIALEQUIPMENTAQXJ_ID"));
		pd2.put("TYPE",24);
		List<PageData> faultImgs = imgfilesService.listAll(pd2);		//特种设备故障照片

		pd2 = new PageData();
		pd2.put("FOREIGN_KEY", pd.getString("SPECIALEQUIPMENTAQXJ_ID"));
		pd2.put("TYPE",25);
		List<PageData> handleImgs = imgfilesService.listAll(pd2);		//特种设备故障处理后照片

		pd2 = new PageData();
		pd2.put("FOREIGN_KEY", pd.getString("SPECIALEQUIPMENTAQXJ_ID"));
		pd2.put("TYPE",26);
		List<PageData> inspectionImgs = imgfilesService.listAll(pd2);		//特种设备巡检照片
		map.put("pd", pd);
		map.put("faultImgs", faultImgs);
		map.put("handleImgs", handleImgs);
		map.put("inspectionImgs", inspectionImgs);
		map.put("result", errInfo);
		return map;
	}

	/**批量删除
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteAll")
	// @RequiresPermissions("specialequipmentaqxj: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
			specialequipmentaqxjService.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("巡检人");	//2
		titles.add("巡检时间");	//3
		titles.add("巡检内容");	//4
		titles.add("是否有故障");	//5
		titles.add("是否删除(0:有效 1：删除)");	//6
		titles.add("添加人");	//7
		titles.add("添加时间");	//8
		titles.add("修改人");	//9
		titles.add("修改时间");	//10
		dataMap.put("titles", titles);
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID
		List<PageData> varOList = specialequipmentaqxjService.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("SPECIALEQUIPMENT_ID"));	    //1
			vpd.put("var2", varOList.get(i).getString("INSPECTOR"));	    //2
			vpd.put("var3", varOList.get(i).getString("INSPECTIONTIME"));	    //3
			vpd.put("var4", varOList.get(i).getString("INSPECTIONCONTENT"));	    //4
			vpd.put("var5", varOList.get(i).getString("ISFAULT"));	    //5
			vpd.put("var6", varOList.get(i).get("ISDELETE").toString());	//6
			vpd.put("var7", varOList.get(i).getString("CREATOR"));	    //7
			vpd.put("var8", varOList.get(i).getString("CREATTIME"));	    //8
			vpd.put("var9", varOList.get(i).getString("OPERATOR"));	    //9
			vpd.put("var10", varOList.get(i).getString("OPERATTIME"));	    //10
			varList.add(vpd);
		}
		dataMap.put("varList", varList);
		ObjectExcelView erv = new ObjectExcelView();
		mv = new ModelAndView(erv,dataMap);
		return mv;
	}

}
