package com.zcloud.service.hiddenApi.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.hiddenApi.HiddenApiMapper;
import com.zcloud.service.bus.HiddenRegionService;
import com.zcloud.service.hiddenApi.HiddenApiService;
import com.zcloud.service.system.ImgFilesService;
import com.zcloud.service.system.UsersService;
import com.zcloud.util.DateUtil;
import com.zcloud.util.HttpEmisDuijieUtil;
import com.zcloud.util.Jurisdiction;
import com.zcloud.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 说明：隐患
 * 作者：luoxiaobao
 * 时间：2021-01-04
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class HiddenApiServiceImpl implements HiddenApiService{

	@Resource
	private HiddenApiMapper hiddenApiMapper;
	@Autowired
	private UsersService usersService;
	@Autowired
	private HiddenRegionService hiddenRegionService; // 隐患区域维护
	@Autowired
	private ImgFilesService imgFilesService;
	/**新增
	 * @param pd
	 * @throws Exception
	 */
	public void saveApi(PageData pd)throws Exception{
		hiddenApiMapper.save(pd);
	}

	/**删除
	 * @param
	 * @throws Exception
	 */
	public int deleteApi(String hiddenId)throws Exception{
		return  hiddenApiMapper.delete(hiddenId);
	}
	/**通过id获取数据
	 * @param pd
	 * @throws Exception
	 */
	public PageData findById(PageData pd)throws Exception{

		return hiddenApiMapper.findById(pd);
	}
	public void confirmHidden(PageData pd) throws Exception{
		PageData conPd = new PageData();
		conPd.put("STATE",pd.getString("STATE"));
		conPd.put("CONFIRM_USER", Jurisdiction.getUSER_ID());
		conPd.put("CONFIRM_TIME",DateUtil.date2Str(new Date()));
		conPd.put("RECTIFICATIONDEPT",pd.getString("RECTIFICATIONDEPT"));
		conPd.put("RECTIFICATIONOR",pd.getString("RECTIFICATIONOR"));
		conPd.put("RECTIFICATIONDEADLINE",pd.getString("RECTIFICATIONDEADLINE"));
		conPd.put("HIDDENLEVEL",pd.getString("HIDDENLEVEL"));
		conPd.put("HIDDEN_ID",pd.getString("HIDDEN_ID"));
		hiddenApiMapper.confirmHidden(conPd);
	}
	/**
	 * 隐患整改
	 * @param hiddenId 隐患ID
	 * @param rectifyDescr 整改描述
	 * @param rectificationTime 整改时间
	 * @param havescheme 是否有整改方案  0无  1有
	 * @param investmentFunds 投入资金 港务局添加字段
	 * @return
	 * @throws Exception
	 */
	@Override
	public int rectify(String hiddenId, String rectifyDescr,String rectificationTime, String havescheme, String investmentFunds ) throws Exception {
		PageData pd = new PageData();
		pd.put("RECTIFYDESCR",rectifyDescr);
		pd.put("RECTIFICATIONTIME",rectificationTime);
		pd.put("HAVESCHEME",havescheme);
		pd.put("INVESTMENT_FUNDS",investmentFunds);
		pd.put("STATE","2");
		pd.put("HIDDEN_ID",hiddenId);
		PageData byId = hiddenApiMapper.findById(pd);
		if (byId.get("STATE").equals("-1")){
			pd.put("ISEXPIREREPAIR","1");
		}else{
			pd.put("ISEXPIREREPAIR","0");
		}
		return hiddenApiMapper.rectify(pd);
	}
	/**验收
	 * @param hiddenId
	 * @throws Exception
	 */
	public void check(String hiddenId ,String checkTime)throws Exception{
		PageData pd = new PageData();
		pd.put("HIDDEN_ID",hiddenId);
		pd.put("CHECKTIME",checkTime);
		hiddenApiMapper.check(pd);
	}
	/**
	 * 验收打回
	 * @param hiddenId
	 * @return
	 * @throws Exception
	 */
	public int repulseCheck(String hiddenId)throws Exception{
		return hiddenApiMapper.repulseCheck(hiddenId);
	}
	public Integer goEmis (PageData pd) throws Exception{
		// 1.整理数据
		PageData hiddenPd = this.findById(pd);	/**  确认之前，记录现在的隐患信息 */
		//  隐患确认人
		PageData dianjianUserPd = new PageData();
		dianjianUserPd.put("USER_ID",hiddenPd.getString("CONFIRM_USER"));
		PageData dianjianUser = usersService.findByIdForEmis(dianjianUserPd); //找到确认人
		if(Tools.isEmpty(dianjianUser)){
			return 000210;
		}

		if(Tools.isEmpty(dianjianUser.get("DJBM"))){
			return 000211;
		}
		if(Tools.isEmpty(dianjianUser.get("JCR"))){
			return 000212;
		}
		PageData  hiddenRegion= hiddenRegionService.findById(hiddenPd.getString("CORPINFO_ID"),hiddenPd.getString("HIDDENPART"));
		PageData hiddenRegionPar = new PageData();
		if(null != hiddenRegion){
			if(!hiddenRegion.getString("PARENT_ID").equals("0")){
				hiddenRegionPar = hiddenRegionService.findById(hiddenPd.getString("CORPINFO_ID"),hiddenRegion.getString("PARENT_ID"));
				if(!"0".equals(hiddenRegionPar.getString("PARENT_ID"))){
					return 000213;
				}
			}
		}else{
			return 000214;
		}

		List<PageData> imgList = imgFilesService.getListByKeyAndType(hiddenPd.getString("HIDDEN_ID"),"3");
		String imgStr = "";
		for (PageData pageData:imgList){
			imgStr += "192.168.192.201/file" + pageData.getString("FILEPATH")+";";
		}
		String DJSJ  = DateUtil.date2Str(new Date()); // 点检时间
		String BC  = ""; // 班次,自动算
		String DJBM  = dianjianUser.get("DJBM").toString(); // 点检部门t_b_bm
		String DJBC  = pd.getString("DJBC"); // 点检班次
		String JCR  = dianjianUser.get("JCR").toString(); // 检查人
		String JCLX  =  pd.getString("JCLX"); // 检查类型t_b_jclx--fid
		String SBDL  = hiddenRegionPar.getString("SBDL"); // 设备大类t_b_sbdlxx--sbdlxxdm
		String SBMC  =  hiddenRegion.getString("SBMC");; // 设备名称t_b_shebeixx--shebeidm
		String DJWT  = hiddenPd.getString("HIDDENDESCR"); // 点检问题
		String CJR  = dianjianUser.get("JCR").toString(); // 创建人
		String CJSJ  = DateUtil.date2Str(new Date()); // 创建时间
		String GZFL  = pd.getString("GZFL"); // 故障分类 t_b_kclx
		String SBZJGID  = ""; // 子机构v_b_bujian_zjg
		String BUJIANID  = ""; // 部件v_b_bujian_bj
		String PICTURE  = imgStr; // 图片

		StringBuffer strUrl = new StringBuffer();
		strUrl.append("strDjsj=" + DJSJ);
		strUrl.append("&strBc=" + "");
		strUrl.append("&strDjbm=" + DJBM);
		strUrl.append("&strDjbc=" + DJBC);
		strUrl.append("&strJcr=" + JCR);
		strUrl.append("&strJclx=" + JCLX);
		strUrl.append("&strSbdl=" + SBDL);
		strUrl.append("&strSbmc=" + SBMC);
		strUrl.append("&strDjwt=" + DJWT);
		strUrl.append("&strCjr=" + CJR);
		strUrl.append("&strCjsj=" + CJSJ);
		strUrl.append("&strGzfl=" + GZFL);
		strUrl.append("&strSbzjgId=" + SBZJGID);
		strUrl.append("&strBuJianId=" + BUJIANID);
		strUrl.append("&strPicture=" + PICTURE);
		String resXML = HttpEmisDuijieUtil.sendPostNoAccept(strUrl.toString());
		StringBuffer strUrl2 = new StringBuffer();
		strUrl2.append("djsj:" + DJSJ);
		strUrl2.append(",bc:" + "");
		strUrl2.append(",djbm:" + DJBM);
		strUrl2.append(",djbc:" + DJBC);
		strUrl2.append(",jcr:" + JCR);
		strUrl2.append(",jclx:" + JCLX);
		strUrl2.append(",sbdl:" + SBDL);
		strUrl2.append(",sbmc:" + SBMC);
		strUrl2.append(",djwt:" + DJWT);
		strUrl2.append(",cjr:" + CJR);
		strUrl2.append(",cjsj:" + CJSJ);
		strUrl2.append(",gzfl:" + GZFL);
		strUrl2.append(",sbzjgID:" + SBZJGID);
		strUrl2.append(",bujianid:" + BUJIANID);
		strUrl2.append(",pic:" + PICTURE);
		if(strUrl2.toString().equals(resXML)){
			/**
			 * 成功
			 * 1.删除现在的隐患内容
			 * 2.存储这次的隐患内容
			 */
			this.saveForEmis(hiddenPd.getString("HIDDEN_ID"),resXML);
			this.deleteForEmis(hiddenPd.getString("HIDDEN_ID"));
			//隐患发现人 等信息不要删除.查看对接完成的信息中查看
			return 000000;
		}else{
			//失败
			return 000215;
		}
	}
	public void saveForEmis(String hiddenId,String resXML)throws Exception{
		PageData pd = new PageData();
		pd.put("HIDDEN_ID",hiddenId);
		pd.put("resXML",resXML);
		hiddenApiMapper.saveForEmis(pd);
	}
	public void deleteForEmis(String hiddenId)throws Exception{
		PageData pd = new PageData();
		pd.put("HIDDEN_ID",hiddenId);
		hiddenApiMapper.deleteForEmis(pd);
	}

	/**
	 * 确认打回
	 * @param hiddenId
	 * @return
	 * @throws Exception
	 */
	public int repulseConfirm(String hiddenId)throws Exception{
		return hiddenApiMapper.repulseConfirm(hiddenId);
	}

	/**
	 * 隐患无法整改
	 * @param hiddenId
	 * @return
	 * @throws Exception
	 */
	public int unableRectify(String hiddenId)throws Exception{
		return hiddenApiMapper.unableRectify(hiddenId);
	}
	/**
	 * 隐患列表
	 * @param page
	 * @return
	 * @throws Exception
	 */
	public List<PageData> getList (Page page) throws Exception{
		return hiddenApiMapper.getListdatalistPage(page);
	}
	/**
	 * 删除隐患
	 * @param hiddenId
	 * @return
	 * @throws Exception
	 */
	public  int delete (String hiddenId) throws Exception{
		return hiddenApiMapper.delete(hiddenId);
	}

	public List<PageData> getListAll (PageData pd) throws Exception{
		return hiddenApiMapper.getListAll(pd);
	}
}

