D C
2023-05-26 9a0ef802a678ffc421fc1d416f7f867e89e5536a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
trigger CheckInspectionSubmitUser on Inspection_Report__c (after update) {
    if((!Test.isRunningTest())&&UserInfo.getUserId()==System.Label.ByPassTrigger){
        return;
    }
    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;
                }
            }
        }
    }
}