package com.hotwj.platform.operationManagement.preJobPackage.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.preJobPackage.domain.bo.HotPreJobPackageBo;
import com.hotwj.platform.operationManagement.preJobPackage.domain.vo.HotPreJobPackageVo;
import com.hotwj.platform.operationManagement.preJobPackage.domain.HotPreJobPackage;
import com.hotwj.platform.operationManagement.preJobPackage.mapper.HotPreJobPackageMapper;
import com.hotwj.platform.operationManagement.preJobPackage.service.IHotPreJobPackageService;

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

/**
 * 岗前培训套餐Service业务层处理
 *
 * @author shihongwei
 * @date 2026-04-28
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotPreJobPackageServiceImpl implements IHotPreJobPackageService {

    private final HotPreJobPackageMapper baseMapper;

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

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

    /**
     * 查询符合条件的岗前培训套餐列表
     *
     * @param bo 查询条件
     * @return 岗前培训套餐列表
     */
    @Override
    public List<HotPreJobPackageVo> queryList(HotPreJobPackageBo bo) {
        LambdaQueryWrapper<HotPreJobPackage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotPreJobPackage> buildQueryWrapper(HotPreJobPackageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotPreJobPackage> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotPreJobPackage::getId);
        lqw.eq(bo.getCompanyId() != null, HotPreJobPackage::getCompanyId, bo.getCompanyId());
        lqw.eq(StringUtils.isNotBlank(bo.getPackageCode()), HotPreJobPackage::getPackageCode, bo.getPackageCode());
        lqw.like(StringUtils.isNotBlank(bo.getPackageName()), HotPreJobPackage::getPackageName, bo.getPackageName());
        lqw.eq(StringUtils.isNotBlank(bo.getPackageContent()), HotPreJobPackage::getPackageContent, bo.getPackageContent());
        lqw.eq(bo.getPackageType() != null, HotPreJobPackage::getPackageType, bo.getPackageType());
        lqw.eq(bo.getActivityId() != null, HotPreJobPackage::getActivityId, bo.getActivityId());
        lqw.like(StringUtils.isNotBlank(bo.getActivityName()), HotPreJobPackage::getActivityName, bo.getActivityName());
        lqw.eq(bo.getOriginPrice() != null, HotPreJobPackage::getOriginPrice, bo.getOriginPrice());
        lqw.eq(bo.getActivityPrice() != null, HotPreJobPackage::getActivityPrice, bo.getActivityPrice());
        lqw.eq(bo.getUnitPrice() != null, HotPreJobPackage::getUnitPrice, bo.getUnitPrice());
        lqw.eq(bo.getValidStartTime() != null, HotPreJobPackage::getValidStartTime, bo.getValidStartTime());
        lqw.eq(bo.getValidEndTime() != null, HotPreJobPackage::getValidEndTime, bo.getValidEndTime());
        lqw.eq(bo.getStatus() != null, HotPreJobPackage::getStatus, bo.getStatus());
        lqw.eq(bo.getSortNo() != null, HotPreJobPackage::getSortNo, bo.getSortNo());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HotPreJobPackage::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), HotPreJobPackage::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, HotPreJobPackage::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

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

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

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

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