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
global without sharing class TrackConsumableSaleForecastASPBatch1 implements Database.Batchable<sObject>{
 
    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<TrackConsumableSalesForecast__c> scope){
        Map<String,TrackConsumableSalesForecast__c> thisMonthMap=new Map<String,TrackConsumableSalesForecast__c>();
        for(TrackConsumableSalesForecast__c item : scope){
            thisMonthMap.put(item.Key__c,item);
        }
        Map<String,TrackConsumableSalesForecast__c> lastMonthMap=new Map<String,TrackConsumableSalesForecast__c>();
        
        List<TrackConsumableSalesForecast__c> 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);
    }
}