package com.hotwj.platform.operationManagement.preJobPackagePurchase.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.preJobPackagePurchase.domain.bo.HotPreJobPackagePurchaseBo;
import com.hotwj.platform.operationManagement.preJobPackagePurchase.domain.vo.HotPreJobPackagePurchaseVo;
import com.hotwj.platform.operationManagement.preJobPackagePurchase.domain.HotPreJobPackagePurchase;
import com.hotwj.platform.operationManagement.preJobPackagePurchase.mapper.HotPreJobPackagePurchaseMapper;
import com.hotwj.platform.operationManagement.preJobPackagePurchase.service.IHotPreJobPackagePurchaseService;

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

/**
 * 岗前培训套餐购买记录Service业务层处理
 *
 * @author shihongwei
 * @date 2026-04-28
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotPreJobPackagePurchaseServiceImpl implements IHotPreJobPackagePurchaseService {

    private final HotPreJobPackagePurchaseMapper baseMapper;

    /**
     * 查询岗前培训套餐购买记录
     *
     * @param id 主键
     * @return 岗前培训套餐购买记录
     */
    @Override
    public HotPreJobPackagePurchaseVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询岗前培训套餐购买记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 岗前培训套餐购买记录分页列表
     */
    @Override
    public TableDataInfo<HotPreJobPackagePurchaseVo> queryPageList(HotPreJobPackagePurchaseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotPreJobPackagePurchase> lqw = buildQueryWrapper(bo);
        Page<HotPreJobPackagePurchaseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的岗前培训套餐购买记录列表
     *
     * @param bo 查询条件
     * @return 岗前培训套餐购买记录列表
     */
    @Override
    public List<HotPreJobPackagePurchaseVo> queryList(HotPreJobPackagePurchaseBo bo) {
        LambdaQueryWrapper<HotPreJobPackagePurchase> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotPreJobPackagePurchase> buildQueryWrapper(HotPreJobPackagePurchaseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotPreJobPackagePurchase> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotPreJobPackagePurchase::getId);
        lqw.eq(bo.getCompanyId() != null, HotPreJobPackagePurchase::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getAccountId() != null, HotPreJobPackagePurchase::getAccountId, bo.getAccountId());
        lqw.eq(bo.getUserId() != null, HotPreJobPackagePurchase::getUserId, bo.getUserId());
        lqw.eq(bo.getConsumerCompanyId() != null, HotPreJobPackagePurchase::getConsumerCompanyId, bo.getConsumerCompanyId());
        lqw.like(StringUtils.isNotBlank(bo.getConsumerCompanyName()), HotPreJobPackagePurchase::getConsumerCompanyName, bo.getConsumerCompanyName());
        lqw.eq(bo.getOrderId() != null, HotPreJobPackagePurchase::getOrderId, bo.getOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getOrderNo()), HotPreJobPackagePurchase::getOrderNo, bo.getOrderNo());
        lqw.eq(bo.getPackageId() != null, HotPreJobPackagePurchase::getPackageId, bo.getPackageId());
        lqw.like(StringUtils.isNotBlank(bo.getPackageName()), HotPreJobPackagePurchase::getPackageName, bo.getPackageName());
        lqw.eq(StringUtils.isNotBlank(bo.getPackageContent()), HotPreJobPackagePurchase::getPackageContent, bo.getPackageContent());
        lqw.eq(bo.getPackageType() != null, HotPreJobPackagePurchase::getPackageType, bo.getPackageType());
        lqw.eq(bo.getActivityId() != null, HotPreJobPackagePurchase::getActivityId, bo.getActivityId());
        lqw.like(StringUtils.isNotBlank(bo.getActivityName()), HotPreJobPackagePurchase::getActivityName, bo.getActivityName());
        lqw.eq(bo.getOriginPrice() != null, HotPreJobPackagePurchase::getOriginPrice, bo.getOriginPrice());
        lqw.eq(bo.getActivityPrice() != null, HotPreJobPackagePurchase::getActivityPrice, bo.getActivityPrice());
        lqw.eq(bo.getUnitPrice() != null, HotPreJobPackagePurchase::getUnitPrice, bo.getUnitPrice());
        lqw.eq(bo.getQuantity() != null, HotPreJobPackagePurchase::getQuantity, bo.getQuantity());
        lqw.eq(bo.getTotalAmount() != null, HotPreJobPackagePurchase::getTotalAmount, bo.getTotalAmount());
        lqw.eq(bo.getPayMethod() != null, HotPreJobPackagePurchase::getPayMethod, bo.getPayMethod());
        lqw.eq(bo.getPayStatus() != null, HotPreJobPackagePurchase::getPayStatus, bo.getPayStatus());
        lqw.eq(bo.getOrderCreateTime() != null, HotPreJobPackagePurchase::getOrderCreateTime, bo.getOrderCreateTime());
        lqw.eq(bo.getPayTime() != null, HotPreJobPackagePurchase::getPayTime, bo.getPayTime());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HotPreJobPackagePurchase::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), HotPreJobPackagePurchase::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, HotPreJobPackagePurchase::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增岗前培训套餐购买记录
     *
     * @param bo 岗前培训套餐购买记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotPreJobPackagePurchaseBo bo) {
        HotPreJobPackagePurchase add = MapstructUtils.convert(bo, HotPreJobPackagePurchase.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改岗前培训套餐购买记录
     *
     * @param bo 岗前培训套餐购买记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotPreJobPackagePurchaseBo bo) {
        HotPreJobPackagePurchase update = MapstructUtils.convert(bo, HotPreJobPackagePurchase.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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