package com.infoepoch.pms.agent.domain.baisc.user;

import com.infoepoch.pms.agent.common.utils.SnowFlake;
import com.infoepoch.pms.agent.domain.baisc.user.organizationRela.UserOrganizationRela;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ValidationException;
import java.io.Serializable;
import java.util.List;
import java.util.Objects;

/**
 * 用户
 */
public class User implements Serializable {
    private static final long serialVersionUID = 2105695673309726179L;

    // region

    public String getPositionText() {
        if (StringUtils.isBlank(position))
            return null;
        try {
            String[] arr = position.split("-");
            String positionStr = arr[1];
            switch (positionStr) {
                // 总经理
                case "JS0003":
                    return "总经理";
                // 副总经理
                case "JS0005":
                    return "副总经理";
                // 部门经理
                case "JS0053":
                    return "部门经理";
                // 部门副经理
                case "JS0052":
                    return "部门副经理";
                // 中心经理
                case "JS0055":
                    return "中心经理";
                // 中心副经理
                case "JS0056":
                    return "中心副经理";
                // 职员
                case "JS0054":
                    return "职员";
                default:
                    return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void setBranchCompanyId(String branchCompanyId) {
        this.branchCompanyId = branchCompanyId;
    }

    public void setBranchCompanyName(String branchCompanyName) {
        this.branchCompanyName = branchCompanyName;
    }

    public void setDepartmentName(String departmentName) {
        this.departmentName = departmentName;
    }

    public void setOrganizationId(String organizationId) {
        this.organizationId = organizationId;
    }

    public void setOrgConfirmStatus(Integer orgConfirmStatus) {
        this.orgConfirmStatus = orgConfirmStatus;
    }

    public void setOrganizationName(String organizationName) {
        this.organizationName = organizationName;
    }

    public void setBranchCompanyFlag(boolean branchCompanyFlag) {
        this.branchCompanyFlag = branchCompanyFlag;
    }

    /**
     * 获取
     *
     * @return
     */
    public String getUniqueKey() {
        return this.organizationId + ":" + this.id;
    }

    /**
     * 取排序值
     */
    public Integer getDisplayOrderValue() {
        if (StringUtils.isBlank(displayOrder))
            return Integer.MAX_VALUE;
        return Integer.parseInt(displayOrder);
    }

    /**
     * 取排序值
     */
    public Integer displayOrderValue() {
        if (StringUtils.isBlank(id))
            return Integer.MAX_VALUE;
        return Integer.parseInt(id);
    }

    /**
     * 取显示名，有全名返回全名，否则返回用户名
     *
     * @return
     */
    public String getDisplayName() {
        if (!StringUtils.isBlank(fullname))
            return this.fullname;
        return this.username;
    }

    // endregion

    // region 新增&修改

    /**
     * 修改
     */
    public void modify(String organizationId, UserType type, String position, String displayOrder, List<Integer> permissions
            , String username, String fullname, String sex, String mobile, String email, boolean disabled
            , String description, String jobNumber, Integer orgConfirmStatus) {
        username = StringUtils.trim(username);
        if (StringUtils.isEmpty(username))
            throw new ValidationException("用户名不能为空。");

        if (type == null)
            throw new ValidationException("用户类型不能为空。");

        this.organizationId = organizationId;
        this.type = type;
        this.position = position;
        this.displayOrder = displayOrder;
        this.permissions = permissions;
        this.username = username;
        this.fullname = fullname;
        this.sex = sex;
        this.mobile = mobile;
        this.email = email;
        this.disabled = disabled;
        this.description = description;
        this.jobNumber = jobNumber;
        this.orgConfirmStatus = orgConfirmStatus;
    }

    /**
     * OA同步修改
     */
    public void modify(String organizationId, UserType type, String position, String displayOrder, String username, String fullname, String sex,
                       String mobile, String email, boolean disabled, String jobNumber, Integer orgConfirmStatus) {
        this.organizationId = organizationId;
        this.type = type;
        this.position = position;
        this.displayOrder = displayOrder;
        this.username = username;
        this.fullname = fullname;
        this.sex = sex;
        this.mobile = mobile;
        this.email = email;
        this.disabled = disabled;
        this.jobNumber = jobNumber;
        this.orgConfirmStatus = orgConfirmStatus;
    }

    // endregion 新增&修改

    // region 构造方法

    public User() {
    }

    /**
     * 构造函数，新建
     */
    public User(String organizationId, UserType type, String position, String displayOrder, List<Integer> permissions
            , String username, String fullname, String sex, String mobile, String email, boolean disabled
            , String description, String jobNumber) {
        username = StringUtils.trim(username);
        if (StringUtils.isEmpty(username))
            throw new ValidationException("用户名不能为空。");

        if (type == null)
            throw new ValidationException("用户类型不能为空。");

        this.id = String.valueOf(SnowFlake.instant().nextId());
        this.organizationId = organizationId;
        this.type = type;
        this.position = position;
        this.displayOrder = displayOrder;
        this.permissions = permissions;
        this.username = username;
        this.fullname = fullname;
        this.sex = sex;
        this.mobile = mobile;
        this.email = email;
        this.disabled = disabled;
        this.description = description;
        this.jobNumber = jobNumber;
    }

    /**
     * 构造函数，还原
     */
    public User(String id, String organizationId, UserType type, String position, String displayOrder, List<Integer> permissions
            , String username, String fullname, String sex, String mobile, String email, boolean disabled
            , String description, String oaData, String hrData, String jobNumber, Integer orgConfirmStatus) {
        this.id = id;
        this.organizationId = organizationId;
        this.type = type;
        this.position = position;
        this.displayOrder = displayOrder;
        this.permissions = permissions;
        this.username = username;
        this.fullname = fullname;
        this.sex = sex;
        this.mobile = mobile;
        this.email = email;
        this.disabled = disabled;
        this.description = description;
        this.oaData = oaData;
        this.hrData = hrData;
        this.jobNumber = jobNumber;
        this.orgConfirmStatus = orgConfirmStatus;
    }

    /**
     * 仓储还原（关联组织关联表）
     *
     * @param id
     * @param organizationId
     * @param type
     * @param position
     * @param displayOrder
     * @param username
     * @param fullname
     * @param sex
     * @param mobile
     * @param email
     * @param disabled
     * @param description
     * @param oaData
     * @param hrData
     * @param jobNumber
     * @param orgConfirmStatus
     * @param organizationName
     */
    public User(String id, String organizationId, UserType type, String position, String displayOrder, String username, String fullname,
                String sex, String mobile, String email, boolean disabled, String description, String oaData, String hrData, String jobNumber,
                Integer orgConfirmStatus, String organizationName) {
        this.id = id;
        this.organizationId = organizationId;
        this.type = type;
        this.position = position;
        this.displayOrder = displayOrder;
        this.username = username;
        this.fullname = fullname;
        this.sex = sex;
        this.mobile = mobile;
        this.email = email;
        this.disabled = disabled;
        this.description = description;
        this.oaData = oaData;
        this.hrData = hrData;
        this.jobNumber = jobNumber;
        this.orgConfirmStatus = orgConfirmStatus;
        this.organizationName = organizationName;
    }

    //关联角色拉取用户列表专用
    public User(String id, String organizationId, String fullname) {
        this.id = id;
        this.organizationId = organizationId;
        this.fullname = fullname;
    }

    /**
     *
     */
    public User(String uorId, String majorPositionFlag, String id, String organizationId,String organizationName,String orgShortName,
                UserType type, String position, String displayOrder, String username, String fullname, String sex, String mobile,
                String email, boolean disabled, String description, String oaData, String hrData, String jobNumber, Integer orgConfirmStatus) {
        this.uorId = uorId;
        this.majorPositionFlag = majorPositionFlag;
        this.id = id;
        this.organizationId = organizationId;
        this.organizationName = organizationName;
        this.type = type;
        this.position = position;
        this.displayOrder = displayOrder;
        this.username = username;
        this.fullname = fullname;
        this.sex = sex;
        this.mobile = mobile;
        this.email = email;
        this.disabled = disabled;
        this.description = description;
        this.oaData = oaData;
        this.hrData = hrData;
        this.jobNumber = jobNumber;
        this.orgConfirmStatus = orgConfirmStatus;
    }

    // endregion 构造方法

    // region 额外属性

    /**
     * 用户组织关联列表
     */
    private List<UserOrganizationRela> userOrganizationRelaList;
    /**
     * 是否选中标识
     */
    private Boolean checked;
    /**
     * 用户组织关联ID
     */
    private String uorId;
    /**
     *
     */
    private String majorPositionFlag;
    /**
     * 组织简称
     */
    private String orgShortName;

    // region get&set

    public List<UserOrganizationRela> getUserOrganizationRelaList() {
        return userOrganizationRelaList;
    }

    public void setUserOrganizationRelaList(List<UserOrganizationRela> userOrganizationRelaList) {
        this.userOrganizationRelaList = userOrganizationRelaList;
    }

    public Boolean getChecked() {
        return checked != null && checked;
    }

    public void setChecked(Boolean checked) {
        this.checked = checked;
    }

    public String getUorId() {
        return uorId;
    }

    public void setUorId(String uorId) {
        this.uorId = uorId;
    }

    public String getMajorPositionFlag() {
        return majorPositionFlag;
    }

    public void setMajorPositionFlag(String majorPositionFlag) {
        this.majorPositionFlag = majorPositionFlag;
    }

    public String getOrgShortName() {
        return orgShortName;
    }

    public void setOrgShortName(String orgShortName) {
        this.orgShortName = orgShortName;
    }

    public void setUserType(UserLoginType userType) {
        this.userType = userType;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setType(UserType type) {
        this.type = type;
    }

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

    public void setDisplayOrder(String displayOrder) {
        this.displayOrder = displayOrder;
    }

    public void setPermissions(List<Integer> permissions) {
        this.permissions = permissions;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setFullname(String fullname) {
        this.fullname = fullname;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

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

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

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setOaData(String oaData) {
        this.oaData = oaData;
    }

    public void setHrData(String hrData) {
        this.hrData = hrData;
    }

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

    // endregion get&set

    // endregion 额外属性

    // region 基础属性

    private String id;                  //id
    private String organizationId;      //所属组织id
    private UserType type;              //类型
    private String position;            //职位
    private String displayOrder;        //显示顺序
    private List<Integer> permissions;  //权限
    private String username;            //用户名
    private String fullname;            //全名
    private String sex;                 //性别
    private String mobile;              //移动电话
    private String email;               //电子邮箱
    private boolean disabled;           //是否禁用
    private String description;         //描述
    private String oaData;              //OA数据
    private String hrData;              //HR数据
    private String jobNumber;           // 工号
    private Integer orgConfirmStatus;   // 组织确认状态

    private String organizationName; // 组织名称

    private boolean branchCompanyFlag; // 分公司标识

    private String departmentName;// 部门名称


    private String departmentId;// 部门ID
    /**
     * 分公司ID
     */
    private String branchCompanyId;
    /**
     * 分公司名称
     */
    private String branchCompanyName;

    private Integer organizationDisplayOrder; // 组织排序

    /**
     * 当前登录用户类型
     */
    private UserLoginType userType;


    public String getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(String departmentId) {
        this.departmentId = departmentId;
    }
    public String getId() {
        return id == null ? "" : id;
    }

    public String getOrganizationId() {
        return organizationId == null ? "" : organizationId;
    }

    public UserType getType() {
        return type;
    }

    public String getPosition() {
        return position == null ? "" : position;
    }

    public String getDisplayOrder() {
        return displayOrder == null ? "" : displayOrder;
    }

    public List<Integer> getPermissions() {
        return permissions;
    }

    public String getUsername() {
        return username == null ? "" : username;
    }

    public String getFullname() {
        return fullname == null ? "" : fullname;
    }

    public String getSex() {
        return sex == null ? "" : sex;
    }

    public String getMobile() {
        return mobile == null ? "" : mobile;
    }

    public String getEmail() {
        return email == null ? "" : email;
    }

    public boolean isDisabled() {
        return disabled;
    }

    public String getDescription() {
        return description == null ? "" : description;
    }

    public String getOaData() {
        return oaData == null ? "" : oaData;
    }

    public String getHrData() {
        return hrData == null ? "" : hrData;
    }

    public String getJobNumber() {
        return jobNumber;
    }

    public Integer getOrgConfirmStatus() {
        return orgConfirmStatus;
    }

    public String getOrganizationName() {
        return organizationName;
    }

    public boolean getBranchCompanyFlag() {
        return branchCompanyFlag;
    }

    public String getDepartmentName() {
        return departmentName;
    }

    public String getBranchCompanyId() {
        return branchCompanyId;
    }

    public String getBranchCompanyName() {
        return branchCompanyName;
    }

    public Integer getOrganizationDisplayOrder() {
        return organizationDisplayOrder;
    }

    public void setOrganizationDisplayOrder(Integer organizationDisplayOrder) {
        this.organizationDisplayOrder = organizationDisplayOrder;
    }

    public UserLoginType getUserType() {
        return userType;
    }

    // endregion 基础属性

    // region 重构方法

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return disabled == user.disabled &&
                Objects.equals(id, user.id) &&
                Objects.equals(organizationId, user.organizationId) &&
                type == user.type &&
                Objects.equals(position, user.position) &&
                Objects.equals(displayOrder, user.displayOrder) &&
                Objects.equals(permissions, user.permissions) &&
                Objects.equals(username, user.username) &&
                Objects.equals(fullname, user.fullname) &&
                Objects.equals(sex, user.sex) &&
                Objects.equals(mobile, user.mobile) &&
                Objects.equals(email, user.email) &&
                Objects.equals(description, user.description) &&
                Objects.equals(oaData, user.oaData) &&
                Objects.equals(hrData, user.hrData) &&
                userType == user.userType;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, organizationId, type, position, displayOrder, permissions, username, fullname, sex, mobile, email, disabled, description, oaData, hrData, userType);
    }

    // endregion 重构方法

}
