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.KnowledgeRoleGroup;
import org.dromara.knowledge.domain.bo.KnowledgeRoleGroupBo;
import org.dromara.knowledge.domain.vo.KnowledgeRoleGroupVo;
import org.dromara.knowledge.mapper.KnowledgeRoleGroupMapper;
import org.dromara.knowledge.service.KnowledgeRoleGroupService;
import org.springframework.stereotype.Service;

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

/**
 * 知识库角色分组Service业务层处理
 *
 * @author pengles
 */
@RequiredArgsConstructor
@Service
public class KnowledgeRoleGroupServiceImpl extends ServiceImpl<KnowledgeRoleGroupMapper, KnowledgeRoleGroup> implements KnowledgeRoleGroupService {

    private final KnowledgeRoleGroupMapper baseMapper;

    /**
     * 查询知识库角色分组
     */
    @Override
    public KnowledgeRoleGroupVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询知识库角色分组列表
     */
    @Override
    public TableDataInfo<KnowledgeRoleGroupVo> queryPageList(KnowledgeRoleGroupBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KnowledgeRoleGroup> lqw = buildQueryWrapper(bo);
        Page<KnowledgeRoleGroupVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询知识库角色分组列表
     */
    @Override
    public List<KnowledgeRoleGroupVo> queryList(KnowledgeRoleGroupBo bo) {
        LambdaQueryWrapper<KnowledgeRoleGroup> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KnowledgeRoleGroup> buildQueryWrapper(KnowledgeRoleGroupBo bo) {
        LambdaQueryWrapper<KnowledgeRoleGroup> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getGroupName()), KnowledgeRoleGroup::getGroupName, bo.getGroupName());
        lqw.eq(StringUtils.isNotBlank(bo.getGroupCode()), KnowledgeRoleGroup::getGroupCode, bo.getGroupCode());
        lqw.like(StringUtils.isNotBlank(bo.getGroupDesc()), KnowledgeRoleGroup::getGroupDesc, bo.getGroupDesc());
        lqw.eq(ObjectUtil.isNotNull(bo.getStatus()), KnowledgeRoleGroup::getStatus, bo.getStatus());
        lqw.orderByAsc(KnowledgeRoleGroup::getSort);
        return lqw;
    }

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

    /**
     * 修改知识库角色分组
     */
    @Override
    public Boolean updateByBo(KnowledgeRoleGroupBo bo) {
        KnowledgeRoleGroup update = BeanUtil.toBean(bo, KnowledgeRoleGroup.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    /**
     * 根据分组编码查询分组信息
     */
    @Override
    public KnowledgeRoleGroupVo queryByGroupCode(String groupCode) {
        LambdaQueryWrapper<KnowledgeRoleGroup> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgeRoleGroup::getGroupCode, groupCode);
        return baseMapper.selectVoOne(lqw);
    }

    /**
     * 查询启用状态的分组列表
     */
    @Override
    public List<KnowledgeRoleGroupVo> queryEnabledList() {
        LambdaQueryWrapper<KnowledgeRoleGroup> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgeRoleGroup::getStatus, 1);
        lqw.orderByAsc(KnowledgeRoleGroup::getSort);
        return baseMapper.selectVoList(lqw);
    }

}