package com.sunxung.factoring.entity.sys;

import java.util.ArrayList;
import java.util.List;

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

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.BaseEntity;
import com.sunxung.factoring.service.sys.RoleService;

/**
 * @Description:系统角色实体类
 * @Date:2017年6月6日 下午3:16:28
 */
public class Role extends BaseEntity {

    private static final long serialVersionUID = 1L;
    @NotBlank(message = "角色名称不能为空", groups = {
            Add.class, Update.class
    })
    @Length(max = 30, message = "角色名称必须是30个字符以内", groups = {
            Add.class, Update.class
    })
    /** 角色名称 */
    private String name;
    /** 父角色 **/
    private Role parent;
    /** 岗位是否被选中 */
    private boolean check;
    /** 岗位管理员是否被选中 */
    private boolean admin;// 保理一期字段，二期已弃用
    /** 拥有的权限列表 */
    private List<Permission> permissions = new ArrayList<Permission>();
    /** 子角色集合 **/
    private List<Role> childList;

    public String getName() {
        return name;
    }

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

    public boolean isCheck() {
        return check;
    }

    public void setCheck(boolean check) {
        this.check = check;
    }

    public boolean isAdmin() {
        return admin;
    }

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

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

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

    public Role getParent() {
        return parent;
    }

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

    public List<Role> getChildList() {
        return childList;
    }

    public void setChildList(List<Role> childList) {
        this.childList = childList;
    }

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

    /** 新增数据验证 */
    private void addValidator() {
        Role role = getRoleService().getByRoleName(name);
        if (role != null) {
            throw new ValidatorException(CodeUtil.FAIL, "该角色名:" + this.name + "已存在");
        }
    }

    /** 修改数据验证 */
    private void updateValidator() {
        Role role = getRoleService().getByRoleName(name);
        if (role != null && !getName().equals(role.name)) {
            throw new ValidatorException(CodeUtil.FAIL, "该角色名:" + this.name + "已存在");
        }
    }

    private static RoleService roleService;

    private RoleService getRoleService() {
        if (roleService == null) {
            roleService = SpringUtil.getBeanByType(RoleService.class);
        }
        return roleService;
    }
}
