UserRoleRpcService.java 4.36 KB
package cn.fw.valhalla.rpc.erp;

import cn.fw.data.base.domain.common.Message;
import cn.fw.erp.sdk.api.UserRoleApi;
import cn.fw.erp.sdk.api.result.UserRoleDataRange;
import cn.fw.erp.sdk.api.result.UserRoleInfo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 用户角色RPC服务
 * <p>
 * create at 2020-06-15
 *
 * @author kurisu
 */
@Slf4j
@Service
public class UserRoleRpcService {

    private final UserRoleApi userRoleApi;

    public UserRoleRpcService(UserRoleApi userRoleApi) {
        this.userRoleApi = userRoleApi;
    }

    /**
     * 查询用户角色的数据权限范围(门店)
     *
     * @param userId   用户id
     * @param roleCode 角色码
     * @return 门店ids
     */
    public List<Long> getManageShopIds(Long userId, String roleCode) {
        if (userId == null) {
            return null;
        }
        try {
            Message<List<UserRoleDataRange>> msg = userRoleApi.queryUserRoleDataRange(userId, roleCode);
            log.warn("调用ERP系统查询用户数据范围,userId:{},roleCode:{}, result:{}", userId, roleCode, JSONObject.toJSONString(msg));
            if (!msg.isSuccess()) {
                return Collections.emptyList();
            }
            List<UserRoleDataRange> userRoleDataRangeList = msg.getData();
            return userRoleDataRangeList.stream().map(UserRoleDataRange::getRangeValue)
                .collect(Collectors.toList());

        } catch (Exception e) {
            log.error("调用ERP系统查询用户数据范围,userId:{},roleCode:{}, 异常:{}", userId, roleCode, e);
        }
        return Collections.emptyList();
    }

    /**
     * 查询用户拥有的角色
     *
     * @param userId 用户id
     */
    public List<UserRoleInfo> getUserRoles(Long userId) {
        if (Objects.isNull(userId)) {
            return Collections.emptyList();
        }

        String func = "调用ERP系统查询用户拥有的角色";
        try {
            Message<List<UserRoleInfo>> msg = userRoleApi.queryUserRoleList(userId);
            log.info("{},userId:{},result:{}", func, userId, JSON.toJSONString(msg));
            if (msg.isSuccess()) {
                return msg.getData();
            }
        } catch (Exception e) {
            log.error("{}失败,userId:{}", func, userId, e);
        }
        return Collections.emptyList();
    }

    /**
     * 查询用户角色的数据权限范围
     *
     * @param userId   用户id
     * @param roleCode 角色代码
     */
    public List<UserRoleDataRange> getUserRoleDataRange(Long userId, String roleCode) {
        if (Objects.isNull(userId) || Objects.isNull(roleCode)) {
            return Collections.emptyList();
        }

        String func = "调用ERP系统查询用户角色的数据权限范围";
        try {
            Message<List<UserRoleDataRange>> msg = userRoleApi.queryUserRoleDataRange(userId, roleCode);
            log.info("{},userId:{},roleCode:{},result:{}", func, userId, roleCode, JSON.toJSONString(msg));
            if (msg.isSuccess()) {
                return msg.getData();
            }
        } catch (Exception e) {
            log.error("{}失败,userId:{},roleCode:{}", func, userId, roleCode, e);
        }
        return Collections.emptyList();
    }

    /**
     * 查询指定服务站指定角色的用户信息
     *
     * @param shopId   服务站id
     * @param roleCode 角色代码
     */
    public List<UserRoleInfo> getUsers(Long shopId, String roleCode) {
        if (Objects.isNull(shopId) || Objects.isNull(roleCode)) {
            return Collections.emptyList();
        }

        String func = "调用ERP系统查询指定服务站指定角色的用户信息";
        try {
            Message<List<UserRoleInfo>> msg = userRoleApi.queryUserRoleInfos(roleCode, shopId);
            log.info("{},shopId:{},roleCode:{},result:{}", func, shopId, roleCode, JSON.toJSONString(msg));
            if (msg.isSuccess()) {
                return msg.getData();
            }
        } catch (Exception e) {
            log.error("{}失败,shopId:{},roleCode:{}", func, shopId, roleCode, e);
        }
        return Collections.emptyList();
    }
}