package com.hotwj.platform.securityManagement.safetyInvestment.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.securityManagement.safetyInvestment.domain.HotSafetyInvestment;
import com.hotwj.platform.securityManagement.safetyInvestment.domain.bo.HotSafetyInvestmentBo;
import com.hotwj.platform.securityManagement.safetyInvestment.domain.vo.HotSafetyInvestmentVo;
import com.hotwj.platform.securityManagement.safetyInvestment.mapper.HotSafetyInvestmentMapper;
import com.hotwj.platform.securityManagement.safetyInvestment.service.IHotSafetyInvestmentService;
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 2025-12-17
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotSafetyInvestmentServiceImpl implements IHotSafetyInvestmentService {

    private final HotSafetyInvestmentMapper baseMapper;

    /**
     * 查询安全生产投入
     *
     * @param id 主键
     * @return 安全生产投入
     */
    @Override
    public HotSafetyInvestmentVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询安全生产投入列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 安全生产投入分页列表
     */
    @Override
    public TableDataInfo<HotSafetyInvestmentVo> queryPageList(HotSafetyInvestmentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotSafetyInvestment> lqw = buildQueryWrapper(bo);
        Page<HotSafetyInvestmentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的安全生产投入列表
     *
     * @param bo 查询条件
     * @return 安全生产投入列表
     */
    @Override
    public List<HotSafetyInvestmentVo> queryList(HotSafetyInvestmentBo bo) {
        LambdaQueryWrapper<HotSafetyInvestment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotSafetyInvestment> buildQueryWrapper(HotSafetyInvestmentBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotSafetyInvestment> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotSafetyInvestment::getId);
        lqw.eq(bo.getCompanyId() != null, HotSafetyInvestment::getCompanyId, bo.getCompanyId());
        lqw.eq(StringUtils.isNotBlank(bo.getInvestmentType()), HotSafetyInvestment::getInvestmentType, bo.getInvestmentType());
        lqw.like(StringUtils.isNotBlank(bo.getInvestmentName()), HotSafetyInvestment::getInvestmentName, bo.getInvestmentName());
        lqw.eq(bo.getQuantity() != null, HotSafetyInvestment::getQuantity, bo.getQuantity());
        lqw.eq(bo.getInvestmentAmount() != null, HotSafetyInvestment::getInvestmentAmount, bo.getInvestmentAmount());
        lqw.eq(bo.getInvestmentTime() != null, HotSafetyInvestment::getInvestmentTime, bo.getInvestmentTime());
        lqw.eq(StringUtils.isNotBlank(bo.getReceiptAttachments()), HotSafetyInvestment::getReceiptAttachments, bo.getReceiptAttachments());
        lqw.eq(StringUtils.isNotBlank(bo.getUseDept()), HotSafetyInvestment::getUseDept, bo.getUseDept());
        lqw.eq(StringUtils.isNotBlank(bo.getUseReason()), HotSafetyInvestment::getUseReason, bo.getUseReason());
        lqw.eq(StringUtils.isNotBlank(bo.getResponsibleSignatureId()), HotSafetyInvestment::getResponsibleSignatureId, bo.getResponsibleSignatureId());
        lqw.eq(bo.getIsDeleted() != null, HotSafetyInvestment::getIsDeleted, bo.getIsDeleted());
        if (params != null) {
            Object beginInvestmentTime = params.get("beginInvestmentTime");
            Object endInvestmentTime = params.get("endInvestmentTime");
            if (beginInvestmentTime instanceof java.util.Date) {
                lqw.ge(HotSafetyInvestment::getInvestmentTime, (java.util.Date) beginInvestmentTime);
            }
            if (endInvestmentTime instanceof java.util.Date) {
                lqw.le(HotSafetyInvestment::getInvestmentTime, (java.util.Date) endInvestmentTime);
            }
        }
        return lqw;
    }

    /**
     * 新增安全生产投入
     *
     * @param bo 安全生产投入
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotSafetyInvestmentBo bo) {
        HotSafetyInvestment add = MapstructUtils.convert(bo, HotSafetyInvestment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改安全生产投入
     *
     * @param bo 安全生产投入
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotSafetyInvestmentBo bo) {
        HotSafetyInvestment update = MapstructUtils.convert(bo, HotSafetyInvestment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除安全生产投入信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
