public with sharing class VirtualDealerToTmsBatch implements Database.Batchable{ 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 OppoList) { List dealerNameList = new List(); Map dealerlocationList = new Map(); Map dealerNameList2 = new Map(); Set dealerNamemap = new Set(); List dcmList = new List(); List dcmList2 = new List(); 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 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 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) { } }