package com.zcloud.service.inspection.impl;

import com.zcloud.entity.Page;
import com.zcloud.entity.PageData;
import com.zcloud.mapper.datasource.inspection.SafetyEnvironmentalExplainMapper;
import com.zcloud.service.hidden.HiddenService;
import com.zcloud.service.inspection.SafetyEnvironmentalExplainService;
import com.zcloud.service.inspection.SafetyEnvironmentalLogService;
import com.zcloud.service.inspection.SafetyEnvironmentalService;
import com.zcloud.util.Tools;
import com.zcloud.util.UuidUtil;
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.List;

/**
 * 说明：安全环保检查申辩
 * 作者：luoxiaobao
 * 时间：2022-06-08
 * 官网：www.zcloudchina.com
 */
@Service
@Transactional //开启事物
public class SafetyEnvironmentalExplainServiceImpl implements SafetyEnvironmentalExplainService{

	@Resource
	private SafetyEnvironmentalExplainMapper safetyenvironmentalexplainMapper;

	@Autowired
	private SafetyEnvironmentalService safetyEnvironmentalService;
	@Autowired
	private HiddenService hiddenService;
	@Autowired
	private SafetyEnvironmentalLogService safetyenvironmentallogService;

	/**新增
	 * @param pd
	 * @throws Exception
	 */
	@Transactional
	public void save(PageData pd)throws Exception{
		PageData inspection = safetyEnvironmentalService.findById(pd);
		inspection.put("INSPECTED_EXPLAIN", pd.getString("INSPECTED_EXPLAIN"));
		if (Tools.notEmpty(pd.getString("INSPECTED_EXPLAIN_FILENAME"))) {
			inspection.put("INSPECTED_EXPLAIN_FILENAME", pd.getString("INSPECTED_EXPLAIN_FILENAME"));
		}
		if (Tools.notEmpty(pd.getString("INSPECTED_EXPLAIN_FILEPATH"))) {
			inspection.put("INSPECTED_EXPLAIN_FILEPATH", pd.getString("INSPECTED_EXPLAIN_FILEPATH"));
		}
		inspection.put("INSPECTED_SITEUSER_SIGN_IMG", pd.getString("INSPECTED_SITEUSER_SIGN_IMG"));
		inspection.put("INSPECTED_SITEUSER_SIGN_TIME", pd.getString("INSPECTED_SITEUSER_SIGN_TIME"));
		inspection.put("INSPECTION_STATUS", pd.getString("INSPECTION_STATUS"));
		inspection.put("OPERATOR", pd.getString("OPERATOR"));
		inspection.put("OPERATTIME", pd.getString("OPERATTIME"));
		PageData inspectionList = new PageData(); //如果安全环保检查没有隐患，直接把状态改为7已验收
		inspectionList.put("FOREIGN_ID",inspection.getString("INSPECTION_ID"));
		List<PageData> hiddenList = hiddenService.listAllInspection(inspectionList);
		if(hiddenList == null || hiddenList.size() < 1){
			inspection.put("INSPECTION_STATUS","8");
			pd.put("INSPECTION_STATUS","8");
		}
		inspection.put("unClear","1");
		inspection.put("explainId",pd.getString("INSPECTION_EXPLAIN_ID"));
		safetyEnvironmentalService.edit(inspection);
		if ("3".equals(pd.getString("INSPECTION_STATUS"))) {
			pd.put("STATE", "101");
			pd.put("INSPECTION_SOURCE",inspection.getString("INSPECTION_SOURCE"));
			hiddenService.editStateInspection(pd);
		}

		PageData log = new PageData();
		log.put("INSPECTION_LOG_ID", UuidUtil.get32UUID());    //安全环保检查操作记录ID
		log.put("INSPECTION_ID", pd.getString("INSPECTION_ID"));    //安全环保检查ID
		log.put("ACTION", "被检查人确认检查");    //操作内容
		log.put("CREATTIME", pd.getString("OPERATTIME"));	//添加时间
		log.put("CREATOR", pd.getString("ACTION_USER"));	//添加人姓名
		safetyenvironmentallogService.save(log);

		safetyenvironmentalexplainMapper.save(pd);
	}

	/**删除
	 * @param pd
	 * @throws Exception
	 */
	public void delete(PageData pd)throws Exception{
		safetyenvironmentalexplainMapper.delete(pd);
	}

	/**修改
	 * @param pd
	 * @throws Exception
	 */
	public void edit(PageData pd)throws Exception{
		safetyenvironmentalexplainMapper.edit(pd);
	}

	/**列表
	 * @param page
	 * @throws Exception
	 */
	public List<PageData> list(Page page)throws Exception{
		return safetyenvironmentalexplainMapper.datalistPage(page);
	}

	/**列表(全部)
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> listAll(PageData pd)throws Exception{
		return safetyenvironmentalexplainMapper.listAll(pd);
	}

	/**通过id获取数据
	 * @param pd
	 * @throws Exception
	 */
	public PageData findById(PageData pd)throws Exception{
		return safetyenvironmentalexplainMapper.findById(pd);
	}

	/**批量删除
	 * @param pd
	 * @throws Exception
	 */
	public void deleteAll(PageData pd)throws Exception{
		safetyenvironmentalexplainMapper.deleteAll(pd);
	}

	/**列表(根据所选ID获取数据)
	 * @param pd
	 * @throws Exception
	 */
	public List<PageData> findByIds(PageData pd) throws Exception {
		return safetyenvironmentalexplainMapper.findByIds(pd);
	}

	/**删除（根据检查ID）
	 * @param pd
	 * @throws Exception
	 */
	@Override
	public void deleteByInspection(PageData pd) throws Exception {
		safetyenvironmentalexplainMapper.deleteByInspection(pd);
	}

	@Override
	public List<PageData> findExplainById(PageData pd) throws Exception {
		return safetyenvironmentalexplainMapper.findExplainById(pd);
	}

	@Override
	public void killAllMessage(PageData explain_condition) throws Exception {
		safetyenvironmentalexplainMapper.killAllMessage(explain_condition);
	}

	@Override
	public PageData findMessage(PageData explain_condition) throws Exception {
		return safetyenvironmentalexplainMapper.findMessage(explain_condition);
	}

	@Override
	public PageData findMessageByTime(PageData explain_condition) {
		return safetyenvironmentalexplainMapper.findMessageByTime(explain_condition);
	}

}

