package org.jk.rag.service.impl;

import org.jk.common.core.utils.MapstructUtils;
import org.jk.common.core.utils.StringUtils;
import org.jk.common.mybatis.core.page.TableDataInfo;
import org.jk.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.jk.rag.domain.EvaluationTaskHistory;
import org.jk.rag.domain.bo.EvaluationTaskHistoryBo;
import org.jk.rag.domain.vo.EvaluationTaskHistoryVo;
import org.jk.rag.mapper.EvaluationTaskHistoryMapper;
import org.jk.rag.service.IEvaluationTaskHistoryService;
import org.springframework.stereotype.Service;

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

/**
 * 评测任务历史Service业务层处理
 *
 * @author Lion Li
 * @date 2026-01-27
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EvaluationTaskHistoryServiceImpl implements IEvaluationTaskHistoryService {

    private final EvaluationTaskHistoryMapper baseMapper;

    /**
     * 查询评测任务历史
     *
     * @param id 主键
     * @return 评测任务历史
     */
    @Override
    public EvaluationTaskHistoryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询评测任务历史列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 评测任务历史分页列表
     */
    @Override
    public TableDataInfo<EvaluationTaskHistoryVo> queryPageList(EvaluationTaskHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<EvaluationTaskHistory> lqw = buildQueryWrapper(bo);
        Page<EvaluationTaskHistoryVo> result = baseMapper.selectPageList(bo.getTaskName(),pageQuery.build());
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的评测任务历史列表
     *
     * @param bo 查询条件
     * @return 评测任务历史列表
     */
    @Override
    public List<EvaluationTaskHistoryVo> queryList(EvaluationTaskHistoryBo bo) {
        LambdaQueryWrapper<EvaluationTaskHistory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<EvaluationTaskHistory> buildQueryWrapper(EvaluationTaskHistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<EvaluationTaskHistory> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(EvaluationTaskHistory::getId);
        lqw.eq(bo.getTaskId() != null, EvaluationTaskHistory::getTaskId, bo.getTaskId());
        lqw.like(StringUtils.isNotBlank(bo.getTaskName()), EvaluationTaskHistory::getTaskName, bo.getTaskName());
        lqw.eq(StringUtils.isNotBlank(bo.getTaskDescription()), EvaluationTaskHistory::getTaskDescription, bo.getTaskDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getEvaluationType()), EvaluationTaskHistory::getEvaluationType, bo.getEvaluationType());
        lqw.eq(StringUtils.isNotBlank(bo.getReviewRag()), EvaluationTaskHistory::getReviewRag, bo.getReviewRag());
        lqw.eq(StringUtils.isNotBlank(bo.getModel()), EvaluationTaskHistory::getModel, bo.getModel());
        lqw.eq(StringUtils.isNotBlank(bo.getEvaluationData()), EvaluationTaskHistory::getEvaluationData, bo.getEvaluationData());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), EvaluationTaskHistory::getStatus, bo.getStatus());
        lqw.eq(bo.getResultStore() != null, EvaluationTaskHistory::getResultStore, bo.getResultStore());
        lqw.eq(bo.getAnswerLoyalty() != null, EvaluationTaskHistory::getAnswerLoyalty, bo.getAnswerLoyalty());
        lqw.eq(bo.getCorrectAnswer() != null, EvaluationTaskHistory::getCorrectAnswer, bo.getCorrectAnswer());
        lqw.eq(bo.getAnswerRelevance() != null, EvaluationTaskHistory::getAnswerRelevance, bo.getAnswerRelevance());
        lqw.eq(bo.getNoiseSensitivity() != null, EvaluationTaskHistory::getNoiseSensitivity, bo.getNoiseSensitivity());
        lqw.eq(bo.getContextRecall() != null, EvaluationTaskHistory::getContextRecall, bo.getContextRecall());
        lqw.eq(bo.getContextRelevance() != null, EvaluationTaskHistory::getContextRelevance, bo.getContextRelevance());
        lqw.eq(StringUtils.isNotBlank(bo.getEmbeddingId()), EvaluationTaskHistory::getEmbeddingId, bo.getEmbeddingId());
        lqw.eq(bo.getStartTime() != null, EvaluationTaskHistory::getStartTime, bo.getStartTime());
        lqw.eq(bo.getEndTime() != null, EvaluationTaskHistory::getEndTime, bo.getEndTime());
        lqw.eq(StringUtils.isNotBlank(bo.getFullUrl()), EvaluationTaskHistory::getFullUrl, bo.getFullUrl());
        return lqw;
    }

    /**
     * 新增评测任务历史
     *
     * @param bo 评测任务历史
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(EvaluationTaskHistoryBo bo) {
        EvaluationTaskHistory add = MapstructUtils.convert(bo, EvaluationTaskHistory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改评测任务历史
     *
     * @param bo 评测任务历史
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(EvaluationTaskHistoryBo bo) {
        EvaluationTaskHistory update = MapstructUtils.convert(bo, EvaluationTaskHistory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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