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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
public without sharing class DiscountProductApplicationApproveHandler extends Oly_TriggerHandler {
    
    //private Map<Id, DiscountProductApplication__c> newMap;
    private Map<Id, DiscountProductApplication__c> oldMap;
    private List<DiscountProductApplication__c> newList;
    //private List<DiscountProductApplication__c> oldList;
 
    public DiscountProductApplicationApproveHandler(){
        this.newList = (List<DiscountProductApplication__c>) Trigger.new;
        //this.oldList = (List<DiscountProductApplication__c>) Trigger.old;
        //this.newMap = (Map<Id, DiscountProductApplication__c>) Trigger.newMap;
        this.oldMap = (Map<Id, DiscountProductApplication__c>) Trigger.oldMap;
    }
 
    protected override void afterUpdate(){
        insertHospitalPrice();
        insertOrDelPrice();
    }
    public void insertHospitalPrice() {
        List<String> dpaIdList = new List<String>();
        //add by rentx 
        Id HosTypeId = [select Id from RecordType where IsActive = true and DeveloperName = 'HospitalSpecials'].Id;
 
 
        //比对是否为变更后为批准
        for(DiscountProductApplication__c dpa :newList){
            if(oldMap.get(dpa.Id).get('ApplicationStatus__c') != dpa.ApplicationStatus__c && dpa.ApplicationStatus__c == '批准'){
                //update by rentx 2020-12-23
                // dpaIdList.add(dpa.Id);
                if (dpa.RecordTypeId == HosTypeId) {
                    dpaIdList.add(dpa.Id);
                }
                //update by rentx 2020-12-23
            }
        }
 
        //特价产品明细
        List<DiscountProductApplicationDetail__c> dpadList = new List<DiscountProductApplicationDetail__c>();
           if(dpaIdList.size()>0){
               dpadList = [select id,name,Product2__c,ProductDiscount__c,DiscountProductApplication__c,DiscountProductApplication__r.RawAccount__c,DiscountProductApplication__r.AimsAccount__c from DiscountProductApplicationDetail__c where DiscountProductApplication__c in: dpaIdList];
           }
           //本次特价产品明细生成的经销商医院产品
        List<String> hpaphList = new List<String>();
        //待删除的经销商医院特价产品
           List<String> hpaphDelList = new List<String>();
           if(dpadList.size()>0){
               String hpaph;
               for(DiscountProductApplicationDetail__c dpad : dpadList){
                   String a = dpad.DiscountProductApplication__r.AimsAccount__c;
                   String p = dpad.DiscountProductApplication__r.RawAccount__c;
                   String h = dpad.Product2__c;
                   hpaph = a + p + h;
                //update by rentx 2021-22-23 start
                   // hpaphList.add(hpaph);
                if (dpad.ProductDiscount__c == null) {
                    hpaphDelList.add(hpaph);
                }else{
                    hpaphList.add(hpaph);
                }
                //update by rentx 2021-22-23 end
               }
           }
        //add by rentx  2021-2-23 start
        //待删除的医院特价产品 
        List<hospitalprice__c> hpDelList = [select id,aph__c from hospitalprice__c where aph__c in :hpaphDelList];
        //add by rentx  2021-2-23 end
 
           //已生成的医院特价关系表
           List<hospitalprice__c> hpOldList = [select id,name,hospital__c,product__c,account__c,mPrice__c,aph__c from hospitalprice__c where aph__c in: hpaphList];
 
        //医院特价关系表赋值
           List<hospitalprice__c> hpList= new List<hospitalprice__c>();
           if(dpadList.size()>0){
               for(DiscountProductApplicationDetail__c dpad : dpadList){
                if (dpad.ProductDiscount__c != null) {
                    hospitalprice__c hp = new hospitalprice__c();
                    String a = dpad.DiscountProductApplication__r.AimsAccount__c;
                    String p = dpad.DiscountProductApplication__r.RawAccount__c;
                    String h = dpad.Product2__c;
                    //hp.aph__c = dpad.DiscountProductApplication__r.AimsAccount__c + dpad.DiscountProductApplication__r.RawAccount__c + dpad.Product2__c + '';
                    hp.aph__c = a + p + h;
                    for(hospitalprice__c hpold:hpOldList){
                            if(hp.aph__c == hpold.aph__c){
                                hp.aph__c = hpold.aph__c;
                                hp.id = hpold.id;
                            }
                    }
                    hp.hospital__c = dpad.DiscountProductApplication__r.AimsAccount__c;
                    hp.mPrice__c = dpad.ProductDiscount__c;
                    hp.product__c = dpad.Product2__c;
                    hp.account__c = dpad.DiscountProductApplication__r.RawAccount__c;
                hpList.add(hp);
                }
                   
               }
           }
 
           if(hpList.size()>0){
               upsert hpList;
           }
 
        if (hpDelList.size() > 0) {
            delete hpDelList;
        }
 
    }
 
    //add by rentx 2020-12-23
    public void insertOrDelPrice(){
        Id ProductTypeId = [select Id from RecordType where IsActive = true and DeveloperName = 'ProductDiscount'].Id;
 
        List<Id> ids = new List<Id>();
        List<Dealer_Product__c> dealerPDel = new List<Dealer_Product__c>();
        List<Dealer_Product__c> dealerPIns = new List<Dealer_Product__c>();
        List<DiscountProductApplication__c> applist = new List<DiscountProductApplication__c>();
 
        for (DiscountProductApplication__c dpac : newList) {
            if (dpac.RecordTypeId == ProductTypeId) {
                if (dpac.ApplicationStatus__c == '批准' && oldMap.get(dpac.Id).ApplicationStatus__c != '批准') {
                    ids.add(dpac.Id);
                    applist.add(dpac);
                }
            }
        }
        //获取对应的明细
        if (ids.size() > 0) {
            Map<Id,List<DiscountProductApplicationDetail__c>> dismap = new Map<Id,List<DiscountProductApplicationDetail__c>>();
            List<DiscountProductApplicationDetail__c> dpalist = 
            [select id,Product2__c,ProductDiscount__c,DiscountProductApplication__c,Special_Discount__c,DealerProductId__c from DiscountProductApplicationDetail__c where DiscountProductApplication__c in: ids];
            for (DiscountProductApplicationDetail__c dis : dpalist) {
                if (!dismap.containsKey(dis.DiscountProductApplication__c)) {
                    dismap.put(dis.DiscountProductApplication__c, new List<DiscountProductApplicationDetail__c>());
                }
                dismap.get(dis.DiscountProductApplication__c).add(dis);
            }
 
 
            for (DiscountProductApplication__c dpac : applist) {
                //获取需要新建的经销商产品
                List<DiscountProductApplicationDetail__c> disList = new List<DiscountProductApplicationDetail__c>();
                disList = dismap.get(dpac.Id);
                if (disList!= null && disList.size() > 0) {
                    for (DiscountProductApplicationDetail__c dis : disList) {
                        if ((dis.Special_Discount__c == null) && (dis.ProductDiscount__c == null)) {
 
                            Dealer_Product__c dpc = new Dealer_Product__c();
                            dpc.Id = dis.DealerProductId__c;
                            dealerPDel.add(dpc);
                        }else{
                            Dealer_Product__c dpc = new Dealer_Product__c();
                            dpc.Id = dis.DealerProductId__c;
                            dpc.Dealer_Product2__c = dis.Product2__c;
                            dpc.Dealer_Contact__c = dpac.Dealer_Contact__c;
                            dpc.Special_Discount__c = dis.Special_Discount__c;
                            dpc.Special_Campaign_Price__c = dis.ProductDiscount__c;
                            dealerPIns.add(dpc);
                        }
                    }
                }
 
            }
        }
        
 
        //1.删除经销商产品
        if (dealerPDel.size() > 0 ) {
            delete dealerPDel;
        }
        //2.添加/修改经销商产品
        if (dealerPIns.size() > 0) {
            upsert dealerPIns;
        }
    }
    //add by rentx 2020-12-23
 
}