package org.dromara.knowledge.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.KnowledgeSchemaGroup;
import org.dromara.knowledge.domain.bo.KnowledgeSchemaGroupBo;
import org.dromara.knowledge.domain.vo.KnowledgeSchemaGroupVo;
import org.dromara.knowledge.mapper.KnowledgeSchemaGroupMapper;
import org.dromara.knowledge.service.KnowledgeSchemaGroupService;
import org.springframework.stereotype.Service;

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

/**
 * 知识库模式组Service业务层处理
 *
 * @author pengles
 */
@RequiredArgsConstructor
@Service
public class KnowledgeSchemaGroupServiceImpl extends ServiceImpl<KnowledgeSchemaGroupMapper, KnowledgeSchemaGroup> implements KnowledgeSchemaGroupService {

    private final KnowledgeSchemaGroupMapper baseMapper;

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

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

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

    private LambdaQueryWrapper<KnowledgeSchemaGroup> buildQueryWrapper(KnowledgeSchemaGroupBo bo) {
        LambdaQueryWrapper<KnowledgeSchemaGroup> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), KnowledgeSchemaGroup::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), KnowledgeSchemaGroup::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getIcon()), KnowledgeSchemaGroup::getIcon, bo.getIcon());
        return lqw;
    }

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

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

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

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

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

    /**
     * 根据名称查询模式组信息
     */
    @Override
    public KnowledgeSchemaGroupVo queryByName(String name) {
        LambdaQueryWrapper<KnowledgeSchemaGroup> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgeSchemaGroup::getName, name);
        return baseMapper.selectVoOne(lqw);
    }
}