高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
public without sharing class RentalApplyEquipmentCheckHandler extends Oly_TriggerHandler {
    private Map<Id, Rental_Apply_Equipment_Set_Detail__c> newMap;
    private Map<Id, Rental_Apply_Equipment_Set_Detail__c> oldMap;
    private List<Rental_Apply_Equipment_Set_Detail__c> newList;
    private List<Rental_Apply_Equipment_Set_Detail__c> oldList;
 
    public RentalApplyEquipmentCheckHandler() {
        this.newMap = (Map<Id, Rental_Apply_Equipment_Set_Detail__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Rental_Apply_Equipment_Set_Detail__c>) Trigger.oldMap;
        this.newList = (List<Rental_Apply_Equipment_Set_Detail__c>) Trigger.new;
        this.oldList = (List<Rental_Apply_Equipment_Set_Detail__c>) Trigger.old;
    }
    protected override void beforeInsert() {
        beforeSetValue();
    }
 
    private void beforeSetValue() {
        //获取 key-备品借出申请的Id value-借出备品配套一览Id集合
        Map<Id, Map<Id, Id>> raIdMap = new Map<Id, Map<Id, Id>>();
        for (Rental_Apply_Equipment_Set_Detail__c nObj : newList) {
 
            if (String.isNotBlank(nObj.UniqueKey__c)) {
                String raId = nobj.Rental_Apply__c;
                String raesId = nobj.Rental_Apply_Equipment_Set__c;
                system.debug('raesId--->' + raesId);
                Map<Id, Id> raesIdMap = new Map<Id, Id>();
                if (raIdMap.containskey(raId)) {
                    raesIdMap = raIdMap.get(raId);
                }
                raesIdMap.put(raesId, raesId);
                raIdMap.put(raId, raesIdMap);
            }
        }
 
        if (raIdMap.size() > 0) {
            //查找备品借出申请 是不是 补充申请创建 SupplementCreated__c
            List<Rental_Apply__c> raLsit = [
                                               SELECT Id,
                                               OPDPlan__r.OriginalOpdPlanApplication__r.Rental_Apply2__c
                                               from Rental_Apply__c
                                               where SupplementCreated__c = true
                                                       AND OPDPlan__c != null
                                                       AND Id In :raIdMap.keySet()];
            if (raLsit.size() > 0 ) {
                // 获取 补充申请OPD计划创建的 备品借出申请Id 以及 被克隆的备品借出申请Id
                Set<Id> raIdSet = new Set<Id>();
                for (Rental_Apply__c ra : raLsit) {
                    Id raId = ra.Id;
                    Id cloneRaId = ra.OPDPlan__r.OriginalOpdPlanApplication__r.Rental_Apply2__c;
                    raIdSet.add(raId);
                    raIdSet.add(cloneRaId);
                }
 
                Map<String, Integer> raIdCntMap = new Map<String, Integer>();
                List<Rental_Apply_Equipment_Set__c> raesList = new List<Rental_Apply_Equipment_Set__c>();
                //查找备品借出申请下 借出备品配套一览
                raesList = [
                               SELECT Id, Rental_Apply__c, Skip__c
                               FROM Rental_Apply_Equipment_Set__c
                               WHERE Rental_Apply__c
                               In :raIdSet
                           ];
 
 
                Map<Id, Map<Id, Rental_Apply_Equipment_Set__c>> raIdMaps = new Map<Id, Map<Id, Rental_Apply_Equipment_Set__c>>();
                Set<Id> raesIdSet = new Set<Id>();
                for (Rental_Apply_Equipment_Set__c raes : raesList) {
                    String raId = raes.Rental_Apply__c;
                    String raesId = raes.Id;
                    Map<Id, Rental_Apply_Equipment_Set__c> raesIdMap = new Map<Id, Rental_Apply_Equipment_Set__c>();
                    if (raIdMaps.containskey(raId)) {
                        raesIdMap = raIdMaps.get(raId);
                    }
                    raesIdMap.put(raesId, raes);
                    raIdMaps.put(raId, raesIdMap);
                    raesIdSet.add(raesId);
                }
                //Map<String, Integer> raesIdCntMap = new Map<String, Integer>();
                //AggregateResult[] aggArr = [
                //                               SELECT Rental_Apply_Equipment_Set__c raesId, count(Id) cnt
                //                               FROM Rental_Apply_Equipment_Set_Detail__c
                //                               WHERE Rental_Apply_Equipment_Set__c
                //                               In :raesIdSet
                //                               group by Rental_Apply_Equipment_Set__c
                //                           ];
 
 
                //for (AggregateResult ar : aggArr ) {
                //    String raesId = String.valueOf(ar.get('raesId'));
                //    system.debug('raesId---1>' + raesId);
                //    Integer cnt = Integer.valueOf(ar.get('cnt'));
                //    raesIdCntMap.put(raesId, cnt);
 
                //}
                //system.debug('raesIdCntMap--->' + raesIdCntMap);
                Map<Id, Boolean> raCheckMap = new Map<Id, Boolean>();
                for (Rental_Apply__c ra : raLsit) {
                    String raId = ra.Id;
                    Map<Id, Id> raesIdMap = raIdMap.get(raId);
                    Integer  raCnt = 0;
                    Map<Id, Rental_Apply_Equipment_Set__c> raesMap = raIdMaps.get(raId);
 
                    for (Id raesId : raesIdMap.keySet()) {
                        //if (raIdMaps.containskey(raId)) {
                        //if (raIdMaps.get(raId).containskey(raesId)) {
                        //raesMap = raIdMaps.get(raId);
                        if (raesMap.containskey(raesId)) {
                            Rental_Apply_Equipment_Set__c rase = raesMap.get(raesId);
                            if (!rase.Skip__c) {
                                raCnt += 1;
                            }
                        }
                    }
                    for (Id raesId : raesMap.keySet()) {
                        Rental_Apply_Equipment_Set__c rase = raesMap.get(raesId);
                        if (rase.Skip__c) {
                            raCnt += 1;
                        }
                    }
                    system.debug('raCnt-1------>' + raCnt);
                    //if (raIdMaps.containskey(raId)) {
                    //    raCnt += raIdMaps.get(raId).size();
                    //}
                    //raCnt += raesIdCntMap.size();
                    //system.debug('raCnt-2------>' + raCnt);
                    String cloneRaId = ra.OPDPlan__r.OriginalOpdPlanApplication__r.Rental_Apply2__c;
 
                    Integer cloneRacnt = 0;
                    if (raIdMaps.containskey(cloneRaId)) {
                        cloneRacnt = raIdMaps.get(cloneRaId).size();
                    }
                    system.debug('cloneRacnt------>' + cloneRacnt);
                    if ( raCnt > cloneRacnt) {
                        raCheckMap.put(raId, true);
                    }
 
                }
                //List<Rental_Apply_Equipment_Set__c> updateRaseList = new Rental_Apply_Equipment_Set__c();
                Map<Id,Rental_Apply_Equipment_Set__c> updateRaseMap = new Map<Id,Rental_Apply_Equipment_Set__c>();
                for (Rental_Apply_Equipment_Set_Detail__c nObj : newList) {
                    String raId = nobj.Rental_Apply__c;
 
                    if (System.Test.isRunningTest() == false && raCheckMap.containskey(raId)) {
                        nObj.addError('借出备品配套一览数量 超出 原备品借出申请借出备品配套一览数量');
                    }
 
                    String raesId = nobj.Rental_Apply_Equipment_Set__c;
                    Rental_Apply_Equipment_Set__c rase = new Rental_Apply_Equipment_Set__c();
                    rase.Id = raesId;
                    rase.Skip__c = true;
                    updateRaseMap.put(raesId,rase);
                }
 
                if (updateRaseMap.size() > 0) {
                    update updateRaseMap.values();
                }
            }
        }
 
    }
}