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.KnowledgeRole;
import org.dromara.knowledge.domain.bo.KnowledgeRoleBo;
import org.dromara.knowledge.domain.vo.KnowledgeRoleVo;
import org.dromara.knowledge.mapper.KnowledgeRoleMapper;
import org.dromara.knowledge.service.KnowledgeRoleService;
import org.springframework.stereotype.Service;

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

/**
 * 知识库角色Service业务层处理
 *
 * @author pengles
 */
@RequiredArgsConstructor
@Service
public class KnowledgeRoleServiceImpl extends ServiceImpl<KnowledgeRoleMapper, KnowledgeRole> implements KnowledgeRoleService {

    private final KnowledgeRoleMapper baseMapper;

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

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

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

    private LambdaQueryWrapper<KnowledgeRole> buildQueryWrapper(KnowledgeRoleBo bo) {
        LambdaQueryWrapper<KnowledgeRole> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getRoleName()), KnowledgeRole::getRoleName, bo.getRoleName());
        lqw.eq(StringUtils.isNotBlank(bo.getRoleCode()), KnowledgeRole::getRoleCode, bo.getRoleCode());
        lqw.like(StringUtils.isNotBlank(bo.getRoleDesc()), KnowledgeRole::getRoleDesc, bo.getRoleDesc());
        lqw.eq(ObjectUtil.isNotNull(bo.getStatus()), KnowledgeRole::getStatus, bo.getStatus());
        lqw.orderByAsc(KnowledgeRole::getSort);
        return lqw;
    }

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

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

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

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

    /**
     * 根据角色编码查询角色信息
     */
    @Override
    public KnowledgeRoleVo queryByRoleCode(String roleCode) {
        LambdaQueryWrapper<KnowledgeRole> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgeRole::getRoleCode, roleCode);
        return baseMapper.selectVoOne(lqw);
    }

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

}