package com.zcloud.controller.safetyInvestment;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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.safetyInvestment.SafetyInvestmentPlanInfoService;
import com.zcloud.service.safetyInvestment.SafetyInvestmentPlanService;
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.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * 说明：安全投入计划
 * 作者：luoxiaobao
 * 时间：2022-05-13
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/safetyinvestmentplan")
public class SafetyInvestmentPlanController extends BaseController {

	@Autowired
	private SafetyInvestmentPlanService safetyinvestmentplanService;
	@Autowired
	private SafetyInvestmentPlanInfoService safetyinvestmentplanInfoService;

	/**新增
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/add")
	// @RequiresPermissions("safetyinvestmentplan:add")
	@ResponseBody
	@Transactional
	@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("SAFETYINVESTMENTPLAN_ID", this.get32UUID());	//主键
		pd.put("CREATOR", Jurisdiction.getName());	//添加人
		pd.put("CREATTIME", DateUtil.date2Str(new Date()));	//添加时间
		pd.put("OPERATOR", Jurisdiction.getName());	//修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		pd.put("ISDELETE", "0");	//是否删除 1-是  0-否
		pd.put("STATUS", 0);
		pd.put("CORPINFO_ID", Jurisdiction.getCORPINFO_ID());	//企业ID
		safetyinvestmentplanService.save(pd);
		List<JSONObject> list = (List<JSONObject>) JSON.parse(pd.get("info").toString());
		for (JSONObject json : list) {
			PageData info = new PageData();
			info.put("SAFETYINVESTMENTPLANINFO_ID", this.get32UUID()); // 主键
			info.put("SAFETYINVESTMENTPLAN_ID", pd.get("SAFETYINVESTMENTPLAN_ID")); // 主键
			info.put("TYPE", json.get("TYPE"));
			info.put("DESCR", json.get("DESCR"));
			info.put("AMOUNT", json.get("AMOUNT"));
			safetyinvestmentplanInfoService.save(info);
		}
		map.put("result", errInfo);
		return map;
	}

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

	/**修改
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/edit")
	// @RequiresPermissions("safetyinvestmentplan:edit")
	@ResponseBody
	@Transactional
	@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.getName());	//修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		safetyinvestmentplanService.edit(pd);
		safetyinvestmentplanInfoService.delete(pd);
		List<JSONObject> list = (List<JSONObject>) JSON.parse(pd.get("info").toString());
		for (JSONObject json : list) {
			PageData info = new PageData();
			info.put("SAFETYINVESTMENTPLANINFO_ID", this.get32UUID()); // 主键
			info.put("SAFETYINVESTMENTPLAN_ID", pd.get("SAFETYINVESTMENTPLAN_ID")); // 主键
			info.put("TYPE", json.get("TYPE"));
			info.put("DESCR", json.get("DESCR"));
			info.put("AMOUNT", json.get("AMOUNT"));
			safetyinvestmentplanInfoService.save(info);
		}
		map.put("result", errInfo);
		return map;
	}

	/**修改
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/editStatus")
	// @RequiresPermissions("safetyinvestmentplan:edit")
	@ResponseBody
	@Transactional
	@LogAnno(menuType= "综合管理",menuServer= "安全投入",instructionsOperate = "安全投入计划",instructionsType = "修改")
	public Object editStatus() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("OPERATOR", Jurisdiction.getName());	//修改人
		pd.put("OPERATTIME", DateUtil.date2Str(new Date()));	//修改时间
		safetyinvestmentplanService.editStatus(pd);
		map.put("result", errInfo);
		return map;
	}

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

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

	 /**批量删除
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/deleteAll")
	// @RequiresPermissions("safetyinvestmentplan: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(",");
			safetyinvestmentplanService.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("计划年份");	//1
		titles.add("计划周期");	//2
		titles.add("总金额");	//3
		titles.add("是否删除");	//4
		titles.add("添加人");	//5
		titles.add("添加时间");	//6
		titles.add("修改人");	//7
		titles.add("修改时间");	//8
		dataMap.put("titles", titles);
		List<PageData> varOList = safetyinvestmentplanService.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).get("PLAN_YEAR").toString());	//1
			vpd.put("var2", varOList.get(i).getString("PLAN_PERIOD"));	    //2
			vpd.put("var3", varOList.get(i).getString("TOTAL_AMOUNT"));	    //3
			vpd.put("var4", varOList.get(i).get("ISDELETE").toString());	//4
			vpd.put("var5", varOList.get(i).getString("CREATOR"));	    //5
			vpd.put("var6", varOList.get(i).getString("CREATTIME"));	    //6
			vpd.put("var7", varOList.get(i).getString("OPERATOR"));	    //7
			vpd.put("var8", varOList.get(i).getString("OPERATTIME"));	    //8
			varList.add(vpd);
		}
		dataMap.put("varList", varList);
		ObjectExcelView erv = new ObjectExcelView();
		mv = new ModelAndView(erv,dataMap);
		return mv;
	}

}
