EhrRpcService.java 4.13 KB
package cn.fw.shirasawa.rpc.ehr;

import cn.fw.data.base.domain.common.Message;
import cn.fw.ehr.sdk.api.StaffApi;
import cn.fw.ehr.sdk.api.result.StaffInfo;
import cn.fw.shirasawa.common.utils.StringUtils;
import cn.fw.shirasawa.rpc.AbsBaseRpcService;
import cn.fw.shirasawa.rpc.ehr.dto.StaffInfoDTO;
import cn.fw.shirasawa.rpc.erp.dto.UserInfoDTO;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Objects;

/**
 * @author : kurisu
 * @className : EhrRpcService
 * @description : 人事
 * @date: 2021-01-19 17:34
 */
@Slf4j
@Service
public class EhrRpcService extends AbsBaseRpcService {
    private final StaffApi staffApi;

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

    @Autowired
    public EhrRpcService(final StaffApi staffApi) {
        this.staffApi = staffApi;
    }

    @Nullable
    public UserInfoDTO user(final Long userId) {
        StaffInfoDTO staffInfoDTO = queryStaffInfo(userId);
        if (Objects.isNull(staffInfoDTO)) {
            return null;
        }
        return UserInfoDTO.builder()
                .id(staffInfoDTO.getId())
                .userName(staffInfoDTO.getName())
                .headImg(staffInfoDTO.getAvatar())
                .mobile(staffInfoDTO.getMobile())
                .nickName(staffInfoDTO.getName())
                .groupId(staffInfoDTO.getGroupId())
                .build();
    }

    @Nullable
    public StaffInfoDTO queryStaffInfo(final Long userId) {
        if (userId == null) {
            return null;
        }
        final String key = generateKey(userId);
        StaffInfoDTO dto = getFromCache(key, StaffInfoDTO.class);
        if (Objects.nonNull(dto)) {
            return dto;
        }
        try {
            final Message<StaffInfo> msg = staffApi.getStaffInfoById(userId);
            log.info("StaffApi.queryStaffInfo: msg.code={}, msg.result={}, msg.data={}", msg.getCode(), msg.getResult(), msg.getData());
            if (msg.isSuccess() && !Objects.isNull(msg.getData())) {
                final StaffInfoDTO staffInfoDTO = new StaffInfoDTO();
                BeanUtils.copyProperties(msg.getData(), staffInfoDTO);
                setToCache(key, JSONObject.toJSONString(staffInfoDTO), 60 * 60);
                return staffInfoDTO;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Nullable
    public StaffInfoDTO queryStaffInfoByMobile(final String mobile) {
        if (StringUtils.isEmpty(mobile)) {
            return null;
        }
        final String key = generateKey(mobile);
        StaffInfoDTO dto = getFromCache(key, StaffInfoDTO.class);
        if (Objects.nonNull(dto)) {
            return dto;
        }
        try {
            final Message<StaffInfo> msg = staffApi.getStaffInfoByMobile(mobile);
            log.info("StaffApi.queryStaffInfoByMobile: msg.code={}, msg.result={}, msg.data={}", msg.getCode(), msg.getResult(), msg.getData());
            if (msg.isSuccess() && !Objects.isNull(msg.getData())) {
                final StaffInfoDTO staffInfoDTO = new StaffInfoDTO();
                BeanUtils.copyProperties(msg.getData(), staffInfoDTO);
                setToCache(key, JSONObject.toJSONString(staffInfoDTO), 60 * 60);
                return staffInfoDTO;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected String getKeyPrefix() {
        return this.keyPrefix;
    }

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

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

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

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