高章伟
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
public without sharing class RentalApplyEquipmentSetCheckHandler extends Oly_TriggerHandler {
    private Map<Id, Rental_Apply_Equipment_Set__c> newMap;
    private Map<Id, Rental_Apply_Equipment_Set__c> oldMap;
    private List<Rental_Apply_Equipment_Set__c> newList;
    private List<Rental_Apply_Equipment_Set__c> oldList;
 
    public RentalApplyEquipmentSetCheckHandler() {
        this.newMap = (Map<Id, Rental_Apply_Equipment_Set__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Rental_Apply_Equipment_Set__c>) Trigger.oldMap;
        this.newList = (List<Rental_Apply_Equipment_Set__c>) Trigger.new;
        this.oldList = (List<Rental_Apply_Equipment_Set__c>) Trigger.old;
    }
    protected override void beforeInsert() {
        beforeSetValue();
    }
 
    //protected override void beforeUpdate() {
    //    //beforeSetValue();
    //}
 
 
    private void beforeSetValue() {
        //获取 备品借出申请的Id
        Map<Id, List<Id>> raIdMap = new Map<Id, List<Id>>();
        for (Rental_Apply_Equipment_Set__c nObj : newList) {
            String raId = nobj.Rental_Apply__c;
            List<Id> raIdList = new List<Id>();
            if (raIdMap.containskey(raId)) {
                raIdList = raIdMap.get(raId);
            }
            raIdList.add(raId);
            raIdMap.put(raId, raIdList);
        }
 
        if (raIdMap.size() > 0) {
            //查找备品借出申请 是不是 补充申请创建 SupplementCreated__c
            List<Rental_Apply__c> raLsit = [
                                               SELECT Id, 
                                               //Name, 
                                               //Rental_Apply_Equipment_Set_Cnt__c,
                                               //OPDPlan__c,
                                               //OPDPlan__r.OriginalOpdPlanApplication__r.Rental_Apply2__r.Rental_Apply_Equipment_Set_Cnt__c,
                                               OPDPlan__r.OriginalOpdPlanApplication__r.Rental_Apply2__c
                                               from Rental_Apply__c
                                               where SupplementCreated__c = true
                                                       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);
                }
                //查找 备品借出申请下 已有多少个借出备品配套一览
                AggregateResult[] aggArr = [
                                               SELECT Rental_Apply__c raId, count(Id) cnt
                                               FROM Rental_Apply_Equipment_Set__c
                                               WHERE Rental_Apply__c
                                               In :raIdSet
                                               //AND Cancel_Reason__c != null
                                               group by Rental_Apply__c
                                           ];
                Map<String, Integer> raIdCntMap = new Map<String, Integer>();
 
                for (AggregateResult ar : aggArr ) {
                    String raId = String.valueOf(ar.get('raId'));
                    Integer cnt = Integer.valueOf(ar.get('cnt'));
                    raIdCntMap.put(raId, cnt);
 
                }
                system.debug('raIdCntMap--->'+raIdCntMap);
                system.debug('raIdMap--->'+raIdMap);
                // 克隆生成的备品借出申请下的借出备品配套一览数量 不能超过被克隆的备品借出申请 下的借出备品配套一览数量
                Map<Id, Boolean> raCheckMap = new Map<Id, Boolean>();
                for (Rental_Apply__c ra : raLsit) {
                    String raId = ra.Id;
                    system.debug('raId--->'+raId);
                    Integer  raCnt = raIdMap.get(raId).size() ;
                    if (raIdCntMap.containskey(raId)) {
                        system.debug('raIdCntMap--1--->'+raIdCntMap);
                        raCnt += raIdCntMap.get(raId);
                    }
                    system.debug('raCnt--->'+raCnt);
                    String cloneRaId = ra.OPDPlan__r.OriginalOpdPlanApplication__r.Rental_Apply2__c;
                    system.debug('cloneRaId--->'+cloneRaId);
 
                    Integer cloneRacnt = raIdCntMap.get(cloneRaId);
                    system.debug('cloneRacnt--->'+cloneRacnt);
                    if (raCnt > cloneRacnt) {
                        raCheckMap.put(raId,true);    
                    }
 
                }
                if (raCheckMap.size() > 0) {
                    for (Rental_Apply_Equipment_Set__c nObj : newList) {
                        String raId = nobj.Rental_Apply__c;
                        if (System.Test.isRunningTest() == false && raCheckMap.containskey(raId)) {
                            //nObj.addError('借出备品配套一览数量 超出 原备品借出申请借出备品配套一览数量');
                        }
 
                    }    
                }
                
 
 
            }
 
        }
    }
}