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.KnowledgeSchemaField;
import org.dromara.knowledge.domain.bo.KnowledgeSchemaFieldBo;
import org.dromara.knowledge.domain.vo.KnowledgeSchemaFieldVo;
import org.dromara.knowledge.mapper.KnowledgeSchemaFieldMapper;
import org.dromara.knowledge.service.KnowledgeSchemaFieldService;
import org.springframework.stereotype.Service;

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

/**
 * 知识库模式字段Service业务层处理
 *
 * @author pengles
 */
@RequiredArgsConstructor
@Service
public class KnowledgeSchemaFieldServiceImpl extends ServiceImpl<KnowledgeSchemaFieldMapper, KnowledgeSchemaField> implements KnowledgeSchemaFieldService {

    private final KnowledgeSchemaFieldMapper baseMapper;

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

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

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

    private LambdaQueryWrapper<KnowledgeSchemaField> buildQueryWrapper(KnowledgeSchemaFieldBo bo) {
        LambdaQueryWrapper<KnowledgeSchemaField> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtil.isNotNull(bo.getSchemaId()), KnowledgeSchemaField::getSchemaId, bo.getSchemaId());
        lqw.eq(StringUtils.isNotBlank(bo.getSchemaName()), KnowledgeSchemaField::getSchemaName, bo.getSchemaName());
        lqw.like(StringUtils.isNotBlank(bo.getName()), KnowledgeSchemaField::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getCode()), KnowledgeSchemaField::getCode, bo.getCode());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), KnowledgeSchemaField::getType, bo.getType());
        lqw.eq(ObjectUtil.isNotNull(bo.getIsPk()), KnowledgeSchemaField::getIsPk, bo.getIsPk());
        lqw.eq(ObjectUtil.isNotNull(bo.getIsRequired()), KnowledgeSchemaField::getIsRequired, bo.getIsRequired());
        lqw.eq(ObjectUtil.isNotNull(bo.getIsUnique()), KnowledgeSchemaField::getIsUnique, bo.getIsUnique());
        lqw.eq(StringUtils.isNotBlank(bo.getQueryType()), KnowledgeSchemaField::getQueryType, bo.getQueryType());
        lqw.eq(StringUtils.isNotBlank(bo.getHtmlType()), KnowledgeSchemaField::getHtmlType, bo.getHtmlType());
        lqw.eq(StringUtils.isNotBlank(bo.getDictType()), KnowledgeSchemaField::getDictType, bo.getDictType());
        return lqw;
    }

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

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

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

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

    /**
     * 根据模式ID查询字段列表
     */
    @Override
    public List<KnowledgeSchemaFieldVo> queryListBySchemaId(Long schemaId) {
        LambdaQueryWrapper<KnowledgeSchemaField> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgeSchemaField::getSchemaId, schemaId);
        lqw.orderByAsc(KnowledgeSchemaField::getSort);
        return baseMapper.selectVoList(lqw);
    }

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

    /**
     * 根据模式ID和字段编码查询字段信息
     */
    @Override
    public KnowledgeSchemaFieldVo queryBySchemaIdAndCode(Long schemaId, String code) {
        LambdaQueryWrapper<KnowledgeSchemaField> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgeSchemaField::getSchemaId, schemaId);
        lqw.eq(KnowledgeSchemaField::getCode, code);
        return baseMapper.selectVoOne(lqw);
    }
}