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

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

    @Autowired
    private EhsRiskMapper ehsRiskMapper;
    /**
     * 查询风险信息
     *
     * @param riskId 风险信息主键
     * @return 风险信息
     */
    @Override
    public EhsRisk selectEhsRiskByRiskId(Long riskId)
    {
        return ehsRiskMapper.selectEhsRiskByRiskId(riskId);
    }

    /**
     * 查询风险信息列表
     *
     * @param ehsRisk 风险信息
     * @return 风险信息
     */
    @Override
    public List<EhsRisk> selectEhsRiskList(EhsRisk ehsRisk)
    {
        if(SecurityUtils.getLoginUser().getUser().getDept().getParentId()!=null &&
                SecurityUtils.getLoginUser().getUser().getDept().getParentId().longValue()!=0){
            ehsRisk.setDeptId(SecurityUtils.getLoginUser().getUser().getDept().getDeptId().longValue());
        }
        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.getTel()), EhsRisk::getTel, query.getTel());
        lqw.between(params.get("beginPeopleQuantity") != null && params.get("endPeopleQuantity") != null,
                EhsRisk::getPeopleQuantity ,params.get("beginPeopleQuantity"), params.get("endPeopleQuantity"));
        lqw.between(params.get("beginMaterialsQuantity") != null && params.get("endMaterialsQuantity") != null,
                EhsRisk::getMaterialsQuantity ,params.get("beginMaterialsQuantity"), params.get("endMaterialsQuantity"));
        lqw.between(params.get("beginRepairTeam") != null && params.get("endRepairTeam") != null,
                EhsRisk::getRepairTeam ,params.get("beginRepairTeam"), params.get("endRepairTeam"));
        lqw.like(StringUtils.isNotBlank(query.getPersonResponsible()), EhsRisk::getPersonResponsible, query.getPersonResponsible());
        lqw.like(StringUtils.isNotBlank(query.getVillagePersonResponsible()), EhsRisk::getVillagePersonResponsible, query.getVillagePersonResponsible());
        lqw.like(StringUtils.isNotBlank(query.getVillagePersonResponsible2()), EhsRisk::getVillagePersonResponsible2, query.getVillagePersonResponsible2());
        lqw.like(StringUtils.isNotBlank(query.getTel2()), EhsRisk::getTel2, query.getTel2());
        lqw.orderByDesc(EhsRisk::getCreateTime);
        return lqw;
    }

}
