package com.zcloud.controller.bus;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zcloud.service.bus.MaintenanceUserInfoService;
import com.zcloud.util.Jurisdiction;
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.servlet.ModelAndView;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.Page;
import com.zcloud.util.DateUtil;
import com.zcloud.util.ObjectExcelView;
import com.zcloud.util.Tools;
import com.zcloud.entity.PageData;
import com.zcloud.service.bus.CorpVisitService;

/**
 * 说明：企业回访
 * 作者：luoxiaobao
 * 时间：2023-08-04
 * 官网：www.zcloudchina.com
 */
@Controller
@RequestMapping("/corpvisit")
public class CorpVisitController extends BaseController {
	@Autowired
	private CorpVisitService corpvisitService;
	@Autowired
	private MaintenanceUserInfoService maintenanceUserInfoService;

	/**获取回访设置
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/getConfig")
	@ResponseBody
	public Object getConfig() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd = corpvisitService.getConfig(pd);
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}

	/**回访设置
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/setConfig")
	@ResponseBody
	public Object setConfig() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		maintenanceUserInfoService.setVisitConfig(pd);

		pd.put("CURRENT",1);
		List<PageData> records = corpvisitService.listAll(pd); //获取企业是否有周期包含当前时间的回访记录

		// 如果企业没有回访记录,新建一条本周期临时数据 如果企业有回访记录,则在添加回访记录时新建一条下一周期临时数据
		if(records.size() == 0 && pd.getString("VISIT_STATE").equals("1")) {
			PageData visit = new PageData();
			visit.put("CORPVISIT_ID", this.get32UUID());    //主键
			visit.put("CORPINFO_ID", pd.getString("CORPINFO_ID"));
			visit.put("PERIOD_START_TIME", DateUtil.date2Str(new Date()));
			visit.put("PERIOD_END_TIME", DateUtil.date2Str(DateUtil.TimePlus(new Date(), Integer.parseInt(pd.getString("VISIT_PERIOD")))));
			visit.put("STATUS", -1);
			corpvisitService.save(visit);
		}else if(pd.getString("VISIT_STATE").equals("2")){//如果是停止回访,则删除所有临时周期数据
			corpvisitService.deleteAll(pd);
		}
		map.put("result", errInfo);
		return map;
	}

	/**新增
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/add")
	@ResponseBody
	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("CURRENT",1);
		List<PageData> records = corpvisitService.listAll(pd); //获取企业是否有周期包含当前时间的回访记录
		if(records.size() > 0 ){
			if("-1".equals(records.get(0).get("STATUS").toString())){
				pd.put("STATUS",1);
				pd.put("VISIT_TIME",DateUtil.date2Str(new Date()));
				pd.put("CREATOR",Jurisdiction.getUsername());
				pd.put("CORPVISIT_ID",records.get(0).getString("CORPVISIT_ID"));
				corpvisitService.edit(pd);
			} else {
				PageData visit = new PageData();
				visit.put("CORPVISIT_ID", this.get32UUID());    //主键
				visit.put("CORPINFO_ID", pd.getString("CORPINFO_ID"));
				visit.put("PERIOD_START_TIME", records.get(0).get("PERIOD_START_TIME"));
				visit.put("PERIOD_END_TIME", records.get(0).get("PERIOD_END_TIME"));
				visit.put("DESCR1", pd.getString("DESCR1"));
				visit.put("DESCR2", pd.getString("DESCR2"));
				visit.put("DESCR3", pd.getString("DESCR3"));
				visit.put("STATUS", 1);
				visit.put("CREATOR",Jurisdiction.getUsername());
				visit.put("VISIT_TIME",DateUtil.date2Str(new Date()));
				corpvisitService.save(visit);
			}

			pd.put("FUTURE",1);
			corpvisitService.deleteAll(pd);  //先删除下一个周期的临时记录
			PageData record =records.get(0);
			String PERIOD_END_TIME = record.getString("PERIOD_END_TIME");
			SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
			Date periodEndTime = sdf.parse(PERIOD_END_TIME);
			PageData visit = new PageData();
			visit.put("CORPVISIT_ID", this.get32UUID());    //主键
			visit.put("CORPINFO_ID", pd.getString("CORPINFO_ID"));
			visit.put("PERIOD_START_TIME", DateUtil.date2Str(periodEndTime));
			visit.put("PERIOD_END_TIME", DateUtil.date2Str(DateUtil.TimePlus(periodEndTime, Integer.parseInt(records.get(0).get("VISIT_PERIOD").toString()))));
			visit.put("STATUS", -1);
			corpvisitService.save(visit);
		} else {
			PageData visit = new PageData();
			visit.put("CORPVISIT_ID", this.get32UUID());    //主键
			visit.put("CORPINFO_ID", pd.getString("CORPINFO_ID"));
			visit.put("DESCR1", pd.getString("DESCR1"));
			visit.put("DESCR2", pd.getString("DESCR2"));
			visit.put("DESCR3", pd.getString("DESCR3"));
			visit.put("STATUS", 1);
			visit.put("CREATOR",Jurisdiction.getUsername());
			visit.put("VISIT_TIME",DateUtil.date2Str(new Date()));
			corpvisitService.save(visit);
		}

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

	/**列表
	 * @param page
	 * @throws Exception
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	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 = corpvisitService.list(page);	//列出CorpVisit列表
		map.put("varList", varList);
		map.put("page", page);
		map.put("result", errInfo);
		return map;
	}

	 /**去修改页面获取数据
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/goEdit")
	@ResponseBody
	public Object goEdit() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		pd = corpvisitService.findById(pd);	//根据ID读取
		map.put("pd", pd);
		map.put("result", errInfo);
		return map;
	}


	 /**导出到excel
	 * @param
	 * @throws Exception
	 */
	@RequestMapping(value="/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
		titles.add("提出的优化");	//9
		dataMap.put("titles", titles);
		List<PageData> varOList = corpvisitService.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("CORPINFO_ID"));	    //1
			vpd.put("var2", varOList.get(i).getString("PERIOD_START_TIME"));	    //2
			vpd.put("var3", varOList.get(i).getString("PERIOD_END_TIME"));	    //3
			vpd.put("var4", varOList.get(i).getString("VISIT_TIME"));	    //4
			vpd.put("var5", varOList.get(i).get("STATUS").toString());	//5
			vpd.put("var6", varOList.get(i).getString("CREATOR"));	    //6
			vpd.put("var7", varOList.get(i).getString("DESCR1"));	    //7
			vpd.put("var8", varOList.get(i).getString("DESCR2"));	    //8
			vpd.put("var9", varOList.get(i).getString("DESCR3"));	    //9
			varList.add(vpd);
		}
		dataMap.put("varList", varList);
		ObjectExcelView erv = new ObjectExcelView();
		mv = new ModelAndView(erv,dataMap);
		return mv;
	}

}
