package com.njzscloud.dispose.sys.auth.pojo.result;

import cn.hutool.core.lang.Assert;
import com.njzscloud.common.core.ex.Exceptions;
import com.njzscloud.common.security.support.UserDetail;
import com.njzscloud.dispose.sys.user.constant.Gender;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@Getter
@Setter
@Accessors(chain = true)
public class MyResult extends UserDetail {

    public static final String ROLE_KANLIAO = "ROLE_KANLIAO";

    /**
     * 菜单信息
     */
    private List<MenuResource> menus;

    /**
     * 端点信息
     */
    private List<EndpointResource> endpoints;
    /**
     * 身份信息
     */
    private List<IdentityInfo> identities;

    /**
     * 系统设置
     */
    private List<Map<String, Object>> setting;

    /**
     * 当前身份 Id
     *
     */
    private Long currentCustomerId;

    /**
     * 当前组织 Id
     *
     */
    private Long currentOrgId;

    /**
     * 头像
     */
    private String avatar;
    /**
     * 性别; 字典代码：gender
     */
    private Gender gender;
    /**
     * 手机号
     */
    private String phone;
    /**
     * 邮箱
     */
    private String email;
    /**
     * 余额
     */
    private BigDecimal recharge;
    /**
     * 营收
     */
    private BigDecimal revenue;

    public IdentityInfo currentIdentity() {
        Optional<IdentityInfo> identityInfo = identities.stream().filter(it -> Objects.equals(it.getCustomerId(), currentCustomerId)).findFirst();

        Assert.isTrue(identityInfo.isPresent(), () -> Exceptions.error("无身份信息"));
        // noinspection OptionalGetWithoutIsPresent
        return identityInfo.get();
    }

    /**
     * 是否是看料员
     *
     * @return true/false
     */
    public boolean isKanLiao() {
        return this.getRoles().contains(ROLE_KANLIAO);
    }

}
