package com.sonic.bear.domain.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.sonic.bear.domain.typehandler.AccountTypeSetTypeHandler;
import com.sonic.bear.enums.AccountType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.Set;

/**
 * @author coder
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@TableName(value = "app_client", autoResultMap = true)
public class AppClient {
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    /** 编码 */
    private String code;
    /** 描述 */
    @TableField("`desc`")
    private String desc;
    /** 允许登陆的账号类型 */
    @TableField(typeHandler = AccountTypeSetTypeHandler.class)
    private Set<AccountType> allowAccountTypes;
    /** session过期时间,单位为分钟 */
    private Integer sessionAlive;
    /** session存活时间, session 在未过期的情况，最大可以续存时间，单位为分钟 */
    private Integer sessionExpired;
    /** 创建时间 */
    private LocalDateTime createTime;
    /** 更新时间 */
    private LocalDateTime editTime;

    public boolean allowUser(AccountType accountType) {
        if (this.getAllowAccountTypes().isEmpty()) {
            return false;
        }
        return this.getAllowAccountTypes().stream().anyMatch(type -> type == accountType);
    }
}
