package com.chushang.system.entity.po;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.annotation.*;
import com.chushang.common.mybatis.base.BaseEntity;
import com.chushang.datascope.entity.DataScopeEntity;
import lombok.*;
import lombok.experimental.Accessors;

import java.util.List;

/**
 * <p>
 * 
 * </p>
 *
 * @author author
 * @since 2022-08-18
 */
@Data
@EqualsAndHashCode(callSuper = true)
@Accessors(chain = true)
@TableName("sys_user")
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class SysUser extends BaseEntity {

    /**
     * 自增主键id
     */
    @TableId(value = "user_id", type = IdType.ASSIGN_ID)
    private Long userId;

    /**
     * 用户账号
     */
    private String username;

    /**
     * 密码
     */
    @TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
    private String password;

    /**
     * 账号状态
     */
    private Boolean status;

    /**
     * 密码盐
     */
    @TableField(updateStrategy = FieldStrategy.NOT_EMPTY)
    private String salt;

    /**
     * 所属部门id
     */
    private Long deptId;
    /**
     * 头像
     */
    private String avatar;
    /**
     * 性别
     */
    private Integer gender;
    /**
     * 手机号
     */
    private String phone;
    /**
     * 昵称
     */
    private String email;
    @TableField(value = "nick_name")
    private String nickName;

    /**
     * 创建人角色
     */
    @TableField(updateStrategy = FieldStrategy.NOT_NULL)
    private String createBy;

    /**
     * 修改人
     */
    @TableField(updateStrategy = FieldStrategy.NOT_NULL)
    private String updateBy;


    private String deptName;

    @TableField(exist = false)
    private SysDept dept;

    @TableField(exist = false)
    private List<SysRole> roles;

    @TableField(exist = false)
    private List<DataScopeEntity> dataScopes;
    /**
     * 角色组
     */
    @TableField(exist = false)
    private Long[] roleIds;
    /**
     * 角色id
     */
    @TableField(exist = false)
    private Long roleId;

    public boolean isAdmin()
    {
        return isAdmin(this.userId) || isAdmin(this.roles);
    }

    public static boolean isAdmin(Long userId)
    {
        return userId != null && 1 == userId;
    }

    public static boolean isAdmin(List<SysRole> roles)
    {
        long admin = 0;
        if (CollectionUtil.isNotEmpty(roles)){
            admin = roles.stream().filter(r -> r.getRoleKey().equals("admin")).count();
        }
        return admin > 0;
    }
    public SysUser(Long userId)
    {
        this.userId = userId;
    }

}
