package com.artfess.rescue.context;

import com.artfess.base.constants.SystemConstants;
import com.artfess.base.feign.UCFeignService;
import com.artfess.base.model.CommonResult;
import com.artfess.base.util.AppUtil;
import com.artfess.base.util.BeanUtils;
import com.artfess.base.util.StringUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.springframework.stereotype.Component;

/**
 * @Author: wsf
 * @Description: 用户相关操作
 * @DateTime: 2024/8/8 09:39
 **/
@Component
public class UserContextUtil {

    /**
     * 给定用户ID判断用户是否为系统管理员Admin
     *
     * @param userId 用户ID
     * @return
     */
    public boolean isAdmin(String userId) {
        UCFeignService service = AppUtil.getBean(UCFeignService.class);
        if (StringUtil.isNotEmpty(userId)) {
            CommonResult<JsonNode> result = service.getUserById(userId);
            JsonNode userJson = result.getValue();
            if (BeanUtils.isNotEmpty(userJson)) {
                return userJson.get("account").asText().contains(SystemConstants.SYSTEM_ACCOUNT);
            }
        }
        return false;
    }

    /**
     * 给定用户ID获取用户所属组织ID
     *
     * @param userId 用户ID
     * @return
     */
    public String getCurrentOrgIdByUserId(String userId) {
        UCFeignService service = AppUtil.getBean(UCFeignService.class);
        if (StringUtil.isNotEmpty(userId)) {
            ObjectNode orgObj = service.getMainGroup(userId);
            if (BeanUtils.isNotEmpty(orgObj)) {
                String orgKind = orgObj.get("orgKind").asText();
                if (orgKind.equals("ogn")) {
                    return orgObj.get("id").asText();
                }
                if (orgKind.equals("dept")) {
                    return getParentOrgAttr(orgObj.get("parentId").asText(), "id");
                }
            }
        }
        return "";
    }

    /**
     * 给定用户ID获取用户所属组织name
     *
     * @return
     */
    public String getCurrentOrgNameByUserId(String userId) {
        UCFeignService service = AppUtil.getBean(UCFeignService.class);
        if (StringUtil.isNotEmpty(userId)) {
            ObjectNode orgObj = service.getMainGroup(userId);
            if (BeanUtils.isNotEmpty(orgObj)) {
                String orgKind = orgObj.get("orgKind").asText();
                if (orgKind.equals("ogn")) {
                    return orgObj.get("name").asText();
                }
                if (orgKind.equals("dept")) {
                    return getParentOrgAttr(orgObj.get("parentId").asText(), "name");
                }
            }
        }
        return "";
    }

    /**
     * 给定用户ID获取用户所属部门ID
     *
     * @return
     */
    public String getCurrentDeptIdByUserId(String userId) {
        UCFeignService service = AppUtil.getBean(UCFeignService.class);
        if (StringUtil.isNotEmpty(userId)) {
            ObjectNode orgObj = service.getMainGroup(userId);
            if (BeanUtils.isNotEmpty(orgObj)) {
                String orgKind = orgObj.get("orgKind").asText();
                if (orgKind.equals("dept")) {
                    return orgObj.get("id").asText();
                }
            }
        }
        return "";
    }

    /**
     * 给定用户ID获取用户所属部门name
     *
     * @return
     */
    public String getCurrentDeptNameByUserId(String userId) {
        UCFeignService service = AppUtil.getBean(UCFeignService.class);
        if (StringUtil.isNotEmpty(userId)) {
            ObjectNode orgObj = service.getMainGroup(userId);
            if (BeanUtils.isNotEmpty(orgObj)) {
                String orgKind = orgObj.get("orgKind").asText();
                if (orgKind.equals("dept")) {
                    return orgObj.get("name").asText();
                }
            }
        }
        return "";
    }

    public static String getParentOrgAttr(String parentId, String attrName) {
        UCFeignService service = AppUtil.getBean(UCFeignService.class);
        ObjectNode parentOrgObj = service.getOrgByIdOrCode(parentId);
        if (BeanUtils.isNotEmpty(parentOrgObj)) {
            String orgKind = parentOrgObj.get("orgKind").asText();
            if (orgKind.equals("ogn")) {
                return parentOrgObj.get(attrName).asText();
            }
            if (orgKind.equals("dept")) {
                String parentOrgParentId = parentOrgObj.get("parentId").asText();
                return getParentOrgAttr(parentOrgParentId, attrName);
            }
        }
        return "";
    }

}
