package com.qmth.teachcloud.common.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.qmth.teachcloud.common.base.BaseEntity;
import com.qmth.teachcloud.common.contant.SpringContextHolder;
import com.qmth.teachcloud.common.contant.SystemConstant;
import com.qmth.teachcloud.common.enums.userPush.SyncStatusEnum;
import com.qmth.teachcloud.common.service.SysConfigService;
import com.qmth.teachcloud.common.util.Base64Util;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Objects;

/**
 * <p>
 * 用户表
 * </p>
 *
 * @author xf
 * @since 2021-03-23
 */
@TableName("sys_user")
public class SysUser extends BaseEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "学校id")
    @JsonSerialize(using = ToStringSerializer.class)
    @TableField("school_id")
    private Long schoolId;
    /**
     * 用户名
     */
    @TableField("login_name")
    @NotBlank(message = "请输入登录名")
    @Length(message = "登录名不能超过{max}个字符", max = 50)
    private String loginName;
    /**
     * 姓名
     */
    @TableField("real_name")
    @NotBlank(message = "请输入姓名")
    @Length(message = "姓名不能超过{max}个字符", max = 50)
    private String realName;

    /**
     * 姓名
     */
    @TableField("code")
    @NotBlank(message = "请输入工号")
    @Length(message = "工号不能超过{max}个字符", max = 50)
    private String code;
    /**
     * 密码
     */
    private String password;
    /**
     * 手机号
     */
    @TableField("mobile_number")
//    @NotBlank(message = "请输入手机号码")
    @Length(message = "手机号码不能超过{max}个字符", max = 25)
    private String mobileNumber;

    @ApiModelProperty(value = "机构id")
    @JsonSerialize(using = ToStringSerializer.class)
    @TableField("org_id")
    private Long orgId;

    @ApiModelProperty(value = "是否启用，0：停用，1：启用")
    private Boolean enable = true;

    /**
     * 密码修改次数
     */
    @TableField("pwd_count")
    private Integer pwdCount;

    /**
     * 密码修改时间
     */
    @TableField("pwd_update_time")
    private Long pwdUpdateTime;
    /**
     * 备注
     */
    private String remark;

    @TableField(exist = false)
    private String oldPassword;

    @TableField(exist = false)
    @ApiModelProperty(value = "前端传的修改手机号码时的验证码")
    private String verifyCode;

    @JsonSerialize(using = ToStringSerializer.class)
    @TableField(exist = false)
    @NotNull(message = "请选择角色")
    private Long[] roleIds;

    @JsonSerialize(using = ToStringSerializer.class)
    @TableField(exist = false)
    private Long[] courseIds;

    @ApiModelProperty(value = "同步状态")
    @TableField("sync_status")
    private SyncStatusEnum syncStatus;

    @ApiModelProperty("历史用户名")
    private String historicName;

    public String getHistoricName() {
        return historicName;
    }

    public void setHistoricName(String historicName) {
        this.historicName = historicName;
    }

    public SysUser() {

    }

    public SysUser(Long schoolId, String loginName, String realName, String mobileNumber, Boolean enable) {
        setId(SystemConstant.getDbUuid());
        this.schoolId = schoolId;
        this.loginName = loginName;
        this.realName = realName;
        this.mobileNumber = mobileNumber;
        this.pwdCount = 1;
        this.password = SystemConstant.DEFAULT_PASSWORD;
        this.enable = Objects.isNull(enable) ? true : enable;
    }

    @Override
    public void insertInfo(Long userId) {
        super.insertInfo(userId);
        SysConfigService sysConfigService = SpringContextHolder.getBean(SysConfigService.class);
        SysConfig sysConfig = sysConfigService.getByKey("sys.user.initPassword");
        setPassword(Base64Util.encode(StringUtils.isNoneBlank(sysConfig.getConfigValue()) ? sysConfig.getConfigValue().getBytes() : "123456".getBytes()));
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public SyncStatusEnum getSyncStatus() {
        return syncStatus;
    }

    public void setSyncStatus(SyncStatusEnum syncStatus) {
        this.syncStatus = syncStatus;
    }

    public Long getSchoolId() {
        return schoolId;
    }

    public void setSchoolId(Long schoolId) {
        this.schoolId = schoolId;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getMobileNumber() {
        return mobileNumber;
    }

    public void setMobileNumber(String mobileNumber) {
        this.mobileNumber = mobileNumber;
    }

    public Long getOrgId() {
        return orgId;
    }

    public void setOrgId(Long orgId) {
        this.orgId = orgId;
    }

    public Boolean getEnable() {
        return enable;
    }

    public void setEnable(Boolean enable) {
        this.enable = enable;
    }

    public Integer getPwdCount() {
        return pwdCount;
    }

    public void setPwdCount(Integer pwdCount) {
        this.pwdCount = pwdCount;
    }

    public Long getPwdUpdateTime() {
        return pwdUpdateTime;
    }

    public void setPwdUpdateTime(Long pwdUpdateTime) {
        this.pwdUpdateTime = pwdUpdateTime;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Long[] getRoleIds() {
        return roleIds;
    }

    public void setRoleIds(Long[] roleIds) {
        this.roleIds = roleIds;
    }

    public Long[] getCourseIds() {
        return courseIds;
    }

    public void setCourseIds(Long[] courseIds) {
        this.courseIds = courseIds;
    }

    public String getOldPassword() {
        return oldPassword;
    }

    public void setOldPassword(String oldPassword) {
        this.oldPassword = oldPassword;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getVerifyCode() {
        return verifyCode;
    }

    public void setVerifyCode(String verifyCode) {
        this.verifyCode = verifyCode;
    }
}
