public without sharing class SubAuthorizedHandler extends Oly_TriggerHandler {
    private Map<Id, SubAuthorized__c> newMap;
    private Map<Id, SubAuthorized__c> oldMap;
    private List<SubAuthorized__c> newList;
    private List<SubAuthorized__c> oldList;
    public SubAuthorizedHandler() {
        this.newMap = (Map<Id, SubAuthorized__c>) Trigger.newMap;
        this.oldMap = (Map<Id, SubAuthorized__c>) Trigger.oldMap;
        this.newList = (List<SubAuthorized__c>) Trigger.new;
        this.oldList = (List<SubAuthorized__c>) Trigger.old;
    }
    protected override void beforeInsert() {
        CheckValue();
    }

    protected override void beforeUpdate() {
        CheckValue();
    }
    // 检查被授权人是否是角色层级的上司
    private void CheckValue() {
        for (SubAuthorized__c tempSub : newList) {
            //Set<ID> parentIDset = getRoleSuperiorUsers(tempSub.ownerid);
            if (!tempSub.IsAuthorize__c && getsubUsers(tempSub.Authorizer__c).size() > 0) {
                tempSub.addError('您不能再次转授权');
            }

            //boolean isError = true;
            //for (ID tempID : parentIDset) {
            //    if (tempSub.Authorizee__c == null || tempID == tempSub.Authorizee__c) {
            //        isError = false;
            //        break;
            //    }
            //}
            //if (isError) {
            //    tempSub.Authorizee__c.addError('只能选择您的领导！');
            //}
        }
    }
    public static Set<ID> getsubUsers(Id userId) {
        Map<id, user>  allSubUserMap = new Map<id, user> (
            [select id
             from user
             where
             ZongjianApprovalManager_copy__c = : userId
               or TongkuoZongjian_copy__c = : userId
                   or BuchangApprovalManager_copy__c = : userId
                           or BuchangApprovalManagerSales_copy__c = : userId
                                   or JingliApprovalManager_copy__c = : userId
                                           or SalesManager_copy__c = : userId
                //SWAG-BEM9EP start
                or ManagerId_copy__c = : userId
                or JingliEquipmentManager_copy__c = : userId
                or Buzhang_Equipment_Manager_copy__c = : userId
                //SWAG-BEM9EP end
            ]);

        return allSubUserMap.keySet();
    }

    //public static Set<ID> getRoleSuperiorUsers(Id userId) {
    //    // get requested user's role
    //    Id roleId = [select UserRoleId from User where Id = :userId].UserRoleId;
    //    // get all of the roles above the user
    //    Set<Id> allSubRoleIds = getAllUpRoleIds(new Set<ID> {roleId});
    //    // get all of the ids for the users in those roles
    //    Map<Id, User> users = new Map<Id, User>([Select Id, Name From User
    //                                            where UserRoleId IN :allSubRoleIds]);
    //    // return the ids as a set so you can do what you want with them
    //    return users.keySet();

    //}

    //private static Set<ID> getAllUpRoleIds(Set<ID> roleIds) {

    //    Set<ID> currentRoleIds = new Set<ID>();
    //    // get all of the roles above the passed roles
    //    for (UserRole userRole : [select Id, ParentRoleId from UserRole where id
    //                              IN :roleIds AND ParentRoleID != null])
    //        currentRoleIds.add(userRole.ParentRoleId);
    //    // go fetch some more rolls!
    //    if (currentRoleIds.size() > 0) {
    //        currentRoleIds.addAll(getAllUpRoleIds(currentRoleIds));
    //    }

    //    return currentRoleIds;
    //}

}