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.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.knowledge.domain.KnowledgeRoleRelation;
import org.dromara.knowledge.domain.bo.KnowledgeRoleRelationBo;
import org.dromara.knowledge.domain.vo.KnowledgeRoleRelationVo;
import org.dromara.knowledge.mapper.KnowledgeRoleRelationMapper;
import org.dromara.knowledge.service.KnowledgeRoleRelationService;
import org.springframework.stereotype.Service;

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

/**
 * 知识库角色关系Service业务层处理
 *
 * @author pengles
 */
@RequiredArgsConstructor
@Service
public class KnowledgeRoleRelationServiceImpl extends ServiceImpl<KnowledgeRoleRelationMapper, KnowledgeRoleRelation> implements KnowledgeRoleRelationService {

    private final KnowledgeRoleRelationMapper baseMapper;

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

    /**
     * 查询知识库角色关系列表
     */
    @Override
    public TableDataInfo<KnowledgeRoleRelationVo> queryPageList(KnowledgeRoleRelationBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KnowledgeRoleRelation> lqw = buildQueryWrapper(bo);
        Page<KnowledgeRoleRelationVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询知识库角色关系列表
     */
    @Override
    public List<KnowledgeRoleRelationVo> queryList(KnowledgeRoleRelationBo bo) {
        LambdaQueryWrapper<KnowledgeRoleRelation> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KnowledgeRoleRelation> buildQueryWrapper(KnowledgeRoleRelationBo bo) {
        LambdaQueryWrapper<KnowledgeRoleRelation> lqw = Wrappers.lambdaQuery();
        lqw.eq(ObjectUtil.isNotNull(bo.getRoleId()), KnowledgeRoleRelation::getRoleId, bo.getRoleId());
        lqw.eq(ObjectUtil.isNotNull(bo.getGroupId()), KnowledgeRoleRelation::getGroupId, bo.getGroupId());
        lqw.eq(ObjectUtil.isNotNull(bo.getRelationType()), KnowledgeRoleRelation::getRelationType, bo.getRelationType());
        lqw.eq(ObjectUtil.isNotNull(bo.getStatus()), KnowledgeRoleRelation::getStatus, bo.getStatus());
        lqw.orderByAsc(KnowledgeRoleRelation::getSort);
        return lqw;
    }

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

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

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

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

    /**
     * 根据角色ID查询关系列表
     */
    @Override
    public List<KnowledgeRoleRelationVo> queryByRoleId(Long roleId) {
        LambdaQueryWrapper<KnowledgeRoleRelation> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgeRoleRelation::getRoleId, roleId);
        lqw.orderByAsc(KnowledgeRoleRelation::getSort);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 根据分组ID查询关系列表
     */
    @Override
    public List<KnowledgeRoleRelationVo> queryByGroupId(Long groupId) {
        LambdaQueryWrapper<KnowledgeRoleRelation> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgeRoleRelation::getGroupId, groupId);
        lqw.orderByAsc(KnowledgeRoleRelation::getSort);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 根据角色ID和分组ID查询关系
     */
    @Override
    public KnowledgeRoleRelationVo queryByRoleIdAndGroupId(Long roleId, Long groupId) {
        LambdaQueryWrapper<KnowledgeRoleRelation> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgeRoleRelation::getRoleId, roleId);
        lqw.eq(KnowledgeRoleRelation::getGroupId, groupId);
        return baseMapper.selectVoOne(lqw);
    }

}