package com.cool.store.dao;

import com.cool.store.entity.SysRoleDO;
import com.cool.store.enums.DataSourceEnum;
import com.cool.store.mapper.SysRoleMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author zhangchenbiao
 * @FileName: SysRoleDAO
 * @Description:
 * @date 2023-06-08 11:45
 */
@Repository
public class SysRoleDAO {

    @Resource
    private SysRoleMapper sysRoleMapper;

    public Integer batchInsertSelective(List<SysRoleDO> insertOrUpdateList){
        if(CollectionUtils.isEmpty(insertOrUpdateList)){
            return null;
        }
        return sysRoleMapper.batchInsertSelective(insertOrUpdateList);
    }

    public Integer deleteRole(DataSourceEnum dataSourceEnum, List<String> roleIds){
        if(Objects.isNull(dataSourceEnum) || CollectionUtils.isEmpty(roleIds)){
            return null;
        }
        return sysRoleMapper.deleteRole(dataSourceEnum.getCode(), roleIds);
    }

    public SysRoleDO getHighestPriorityRoleByUserId(String userId) {
        if(StringUtils.isBlank(userId)){
            return null;
        }
        return sysRoleMapper.getHighestPriorityRoleByUserId(userId);
    }

    public SysRoleDO getRoleByName(String roleName, DataSourceEnum dataSource){
        if(StringUtils.isBlank(roleName) || Objects.isNull(dataSource)){
            return null;
        }
        return sysRoleMapper.getRoleByName(roleName, dataSource.getCode());
    }

    public Page<SysRoleDO> getRolePage(Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        return sysRoleMapper.getRolePage();
    }

    public SysRoleDO getRoleDetail(String roleId){
        if(StringUtils.isBlank(roleId)){
            return null;
        }
        return sysRoleMapper.getRoleDetail(roleId);
    }
}
