package com.railway.common.core.domain.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.railway.common.annotation.Excel;
import com.railway.common.annotation.Excel.ColumnType;
import com.railway.common.annotation.Excel.Type;
import com.railway.common.annotation.Excels;
import com.railway.common.core.domain.BaseEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.Date;
import java.util.List;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 用户对象 sys_user
 *
 * @author railway
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ApiModel("系统管理 - 用户信息")
public class SysUser extends BaseEntity {

  private static final long serialVersionUID = 1L;

  /**
   * 用户ID
   */
  @ApiModelProperty(value = "用户ID", hidden = true)
  @Excel(name = "用户序号", cellType = ColumnType.NUMERIC, prompt = "用户编号")
  private Long userId;

  /**
   * 部门ID
   */
  @ApiModelProperty(value = "部门ID")
  @Excel(name = "部门编号", type = Type.IMPORT)
  private Long deptId;

  /**
   * 部门父ID
   */
  private Long parentId;

  /**
   * 角色ID
   */
  @ApiModelProperty(value = "角色ID")
  private Long roleId;

  /**
   * 登录名称
   */
  @ApiModelProperty(value = "登录用户名")
  @NotBlank(message = "登录账号不能为空")
  @Size(max = 30, message = "登录账号长度不能超过30个字符")
  @Excel(name = "登录用户名")
  private String userName;

  /**
   * 用户昵称（工号）
   */
  @ApiModelProperty(value = "工号")
  @Size(max = 30, message = "用户昵称长度不能超过30个字符")
  @Excel(name = "工号")
  private String nickName;

  /**
   * 员工姓名
   */
  @ApiModelProperty(value = "员工姓名")
  @Size(max = 60, message = "用户姓名长度不能超过60个字符")
  @Excel(name = "员工姓名")
  private String realName;

  /**
   * 用户类型
   */
  private String userType;

  /**
   * 岗位
   */
  private String post;

  /**
   * 岗位名
   */
  private String postText;

  /**
   * 用户邮箱
   */
  @ApiModelProperty(value = "用户邮箱")
  @Email(message = "邮箱格式不正确")
  @Size(max = 50, message = "邮箱长度不能超过50个字符")
  @Excel(name = "用户邮箱")
  private String email;

  /**
   * 手机号码
   */
  @ApiModelProperty(value = "手机号码")
  @Size(max = 11, message = "手机号码长度不能超过11个字符")
  @Excel(name = "手机号码")
  private String phone;

  /**
   * 微信号
   */
  @ApiModelProperty(value = "微信号")
  @Size(max = 64, message = "微信号长度不能超过64个字符")
  @Excel(name = "微信号")
  private String wechat;

  /**
   * 用户性别
   */
  @ApiModelProperty(value = "用户性别")
  @Excel(name = "用户性别", readConverterExp = "0=男,1=女,2=未知")
  private String sex;

  /**
   * 出生日期
   */
  @ApiModelProperty(value = "出生日期")
  @Excel(name = "出生日期", width = 30, dateFormat = "yyyy-MM-dd", type = Type.EXPORT)
  private Date birthday;

  /**
   * 用户头像
   */
  @ApiModelProperty(value = "用户头像")
  private String avatar;

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

  /**
   * 盐加密
   */
  private String salt;

  /**
   * 帐号状态（0正常 1停用）
   */
  @ApiModelProperty(value = "帐号状态（0正常 1停用）")
  @Excel(name = "帐号状态", readConverterExp = "0=正常,1=停用")
  private String status;

  /**
   * 删除标志（0代表存在 1代表删除）
   */
  private String delFlag;

  /**
   * 最后登录IP
   */
  @Excel(name = "最后登录IP", type = Type.EXPORT)
  private String loginIp;

  /**
   * 最后登录时间
   */
  @Excel(name = "最后登录时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss", type = Type.EXPORT)
  private Date loginDate;

  /**
   * 密码最后更新时间
   */
  private Date pwdUpdateDate;

  /**
   * 部门对象
   */
  @Excels({
      @Excel(name = "部门名称", targetAttr = "deptName", type = Type.EXPORT),
      @Excel(name = "部门负责人", targetAttr = "leader", type = Type.EXPORT)
  })
  private SysDept dept = new SysDept();

  /**
   * 角色名，以逗号分隔多个角色名
   */
  private String roleName;

  private List<SysRole> roles;

  /**
   * 角色组
   */
  private Long[] roleIds;

  /**
   * 岗位组
   */
  private Long[] postIds;

  public SysUser() {

  }

  public SysUser(Long userId) {
    this.userId = userId;
  }


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

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

}
