package org.dromara.knowledge.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
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.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
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.dromara.knowledge.domain.KnowledgeAttach;
import org.dromara.knowledge.domain.bo.KnowledgeAttachBo;
import org.dromara.knowledge.domain.vo.KnowledgeAttachVo;
import org.dromara.knowledge.mapper.KnowledgeAttachMapper;
import org.dromara.knowledge.service.KnowledgeAttachService;
import org.springframework.stereotype.Service;

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

/**
 * 知识库附件Service业务层处理
 *
 * @author pengles
 */
@RequiredArgsConstructor
@Service
public class KnowledgeAttachServiceImpl extends ServiceImpl<KnowledgeAttachMapper, KnowledgeAttach> implements KnowledgeAttachService {

    private final KnowledgeAttachMapper baseMapper;

    /**
     * 查询知识库附件
     */
    @Override
    public KnowledgeAttachVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询知识库附件列表
     */
    @Override
    public TableDataInfo<KnowledgeAttachVo> queryPageList(KnowledgeAttachBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KnowledgeAttach> lqw = buildQueryWrapper(bo);
        Page<KnowledgeAttachVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询知识库附件列表
     */
    @Override
    public List<KnowledgeAttachVo> queryList(KnowledgeAttachBo bo) {
        LambdaQueryWrapper<KnowledgeAttach> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KnowledgeAttach> buildQueryWrapper(KnowledgeAttachBo bo) {
        LambdaQueryWrapper<KnowledgeAttach> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getKnowledgeId()), KnowledgeAttach::getKnowledgeId, bo.getKnowledgeId());
        lqw.eq(StringUtils.isNotBlank(bo.getDocId()), KnowledgeAttach::getDocId, bo.getDocId());
        lqw.like(StringUtils.isNotBlank(bo.getDocName()), KnowledgeAttach::getDocName, bo.getDocName());
        lqw.eq(StringUtils.isNotBlank(bo.getDocType()), KnowledgeAttach::getDocType, bo.getDocType());
        lqw.eq(ObjectUtil.isNotNull(bo.getOssId()), KnowledgeAttach::getOssId, bo.getOssId());
        lqw.eq(ObjectUtil.isNotNull(bo.getPicStatus()), KnowledgeAttach::getPicStatus, bo.getPicStatus());
        lqw.eq(ObjectUtil.isNotNull(bo.getPicAnysStatus()), KnowledgeAttach::getPicAnysStatus, bo.getPicAnysStatus());
        lqw.eq(ObjectUtil.isNotNull(bo.getVectorStatus()), KnowledgeAttach::getVectorStatus, bo.getVectorStatus());
        return lqw;
    }

    /**
     * 新增知识库附件
     */
    @Override
    public Boolean insertByBo(KnowledgeAttachBo bo) {
        KnowledgeAttach add = BeanUtil.toBean(bo, KnowledgeAttach.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改知识库附件
     */
    @Override
    public Boolean updateByBo(KnowledgeAttachBo bo) {
        KnowledgeAttach update = BeanUtil.toBean(bo, KnowledgeAttach.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除知识库附件
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据知识库ID查询附件列表
     */
    @Override
    public List<KnowledgeAttachVo> queryListByKnowledgeId(String knowledgeId) {
        LambdaQueryWrapper<KnowledgeAttach> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgeAttach::getKnowledgeId, knowledgeId);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 根据文档ID查询附件信息
     */
    @Override
    public KnowledgeAttachVo queryByDocId(String docId) {
        LambdaQueryWrapper<KnowledgeAttach> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgeAttach::getDocId, docId);
        return baseMapper.selectVoOne(lqw);
    }
}