global without sharing class TrackConsumableSaleForecastASPBatch1 implements Database.Batchable{ public Date currentMonth; public Date endDate; public Date lastMonth; public String queryStr; public String recordIdET; public String recordIdENG; public Boolean hisFLag; public Integer fyear; global TrackConsumableSaleForecastASPBatch1(Date currentMonth,Date endDate) { this.currentMonth = currentMonth; this.endDate = endDate; //确保开始日期和结束日期为每月的1号 this.endDate = Date.newInstance(endDate.year(), endDate.month(), 1); Integer year = currentMonth.year(); Integer month = currentMonth.month(); this.currentMonth = Date.newInstance(year, month, 1); if(month == 1){ lastMonth = Date.newInstance(year-1, 12, 1); }else { lastMonth = Date.newInstance(year, month-1, 1); } //20231225 DB202312609378【IMS/TMS】ASP价格上传--平均价格,平均月销量 lwt //20240222 耗材销量分析销售状态修改 start this.hisFLag=false; this.fyear=year; if(currentMonth.month()>=4){ this.fyear=year+1; } queryStr = 'SELECT id,Key__c FROM TrackConsumableSalesForecast__c WHERE SnapshotMonth__c = :currentMonth'; } global Database.QueryLocator start(Database.BatchableContext BC) { return Database.getQueryLocator(queryStr); } global void execute(Database.BatchableContext BC, List scope){ Map thisMonthMap=new Map(); for(TrackConsumableSalesForecast__c item : scope){ thisMonthMap.put(item.Key__c,item); } Map lastMonthMap=new Map(); List lastMonthList=[select id,Key__c,ThisMonthDelivery_Price_IMS__c from TrackConsumableSalesForecast__c where SnapshotMonth__c=:lastMonth and Key__c in :thisMonthMap.keySet() ]; for(TrackConsumableSalesForecast__c item:lastMonthList){ lastMonthMap.put(item.Key__c,item); } for(TrackConsumableSalesForecast__c item:scope){ if(lastMonthMap.containsKey(item.key__c)){ item.LastMonthDelivery_Price_IMS__c=lastMonthMap.get(item.key__c).ThisMonthDelivery_Price_IMS__c != null ? lastMonthMap.get(item.key__c).ThisMonthDelivery_Price_IMS__c : 0; }else{ item.LastMonthDelivery_Price_IMS__c=0; } item.ThisMonthDelivery_Price_IMS__c=0; } update scope; //update lastMonthMap.values(); } global void finish(Database.BatchableContext BC) { Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastASPBatch2(currentMonth,endDate), 200); } }