package com.sunxung.factoring.entity.sys;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotEmpty;

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.OrgService;

/**
 * @Description:组织机构实体类
 * @Author:JingJiang
 * @E-mail:254702920@qq.com
 * @Date:2017年6月6日 下午12:01:11
 */
public class Org extends PinYinEntity {

    private static final long serialVersionUID = 1L;
    /** 对应钉钉的部门ID **/
    @NotNull(message = "departmentId不能为空！", groups = {
            Add.class
    })
    private Long departmentId;
    /** 父级组织机构 */
    private Org parent;
    /** 名称 */
    @NotEmpty(message = "请输入部门名称")
    @Length(max = 30, message = "名称必须是30个字符以内", groups = {
            Add.class, Update.class
    })
    private String name;
    /** 部门编码 */
    private String deptNo;
    /** 内置编码 */
    private String internalCode;
    /** 子组织机构最大数 */
    private int subCountMax;
    /** 子组织机构数 */
    private int subCount;
    /** 显示顺序 */
    private int gradation;
    /** 角色集合 **/
    private List<Role> roleList;
    private Integer seq;

    public Org getParent() {
        return parent;
    }

    public void setParent(Org parent) {
        this.parent = parent;
    }

    public String getName() {
        return name;
    }

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

    public String getDeptNo() {
        return deptNo;
    }

    public void setDeptNo(String deptNo) {
        this.deptNo = deptNo;
    }

    public String getInternalCode() {
        return internalCode;
    }

    public void setInternalCode(String internalCode) {
        this.internalCode = internalCode;
    }

    public int getSubCountMax() {
        return subCountMax;
    }

    public void setSubCountMax(int subCountMax) {
        this.subCountMax = subCountMax;
    }

    public int getSubCount() {
        return subCount;
    }

    public void setSubCount(int subCount) {
        this.subCount = subCount;
    }

    public int getGradation() {
        return gradation;
    }

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

    @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() {
        Org org = getOrgService().getByCode(deptNo);
        if (org != null) {
            throw new ValidatorException(CodeUtil.FAIL, "该编号已存在");
        }
        if (null != parent && null != parent.getId()) {
            Org org2 = getOrgService().getByParentIdAndName(parent.getId(), name);
            if (null != org2) {
                throw new ValidatorException(CodeUtil.FAIL, "该部门名称已存在");
            }
        }
    }

    /** 修改数据验证 */
    private void updateValidator() {
        Org org = getOrgService().get(getId());
        if (org == null) {
            throw new ValidatorException(CodeUtil.FAIL, "该组织机构不存在");
        }
        Org codeOrg = getOrgService().getByCode(deptNo);
        if (codeOrg != null && !getId().equals(codeOrg.getId())) {
            throw new ValidatorException(CodeUtil.FAIL, "该编号已存在");
        }
    }

    private static OrgService orgService;

    private OrgService getOrgService() {
        if (orgService == null) {
            orgService = SpringUtil.getBeanByType(OrgService.class);
        }
        return orgService;
    }

    public List<Role> getRoleList() {
        return roleList;
    }

    public void setRoleList(List<Role> roleList) {
        this.roleList = roleList;
    }

    public Long getDepartmentId() {
        return departmentId;
    }

    public void setDepartmentId(Long departmentId) {
        this.departmentId = departmentId;
    }

    public Integer getSeq() {
        return seq;
    }

    public void setSeq(Integer seq) {
        this.seq = seq;
    }
}
