package com.ruoyi.ehsRisk.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 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.ehsRisk.mapper.EhsRiskMapper;
import com.ruoyi.ehsRisk.domain.EhsRisk;
import com.ruoyi.ehsRisk.service.IEhsRiskService;

/**
 * 风险信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-12-13
 */
@Service
public class EhsRiskServiceImpl extends ServiceImpl<EhsRiskMapper, EhsRisk> implements IEhsRiskService {

	@Autowired
	private EhsRiskMapper ehsRiskMapper;

	/**
	 * 查询风险信息
	 *
	 * @param riskId 风险信息主键
	 * @return 风险信息
	 */
	@Override
	public EhsRisk selectEhsRiskByRiskId(Long riskId)
	{
		EhsRisk ehsRisk = ehsRiskMapper.selectEhsRiskByRiskId(riskId);
		// 风险点位置
		String[] riskLocationArr = ehsRisk.getRiskLocation().split(";");
		if(riskLocationArr.length==2) {
			ehsRisk.setRiskLocation1(riskLocationArr[0]);// 村 (社区)
			ehsRisk.setRiskLocation2(riskLocationArr[1]);// 位置
		}else {
			ehsRisk.setRiskLocation1(riskLocationArr[0]);// 村 (社区)
			ehsRisk.setRiskLocation2("");// 位置
		}
		// 涉及群众数量
		String[] peopleNumArr = ehsRisk.getPeopleQuantity().split(";");
		if(peopleNumArr.length==2) {
			ehsRisk.setPeopleQuantity1(peopleNumArr[0]);// 户
			ehsRisk.setPeopleQuantity2(peopleNumArr[1]);// 人
		}else {
			ehsRisk.setPeopleQuantity1(peopleNumArr[0]);// 户
			ehsRisk.setPeopleQuantity2("");// 人
		}
		return ehsRisk;
	}

	/**
	 * 查询风险信息列表
	 *
	 * @param ehsRisk 风险信息
	 * @return 风险信息
	 */
	@Override
	public List<EhsRisk> selectEhsRiskList(EhsRisk ehsRisk)
	{
		return ehsRiskMapper.selectEhsRiskList(ehsRisk);
	}


	private LambdaQueryWrapper<EhsRisk> buildQueryWrapper(EhsRisk query) {
		Map<String, Object> params = query.getParams();
		LambdaQueryWrapper<EhsRisk> lqw = Wrappers.lambdaQuery();
					lqw.eq(query.getRiskGrade() != null, EhsRisk::getRiskGrade, query.getRiskGrade());
					lqw.eq(StringUtils.isNotBlank(query.getReportAddressType()), EhsRisk::getReportAddressType, query.getReportAddressType());
					lqw.like(StringUtils.isNotBlank(query.getRiskLocation()), EhsRisk::getRiskLocation, query.getRiskLocation());
					lqw.like(StringUtils.isNotBlank(query.getRiskDesc()), EhsRisk::getRiskDesc, query.getRiskDesc());
					lqw.like(StringUtils.isNotBlank(query.getPeopleQuantity()), EhsRisk::getPeopleQuantity, query.getPeopleQuantity());
					lqw.like(StringUtils.isNotBlank(query.getMaterialsAddr()), EhsRisk::getMaterialsAddr, query.getMaterialsAddr());
					lqw.between(params.get("beginMaterialsQuantity") != null && params.get("endMaterialsQuantity") != null,
                        EhsRisk::getMaterialsQuantity ,params.get("beginMaterialsQuantity"), params.get("endMaterialsQuantity"));
					lqw.like(StringUtils.isNotBlank(query.getLifeAddr()), EhsRisk::getLifeAddr, query.getLifeAddr());
					lqw.like(StringUtils.isNotBlank(query.getResettleAddr()), EhsRisk::getResettleAddr, query.getResettleAddr());
					lqw.like(StringUtils.isNotBlank(query.getVillageName()), EhsRisk::getVillageName, query.getVillageName());
					lqw.like(StringUtils.isNotBlank(query.getVillageDuties()), EhsRisk::getVillageDuties, query.getVillageDuties());
					lqw.like(StringUtils.isNotBlank(query.getVillageTel()), EhsRisk::getVillageTel, query.getVillageTel());
					lqw.like(StringUtils.isNotBlank(query.getVillageName1()), EhsRisk::getVillageName1, query.getVillageName1());
					lqw.like(StringUtils.isNotBlank(query.getVillageDuties1()), EhsRisk::getVillageDuties1, query.getVillageDuties1());
					lqw.like(StringUtils.isNotBlank(query.getVillageTel1()), EhsRisk::getVillageTel1, query.getVillageTel1());
				lqw.orderByDesc(EhsRisk::getCreateTime);
					lqw.eq(query.getDeptId() != null, EhsRisk::getDeptId, query.getDeptId());
					lqw.eq(query.getCreateUserId() != null, EhsRisk::getCreateUserId, query.getCreateUserId());
					lqw.eq(query.getUpdateUserId() != null, EhsRisk::getUpdateUserId, query.getUpdateUserId());
					lqw.like(StringUtils.isNotBlank(query.getInspectionName()), EhsRisk::getInspectionName, query.getInspectionName());
					lqw.like(StringUtils.isNotBlank(query.getInspectionDuties()), EhsRisk::getInspectionDuties, query.getInspectionDuties());
					lqw.like(StringUtils.isNotBlank(query.getInspectionTel()), EhsRisk::getInspectionTel, query.getInspectionTel());
		return lqw;
	}

}
