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
public with sharing class VirtualDealerToTmsBatch implements Database.Batchable<sObject>{
    String query;
    public VirtualDealerToTmsBatch() {
 
    }
 
    public Database.QueryLocator start(Database.BatchableContext BC) {
        query = 'select id,name,Target_Source__c,CloseDate, Amount, Opportunity_Category__c ,RecordType.DeveloperName,OCM_Target_period_New__c,Objective_Tax__c,Shipping_Date_For_Report__c  from Opportunity where Target_category__c = \'省目标\' And OCM_Target_period__c = \'156P\' and Opportunity_Category__c = \'ET\' and RecordType.name = \'目標\'';
        
        return Database.getQueryLocator(query);
    }
 
 
    public void execute(Database.BatchableContext BC, List<Opportunity> OppoList) { 
        List<String> dealerNameList =  new List<String>();
        Map<String,String> dealerlocationList =  new Map<String,String>();
        Map<String,id> dealerNameList2 =  new Map<String,id>();
        Set<String> dealerNamemap =  new Set<String>();
        List<Dealer_ConsumableManage__c> dcmList = new  List<Dealer_ConsumableManage__c>();
        List<Dealer_ConsumableManage__c> dcmList2 = new  List<Dealer_ConsumableManage__c>();
        for (Opportunity opp : OppoList) {
            // String year = opp.OCM_Target_period_New__c.split('FY')[1];
            Integer year = opp.Shipping_Date_For_Report__c.year();
            String OCM;
            Integer Month = opp.Shipping_Date_For_Report__c.month();
            if (Month>=1 && Month<4) {
                
                 OCM = 'FY'+year;
            }else {
                 OCM = 'FY'+(year+1);
            }
            if (Double.valueOf(year)>=2023) {
                system.debug('year:'+year);
                system.debug('Month:'+Month);
                integer startIndex = opp.name.indexOf(':');
                
                String dealerName = opp.name.substring(startIndex+2, startIndex+5);
                if (!dealerNameList.contains(dealerName)) {
                    dealerNameList.add(dealerName);
                }
                String key = dealerName+':'+opp.Opportunity_Category__c+':'+OCM+':'+opp.Objective_Tax__c+':'+Month;
                dealerNamemap.add(key);
            }
            
        }
        // 虚拟经销商
        List<account> accList =[select id,name,OCM_man_province_HP__c from Account where ISVirtualDealer__c = true];
        for (account acc : accList) {
            for(String deName:dealerNameList)
                if (acc.name.indexOf(deName) != -1 && !dealerNameList2.containsKey(deName)) {
                    dealerNameList2.put(deName,acc.id);
                    dealerlocationList.put(acc.id,acc.OCM_man_province_HP__c);
                }
        }
        System.debug('dealerNameList='+dealerNameList);
        System.debug('dealerNameList2='+dealerNameList2);
        List<Dealer_ConsumableManage__c> dcList = [select id ,ET_ENG__c,FY__c,Dealer_Name__c,Dealer_Name__r.name from Dealer_ConsumableManage__c where Dealer_Name__c in :dealerNameList2.values() ];
        for (Dealer_ConsumableManage__c dc : dcList) {
            for (String key : dealerNamemap) {
                String name = key.split(':')[0];
                String oc = key.split(':')[1];
                String otp = key.split(':')[2];
                String Amount = key.split(':')[3];
                String Month = key.split(':')[4];
                if (dc.Dealer_Name__r.name.indexOf(name)!=-1) {
                    if (dc.ET_ENG__c == 'ET' && oc =='ET' && dc.FY__c == otp) {
                        Dealer_ConsumableManage__c dcm =  new Dealer_ConsumableManage__c();
                        dcm.id = dc.id;
                        if (Month=='1') {
                            dcm.FY_P_1__c = Double.valueOf(Amount);
                        }
                        if (Month=='10') {
                            dcm.FY_P_10__c = Double.valueOf(Amount);
                        }
                        if (Month=='11') {
                            dcm.FY_P_11__c = Double.valueOf(Amount);
                        }
                        if (Month=='12') {
                            dcm.FY_P_12__c = Double.valueOf(Amount);
                        }
                        if (Month=='2') {
                            dcm.FY_P_2__c = Double.valueOf(Amount);
                        }
                        if (Month=='3') {
                            dcm.FY_P_3__c = Double.valueOf(Amount);
                        }
                        if (Month=='4') {
                            dcm.FY_P_4__c = Double.valueOf(Amount);
                        }
                        if (Month=='5') {
                            dcm.FY_P_5__c = Double.valueOf(Amount);
                        }
                        if (Month=='6') {
                            dcm.FY_P_6__c = Double.valueOf(Amount);
                        }
                        if (Month=='7') {
                            dcm.FY_P_7__c = Double.valueOf(Amount);
                        }
                        if (Month=='8') {
                            dcm.FY_P_8__c = Double.valueOf(Amount);
                        }
                        if (Month=='9') {
                            dcm.FY_P_9__c = Double.valueOf(Amount);
                        }
                        dcmList.add(dcm);
                        dealerNamemap.remove(key);
                    }
                    if (dc.ET_ENG__c == 'ENG2' && oc =='ENG' && dc.FY__c == otp) {
                        Dealer_ConsumableManage__c dcm =  new Dealer_ConsumableManage__c();
                        dcm.id = dc.id;
                        if (Month=='1') {
                            dcm.FY_P_1__c = Double.valueOf(Amount);
                        }
                        if (Month=='10') {
                            dcm.FY_P_10__c = Double.valueOf(Amount);
                        }
                        if (Month=='11') {
                            dcm.FY_P_11__c = Double.valueOf(Amount);
                        }
                        if (Month=='12') {
                            dcm.FY_P_12__c = Double.valueOf(Amount);
                        }
                        if (Month=='2') {
                            dcm.FY_P_2__c = Double.valueOf(Amount);
                        }
                        if (Month=='3') {
                            dcm.FY_P_3__c = Double.valueOf(Amount);
                        }
                        if (Month=='4') {
                            dcm.FY_P_4__c = Double.valueOf(Amount);
                        }
                        if (Month=='5') {
                            dcm.FY_P_5__c = Double.valueOf(Amount);
                        }
                        if (Month=='6') {
                            dcm.FY_P_6__c = Double.valueOf(Amount);
                        }
                        if (Month=='7') {
                            dcm.FY_P_7__c = Double.valueOf(Amount);
                        }
                        if (Month=='8') {
                            dcm.FY_P_8__c = Double.valueOf(Amount);
                        }
                        if (Month=='9') {
                            dcm.FY_P_9__c = Double.valueOf(Amount);
                        }
                        dcmList.add(dcm);
                        dealerNamemap.remove(key);
                    }
                }
            }
        }
        for (String  key : dealerNamemap) {
            String name = key.split(':')[0];
            String oc = key.split(':')[1];
            String otp = key.split(':')[2];
            String Amount = key.split(':')[3];
            String Month = key.split(':')[4];
            if (dealerNameList2.get(name) !=null) {
                Dealer_ConsumableManage__c dcm =  new Dealer_ConsumableManage__c();
                if (oc=='ET') {
                    dcm.ET_ENG__c = oc; 
                }
                if (oc=='ENG2') {
                    dcm.ET_ENG__c = 'ENG'; 
                }
                if (Month=='1') {
                    dcm.FY_P_1__c = Double.valueOf(Amount);
                }
                if (Month=='10') {
                    dcm.FY_P_10__c = Double.valueOf(Amount);
                }
                if (Month=='11') {
                    dcm.FY_P_11__c = Double.valueOf(Amount);
                }
                if (Month=='12') {
                    dcm.FY_P_12__c = Double.valueOf(Amount);
                }
                if (Month=='2') {
                    dcm.FY_P_2__c = Double.valueOf(Amount);
                }
                if (Month=='3') {
                    dcm.FY_P_3__c = Double.valueOf(Amount);
                }
                if (Month=='4') {
                    dcm.FY_P_4__c = Double.valueOf(Amount);
                }
                if (Month=='5') {
                    dcm.FY_P_5__c = Double.valueOf(Amount);
                }
                if (Month=='6') {
                    dcm.FY_P_6__c = Double.valueOf(Amount);
                }
                if (Month=='7') {
                    dcm.FY_P_7__c = Double.valueOf(Amount);
                }
                if (Month=='8') {
                    dcm.FY_P_8__c = Double.valueOf(Amount);
                }
                if (Month=='9') {
                    dcm.FY_P_9__c = Double.valueOf(Amount);
                }
                dcm.FY__c = otp;
                String deid = dealerNameList2.get(name);
                dcm.TMS_Key__c = dealerNameList2.get(name)+'-'+dcm.ET_ENG__c+'-'+dcm.FY__c+'-'+dealerlocationList.get(deid);
                dcm.Dealer_Name__c = dealerNameList2.get(name);
                // dcm.Dealer_Name_Account__c = dealerNameList2.get(name);
                if (!dcmList2.contains(dcm)) {
                    dcmList2.add(dcm);
                }
            }
            
 
        }
        System.debug('dcmList='+dcmList);
        System.debug('dcmList2='+dcmList2);
        if (dcmList.size()>0) {
            update dcmList;
        }
        if (dcmList2.size()>0) {
            insert dcmList2;
        }
    }
 
    public void finish(Database.BatchableContext BC) {
      
    }
}