trigger CheckInspectionSubmitUser on Inspection_Report__c (after update) { 
 | 
    List<String> reporters = new List<String>(); 
 | 
    Map<Id, Id> irHpMap = new Map<Id, Id>(); 
 | 
    for (Inspection_Report__c ir : Trigger.new) { 
 | 
        if (Trigger.oldMap.get(ir.Id).get('Status__c') != ir.Status__c 
 | 
                && ir.Status__c == '填写完毕' 
 | 
                && ir.Reporter__c != null) { 
 | 
            reporters.add(ir.Reporter__c); 
 | 
        } 
 | 
        if (Trigger.oldMap.get(ir.Id).get('Hospital__c') != ir.Hospital__c) { 
 | 
            irHpMap.put(ir.Id, ir.Hospital__c); 
 | 
        } 
 | 
    } 
 | 
     
 | 
    // 提出権限のチェック 
 | 
    if (reporters.size() > 0) { 
 | 
        Map<Id, Id> reportManagerMap = new Map<Id, Id>(); 
 | 
        for (User u : [select Id, ManagerId from User where Id in :reporters]) { 
 | 
            reportManagerMap.put(u.Id, u.ManagerId); 
 | 
        } 
 | 
         
 | 
        for (Inspection_Report__c ir : Trigger.new) { 
 | 
            if (Trigger.oldMap.get(ir.Id).get('Status__c') != ir.Status__c 
 | 
                    && ir.Status__c == '填写完毕') { 
 | 
                System.debug('ir.Status__c=' + ir.Status__c); 
 | 
                System.debug('loginUserId=' + UserInfo.getUserId()); 
 | 
                System.debug('LastModifiedById=' + ir.LastModifiedById); 
 | 
                System.debug('ir.Reporter__c=' + ir.Reporter__c); 
 | 
                System.debug('ir.CreatedById=' + ir.CreatedById); 
 | 
                System.debug('reportManagerMap=' + reportManagerMap.get(ir.Reporter__c)); 
 | 
                // 更新者!=報告者、!=作成者、!=報告者のマネージャ 
 | 
                if (ir.LastModifiedById != ir.Reporter__c 
 | 
                        && ir.LastModifiedById != ir.CreatedById 
 | 
                        && ir.LastModifiedById != reportManagerMap.get(ir.Reporter__c)) { 
 | 
                    ir.addError('你没有提交点检报告书的权限'); 
 | 
                    continue; 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
     
 | 
    // 病院変更のチェック 
 | 
    if (irHpMap.values().size() > 0) { 
 | 
        Map<Id, Map<Id, Id>> iiHpMap = new Map<Id, Map<Id, Id>>(); 
 | 
        for (Inspection_Item__c ii : [select Id, Inspection_ReportId__c, AssetId__r.Hospital__c 
 | 
                                      from Inspection_Item__c where Inspection_ReportId__c in :irHpMap.keySet() and AssetId__c != null]) { 
 | 
            Map<Id, Id> tmpMap = new Map<Id, Id>(); 
 | 
            if (iiHpMap.containsKey(ii.Inspection_ReportId__c)) { 
 | 
                tmpMap = iiHpMap.get(ii.Inspection_ReportId__c); 
 | 
            } 
 | 
            tmpMap.put(ii.Id, ii.AssetId__r.Hospital__c); 
 | 
            iiHpMap.put(ii.Inspection_ReportId__c, tmpMap); 
 | 
        } 
 | 
         
 | 
        for (Inspection_Report__c ir : Trigger.new) { 
 | 
            if (Trigger.oldMap.get(ir.Id).get('Hospital__c') != ir.Hospital__c) { 
 | 
                if (ControllerUtil.checkInsHpChange((Id)ir.Hospital__c, iiHpMap.get(ir.Id))) { 
 | 
                    ir.Hospital__c.addError('点检报告书的医院与明细中保有设备的医院不符'); 
 | 
                    continue; 
 | 
                } 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
} 
 |