package com.zcloud.controller.commitment;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.PageData;
import com.zcloud.logs.LogAnno;
import com.zcloud.service.commitment.CommitmentCompanyService;
import com.zcloud.service.system.UsersService;
import com.zcloud.util.*;
import org.apache.commons.lang.StringUtils;
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.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 说明：公司级承诺
 * 作者：luoxiaobao
 * 时间：2021-10-08
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/commitmentcompany")
public class CommitmentCompanyController extends BaseController {

	@Autowired
	private CommitmentCompanyService commitmentcompanyService;
	@Autowired
	private UsersService usersService;
	@Autowired
	private Smb smb;
	/**新增
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/add")
	// @RequiresPermissions("commitmentcompany: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("COMMITMENTCOMPANY_ID", this.get32UUID());	//主键
		commitmentcompanyService.save(pd);
		map.put("result", errInfo);
		return map;
	}

	/** 补录
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/replenishSave")
//	// @RequiresPermissions("commitmentteam:add")
	@ResponseBody
	@LogAnno(menuType= "监测预警",menuServer= "安全承诺",instructionsOperate = "公司级承诺公告",instructionsType = "补录")
	public Object replenishSave() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("COMMITMENTCOMPANY_ID", this.get32UUID());	//主键
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID
		pd.put("SIGN_TIME", pd.getString("SIGN_TIME")+" 00:00:00");	//签字时间

		List<PageData>	userList = usersService.listUserBYids(pd.getString("SIGN_USER").split(","));
		if (userList != null && userList.size()>0) {
			pd.put("DEPARTMENT_ID", userList.get(0).getString("DEPARTMENT_ID"));	//车间ID
//			pd.put("DEPARTMENTNAME", userList.get(0).getString("DEPARTMENT_NAME"));	//车间名称
//			pd.put("POSTNAME", userList.get(0).getString("POST_NAME"));	//岗位名称
		}
		if (StringUtils.isNotBlank(pd.getString("SIGN_PATH"))) {
			MultipartFile file = BASE64DecodedMultipartFile.base64ToMultipart(pd.getString("SIGN_PATH"));
			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);
		}
		commitmentcompanyService.save(pd);
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}

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

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

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	// @RequiresPermissions("commitmentcompany:list")
	@ResponseBody
	@LogAnno(menuType= "监测预警",menuServer= "安全承诺",instructionsOperate = "公司级承诺公告",instructionsType = "列表")
	public Object list() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		List<PageData>	varList = commitmentcompanyService.listAll(pd);	//列出CommitmentCompany列表
		map.put("varList", varList);
		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 = commitmentcompanyService.findById(pd);	//根据ID读取
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}

	 /**批量删除
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteAll")
	// @RequiresPermissions("commitmentcompany: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(",");
			commitmentcompanyService.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("radio1");	//16
		titles.add("radio2");	//17
		titles.add("radio3");	//18
		dataMap.put("titles", titles);
		List<PageData> varOList = commitmentcompanyService.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("RADIO1").toString());	//16
			vpd.put("var17", varOList.get(i).get("RADIO2").toString());	//17
			vpd.put("var18", varOList.get(i).get("RADIO3").toString());	//18
			varList.add(vpd);
		}
		dataMap.put("varList", varList);
		ObjectExcelView erv = new ObjectExcelView();
		mv = new ModelAndView(erv,dataMap);
		return mv;
	}

}
