package com.sysu.admin.support.system.user;

import com.sysu.admin.support.system.organ.Organ;
import com.xiesx.fastboot.core.jpa.entity.JpaPlusEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import lombok.experimental.FieldNameConstants;
import org.hibernate.annotations.*;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.Map;

/**
 * @title User
 * @description
 * @author XIE
 * @date 2020年4月24日下午10:07:55
 */
@Data
@Accessors(chain = true)
@EqualsAndHashCode(callSuper = false)
@FieldNameConstants(innerTypeName = "FIELDS")
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "sys_user")
@DynamicInsert
@DynamicUpdate
@Where(clause = "is_del=0")
@SQLDelete(sql = "update sys_user set is_del=1 where id = ?")
@SQLDeleteAll(sql = "update sys_user set is_del=1 where id = ?")
public class User extends JpaPlusEntity<User> {

    private static final long serialVersionUID = 1L;

    /**
     * 主键
     */
    @Id
    @GeneratedValue(generator = "idGenerator")
    @GenericGenerator(name = "idGenerator", strategy = "com.xiesx.fastboot.core.jpa.identifier.IdWorkerGenerator")
    private Long id;

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(updatable = false, nullable = false)
    private Date createDate;

    /**
     * 更新时间
     */
    @LastModifiedDate
    @Column(nullable = false)
    private Date updateDate;

    /**
     * 帐号
     */
    @Column
    private String username;


    /**
     * 密码明码
     */
    @Column(name = "`code`")
    private String code;

    /**
     * 密码
     */
    @Column
    private String password;

    /**
     * 昵称
     */
    @Column
    private String nickname;

    /**
     * 登录失败次数
     */
    @Column
    private Integer lastLoginFailures;

    /**
     * 最后登录时间
     */
    @Column
    private Date lastLoginDate;

    /**
     * 是否拉黑（0:启用、1:禁用）
     */
    @Column
    private Integer isBlack;

    /**
     * 是否启用（0:启用、1:禁用）
     */
    @Column
    private Integer isEnable;

    /**
     * 是否删用（0:正常、1:删除）
     */
    @Column
    private Integer isDel;

    @Column(name="organ_id")
    @NotNull(message = "organId not null")
    private Long organId;

    @ManyToOne(cascade= CascadeType.REFRESH,fetch=FetchType.LAZY)
    @JoinColumn(name="organ_id" ,insertable = false, updatable = false)//加入一列作为外键
    private Organ organ;

    @Column
    private String openId;

    // ==========================================
    /**
     * 机构名称
     */
    @Transient
    private String organName;
    /**
     * 角色
     */
    @Transient
    private Long roleId;

    /**
     * 角色名称
     */
    @Transient
    private String roleName;

    /**
     * 角色名称
     */
    @Transient
    private String session_key;
    @Transient
    private String token;
    @Transient
    private Map<String , Object> extData;
}
