package com.hotwj.platform.config.examPaperQuestion.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.config.examPaperQuestion.domain.HotExamPaperQuestion;
import com.hotwj.platform.config.examPaperQuestion.domain.bo.HotExamPaperQuestionBo;
import com.hotwj.platform.config.examPaperQuestion.domain.vo.ExamPaperQuestionDetailVo;
import com.hotwj.platform.config.examPaperQuestion.domain.vo.ExamPaperQuestionsGroupedVo;
import com.hotwj.platform.config.examPaperQuestion.domain.vo.HotExamPaperQuestionVo;
import com.hotwj.platform.config.examPaperQuestion.mapper.HotExamPaperQuestionMapper;
import com.hotwj.platform.config.examPaperQuestion.service.IHotExamPaperQuestionService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;
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.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 试卷-题目关联Service业务层处理
 *
 * @author shihongwei
 * @date 2025-12-23
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotExamPaperQuestionServiceImpl implements IHotExamPaperQuestionService {

    private final HotExamPaperQuestionMapper baseMapper;

    /**
     * 查询试卷-题目关联
     *
     * @param id 主键
     * @return 试卷-题目关联
     */
    @Override
    public HotExamPaperQuestionVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询试卷-题目关联列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 试卷-题目关联分页列表
     */
    @Override
    public TableDataInfo<HotExamPaperQuestionVo> queryPageList(HotExamPaperQuestionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotExamPaperQuestion> lqw = buildQueryWrapper(bo);
        Page<HotExamPaperQuestionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的试卷-题目关联列表
     *
     * @param bo 查询条件
     * @return 试卷-题目关联列表
     */
    @Override
    public List<HotExamPaperQuestionVo> queryList(HotExamPaperQuestionBo bo) {
        LambdaQueryWrapper<HotExamPaperQuestion> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotExamPaperQuestion> buildQueryWrapper(HotExamPaperQuestionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotExamPaperQuestion> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotExamPaperQuestion::getId);
        lqw.eq(bo.getCompanyId() != null, HotExamPaperQuestion::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getQuestionType() != null, HotExamPaperQuestion::getQuestionType, bo.getQuestionType());
        lqw.eq(bo.getPaperId() != null, HotExamPaperQuestion::getPaperId, bo.getPaperId());
        lqw.eq(bo.getQuestionId() != null, HotExamPaperQuestion::getQuestionId, bo.getQuestionId());
        lqw.eq(bo.getSortOrder() != null, HotExamPaperQuestion::getSortOrder, bo.getSortOrder());
        lqw.eq(bo.getScore() != null, HotExamPaperQuestion::getScore, bo.getScore());
        // 修复试卷试题顺序问题
        lqw.orderByAsc(HotExamPaperQuestion::getQuestionType)
                .orderByAsc(HotExamPaperQuestion::getSortOrder)
                .orderByAsc(HotExamPaperQuestion::getId);
        return lqw;
    }

    /**
     * 新增试卷-题目关联
     *
     * @param bo 试卷-题目关联
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotExamPaperQuestionBo bo) {
        HotExamPaperQuestion add = MapstructUtils.convert(bo, HotExamPaperQuestion.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改试卷-题目关联
     *
     * @param bo 试卷-题目关联
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotExamPaperQuestionBo bo) {
        HotExamPaperQuestion update = MapstructUtils.convert(bo, HotExamPaperQuestion.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public ExamPaperQuestionsGroupedVo queryGroupedQuestionBankByPaperId(Long paperId) {
        if (paperId == null) {
            throw new ServiceException("试卷ID不能为空");
        }
        List<ExamPaperQuestionDetailVo> list = baseMapper.selectPaperQuestionDetailList(paperId);
        List<ExamPaperQuestionDetailVo> single = new ArrayList<>();
        List<ExamPaperQuestionDetailVo> multi = new ArrayList<>();
        List<ExamPaperQuestionDetailVo> judge = new ArrayList<>();
        if (list != null) {
            for (ExamPaperQuestionDetailVo vo : list) {
                if (vo == null) {
                    continue;
                }
                String t = vo.getQuestionType();
                if ("1".equals(t)) {
                    single.add(vo);
                } else if ("2".equals(t)) {
                    multi.add(vo);
                } else if ("3".equals(t)) {
                    judge.add(vo);
                } else if (StringUtils.isNotBlank(t)) {
                    single.add(vo);
                }
            }
        }
        ExamPaperQuestionsGroupedVo resp = new ExamPaperQuestionsGroupedVo();
        resp.setSingleChoiceList(single);
        resp.setMultiChoiceList(multi);
        resp.setJudgeList(judge);
        return resp;
    }
}
