package com.hotwj.platform.securityManagement.trainingCourseConfig.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.trainingCourseConfig.domain.HotTrainingCourseConfig;
import com.hotwj.platform.securityManagement.trainingCourseConfig.domain.bo.HotTrainingCourseConfigBo;
import com.hotwj.platform.securityManagement.trainingCourseConfig.domain.vo.HotTrainingCourseConfigVo;
import com.hotwj.platform.securityManagement.trainingCourseConfig.domain.vo.TrainingUserCourseStudyDetailVo;
import com.hotwj.platform.securityManagement.trainingCourseConfig.mapper.HotTrainingCourseConfigMapper;
import com.hotwj.platform.securityManagement.trainingCourseConfig.service.IHotTrainingCourseConfigService;
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 2026-01-24
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotTrainingCourseConfigServiceImpl implements IHotTrainingCourseConfigService {

    private final HotTrainingCourseConfigMapper baseMapper;

    /**
     * 查询企业课程配置
     *
     * @param id 主键
     * @return 企业课程配置
     */
    @Override
    public HotTrainingCourseConfigVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询企业课程配置列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 企业课程配置分页列表
     */
    @Override
    public TableDataInfo<HotTrainingCourseConfigVo> queryPageList(HotTrainingCourseConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotTrainingCourseConfig> lqw = buildQueryWrapper(bo);
        Page<HotTrainingCourseConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的企业课程配置列表
     *
     * @param bo 查询条件
     * @return 企业课程配置列表
     */
    @Override
    public List<HotTrainingCourseConfigVo> queryList(HotTrainingCourseConfigBo bo) {
        LambdaQueryWrapper<HotTrainingCourseConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    @Override
    public List<TrainingUserCourseStudyDetailVo> queryTrainingUserCourseStudyDetail(Long companyId, Long trainingId, String userId) {
        return baseMapper.selectTrainingUserCourseStudyDetail(companyId, trainingId, userId);
    }

    private LambdaQueryWrapper<HotTrainingCourseConfig> buildQueryWrapper(HotTrainingCourseConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotTrainingCourseConfig> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotTrainingCourseConfig::getSortNo);
        lqw.eq(bo.getCompanyId() != null, HotTrainingCourseConfig::getCompanyId, bo.getCompanyId());
        lqw.like(StringUtils.isNotBlank(bo.getCourseName()), HotTrainingCourseConfig::getCourseName, bo.getCourseName());
        lqw.eq(bo.getCourseId() != null, HotTrainingCourseConfig::getCourseId, bo.getCourseId());
        lqw.eq(StringUtils.isNotBlank(bo.getCourseBelong()), HotTrainingCourseConfig::getCourseBelong, bo.getCourseBelong());
        lqw.eq(bo.getStudyDuration() != null, HotTrainingCourseConfig::getStudyDuration, bo.getStudyDuration());
        lqw.eq(bo.getSortNo() != null, HotTrainingCourseConfig::getSortNo, bo.getSortNo());
        lqw.eq(bo.getIsDeleted() != null, HotTrainingCourseConfig::getIsDeleted, bo.getIsDeleted());
        lqw.eq(bo.getTrainingId() != null, HotTrainingCourseConfig::getTrainingId, bo.getTrainingId());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HotTrainingCourseConfig::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), HotTrainingCourseConfig::getUpdateByName, bo.getUpdateByName());
        return lqw;
    }

    /**
     * 新增企业课程配置
     *
     * @param bo 企业课程配置
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotTrainingCourseConfigBo bo) {
        HotTrainingCourseConfig add = MapstructUtils.convert(bo, HotTrainingCourseConfig.class);
        HotTrainingCourseConfig maxSort = baseMapper.selectOne(
                Wrappers.<HotTrainingCourseConfig>lambdaQuery()
                        .select(HotTrainingCourseConfig::getSortNo)
                        .eq(HotTrainingCourseConfig::getCompanyId, bo.getCompanyId())
                        .eq(bo.getTrainingId() != null, HotTrainingCourseConfig::getTrainingId, bo.getTrainingId())
                        .orderByDesc(HotTrainingCourseConfig::getSortNo)
                        .last("limit 1")
        );
        long max = (maxSort != null && maxSort.getSortNo() != null) ? maxSort.getSortNo() : 0L;
        add.setSortNo(max + 1);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改企业课程配置
     *
     * @param bo 企业课程配置
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotTrainingCourseConfigBo bo) {
        HotTrainingCourseConfig update = MapstructUtils.convert(bo, HotTrainingCourseConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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


}
