package com.hotwj.platform.operationManagement.hourUsageDetail.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.hotwj.platform.operationManagement.hourUsageDetail.domain.bo.HotHourUsageDetailBo;
import com.hotwj.platform.operationManagement.hourUsageDetail.domain.vo.HotHourUsageDetailVo;
import com.hotwj.platform.operationManagement.hourUsageDetail.domain.HotHourUsageDetail;
import com.hotwj.platform.operationManagement.hourUsageDetail.mapper.HotHourUsageDetailMapper;
import com.hotwj.platform.operationManagement.hourUsageDetail.service.IHotHourUsageDetailService;

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

/**
 * 培训学时使用明细Service业务层处理
 *
 * @author shihongwei
 * @date 2026-04-28
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotHourUsageDetailServiceImpl implements IHotHourUsageDetailService {

    private final HotHourUsageDetailMapper baseMapper;

    /**
     * 查询培训学时使用明细
     *
     * @param id 主键
     * @return 培训学时使用明细
     */
    @Override
    public HotHourUsageDetailVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询培训学时使用明细列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 培训学时使用明细分页列表
     */
    @Override
    public TableDataInfo<HotHourUsageDetailVo> queryPageList(HotHourUsageDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotHourUsageDetail> lqw = buildQueryWrapper(bo);
        Page<HotHourUsageDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的培训学时使用明细列表
     *
     * @param bo 查询条件
     * @return 培训学时使用明细列表
     */
    @Override
    public List<HotHourUsageDetailVo> queryList(HotHourUsageDetailBo bo) {
        LambdaQueryWrapper<HotHourUsageDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotHourUsageDetail> buildQueryWrapper(HotHourUsageDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotHourUsageDetail> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotHourUsageDetail::getId);
        lqw.eq(bo.getCompanyId() != null, HotHourUsageDetail::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getPurchaseDetailId() != null, HotHourUsageDetail::getPurchaseDetailId, bo.getPurchaseDetailId());
        lqw.eq(bo.getAccountId() != null, HotHourUsageDetail::getAccountId, bo.getAccountId());
        lqw.eq(bo.getUserId() != null, HotHourUsageDetail::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getUserName()), HotHourUsageDetail::getUserName, bo.getUserName());
        lqw.eq(bo.getOperatorId() != null, HotHourUsageDetail::getOperatorId, bo.getOperatorId());
        lqw.like(StringUtils.isNotBlank(bo.getOperatorName()), HotHourUsageDetail::getOperatorName, bo.getOperatorName());
        lqw.eq(StringUtils.isNotBlank(bo.getUseType()), HotHourUsageDetail::getUseType, bo.getUseType());
        lqw.eq(bo.getPackageId() != null, HotHourUsageDetail::getPackageId, bo.getPackageId());
        lqw.like(StringUtils.isNotBlank(bo.getPackageName()), HotHourUsageDetail::getPackageName, bo.getPackageName());
        lqw.eq(bo.getUsedHours() != null, HotHourUsageDetail::getUsedHours, bo.getUsedHours());
        lqw.eq(bo.getBeforeRemainingHours() != null, HotHourUsageDetail::getBeforeRemainingHours, bo.getBeforeRemainingHours());
        lqw.eq(bo.getAfterRemainingHours() != null, HotHourUsageDetail::getAfterRemainingHours, bo.getAfterRemainingHours());
        lqw.eq(bo.getRemainingHours() != null, HotHourUsageDetail::getRemainingHours, bo.getRemainingHours());
        lqw.eq(bo.getUseDate() != null, HotHourUsageDetail::getUseDate, bo.getUseDate());
        lqw.eq(bo.getBizOrderId() != null, HotHourUsageDetail::getBizOrderId, bo.getBizOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getBizOrderNo()), HotHourUsageDetail::getBizOrderNo, bo.getBizOrderNo());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HotHourUsageDetail::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), HotHourUsageDetail::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, HotHourUsageDetail::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增培训学时使用明细
     *
     * @param bo 培训学时使用明细
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotHourUsageDetailBo bo) {
        HotHourUsageDetail add = MapstructUtils.convert(bo, HotHourUsageDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改培训学时使用明细
     *
     * @param bo 培训学时使用明细
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotHourUsageDetailBo bo) {
        HotHourUsageDetail update = MapstructUtils.convert(bo, HotHourUsageDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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