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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
//OLY发货
 
global class DealerConsumableManageBatch2 implements Database.Batchable<sObject> {
    public Date currentMonth;
    public Date endDate;
    public Date startDate;
    public String queryStr;
    public String year;
    public Boolean isIncrementOnly;
    global DealerConsumableManageBatch2(Date startDate,Date endDate,Boolean isIncrementOnly) {
        //当年
        this.startDate=startDate;
        this.endDate=endDate;
        this.isIncrementOnly=isIncrementOnly;
        queryStr = 'SELECT id,Statu_Achievements__r.Opportunity__r.OCM_man_province_no_dealer__c,asset__r.Product2.ConsumCategory2__c,NetWorthNoProviston__c,DeliveryDate__c,Statu_Achievements__c,Statu_Achievements__r.Opportunity__c,Statu_Achievements__r.Opportunity__r.Agency1__c,Statu_Achievements__r.Opportunity_Category__c'
                  +' FROM Statu_Achievements_DN_details__c ' 
                  +' WHERE  (Statu_Achievements__r.Opportunity_Category__c = \'ET\' OR asset__r.Product2.ConsumCategory2__c=\'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 ET_ENG__c,FY__c,Dealer_Name__c
                                                 ,FY_O_1__c,FY_O_2__c,FY_O_3__c,FY_O_4__c,FY_O_5__c,FY_O_6__c,FY_O_7__c,FY_O_8__c,FY_O_9__c,FY_O_10__c,FY_O_11__c,FY_O_12__c  
                                                 FROM Dealer_ConsumableManage__c WHERE FY__c = :OCSMYear];
            for(Dealer_ConsumableManage__c dcm:dcmList){
                dcm.FY_S_1__c=0;
                dcm.FY_S_2__c=0;
                dcm.FY_S_3__c=0;
                dcm.FY_S_4__c=0;
                dcm.FY_S_5__c=0;
                dcm.FY_S_6__c=0;
                dcm.FY_S_7__c=0;
                dcm.FY_S_8__c=0;
                dcm.FY_S_9__c=0;
                dcm.FY_S_10__c=0;
                dcm.FY_S_11__c=0;
                dcm.FY_S_12__c=0;
            }
            update dcmList;
        }
        return Database.getQueryLocator(queryStr);
    }
 
    global void execute(Database.BatchableContext BC, list<Statu_Achievements_DN_details__c> scope) {
        Map<String,List<Statu_Achievements_DN_details__c>> saMap = new Map<String,List<Statu_Achievements_DN_details__c>>();
        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>();
        for(Statu_Achievements_DN_details__c sa:scope){
            String hpId='';
            if(sa.Statu_Achievements__c!=null&&sa.Statu_Achievements__r.Opportunity__c!=null&&sa.Statu_Achievements__r.Opportunity__r.Agency1__c!=null){
                hpId=sa.Statu_Achievements__r.Opportunity__r.Agency1__c;
            }
 
            String key;
            if(sa.Statu_Achievements__r.Opportunity_Category__c == 'ET' && sa.asset__r.Product2.ConsumCategory2__c!='ENG耗材'){
                key=hpId+'-ET'+'-'+OCSMYear+'-'+sa.Statu_Achievements__r.Opportunity__r.OCM_man_province_no_dealer__c;
                dcmKeySet.add(sa.Statu_Achievements__r.Opportunity__r.Agency1__c+'-ET-'+OCSMYear+'-'+sa.Statu_Achievements__r.Opportunity__r.OCM_man_province_no_dealer__c);
            }else{
                key=hpId+'-ENG'+'-'+OCSMYear+'-'+sa.Statu_Achievements__r.Opportunity__r.OCM_man_province_no_dealer__c;
                dcmKeySet.add(sa.Statu_Achievements__r.Opportunity__r.Agency1__c+'-ENG-'+OCSMYear+'-'+sa.Statu_Achievements__r.Opportunity__r.OCM_man_province_no_dealer__c);
            }
            if(saMap.containsKey(key)){
                saMap.get(key).add(sa);
            }else{
                List<Statu_Achievements_DN_details__c> arList = new List<Statu_Achievements_DN_details__c>();
                arList.add(sa);
                saMap.put(key,arList);
            }
        }
 
 
        List<Dealer_ConsumableManage__c> dcmList= [SELECT TMS_Key__c,TMS_Province__c,ET_ENG__c,FY__c,Dealer_Name__c
                                                 ,FY_S_1__c,FY_S_2__c,FY_S_3__c,FY_S_4__c,FY_S_5__c,FY_S_6__c,FY_S_7__c,FY_S_8__c,FY_S_9__c,FY_S_10__c,FY_S_11__c,FY_S_12__c  
                                                 FROM Dealer_ConsumableManage__c WHERE TMS_Key__c in :dcmKeySet ];
        for(Dealer_ConsumableManage__c dcm:dcmList){
            String key=dcm.TMS_Key__c;
            if(dcm.FY_S_1__c==null){
                dcm.FY_S_1__c=0;
            }
            if(dcm.FY_S_2__c==null){
                dcm.FY_S_2__c=0;
            }
            if(dcm.FY_S_3__c==null){
                dcm.FY_S_3__c=0;
            }
            if(dcm.FY_S_4__c==null){
                dcm.FY_S_4__c=0;
            }
            if(dcm.FY_S_5__c==null){
                dcm.FY_S_5__c=0;
            }
            if(dcm.FY_S_6__c==null){
                dcm.FY_S_6__c=0;
            }
            if(dcm.FY_S_7__c==null){
                dcm.FY_S_7__c=0;
            }
            if(dcm.FY_S_8__c==null){
                dcm.FY_S_8__c=0;
            }
            if(dcm.FY_S_9__c==null){
                dcm.FY_S_9__c=0;
            }
            if(dcm.FY_S_10__c==null){
                dcm.FY_S_10__c=0;
            }
            if(dcm.FY_S_11__c==null){
                dcm.FY_S_11__c=0;
            }
            if(dcm.FY_S_12__c==null){
                dcm.FY_S_12__c=0;
            }
            if(saMap.containsKey(key)){
                List<Statu_Achievements_DN_details__c> saList=saMap.get(key);
 
                for(Statu_Achievements_DN_details__c sa:saList){
 
                    if(sa.DeliveryDate__c >= Date.newInstance(FYear, 1, 1) && sa.DeliveryDate__c < Date.newInstance(FYear, 2, 1)){
                        dcm.FY_S_1__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear, 2, 1) && sa.DeliveryDate__c < Date.newInstance(FYear, 3, 1)){
                        dcm.FY_S_2__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear, 3, 1) && sa.DeliveryDate__c < Date.newInstance(FYear, 4, 1)){
                        dcm.FY_S_3__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 4, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 5, 1)){
                        dcm.FY_S_4__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 5, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 6, 1)){
                        dcm.FY_S_5__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 6, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 7, 1)){
                        dcm.FY_S_6__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 7, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 8, 1)){
                        dcm.FY_S_7__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 8, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 9, 1)){
                        dcm.FY_S_8__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 9, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 10, 1)){
                        dcm.FY_S_9__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 10, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 11, 1)){
                        dcm.FY_S_10__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 11, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 12, 1)){
                        dcm.FY_S_11__c+=sa.NetWorthNoProviston__c/1000;
                    }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 12, 1) && sa.DeliveryDate__c < Date.newInstance(FYear, 1, 1)){
                        dcm.FY_S_12__c+=sa.NetWorthNoProviston__c/1000;
                    }
                }
            }
            saMap.remove(key);
        }
        List<Dealer_ConsumableManage__c> newDCM=new List<Dealer_ConsumableManage__c>();
        for(String key:saMap.keySet()){
            List<Statu_Achievements_DN_details__c> saList=saMap.get(key);
 
            Dealer_ConsumableManage__c dcm=new Dealer_ConsumableManage__c();
            dcm.Dealer_Name__c = saList[0].Statu_Achievements__r.Opportunity__r.Agency1__c;
 
            if(saList[0].Statu_Achievements__r.Opportunity_Category__c == 'ET' && saList[0].asset__r.Product2.ConsumCategory2__c!='ENG耗材'){
                dcm.ET_ENG__c='ET';
            }else{
                dcm.ET_ENG__c='ENG';
            }
            dcm.FY__c = OCSMYear;
            dcm.TMS_Key__c = dcm.Dealer_Name__c+'-'+dcm.ET_ENG__c+'-'+dcm.FY__c+'-'+saList[0].Statu_Achievements__r.Opportunity__r.OCM_man_province_no_dealer__c;
 
 
            if(dcm.FY_S_1__c==null){
                dcm.FY_S_1__c=0;
            }
            if(dcm.FY_S_2__c==null){
                dcm.FY_S_2__c=0;
            }
            if(dcm.FY_S_3__c==null){
                dcm.FY_S_3__c=0;
            }
            if(dcm.FY_S_4__c==null){
                dcm.FY_S_4__c=0;
            }
            if(dcm.FY_S_5__c==null){
                dcm.FY_S_5__c=0;
            }
            if(dcm.FY_S_6__c==null){
                dcm.FY_S_6__c=0;
            }
            if(dcm.FY_S_7__c==null){
                dcm.FY_S_7__c=0;
            }
            if(dcm.FY_S_8__c==null){
                dcm.FY_S_8__c=0;
            }
            if(dcm.FY_S_9__c==null){
                dcm.FY_S_9__c=0;
            }
            if(dcm.FY_S_10__c==null){
                dcm.FY_S_10__c=0;
            }
            if(dcm.FY_S_11__c==null){
                dcm.FY_S_11__c=0;
            }
            if(dcm.FY_S_12__c==null){
                dcm.FY_S_12__c=0;
            }
            for(Statu_Achievements_DN_details__c sa:saList){
 
                if(sa.DeliveryDate__c >= Date.newInstance(FYear, 1, 1) && sa.DeliveryDate__c < Date.newInstance(FYear, 2, 1)){
                    dcm.FY_S_1__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear, 2, 1) && sa.DeliveryDate__c < Date.newInstance(FYear, 3, 1)){
                    dcm.FY_S_2__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear, 3, 1) && sa.DeliveryDate__c < Date.newInstance(FYear, 4, 1)){
                    dcm.FY_S_3__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 4, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 5, 1)){
                    dcm.FY_S_4__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 5, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 6, 1)){
                    dcm.FY_S_5__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 6, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 7, 1)){
                    dcm.FY_S_6__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 7, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 8, 1)){
                    dcm.FY_S_7__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 8, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 9, 1)){
                    dcm.FY_S_8__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 9, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 10, 1)){
                    dcm.FY_S_9__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 10, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 11, 1)){
                    dcm.FY_S_10__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 11, 1) && sa.DeliveryDate__c < Date.newInstance(FYear-1, 12, 1)){
                    dcm.FY_S_11__c+=sa.NetWorthNoProviston__c/1000;
                }else if(sa.DeliveryDate__c >= Date.newInstance(FYear-1, 12, 1) && sa.DeliveryDate__c < Date.newInstance(FYear, 1, 1)){
                    dcm.FY_S_12__c+=sa.NetWorthNoProviston__c/1000;
                }
            }
            newDCM.add(dcm);
        }
        if(dcmList.size()>0){
            update dcmList;
        }
        if(newDCM.size()>0){
            insert newDCM;
        }
    }
 
    global void finish(Database.BatchableContext BC) {
        Database.executeBatch(new DealerConsumableManageBatch3(startDate,endDate,this.isIncrementOnly),200);
    }
}