UserService.java 5.07 KB
package cn.fw.shirasawa.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 cn.fw.shirasawa.rpc.AbsBaseRpcService;
import cn.fw.shirasawa.rpc.erp.dto.PostUserDTO;
import cn.fw.shirasawa.rpc.erp.dto.UserRoleDataRangeDTO;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * 用户服务
 * <p>
 * create at 2019-03-09
 *
 * @author kurisu
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserService extends AbsBaseRpcService {
    /**
     * 岗位用户服务
     */
    private final UserRoleApi userRoleApi;

    @Value("${spring.cache.custom.global-prefix}:rpc:erp")
    @Getter
    private String keyPrefix;

    /**
     * 获取某个流程角色的权限范围
     *
     * @param userId
     * @param roleCode
     * @return
     */
    public List<UserRoleDataRangeDTO> getUserRoleDataRange(final Long userId, final String roleCode) {
        if (Objects.isNull(userId) || Objects.isNull(roleCode)) {
            return Collections.emptyList();
        }
        String key = generateKey(roleCode, userId);
        try {
            List<UserRoleDataRangeDTO> dtos = getListFromCache(key, UserRoleDataRangeDTO.class);
            if (!CollectionUtils.isEmpty(dtos)) {
                return dtos;
            }
            Message<List<UserRoleDataRange>> msg = userRoleApi.queryUserRoleDataRange(userId, roleCode);
            log.info("userRoleApi.queryUserRoleDataRange: msg.code={}, msg.result={}, msg.data={}", msg.getCode(), msg.getResult(), msg.getData());
            if (!msg.isSuccess() || CollectionUtils.isEmpty(msg.getData())) {
                return Collections.emptyList();
            }
            List<UserRoleDataRange> data = msg.getData();
            List<UserRoleDataRangeDTO> list = new ArrayList<>(data.size());
            for (UserRoleDataRange datum : data) {
                UserRoleDataRangeDTO dto = new UserRoleDataRangeDTO();
                BeanUtils.copyProperties(datum, dto);
                list.add(dto);
                setListToCache(key, JSONObject.toJSONString(dto));
            }
            getQueue(key).expire(15, TimeUnit.SECONDS);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    /**
     * 获取门店下某岗位下员工
     *
     * @param shopId   门店
     * @param roleCode 岗位码
     * @return 用户集合
     */
    public List<PostUserDTO> getUserByRole(final Long shopId, final String roleCode) {
        if (Objects.isNull(shopId) || Objects.isNull(roleCode)) {
            return Collections.emptyList();
        }
        String key = generateKey(shopId, roleCode);
        List<PostUserDTO> dtos = getListFromCache(key, PostUserDTO.class);
        if (!CollectionUtils.isEmpty(dtos)) {
            return dtos;
        }
        try {
            Message<List<UserRoleInfo>> msg = userRoleApi.queryUserRoleInfos(roleCode, shopId);
            log.info("userRoleApi.queryUserRoleInfos: msg.code={}, msg.result={}, msg.data={}", msg.getCode(), msg.getResult(), msg.getData());
            if (!msg.isSuccess() || CollectionUtils.isEmpty(msg.getData())) {
                return Collections.emptyList();
            }
            List<UserRoleInfo> data = msg.getData();
            List<PostUserDTO> list = new ArrayList<>(data.size());
            for (UserRoleInfo datum : data) {
                PostUserDTO dto = new PostUserDTO();
                BeanUtils.copyProperties(datum, dto);
                list.add(dto);
                setListToCache(key, JSONObject.toJSONString(dto));
            }
            getQueue(key).expire(10, TimeUnit.MINUTES);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Collections.emptyList();
    }

    private String generateKey(final Long shopId, final String roleCOde) {
        Assert.notNull(shopId, "id cannot be null");
        Assert.hasLength(roleCOde, "roleCOde cannot be null");

        return String.format("%s:%s:%s:%s", getKeyPrefix(), "post", shopId, roleCOde);
    }

    private String generateKey(final String roleCode, final Long userId) {
        Assert.notNull(userId, "id cannot be null");
        Assert.hasLength(roleCode, "roleCOde cannot be null");

        return String.format("%s:%s:%s:%s", getKeyPrefix(), "range", roleCode, userId);
    }

    private String generateKey(final Long userId) {
        Assert.notNull(userId, "userId cannot be null");

        return String.format("%s:%s:%s", getKeyPrefix(), "user", userId);
    }
}