package com.hotwj.platform.operationManagement.hourPurchaseDetail.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.hourPurchaseDetail.domain.bo.HotHourPurchaseDetailBo;
import com.hotwj.platform.operationManagement.hourPurchaseDetail.domain.vo.HotHourPurchaseDetailVo;
import com.hotwj.platform.operationManagement.hourPurchaseDetail.domain.HotHourPurchaseDetail;
import com.hotwj.platform.operationManagement.hourPurchaseDetail.mapper.HotHourPurchaseDetailMapper;
import com.hotwj.platform.operationManagement.hourPurchaseDetail.service.IHotHourPurchaseDetailService;

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

/**
 * 培训学时购买明细Service业务层处理
 *
 * @author shihongwei
 * @date 2026-04-28
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotHourPurchaseDetailServiceImpl implements IHotHourPurchaseDetailService {

    private final HotHourPurchaseDetailMapper baseMapper;

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

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

    /**
     * 查询符合条件的培训学时购买明细列表
     *
     * @param bo 查询条件
     * @return 培训学时购买明细列表
     */
    @Override
    public List<HotHourPurchaseDetailVo> queryList(HotHourPurchaseDetailBo bo) {
        LambdaQueryWrapper<HotHourPurchaseDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotHourPurchaseDetail> buildQueryWrapper(HotHourPurchaseDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotHourPurchaseDetail> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotHourPurchaseDetail::getId);
        lqw.eq(bo.getCompanyId() != null, HotHourPurchaseDetail::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getAccountId() != null, HotHourPurchaseDetail::getAccountId, bo.getAccountId());
        lqw.eq(bo.getUserId() != null, HotHourPurchaseDetail::getUserId, bo.getUserId());
        lqw.like(StringUtils.isNotBlank(bo.getBuyerName()), HotHourPurchaseDetail::getBuyerName, bo.getBuyerName());
        lqw.eq(bo.getBuyerCompanyId() != null, HotHourPurchaseDetail::getBuyerCompanyId, bo.getBuyerCompanyId());
        lqw.like(StringUtils.isNotBlank(bo.getBuyerCompanyName()), HotHourPurchaseDetail::getBuyerCompanyName, bo.getBuyerCompanyName());
        lqw.eq(bo.getPurchaseId() != null, HotHourPurchaseDetail::getPurchaseId, bo.getPurchaseId());
        lqw.eq(bo.getOrderId() != null, HotHourPurchaseDetail::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), HotHourPurchaseDetail::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getPackageId() != null, HotHourPurchaseDetail::getPackageId, bo.getPackageId());
        lqw.like(StringUtils.isNotBlank(bo.getPackageName()), HotHourPurchaseDetail::getPackageName, bo.getPackageName());
        lqw.eq(StringUtils.isNotBlank(bo.getApplicableTypes()), HotHourPurchaseDetail::getApplicableTypes, bo.getApplicableTypes());
        lqw.eq(bo.getPurchaseCount() != null, HotHourPurchaseDetail::getPurchaseCount, bo.getPurchaseCount());
        lqw.eq(bo.getTotalHours() != null, HotHourPurchaseDetail::getTotalHours, bo.getTotalHours());
        lqw.eq(bo.getUsedHours() != null, HotHourPurchaseDetail::getUsedHours, bo.getUsedHours());
        lqw.eq(bo.getRemainingHours() != null, HotHourPurchaseDetail::getRemainingHours, bo.getRemainingHours());
        lqw.eq(bo.getWalletType() != null, HotHourPurchaseDetail::getWalletType, bo.getWalletType());
        lqw.eq(bo.getWalletId() != null, HotHourPurchaseDetail::getWalletId, bo.getWalletId());
        lqw.eq(bo.getVersion() != null, HotHourPurchaseDetail::getVersion, bo.getVersion());
        lqw.eq(bo.getStatus() != null, HotHourPurchaseDetail::getStatus, bo.getStatus());
        lqw.eq(bo.getLastUseTime() != null, HotHourPurchaseDetail::getLastUseTime, bo.getLastUseTime());
        lqw.eq(bo.getExpireTime() != null, HotHourPurchaseDetail::getExpireTime, bo.getExpireTime());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HotHourPurchaseDetail::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), HotHourPurchaseDetail::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, HotHourPurchaseDetail::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

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

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

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

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