package com.ruoyi.ehsHiddenDanger.service.impl;

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.utils.SecurityUtils;
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;

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

	@Autowired
	private EhsHiddenDangerMapper ehsHiddenDangerMapper;

	/**
	 * 查询隐患信息
	 *
	 * @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);
		}
		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);
	}
}
