package org.dromara.system.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.dromara.system.domain.AiKnowledgeInfoDo;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.AiKnowledgeInfoBo;
import org.dromara.system.domain.vo.AiKnowledgeInfoVo;
import org.dromara.system.mapper.AiKnowledgeInfoMapper;
import org.dromara.system.service.IAiKnowledgeInfoService;

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

/**
 * 知识库Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-17
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AiKnowledgeInfoServiceImpl implements IAiKnowledgeInfoService {

    private final AiKnowledgeInfoMapper baseMapper;

    /**
     * 查询知识库
     *
     * @param id 主键
     * @return 知识库
     */
    @Override
    public AiKnowledgeInfoVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询知识库列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 知识库分页列表
     */
    @Override
    public TableDataInfo<AiKnowledgeInfoVo> queryPageList(AiKnowledgeInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AiKnowledgeInfoDo> lqw = buildQueryWrapper(bo);
        Page<AiKnowledgeInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的知识库列表
     *
     * @param bo 查询条件
     * @return 知识库列表
     */
    @Override
    public List<AiKnowledgeInfoVo> queryList(AiKnowledgeInfoBo bo) {
        LambdaQueryWrapper<AiKnowledgeInfoDo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AiKnowledgeInfoDo> buildQueryWrapper(AiKnowledgeInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AiKnowledgeInfoDo> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(AiKnowledgeInfoDo::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getKnowledgeCode()), AiKnowledgeInfoDo::getKnowledgeCode, bo.getKnowledgeCode());
        lqw.like(StringUtils.isNotBlank(bo.getKnowledgeName()), AiKnowledgeInfoDo::getKnowledgeName, bo.getKnowledgeName());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), AiKnowledgeInfoDo::getDescription, bo.getDescription());
        lqw.eq(bo.getUserId() != null, AiKnowledgeInfoDo::getUserId, bo.getUserId());
        lqw.eq(bo.getShare() != null, AiKnowledgeInfoDo::getShare, bo.getShare());
        lqw.eq(StringUtils.isNotBlank(bo.getKnowledgeSeparator()), AiKnowledgeInfoDo::getKnowledgeSeparator, bo.getKnowledgeSeparator());
        lqw.eq(StringUtils.isNotBlank(bo.getQuestionSeparator()), AiKnowledgeInfoDo::getQuestionSeparator, bo.getQuestionSeparator());
        lqw.eq(bo.getOverlapChar() != null, AiKnowledgeInfoDo::getOverlapChar, bo.getOverlapChar());
        lqw.eq(bo.getRetrieveLimit() != null, AiKnowledgeInfoDo::getRetrieveLimit, bo.getRetrieveLimit());
        lqw.eq(bo.getTextBlockSize() != null, AiKnowledgeInfoDo::getTextBlockSize, bo.getTextBlockSize());
        lqw.like(StringUtils.isNotBlank(bo.getVectorModelName()), AiKnowledgeInfoDo::getVectorModelName, bo.getVectorModelName());
        lqw.like(StringUtils.isNotBlank(bo.getEmbeddingModelName()), AiKnowledgeInfoDo::getEmbeddingModelName, bo.getEmbeddingModelName());
        lqw.eq(StringUtils.isNotBlank(bo.getSystemPrompt()), AiKnowledgeInfoDo::getSystemPrompt, bo.getSystemPrompt());
        return lqw;
    }

    /**
     * 新增知识库
     *
     * @param bo 知识库
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AiKnowledgeInfoBo bo) {
        AiKnowledgeInfoDo add = MapstructUtils.convert(bo, AiKnowledgeInfoDo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改知识库
     *
     * @param bo 知识库
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AiKnowledgeInfoBo bo) {
        AiKnowledgeInfoDo update = MapstructUtils.convert(bo, AiKnowledgeInfoDo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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