liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
public without sharing class RentalApplySequenceTriggerHandler extends Oly_TriggerHandler {
 
    private Map<Id, Rental_Apply_Sequence__c> newMap;
    private Map<Id, Rental_Apply_Sequence__c> oldMap;
    private List<Rental_Apply_Sequence__c> newList;
    private List<Rental_Apply_Sequence__c> oldList;
 
    public RentalApplySequenceTriggerHandler() {
        this.newMap = (Map<Id, Rental_Apply_Sequence__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Rental_Apply_Sequence__c>) Trigger.oldMap;
        this.newList = (List<Rental_Apply_Sequence__c>) Trigger.new;
        this.oldList = (List<Rental_Apply_Sequence__c>) Trigger.old;
    }
 
    protected override void afterUpdate() {
        List<Rental_Apply_Sequence__c> rasList = new List<Rental_Apply_Sequence__c>();
        for (Rental_Apply_Sequence__c ras : newList) {
            Rental_Apply_Sequence__c old = oldMap.get(ras.Id);
            // 无效Flag变为True
            if (old.Invalid_Flag__c != ras.Invalid_Flag__c && ras.Invalid_Flag__c) {
                rasList.add(ras);
            }
        }
 
        updateSingleSequence(rasList);
    }
 
    protected override void afterDelete() {
        updateSingleSequence(oldList);
    }
 
    // 混合序列变为单一序列时,更新序列号
    private void updateSingleSequence(List<Rental_Apply_Sequence__c> rasList) {
        // 先确认是否还原为单一序列
        List<String> externalKeyList = new List<String>();
        for (Rental_Apply_Sequence__c ras : rasList) {
            externalKeyList.add(ras.ExternalKey__c);
        }
 
        if (externalKeyList != null && externalKeyList.size() > 0) {
            // 先检索出混合序列,从更新序列中排除
            List<AggregateResult> arList = [
                    SELECT ExternalKey__c, COUNT(Apply_Set_Detail_ExternalKey__c) num
                    FROM Rental_Apply_Sequence__c 
                    WHERE Invalid_Flag__c = false
                      AND ExternalKey__c IN: externalKeyList
                    GROUP BY ExternalKey__c];
 
            // 更新externalKey List
            List<String> updateKey = new List<String>();
            List<String> mixKey = new List<String>(); 
            for (AggregateResult ar : arList) {
                // 单一序列判断
                if ((Integer)ar.get('num') == 1) {
                    updateKey.add((String)ar.get('ExternalKey__c'));
                }
                // 混合序列判断
                if ((Integer)ar.get('num') > 1) {
                    mixKey.add((String)ar.get('ExternalKey__c'));
                }
            }
 
            // 检索出单一序列,更新单一序列
            List<Rental_Apply_Sequence__c> updList = [
                    SELECT Id, Series_No__c, Apply_Set_Detail__r.Queue_Number__c
                    FROM Rental_Apply_Sequence__c 
                    WHERE Invalid_Flag__c = false
                      AND ExternalKey__c IN: updateKey
                      AND Series_Unequal_Queue_Flag__c = true
                    ORDER BY ExternalKey__c];
 
            for (Rental_Apply_Sequence__c ras : updList) {
                ras.Series_No__c = ras.Apply_Set_Detail__r.Queue_Number__c;
            }
            // 更新单一序列信息
            update updList;
 
            // 检索出混合序列,更新混合序列
            List<Rental_Apply_Sequence__c> updMixList = [
                    SELECT Id, Series_No__c, ExternalKey__c
                    FROM Rental_Apply_Sequence__c 
                    WHERE Invalid_Flag__c = false
                      AND ExternalKey__c IN: mixKey
                    ORDER BY ExternalKey__c, Series_No__c];
 
            String compareKey = null;
            Integer defaultNum = 1;
            for (Rental_Apply_Sequence__c ras : updMixList) {
                if (compareKey != ras.ExternalKey__c) {
                    compareKey = ras.ExternalKey__c;
                    defaultNum = 1;
                    ras.Series_No__c = 1;
                } else {
                    ras.Series_No__c = defaultNum;
                }
                defaultNum++;
            }
            // 更新混合序列信息
            update updMixList;
        }
    }
}