binxie
2024-01-18 0e0dd1e20e7211f3c3c11d77a41090d998dfd06c
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
trigger AssetHpDeptUpd on Asset (before update) {
 
    //deloitte-zhj 20231124 本地化导入 start
    if((!Test.isRunningTest())&&System.Label.ByPassTrigger.contains(UserInfo.getUserId())){
        return;
    }
    //deloitte-zhj 20231124 本地化导入 end
 
    
    // DB202306447415 gzw 接口执行速度优化 start
    if (StaticParameter.EscapeNFM110Trigger) {
        return ;
    }
    // DB202306447415 gzw 接口执行速度优化 end
    List<String> accIds = new List<String>();
 
    for(Asset a : Trigger.new) {
        Asset olda = Trigger.oldMap.get(a.Id);
        if (olda.AccountId != a.AccountId
                || a.AccountParentId__c != a.Department_Class__c
                || a.AccountParentParentId__c != a.Hospital__c) {
            if (!String.isBlank(a.AccountId)) {
                accIds.add(a.AccountId);
            }
        }
    }
 
    if (accIds.size() > 0) {
        // 診療科レコードタイプ
        String[] deptTypes = new String[] {'病院', '診療科 その他', '診療科 呼吸科', '診療科 婦人科', '診療科 普外科', '診療科 泌尿科', '診療科 消化科', '診療科 耳鼻喉科'};
        List<RecordType> deptRects = [select Id, Name from RecordType where IsActive = true and SobjectType = 'Account' and Name IN :deptTypes];
        Map<String, String> deptRectMap = new Map<String, String>();
        for (RecordType rect : deptRects) {
            deptRectMap.put(rect.Id, rect.Name);
        }
        // TODO 販売店のレコードタイプのMapを生成
 
        Map<Id, Account> accMap = new Map<Id, Account>();
        List<Account> accs = ControllerUtil.selectAccountForTrigger(accIds);
        for(Account acc : accs) {
            accMap.put(acc.Id, acc);
        }
        
        for(Asset a : Trigger.new) {
            // 「診療科」に診療科を選択する場合
            Account acc = accMap.get(a.AccountId);
            if (acc != null
                    && deptRectMap.get(acc.RecordTypeId) != null
                    && acc.Management_Code__c != '9999900'
            ) {
                if (deptRectMap.get(acc.RecordTypeId) == '病院') {
                    a.Department_Class__c = null;
                    a.Hospital__c = acc.Id;
                } else {
                    a.Department_Class__c = acc.ParentId;
                    a.Hospital__c = acc.Parent.ParentId;
                }
            }
            // TODO 「販売店」の更新
        }
    }
}