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.KnowledgeSchema;
import org.dromara.knowledge.domain.bo.KnowledgeSchemaBo;
import org.dromara.knowledge.domain.vo.KnowledgeSchemaVo;
import org.dromara.knowledge.mapper.KnowledgeSchemaMapper;
import org.dromara.knowledge.service.KnowledgeSchemaService;
import org.springframework.stereotype.Service;

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

/**
 * 知识库模式Service业务层处理
 *
 * @author pengles
 */
@RequiredArgsConstructor
@Service
public class KnowledgeSchemaServiceImpl extends ServiceImpl<KnowledgeSchemaMapper, KnowledgeSchema> implements KnowledgeSchemaService {

    private final KnowledgeSchemaMapper baseMapper;

    /**
     * 查询知识库模式
     */
    @Override
    public KnowledgeSchemaVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询知识库模式列表
     */
    @Override
    public TableDataInfo<KnowledgeSchemaVo> queryPageList(KnowledgeSchemaBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KnowledgeSchema> lqw = buildQueryWrapper(bo);
        Page<KnowledgeSchemaVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询知识库模式列表
     */
    @Override
    public List<KnowledgeSchemaVo> queryList(KnowledgeSchemaBo bo) {
        LambdaQueryWrapper<KnowledgeSchema> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KnowledgeSchema> buildQueryWrapper(KnowledgeSchemaBo bo) {
        LambdaQueryWrapper<KnowledgeSchema> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtil.isNotNull(bo.getSchemaGroupId()), KnowledgeSchema::getSchemaGroupId, bo.getSchemaGroupId());
        lqw.like(StringUtils.isNotBlank(bo.getName()), KnowledgeSchema::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), KnowledgeSchema::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getTableName()), KnowledgeSchema::getTableName, bo.getTableName());
        return lqw;
    }

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

    /**
     * 修改知识库模式
     */
    @Override
    public Boolean updateByBo(KnowledgeSchemaBo bo) {
        KnowledgeSchema update = BeanUtil.toBean(bo, KnowledgeSchema.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    /**
     * 根据模式组ID查询模式列表
     */
    @Override
    public List<KnowledgeSchemaVo> queryListBySchemaGroupId(Long schemaGroupId) {
        LambdaQueryWrapper<KnowledgeSchema> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgeSchema::getSchemaGroupId, schemaGroupId);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 根据编码查询模式信息
     */
    @Override
    public KnowledgeSchemaVo queryByCode(String code) {
        LambdaQueryWrapper<KnowledgeSchema> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgeSchema::getCode, code);
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 根据表名查询模式信息
     */
    @Override
    public KnowledgeSchemaVo queryByTableName(String tableName) {
        LambdaQueryWrapper<KnowledgeSchema> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgeSchema::getTableName, tableName);
        return baseMapper.selectVoOne(lqw);
    }
}