package com.prolog.rdc.tms.proxy;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.prolog.cs.platform.power.model.Organization;
import com.prolog.cs.platform.power.model.enterprise.User;
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<User> getUserByRoleId(String roleId) {
        RestMessage<List<User>> restMessage = null;
        try {
            restMessage = userFeign.usersByRoleId(null, null, roleId);
        } catch (Exception e) {
            throw new BizException("角色查询用户信息异常");
        } finally {
            log.info("topic={},title={},request={},response={}", TOPIC, "getUserByRoleId", roleId, 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<User> getUserByIdList(String userIdList) {
        RestMessage<List<User>> restMessage = null;
        try {
            restMessage = userFeign.getUserByIdList(userIdList);
        } catch (Exception e) {
            throw new BizException("用户id查询用户信息异常");
        } finally {
            log.info("topic={},title={},request={},response={}", TOPIC, "getUserByIdList", userIdList, JSON.toJSONString(restMessage));
        }
        if (ObjectUtil.isNull(restMessage) || !restMessage.isSuccess()) {
            throw new BizException("用户id查询用户信息失败");
        }
        if (ObjectUtil.isNull(restMessage.getData())) {
            throw new BizException("用户id查询用户信息为空");
        }
        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 (ObjectUtil.isNull(restMessage.getData())) {
            throw new BizException("查询部门信息为空");
        }
        return restMessage.getData();
    }

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