package com.sunxung.factoring.entity.sys;

import com.sunxung.factoring.component.exception.ValidatorException;
import com.sunxung.factoring.component.util.CodeUtil;
import com.sunxung.factoring.component.util.SpringUtil;
import com.sunxung.factoring.entity.PinYinEntity;
import com.sunxung.factoring.service.sys.UserService;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;

import javax.validation.constraints.Pattern;
import java.util.List;

/**
 * 用户实体
 */
public class User extends PinYinEntity {

    private static final long serialVersionUID = 1L;
    /**
     * 钉钉内员工唯一标识ID（不可修改）
     */
    private String userid;
    @Length(max = 30, message = "登录名称必须在30个字符以内", groups = {
            Add.class
    })
    /** 登录名 */
    private String loginName;
    @NotBlank(message = "请填写姓名", groups = {
            Add.class, Update.class
    })
    @Length(max = 8, message = "姓名必须在8个字以内")
    /** 用户名 */
    private String name;
    /**
     * 用户头像图片链接
     */
    private String headImgUrl;
    @NotBlank(message = "密码不能为空", groups = {
            Add.class
    })
//    @Pattern(regexp = "[0-9A-Za-z]{6,16}", message = "密码必须是6~16位数字和字母的组合", groups = {
//            Add.class
//    })
//    @JSONField(serialize = false)
    /** 密码 */
    private String password;
    @NotBlank(message = "手机号不能为空", groups = {
            Add.class, Update.class
    })
    @Pattern(regexp = "(^(1[2,3,4,5,6,7,8,9])\\d{9}$)|", message = "手机号格式不正确")
    /** 手机号 */
    private String mobile;
    /**
     * 工号
     **/
    @Length(max = 30, message = "工号必须在30个字以内")
    private String jobNumber;
    /**
     * 职位
     **/
    @Length(max = 30, message = "职位必须在30个字符以内")
    private String position;
    @Length(max = 10, message = "分机号必须在10个字符以内")
    private String tel;
    /**
     * 是否启用：0 禁用、1 启用
     **/
    private boolean enabled;
    /**
     * 显示顺序
     **/
    private int gradation;
    /**
     * 邮箱
     **/
    @NotBlank(message = "请输入邮箱", groups = {
            Add.class, Update.class
    })
    @Length(max = 50, message = "企业邮箱必须在50个字以内")
    @Pattern(regexp = "(^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$)", message = "邮箱格式不正确")
    private String email;
    /**
     * 是否管理员
     */
    private Boolean admin = false;
    /**
     * 角色
     */
    private List<Role> roles;
    /**
     * 部门
     **/
    private List<Long> orgIdList;
    /**
     * 相对名称（不保存数据库，仅列表展示，如浙江省杭州市西湖区）
     **/
    private String relativeName;
    /**
     * 用户身份 （不保存数据库，页面展示）
     **/
    private String userIdentityDisplayName;
    /**
     * 所在部门字符串，仅用于页面展示
     **/
    private String department;
    /**
     * 是否关联到钉钉:默认true关联 ，0 不关联，1 关联
     **/
    private boolean relationDingDing;
    // ------------
    /**
     * 从页面上，接受用户所在部门的身份
     **/
    private List<UserOrgRel> userOrgRelList;
    /**
     * 职级
     **/
    private String rank;

    /**
     * 跟投类别
     **/
    private Integer followType;

    /**
     * 企业客户信息表ID
     **/
    private Long enterpriseCustomerId;


    private String idCard;

    private String referenceNumber;

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
        this.idCard = idCard;
    }

    public String getReferenceNumber() {
        return referenceNumber;
    }

    public void setReferenceNumber(String referenceNumber) {
        this.referenceNumber = referenceNumber;
    }

    public Long getEnterpriseCustomerId() {
        return enterpriseCustomerId;
    }

    public void setEnterpriseCustomerId(Long enterpriseCustomerId) {
        this.enterpriseCustomerId = enterpriseCustomerId;
    }

    public String getUserid() {
        return userid;
    }

    public void setUserid(String userid) {
        this.userid = userid;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getJobNumber() {
        return jobNumber;
    }

    public void setJobNumber(String jobNumber) {
        this.jobNumber = jobNumber;
    }

    public String getPosition() {
        return position;
    }

    public void setPosition(String position) {
        this.position = position;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public int getGradation() {
        return gradation;
    }

    public void setGradation(int gradation) {
        this.gradation = gradation;
    }

    public String getLoginName() {
        return loginName;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHeadImgUrl() {
        return headImgUrl;
    }

    public void setHeadImgUrl(String headImgUrl) {
        this.headImgUrl = headImgUrl;
    }

    public String getPassword() {
        return password;
    }

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

    public Boolean getAdmin() {
        return admin;
    }

    public void setAdmin(Boolean admin) {
        this.admin = admin;
    }

    public List<Role> getRoles() {
        return roles;
    }

    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

    public List<Long> getOrgIdList() {
        return orgIdList;
    }

    public void setOrgIdList(List<Long> orgIdList) {
        this.orgIdList = orgIdList;
    }

    public String getRelativeName() {
        return relativeName;
    }

    public void setRelativeName(String relativeName) {
        this.relativeName = relativeName;
    }

    public static void setUserService(UserService userService) {
        User.userService = userService;
    }

    public String getUserIdentityDisplayName() {
        return userIdentityDisplayName;
    }

    public void setUserIdentityDisplayName(String userIdentityDisplayName) {
        this.userIdentityDisplayName = userIdentityDisplayName;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public boolean isRelationDingDing() {
        return relationDingDing;
    }

    public void setRelationDingDing(boolean relationDingDing) {
        this.relationDingDing = relationDingDing;
    }

    public List<UserOrgRel> getUserOrgRelList() {
        return userOrgRelList;
    }

    public void setUserOrgRelList(List<UserOrgRel> userOrgRelList) {
        this.userOrgRelList = userOrgRelList;
    }

    public String getRank() {
        return rank;
    }

    public void setRank(String rank) {
        this.rank = rank;
    }

    public Integer getFollowType() {
        return followType;
    }

    public void setFollowType(Integer followType) {
        this.followType = followType;
    }

    @Override
    public void validator(ValidatorType type) {
        super.validator(type);
        if (hasErrors()) {
            return;
        }
        if (ValidatorType.ADD.equals(type)) {
            addValidator();
        } else if (ValidatorType.UPDATE.equals(type)) {
            updateValidator();
        }
    }

    /**
     * 新增数据验证
     */
    private void addValidator() {
        User user = getUserService().getByLoginName(loginName);
        if (user != null) {
            throw new ValidatorException(CodeUtil.FAIL, "该登录名:" + this.loginName + "已存在");
        }
    }

    /**
     * 修改数据验证
     */
    private void updateValidator() {
        User user = getUserService().get(getId());
        if (user == null) {
            throw new ValidatorException(CodeUtil.FAIL, "该登录名:" + this.loginName + "不存在");
        }
    }

    private static UserService userService;

    private UserService getUserService() {
        if (userService == null) {
            userService = SpringUtil.getBeanByType(UserService.class);
        }
        return userService;
    }
}
