package com.wzj.soopin.member.utils;

import lombok.Data;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.core.domain.model.LoginUser;

/**
 * 用户类型工具类
 */
public class UserTypeUtils {

    @Data
    public static class UserInfo {
        private Long userId;
        private Integer userType; // 1-租户 2-会员
        private String userTypeDesc;
        private Long memberId; // 会员ID，如果是会员用户
        private Long tenantId; // 租户ID，如果是租户用户
        private Long accountId; // 账户ID，用于查询
    }

    /**
     * 获取当前登录用户信息
     */
    public static UserInfo getCurrentUserInfo() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        if (loginUser == null) {
            throw new RuntimeException("用户未登录");
        }

        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(loginUser.getUserId());

        String userType = loginUser.getUserType();
        
        if (StringUtils.isNotBlank(userType)) {
            if ("member".equalsIgnoreCase(userType) || "2".equals(userType)) {
                // 会员用户
                userInfo.setUserType(2);
                userInfo.setUserTypeDesc("会员");
                userInfo.setMemberId(loginUser.getUserId());
                userInfo.setAccountId(loginUser.getUserId());
            } else if ("tenant".equalsIgnoreCase(userType) || "1".equals(userType)) {
                // 租户用户
                userInfo.setUserType(1);
                userInfo.setUserTypeDesc("租户");
                userInfo.setTenantId(loginUser.getUserId());
                userInfo.setAccountId(loginUser.getUserId());
            } else {
                // 默认按会员处理
                userInfo.setUserType(2);
                userInfo.setUserTypeDesc("会员");
                userInfo.setMemberId(loginUser.getUserId());
                userInfo.setAccountId(loginUser.getUserId());
            }
        } else {
            // 如果没有用户类型信息，默认按会员处理
            userInfo.setUserType(2);
            userInfo.setUserTypeDesc("会员");
            userInfo.setMemberId(loginUser.getUserId());
            userInfo.setAccountId(loginUser.getUserId());
        }

        return userInfo;
    }

    /**
     * 判断当前用户是否为会员
     */
    public static boolean isMember() {
        UserInfo userInfo = getCurrentUserInfo();
        return userInfo.getUserType() == 2;
    }

    /**
     * 判断当前用户是否为租户
     */
    public static boolean isTenant() {
        UserInfo userInfo = getCurrentUserInfo();
        return userInfo.getUserType() == 1;
    }

    /**
     * 获取当前用户的会员ID（如果是会员）
     */
    public static Long getCurrentMemberId() {
        UserInfo userInfo = getCurrentUserInfo();
        if (userInfo.getUserType() != 2) {
            throw new RuntimeException("当前用户不是会员");
        }
        return userInfo.getMemberId();
    }

    /**
     * 获取当前用户的租户ID（如果是租户）
     */
    public static Long getCurrentTenantId() {
        UserInfo userInfo = getCurrentUserInfo();
        if (userInfo.getUserType() != 1) {
            throw new RuntimeException("当前用户不是租户");
        }
        return userInfo.getTenantId();
    }

    /**
     * 获取当前用户的账户ID
     */
    public static Long getCurrentAccountId() {
        UserInfo userInfo = getCurrentUserInfo();
        return userInfo.getAccountId();
    }
}
