package com.ruoyi.ehsHiddenDanger.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.ehsRisk.domain.EhsRisk;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.common.utils.StringUtils;
import java.util.List;
import java.util.Map;
import com.ruoyi.ehsHiddenDanger.mapper.EhsHiddenDangerMapper;
import com.ruoyi.ehsHiddenDanger.domain.EhsHiddenDanger;
import com.ruoyi.ehsHiddenDanger.service.IEhsHiddenDangerService;

import javax.validation.Validator;

/**
 * 隐患信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-11
 */
@Service
public class EhsHiddenDangerServiceImpl extends ServiceImpl<EhsHiddenDangerMapper, EhsHiddenDanger> implements IEhsHiddenDangerService {

	@Autowired
	private EhsHiddenDangerMapper ehsHiddenDangerMapper;
	@Autowired
	protected Validator validator;
	/**
	 * 查询隐患信息
	 *
	 * @param hiddenDangerId 隐患信息主键
	 * @return 隐患信息
	 */
	@Override
	public EhsHiddenDanger selectEhsHiddenDangerByHiddenDangerId(Long hiddenDangerId)
	{
		return ehsHiddenDangerMapper.selectEhsHiddenDangerByHiddenDangerId(hiddenDangerId);
	}

	/**
	 * 查询隐患信息列表
	 *
	 * @param ehsHiddenDanger 隐患信息
	 * @return 隐患信息
	 */
	@Override
	public List<EhsHiddenDanger> selectEhsHiddenDangerList(EhsHiddenDanger ehsHiddenDanger)
	{
		if(SecurityUtils.getLoginUser().getUser().getDept().getParentId()!=null &&
				SecurityUtils.getLoginUser().getUser().getDept().getParentId().longValue()!=0) {
			ehsHiddenDanger.setDeptId(SecurityUtils.getLoginUser().getUser().getDept().getDeptId().longValue());
		}
		if(StringUtils.isNotEmpty(ehsHiddenDanger.getAssistDept())){//协办就不限制只能查询本部门的数据了。
			ehsHiddenDanger.setDeptId(null);
		}
		if (StringUtils.isNotBlank(ehsHiddenDanger.getTargetTime())) {
			String targetTime = ehsHiddenDanger.getTargetTime() + "-01 00:00:00";
			DateTime targetStartTime = DateUtil.parse(targetTime, "yyyy-MM-dd HH:mm:ss");
			DateTime targetEndTime = DateUtil.endOfMonth(targetStartTime);
			ehsHiddenDanger.setTargetStartTime(targetStartTime);
			ehsHiddenDanger.setTargetEndTime(targetEndTime);
		}
		return ehsHiddenDangerMapper.selectEhsHiddenDangerList(ehsHiddenDanger);
	}


