package com.css.simulation.resource.server.domain.service;

import api.common.pojo.constants.DictConstants;
import com.css.simulation.resource.server.infra.db.entity.SystemUserEntity;
import com.css.simulation.resource.server.infra.db.mysql.mapper.SystemUserMapper;

import javax.annotation.Resource;

@org.springframework.stereotype.Service
public class UserDomainService {

    @Resource
    private SystemUserMapper systemUserMapper;

    public boolean isAdmin(String userId, String roleCode) {
        if (DictConstants.ROLE_CODE_SYSADMIN.equals(roleCode) || DictConstants.ROLE_CODE_ADMIN.equals(roleCode)) {
            return true;
        } else if (DictConstants.ROLE_CODE_UESR.equals(roleCode)) {
            return false;
        } else if (DictConstants.ROLE_CODE_SUBUESR.equals(roleCode)) {
            String parentUserId = systemUserMapper.selectCreateUserIdById(SystemUserEntity.builder().id(userId).build());
            String parentRoleCode = systemUserMapper.selectRoleCodeById(SystemUserEntity.builder().id(parentUserId).build());
            if (DictConstants.ROLE_CODE_SYSADMIN.equals(parentRoleCode) || DictConstants.ROLE_CODE_ADMIN.equals(parentRoleCode)) {
                return true;
            } else if (DictConstants.ROLE_CODE_UESR.equals(parentRoleCode)) {
                return false;
            } else {
                throw new RuntimeException("未知用户" + parentUserId + "角色：" + roleCode);
            }
        } else {
            throw new RuntimeException("未知用户" + userId + "角色：" + roleCode);
        }
    }
}
