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.CommitmentWorkshopService;
import com.zcloud.service.system.DepartmentService;
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.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-11
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/app/commitmentworkshop")
public class AppCommitmentWorkshopController extends BaseController {

	@Autowired
	private CommitmentWorkshopService commitmentworkshopService;
	@Autowired
	private DepartmentService departmentService;
	@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("departmentLevel0002")){
			errInfo = "fail";
			map.put("msg", "无此权限");
		}
		pd.put("SIGN_TIME", DateUtil.date2Str(new Date())); // 创建时间
		if(commitmentworkshopService.listAll(pd).size()>0){
			errInfo = "fail";
			map.put("msg", "此班组本日安全承诺已完成，无需重复操作。");
		}
		map.put("result", errInfo);
		return map;
	}
	/**新增
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/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("COMMITMENTWORKSHOP_ID", this.get32UUID());	//主键
		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);
			pd.put("SIGN_TIME",  DateUtil.date2Str(new Date()));
		}
		commitmentworkshopService.save(pd);
		map.put("result", errInfo);
		return map;
	}

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

	/**修改
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/edit")
	// @RequiresPermissions("commitmentworkshop: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();
		commitmentworkshopService.edit(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 = commitmentworkshopService.list(page);	//列出CommitmentWorkshop列表
		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 = commitmentworkshopService.findById(pd);	//根据ID读取
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}

	 /**批量删除
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteAll")
	// @RequiresPermissions("commitmentworkshop: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(",");
			commitmentworkshopService.deleteAll(ArrayDATA_IDS);
			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("输入1");	//6
		titles.add("输入2");	//7
		titles.add("输入3");	//8
		titles.add("输入4");	//9
		titles.add("输入5");	//10
		titles.add("输入6");	//11
		titles.add("输入7");	//12
		titles.add("输入8");	//13
		titles.add("输入9");	//14
		titles.add("输入10");	//15
		titles.add("INPUT11");	//16
		titles.add("INPUT12");	//17
		titles.add("INPUT13");	//18
		titles.add("INPUT14");	//19
		titles.add("INPUT15");	//20
		titles.add("INPUT16");	//21
		titles.add("INPUT17");	//22
		titles.add("INPUT18");	//23
		titles.add("RADIO1");	//24
		titles.add("RADIO2");	//25
		titles.add("RADIO3");	//26
		titles.add("RADIO4");	//27
		titles.add("RADIO5");	//28
		titles.add("RADIO6");	//29
		titles.add("RADIO7");	//30
		titles.add("RADIO8");	//31
		titles.add("RADIO9");	//32
		titles.add("RADIO10");	//33
		titles.add("RADIO11");	//34
		titles.add("RADIO12");	//35
		titles.add("RADIO13");	//36
		titles.add("RADIO14");	//37
		titles.add("开始时间");	//38
		titles.add("结束时间");	//39
		dataMap.put("titles", titles);
		List<PageData> varOList = commitmentworkshopService.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).get("INPUT1").toString());	//6
			vpd.put("var7", varOList.get(i).get("INPUT2").toString());	//7
			vpd.put("var8", varOList.get(i).get("INPUT3").toString());	//8
			vpd.put("var9", varOList.get(i).get("INPUT4").toString());	//9
			vpd.put("var10", varOList.get(i).get("INPUT5").toString());	//10
			vpd.put("var11", varOList.get(i).get("INPUT6").toString());	//11
			vpd.put("var12", varOList.get(i).get("INPUT7").toString());	//12
			vpd.put("var13", varOList.get(i).get("INPUT8").toString());	//13
			vpd.put("var14", varOList.get(i).getString("INPUT9"));	    //14
			vpd.put("var15", varOList.get(i).get("INPUT10").toString());	//15
			vpd.put("var16", varOList.get(i).get("INPUT11").toString());	//16
			vpd.put("var17", varOList.get(i).get("INPUT12").toString());	//17
			vpd.put("var18", varOList.get(i).get("INPUT13").toString());	//18
			vpd.put("var19", varOList.get(i).get("INPUT14").toString());	//19
			vpd.put("var20", varOList.get(i).get("INPUT15").toString());	//20
			vpd.put("var21", varOList.get(i).get("INPUT16").toString());	//21
			vpd.put("var22", varOList.get(i).get("INPUT17").toString());	//22
			vpd.put("var23", varOList.get(i).get("INPUT18").toString());	//23
			vpd.put("var24", varOList.get(i).get("RADIO1").toString());	//24
			vpd.put("var25", varOList.get(i).get("RADIO2").toString());	//25
			vpd.put("var26", varOList.get(i).get("RADIO3").toString());	//26
			vpd.put("var27", varOList.get(i).get("RADIO4").toString());	//27
			vpd.put("var28", varOList.get(i).get("RADIO5").toString());	//28
			vpd.put("var29", varOList.get(i).get("RADIO6").toString());	//29
			vpd.put("var30", varOList.get(i).get("RADIO7").toString());	//30
			vpd.put("var31", varOList.get(i).get("RADIO8").toString());	//31
			vpd.put("var32", varOList.get(i).get("RADIO9").toString());	//32
			vpd.put("var33", varOList.get(i).get("RADIO10").toString());	//33
			vpd.put("var34", varOList.get(i).get("RADIO11").toString());	//34
			vpd.put("var35", varOList.get(i).get("RADIO12").toString());	//35
			vpd.put("var36", varOList.get(i).get("RADIO13").toString());	//36
			vpd.put("var37", varOList.get(i).get("RADIO14").toString());	//37
			vpd.put("var38", varOList.get(i).getString("START_TIME"));	    //38
			vpd.put("var39", varOList.get(i).getString("END_TIME"));	    //39
			varList.add(vpd);
		}
		dataMap.put("varList", varList);
		ObjectExcelView erv = new ObjectExcelView();
		mv = new ModelAndView(erv,dataMap);
		return mv;
	}

}
