package cn.wisenergy.service.utils;

import cn.wisenergy.common.utils.exception.BASE_RESP_CODE_ENUM;
import cn.wisenergy.common.utils.exception.BaseCustomException;
import cn.wisenergy.mapper.WorkLevelMapper;
import cn.wisenergy.mapper.WorkRoleLevelMapper;
import cn.wisenergy.mapper.WorkRoleMapper;
import cn.wisenergy.mapper.WorkUserRoleMapper;
import cn.wisenergy.model.app.WorkLevel;
import cn.wisenergy.model.app.WorkRole;
import cn.wisenergy.model.dto.UserRoleLevelDto;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.util.Comparator;
import java.util.List;

@Component
public class UserRoleLevelUtils {

    @Autowired
    private WorkRoleMapper workRoleMapper;

    @Autowired
    private WorkLevelMapper workLevelMapper;

    @Autowired
    private WorkRoleLevelMapper workRoleLevelMapper;

    @Autowired
    private WorkUserRoleMapper workUserRoleMapper;


    public static UserRoleLevelUtils utils;


    @PostConstruct
    public void init(){
        utils = this;
        utils.workRoleMapper = workRoleMapper;
        utils.workLevelMapper = workLevelMapper;
        utils.workRoleLevelMapper = workRoleLevelMapper;
        utils.workUserRoleMapper = workUserRoleMapper;
    }

    /**
     * 获取用户的角色
     * @param userId 用户id
     * @return 角色id
     */
    public static List<Integer> getRole(Integer userId) {
        if (null == userId) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        List<Integer> roleIds = utils.workUserRoleMapper.getRole(userId);
        if (CollectionUtils.isEmpty(roleIds)) {
            roleIds.add(0);
            return roleIds;
        }

        roleIds.sort(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        });
        return roleIds;
    }

    /**
     * 获取角色的所有权限
     * @param roleIds 角色ids
     * @return 权限
     */
    public static List<Integer> getlevelIds(List<Integer> roleIds) {
        if (CollectionUtils.isEmpty(roleIds)) {
            return null;
        }
        List<Integer> levelIds = utils.workRoleLevelMapper.getLevelIds(roleIds);
        if (CollectionUtils.isEmpty(levelIds)) {
            return null;
        }
        return levelIds;
    }

    public static List<WorkLevel> getlevelByIds(List<Integer> levelIds) {
        if (CollectionUtils.isEmpty(levelIds)) {
            return null;
        }
        List<WorkLevel> workLevels =utils. workLevelMapper.selectBatchIds(levelIds);
        if (CollectionUtils.isEmpty(workLevels)) {
            return null;
        }


        workLevels.sort(new Comparator<WorkLevel>() {
            @Override
            public int compare(WorkLevel o1, WorkLevel o2) {
                return o2.getId()-o1.getId();
            }
        });
        return workLevels;
    }

    /**
     * 获取所有的角色
     * @return List<WorkRole>
     */
    public static List<WorkRole> getAllWorkRole() {
        List<WorkRole> workRoles = utils.workRoleMapper.selectList(new QueryWrapper<>());
        if (CollectionUtils.isEmpty(workRoles)) {
            return null;
        }
        return workRoles;

    }

    /**
     * 获取所有的权限
     * @return List<WorkLevel>
     */
    public static List<WorkLevel> getAllWorkLevel() {
        List<WorkLevel> workLevels = utils.workLevelMapper.selectList(new QueryWrapper<>());
        if (CollectionUtils.isEmpty(workLevels)) {
            return null;
        }
        return workLevels;
    }

    public static List<UserRoleLevelDto> getRoleUserLevel(Integer userId) {
        if (null == userId) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        List<UserRoleLevelDto> roleUserLevels = utils.workUserRoleMapper.getRoleUserLevel(userId);
        if (CollectionUtils.isEmpty(roleUserLevels)) {
            return null;
        }
        return roleUserLevels;

    }

    /** 是否有管理某方案权限*/
    public static Boolean isManagerLevel(Integer userId, String levelEnum) {
        if (null == userId || StringUtils.isEmpty(levelEnum)) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.INPUT_PARAM_IS_NULL);
        }
        List<UserRoleLevelDto> roleUserLevel = UserRoleLevelUtils.getRoleUserLevel(userId);
        if (CollectionUtils.isEmpty(roleUserLevel)) {
            throw new BaseCustomException(BASE_RESP_CODE_ENUM.THE_USER_NOT_MANAGER_PLASE_MANAGER_LOGIN);
        }
        for (UserRoleLevelDto userRoleLevelDto : roleUserLevel) {
            if (levelEnum.equals(userRoleLevelDto.getLevelName())) {
                return true;
            }
        }
        return false;
    }


}
