package com.prolog.rdc.tms.app.proxy;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.prolog.cs.platform.power.model.Organization;
import com.prolog.framework.authority.core.dto.UserDTO;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.BizException;
import com.prolog.rdc.tms.rmi.feign.UserFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author hongbo.pan
 * @date 2022/10/18
 */
@Slf4j
@Component
public class UserProxy {
    private static final String TOPIC = "UserProxy";

    @Autowired
    private UserFeign userFeign;

    public UserDTO getUserInfo(String username) {
        RestMessage<UserDTO> restMessage = null;
        try {
            restMessage = userFeign.getPlatformUserByUserName(username, null);
        } catch (Exception e) {
            throw new BizException("查询用户信息异常");
        } finally {
            log.info("topic={},title={},request={},response={}", TOPIC, "getUserInfo", username, JSON.toJSONString(restMessage));
        }
        if (ObjectUtil.isNull(restMessage) || !restMessage.isSuccess()) {
            throw new BizException("查询用户信息失败");
        }
        if (ObjectUtil.isNull(restMessage.getData())) {
            throw new BizException("查询用户信息为空");
        }
        return restMessage.getData();
    }

    public List<Organization> getDepartmentByUserId(String userId) {
        RestMessage<List<Organization>> restMessage = null;
        try {
            restMessage = userFeign.selectByUserId(userId);
        } catch (Exception e) {
            throw new BizException("查询部门信息异常");
        } finally {
            log.info("topic={},title={},request={},response={}", TOPIC, "getDepartmentByUserId", userId, JSON.toJSONString(restMessage));
        }
        if (ObjectUtil.isNull(restMessage) || !restMessage.isSuccess()) {
            throw new BizException("查询部门信息失败");
        }
        if (CollectionUtil.isEmpty(restMessage.getData())) {
            throw new BizException("查询部门信息为空");
        }
        return restMessage.getData();
    }
}