	private LambdaQueryWrapper<EhsHiddenDanger> buildQueryWrapper(EhsHiddenDanger query) {
		Map<String, Object> params = query.getParams();
		LambdaQueryWrapper<EhsHiddenDanger> lqw = Wrappers.lambdaQuery();
		lqw.like(StringUtils.isNotBlank(query.getEnterpriseName()), EhsHiddenDanger::getEnterpriseName, query.getEnterpriseName());
		lqw.eq(StringUtils.isNotBlank(query.getEnterpriseStatus()), EhsHiddenDanger::getEnterpriseStatus, query.getEnterpriseStatus());
		lqw.eq(StringUtils.isNotBlank(query.getEnterpriseType()), EhsHiddenDanger::getEnterpriseType, query.getEnterpriseType());
		lqw.like(StringUtils.isNotBlank(query.getHiddenDangerDesc()), EhsHiddenDanger::getHiddenDangerDesc, query.getHiddenDangerDesc());
		lqw.between(params.get("beginCheckTime") != null && params.get("endCheckTime") != null,
				EhsHiddenDanger::getCheckTime ,params.get("beginCheckTime"), params.get("endCheckTime"));
		lqw.like(StringUtils.isNotBlank(query.getCheckType()), EhsHiddenDanger::getCheckType, query.getCheckType());
		lqw.eq(StringUtils.isNotBlank(query.getHiddenDangerImg()), EhsHiddenDanger::getHiddenDangerImg, query.getHiddenDangerImg());
		lqw.like(StringUtils.isNotBlank(query.getRectifyMeasure()), EhsHiddenDanger::getRectifyMeasure, query.getRectifyMeasure());
		lqw.like(StringUtils.isNotBlank(query.getStatus()), EhsHiddenDanger::getStatus, query.getStatus());
		lqw.between(params.get("beginRectifyTime") != null && params.get("endRectifyTime") != null,
				EhsHiddenDanger::getRectifyTime ,params.get("beginRectifyTime"), params.get("endRectifyTime"));
		lqw.like(query.getPrincipalName() != null, EhsHiddenDanger::getPrincipalName, query.getPrincipalName());
		lqw.eq(StringUtils.isNotBlank(query.getRectifyImg()), EhsHiddenDanger::getRectifyImg, query.getRectifyImg());
		lqw.eq(StringUtils.isNotBlank(query.getRegulatoryDept()), EhsHiddenDanger::getRegulatoryDept, query.getRegulatoryDept());
		lqw.like(StringUtils.isNotBlank(query.getRegulatoryName()), EhsHiddenDanger::getRegulatoryName, query.getRegulatoryName());
		lqw.eq(StringUtils.isNotBlank(query.getHiddenDangerGrade()), EhsHiddenDanger::getHiddenDangerGrade, query.getHiddenDangerGrade());
		lqw.eq(StringUtils.isNotBlank(query.getHiddenDangerType()), EhsHiddenDanger::getHiddenDangerType, query.getHiddenDangerType());
		lqw.eq(StringUtils.isNotBlank(query.getHiddenDangerLabel()), EhsHiddenDanger::getHiddenDangerLabel, query.getHiddenDangerLabel());
		lqw.orderByDesc(EhsHiddenDanger::getCreateTime);
		lqw.eq(query.getDeptId() != null, EhsHiddenDanger::getDeptId, query.getDeptId());
		lqw.eq(query.getCreateUserId() != null, EhsHiddenDanger::getCreateUserId, query.getCreateUserId());
		lqw.eq(query.getUpdateUserId() != null, EhsHiddenDanger::getUpdateUserId, query.getUpdateUserId());
		lqw.eq(query.getIsSupervise() != null, EhsHiddenDanger::getIsSupervise, query.getIsSupervise());
		lqw.eq(StringUtils.isNotBlank(query.getAssistDept()), EhsHiddenDanger::getAssistDept, query.getAssistDept());
		lqw.eq(StringUtils.isNotBlank(query.getSuperviseResult()), EhsHiddenDanger::getSuperviseResult, query.getSuperviseResult());
		lqw.eq(query.getRectifyMoney() != null, EhsHiddenDanger::getRectifyMoney, query.getRectifyMoney());
		lqw.between(params.get("beginRectifyTermTime") != null && params.get("endRectifyTermTime") != null,
				EhsHiddenDanger::getRectifyTermTime ,params.get("beginRectifyTermTime"), params.get("endRectifyTermTime"));
		return lqw;
	}
	/**
	 * 督办
	 *
	 * @param hiddenDangerIds 当前部门
	 */
	@Override
	public int superviseEhsHiddenDanger(Long[] hiddenDangerIds)
	{

		return ehsHiddenDangerMapper.superviseEhsHiddenDanger(hiddenDangerIds);
	}
	/**
	 * 协办
	 *
	 * @param ehsHiddenDanger 部门ID组
	 */
	public int assistEhsHiddenDanger(EhsHiddenDanger ehsHiddenDanger){
		return ehsHiddenDangerMapper.assistEhsHiddenDanger(ehsHiddenDanger);
	}

	/**
	 * 导入用户数据
	 *
	 * @param list  数据列表
	 * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
	 * @return 结果
	 */
	@Override
	public String importHiddenDanger(List<EhsHiddenDanger> list, Boolean isUpdateSupport )
	{
		if (StringUtils.isNull(list) || list.size() == 0)
		{
			throw new ServiceException("导入用户数据不能为空！");
		}
		int successNum = 0;
		int failureNum = 0;
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();
		int i =0;
		for (EhsHiddenDanger obj : list)
		{
			i++;
			try
			{
				BeanValidators.validateWithException(validator, obj);
				if(StringUtils.isNotEmpty(obj.getHiddenDangerLabelName()) ){
					String s = DictUtils.getDictValue("hidden_danger_label", obj.getHiddenDangerLabelName(), ",");
					obj.setHiddenDangerLabel(s);
				}
				ehsHiddenDangerMapper.insert(obj);
				successNum++;
				successMsg.append("<br/>" + successNum + "、第 " + i + " 行记录导入成功");
			}
			catch (Exception e)
			{
				failureNum++;
				String msg = "<br/>" + failureNum + "、第 " +i + " 行记录导入失败：";
				failureMsg.append(msg + e.getMessage());
				log.error(msg, e);
			}
		}
		if (failureNum > 0)
		{
			failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
			throw new ServiceException(failureMsg.toString());
		}
		else
		{
			successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
		}
		return successMsg.toString();
	}

	public EhsHiddenDanger selectEhsHiddenDangerOverTime(EhsHiddenDanger ehsHiddenDanger){
		return ehsHiddenDangerMapper.selectEhsHiddenDangerOverTime(ehsHiddenDanger);
	}
	public EhsHiddenDanger selectOverTimeId(EhsHiddenDanger ehsHiddenDanger){
		return ehsHiddenDangerMapper.selectOverTimeId(ehsHiddenDanger);
	}
}
