package com.hotwj.platform.config.accidentSeverityLevelConfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotwj.platform.config.accidentSeverityLevelConfig.domain.HotAccidentSeverityLevelConfig;
import com.hotwj.platform.config.accidentSeverityLevelConfig.domain.bo.HotAccidentSeverityLevelConfigBo;
import com.hotwj.platform.config.accidentSeverityLevelConfig.domain.vo.HotAccidentSeverityLevelConfigVo;
import com.hotwj.platform.config.accidentSeverityLevelConfig.mapper.HotAccidentSeverityLevelConfigMapper;
import com.hotwj.platform.config.accidentSeverityLevelConfig.service.IHotAccidentSeverityLevelConfigService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 事故等级划分（严重程度）Service业务层处理
 *
 * @author shihongwei
 * @date 2026-01-05
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotAccidentSeverityLevelConfigServiceImpl implements IHotAccidentSeverityLevelConfigService {

    private final HotAccidentSeverityLevelConfigMapper baseMapper;

    /**
     * 查询事故等级划分（严重程度）
     *
     * @param id 主键
     * @return 事故等级划分（严重程度）
     */
    @Override
    public HotAccidentSeverityLevelConfigVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询事故等级划分（严重程度）列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 事故等级划分（严重程度）分页列表
     */
    @Override
    public TableDataInfo<HotAccidentSeverityLevelConfigVo> queryPageList(HotAccidentSeverityLevelConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotAccidentSeverityLevelConfig> lqw = buildQueryWrapper(bo);
        Page<HotAccidentSeverityLevelConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的事故等级划分（严重程度）列表
     *
     * @param bo 查询条件
     * @return 事故等级划分（严重程度）列表
     */
    @Override
    public List<HotAccidentSeverityLevelConfigVo> queryList(HotAccidentSeverityLevelConfigBo bo) {
        LambdaQueryWrapper<HotAccidentSeverityLevelConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotAccidentSeverityLevelConfig> buildQueryWrapper(HotAccidentSeverityLevelConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotAccidentSeverityLevelConfig> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotAccidentSeverityLevelConfig::getId);
        lqw.eq(bo.getLevelValue() != null, HotAccidentSeverityLevelConfig::getLevelValue, bo.getLevelValue());
        lqw.eq(StringUtils.isNotBlank(bo.getLawRequirement()), HotAccidentSeverityLevelConfig::getLawRequirement, bo.getLawRequirement());
        lqw.eq(StringUtils.isNotBlank(bo.getPersonnelDescription()), HotAccidentSeverityLevelConfig::getPersonnelDescription, bo.getPersonnelDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getDirectEconomicLoss()), HotAccidentSeverityLevelConfig::getDirectEconomicLoss, bo.getDirectEconomicLoss());
        lqw.eq(StringUtils.isNotBlank(bo.getEnterpriseReputation()), HotAccidentSeverityLevelConfig::getEnterpriseReputation, bo.getEnterpriseReputation());
        lqw.eq(bo.getIsDeleted() != null, HotAccidentSeverityLevelConfig::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增事故等级划分（严重程度）
     *
     * @param bo 事故等级划分（严重程度）
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotAccidentSeverityLevelConfigBo bo) {
        HotAccidentSeverityLevelConfig add = MapstructUtils.convert(bo, HotAccidentSeverityLevelConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改事故等级划分（严重程度）
     *
     * @param bo 事故等级划分（严重程度）
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotAccidentSeverityLevelConfigBo bo) {
        HotAccidentSeverityLevelConfig update = MapstructUtils.convert(bo, HotAccidentSeverityLevelConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HotAccidentSeverityLevelConfig entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除事故等级划分（严重程度）信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
