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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
global class DealerConsumableManageBatch4 implements Database.Batchable<sObject> {
    public Date currentMonth;
    public Date endDate;
    public Date startDate;
    public String queryStr;
    public String year;
    public Boolean isIncrementOnly; //是否只跑增量
    global DealerConsumableManageBatch4(Date startDate,Date endDate,Boolean isIncrementOnly) {
        //当年
        this.startDate=startDate;
        this.endDate=endDate;
        this.isIncrementOnly=isIncrementOnly;
        queryStr = 'SELECT DeliveryDate__c,ContractAmount_F__c,Paid_Amount_Rate_F__c,Status_1__c,Opportunity__r.Agency1__c,Opportunity_Category__c,Opportunity__r.OCM_man_province_no_dealer__c,Opportunity__r.Opp_order_Type__c  '+
        ' FROM Statu_Achievements__c '+
        ' WHERE Status_1__c not in (\'无效\',\'冻结\') and Paid_Amount_Rate_F__c!=100 AND Status_2_Formula__c not in (\'退货\',\'14 已发货\') and ((NOT ContractNO__c like \'%-A\') OR  (NOT ContractNO__c like\'%-B\'))  AND PaymentStatus__c!=\'不需付款\' and Opportunity__r.Opp_order_Type__c in (\'ET\',\'ENG\') ' +
        ' AND DeliveryDate__c>=:startDate and DeliveryDate__c <= :endDate ';
    }
 
    global Database.QueryLocator start(Database.BatchableContext bc) {
        //数据超过一万条会报错
        if(!isIncrementOnly){
            Integer year = startDate.year();
            Integer FYear = year;
            if(startDate>=Date.newInstance(year, 4, 1)){
                FYear = FYear+1;
            }
            String  OCSMYear= 'FY'+FYear;
            List<Dealer_ConsumableManage__c> dcmList= [SELECT  id FROM Dealer_ConsumableManage__c WHERE FY__c = :OCSMYear ];
            if(dcmList.size()>0){
                for(Dealer_ConsumableManage__c dcm:dcmList){
                    dcm.TMS_Unpaid_amount_1__c=0;
                    dcm.TMS_Unpaid_amount_2__c=0;
                    dcm.TMS_Unpaid_amount_3__c=0;
                    dcm.TMS_Unpaid_amount_4__c=0;
                    dcm.TMS_Unpaid_amount_5__c=0;
                    dcm.TMS_Unpaid_amount_6__c=0;
                    dcm.TMS_Unpaid_amount_7__c=0;
                    dcm.TMS_Unpaid_amount_8__c=0;
                    dcm.TMS_Unpaid_amount_9__c=0;
                    dcm.TMS_Unpaid_amount_10__c=0;
                    dcm.TMS_Unpaid_amount_11__c=0;
                    dcm.TMS_Unpaid_amount_12__c=0;
                }
                update dcmList;
            }
            
        }
 
        return Database.getQueryLocator(queryStr);
    }
 
    global void execute(Database.BatchableContext BC,  list<Statu_Achievements__c> scope) {
        Integer year = startDate.year();
        Integer FYear = year;
        if(startDate>=Date.newInstance(year, 4, 1)){
            FYear = FYear+1;
        }
        String  OCSMYear= 'FY'+FYear;
 
 
        Set<String> dcmKeySet=new Set<String>();
        Map<String,List<Statu_Achievements__c>> codMap = new Map<String,List<Statu_Achievements__c>>();
        for(Statu_Achievements__c sa:scope){
            String key;
            if(sa.Opportunity__r.Opp_order_Type__c == 'ET'){
                key=sa.Opportunity__r.Agency1__c+'-ET-'+OCSMYear+'-'+sa.Opportunity__r.OCM_man_province_no_dealer__c;
                dcmKeySet.add(sa.Opportunity__r.Agency1__c+'-ET-'+OCSMYear+'-'+sa.Opportunity__r.OCM_man_province_no_dealer__c);
            }else{
                key=sa.Opportunity__r.Agency1__c+'-ENG-'+OCSMYear+'-'+sa.Opportunity__r.OCM_man_province_no_dealer__c;
                dcmKeySet.add(sa.Opportunity__r.Agency1__c+'-ENG-'+OCSMYear+'-'+sa.Opportunity__r.OCM_man_province_no_dealer__c);
            }
            
            if(codMap.containsKey(key)){
                codMap.get(key).add(sa);
            }else{
                List<Statu_Achievements__c> codList = new List<Statu_Achievements__c>();
                codList.add(sa);
                codMap.put(key,codList);
            }
        }
 
 
        List<Dealer_ConsumableManage__c> ctmList= [SELECT  TMS_Province__c,TMS_Unpaid_amount_1__c,TMS_Unpaid_amount_2__c,TMS_Unpaid_amount_3__c,TMS_Unpaid_amount_4__c
                                                    ,TMS_Unpaid_amount_5__c,TMS_Unpaid_amount_6__c,TMS_Unpaid_amount_7__c,TMS_Unpaid_amount_8__c
                                                    ,TMS_Unpaid_amount_9__c,TMS_Unpaid_amount_10__c,TMS_Unpaid_amount_11__c,TMS_Unpaid_amount_12__c
                                                    ,TMS_Key__c
                                                     FROM Dealer_ConsumableManage__c WHERE  TMS_Key__c in :dcmKeySet];
        System.debug('获取到现存IMS:');
        System.debug(ctmList);
 
        for(Dealer_ConsumableManage__c ctm:ctmList){
            String key = ctm.TMS_Key__c;
            if(ctm.TMS_Unpaid_amount_1__c==null){
                ctm.TMS_Unpaid_amount_1__c=0;
            }
            if(ctm.TMS_Unpaid_amount_2__c==null){
                ctm.TMS_Unpaid_amount_2__c=0;
            }
            if(ctm.TMS_Unpaid_amount_3__c==null){
                ctm.TMS_Unpaid_amount_3__c=0;
            }
            if(ctm.TMS_Unpaid_amount_4__c==null){
                ctm.TMS_Unpaid_amount_4__c=0;
            }
            if(ctm.TMS_Unpaid_amount_5__c==null){
                ctm.TMS_Unpaid_amount_5__c=0;
            }
            if(ctm.TMS_Unpaid_amount_6__c==null){
                ctm.TMS_Unpaid_amount_6__c=0;
            }
            if(ctm.TMS_Unpaid_amount_7__c==null){
                ctm.TMS_Unpaid_amount_7__c=0;
            }
            if(ctm.TMS_Unpaid_amount_8__c==null){
                ctm.TMS_Unpaid_amount_8__c=0;
            }
            if(ctm.TMS_Unpaid_amount_9__c==null){
                ctm.TMS_Unpaid_amount_9__c=0;
            }
            if(ctm.TMS_Unpaid_amount_10__c==null){
                ctm.TMS_Unpaid_amount_10__c=0;
            }
            if(ctm.TMS_Unpaid_amount_11__c==null){
                ctm.TMS_Unpaid_amount_11__c=0;
            }
            if(ctm.TMS_Unpaid_amount_12__c==null){
                ctm.TMS_Unpaid_amount_12__c=0;
            }
            if(codMap.containsKey(key)){
                List<Statu_Achievements__c> saList=codMap.get(key);
                for(Statu_Achievements__c cod:saList){
                       Decimal PARF=cod.Paid_Amount_Rate_F__c;
                    if(PARF==null){
                        PARF=0;
                    }
                    if(cod.DeliveryDate__c >= Date.newInstance(FYear, 1, 1) && cod.DeliveryDate__c < Date.newInstance(FYear, 2, 1)){
                        ctm.TMS_Unpaid_amount_1__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear, 2, 1) && cod.DeliveryDate__c < Date.newInstance(FYear, 3, 1)){
                        ctm.TMS_Unpaid_amount_2__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear, 3, 1) && cod.DeliveryDate__c < Date.newInstance(FYear, 4, 1)){
                        ctm.TMS_Unpaid_amount_3__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 4, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 5, 1)){
                        ctm.TMS_Unpaid_amount_4__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 5, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 6, 1)){
                        ctm.TMS_Unpaid_amount_5__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 6, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 7, 1)){
                        ctm.TMS_Unpaid_amount_6__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 7, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 8, 1)){
                        ctm.TMS_Unpaid_amount_7__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 8, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 9, 1)){
                        ctm.TMS_Unpaid_amount_8__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 9, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 10, 1)){
                        ctm.TMS_Unpaid_amount_9__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 10, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 11, 1)){
                        ctm.TMS_Unpaid_amount_10__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 11, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 12, 1)){
                        ctm.TMS_Unpaid_amount_11__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 12, 1) && cod.DeliveryDate__c < Date.newInstance(FYear, 1, 1)){
                        ctm.TMS_Unpaid_amount_12__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }
                }
                codMap.remove(key);
            }
        }
 
        List<Dealer_ConsumableManage__c> newCtm=new List<Dealer_ConsumableManage__c>();
        for(String key:codMap.keySet()){
            List<Statu_Achievements__c> codList=codMap.get(key);
            Dealer_ConsumableManage__c ctm=new Dealer_ConsumableManage__c();
            ctm.Dealer_Name__c = codList[0].Opportunity__r.Agency1__c;
            if(codList[0].Opportunity__r.Opp_order_Type__c == 'ET'){
                ctm.ET_ENG__c='ET';
            }else{
                ctm.ET_ENG__c='ENG';
            }
            ctm.FY__c = OCSMYear;
            ctm.TMS_Key__c = ctm.Dealer_Name__c+'-'+ctm.ET_ENG__c+'-'+ctm.FY__c+'-'+codList[0].Opportunity__r.OCM_man_province_no_dealer__c;
                    ctm.TMS_Unpaid_amount_1__c=0;
                    ctm.TMS_Unpaid_amount_2__c=0;
                    ctm.TMS_Unpaid_amount_3__c=0;
                    ctm.TMS_Unpaid_amount_4__c=0;
                    ctm.TMS_Unpaid_amount_5__c=0;
                    ctm.TMS_Unpaid_amount_6__c=0;
                    ctm.TMS_Unpaid_amount_7__c=0;
                    ctm.TMS_Unpaid_amount_8__c=0;
                    ctm.TMS_Unpaid_amount_9__c=0;
                    ctm.TMS_Unpaid_amount_10__c=0;
                    ctm.TMS_Unpaid_amount_11__c=0;
                    ctm.TMS_Unpaid_amount_12__c=0;
 
                for(Statu_Achievements__c cod:codList){
                       Decimal PARF=cod.Paid_Amount_Rate_F__c;
                    if(PARF==null){
                        PARF=0;
                    }
                    if(cod.DeliveryDate__c >= Date.newInstance(FYear, 1, 1) && cod.DeliveryDate__c < Date.newInstance(FYear, 2, 1)){
                        ctm.TMS_Unpaid_amount_1__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear, 2, 1) && cod.DeliveryDate__c < Date.newInstance(FYear, 3, 1)){
                        ctm.TMS_Unpaid_amount_2__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear, 3, 1) && cod.DeliveryDate__c < Date.newInstance(FYear, 4, 1)){
                        ctm.TMS_Unpaid_amount_3__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 4, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 5, 1)){
                        ctm.TMS_Unpaid_amount_4__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 5, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 6, 1)){
                        ctm.TMS_Unpaid_amount_5__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 6, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 7, 1)){
                        ctm.TMS_Unpaid_amount_6__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 7, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 8, 1)){
                        ctm.TMS_Unpaid_amount_7__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 8, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 9, 1)){
                        ctm.TMS_Unpaid_amount_8__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 9, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 10, 1)){
                        ctm.TMS_Unpaid_amount_9__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 10, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 11, 1)){
                        ctm.TMS_Unpaid_amount_10__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 11, 1) && cod.DeliveryDate__c < Date.newInstance(FYear-1, 12, 1)){
                        ctm.TMS_Unpaid_amount_11__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }else if(cod.DeliveryDate__c >= Date.newInstance(FYear-1, 12, 1) && cod.DeliveryDate__c < Date.newInstance(FYear, 1, 1)){
                        ctm.TMS_Unpaid_amount_12__c+=cod.ContractAmount_F__c*(1-PARF/100);
                    }
                }
            newCtm.add(ctm);
        }
        if(ctmList.size()>0){
            update ctmList;
        }
        if(newCtm.size()>0){
            insert newCtm;
        }
 
    }
 
    global void finish(Database.BatchableContext BC) {
        Database.executeBatch(new DealerConsumableManageBatch5(startDate,endDate,this.isIncrementOnly),200);
    }
}