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
global without sharing class TrackConsumableSaleForecastBatch3 implements Database.Batchable<sObject>{
    public Date currentMonth;
    public Date endDate;
    public Date lastMonth;
    public String queryStr;
    public String recordIdET;
    public String recordIdENG;
    //20240308 lwt 出库价,销售状态修改 start
    public Boolean hisFlag=false;
    public Boolean onlyETENG=false;
    //20240308 lwt 出库价,销售状态修改 end
    public String recordIdC;
    global TrackConsumableSaleForecastBatch3(Date currentMonth,Date endDate) {
        this.currentMonth = currentMonth;
        this.endDate = endDate;
        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);
        }
 
        //20240131 lwt 耗材销量分析维度修改 start
        this.hisFlag=false;
        queryStr = 'SELECT Hospital__c,HospitalName__c,ProductModel__c,ThisMonthDeliveryNumber__c,LastShipmentDate__c,Type__c,Key__c FROM TrackConsumableSalesForecast__c WHERE SnapshotMonth__c = :currentMonth ';
        //20240131 lwt 耗材销量分析维度修改 end
    }
    //20240131 lwt 耗材销量分析维度修改 start
    global TrackConsumableSaleForecastBatch3(Date currentMonth,Date endDate,Boolean hisFlag) {
        this.currentMonth = currentMonth;
        this.endDate = endDate;
        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);
        }
        this.hisFlag=hisFlag;
 
        queryStr = 'SELECT ThisMonthDelivery_Price_IMS__c,Hospital__c,HospitalName__c,ProductModel__c,ThisMonthDeliveryNumber__c,LastShipmentDate__c,Type__c,Key__c FROM TrackConsumableSalesForecast__c WHERE SnapshotMonth__c = :currentMonth ';
        if(this.hisFlag){
            String recordIdC = Schema.SObjectType.TrackConsumableSalesForecast__c.getRecordTypeInfosByDeveloperName().get('ConsumableCategory3').getRecordTypeId();
            this.recordIdC=recordIdC;
            queryStr+= ' AND RecordTypeId=:recordIdC';
        }
    }
    //20240131 lwt 耗材销量分析维度修改 end
    //20240308 lwt 出库价,销售状态修改 start
    global TrackConsumableSaleForecastBatch3(Date currentMonth,Date endDate,Boolean hisFlag,Boolean onlyETENG) {
        this.currentMonth = currentMonth;
        this.endDate = endDate;
        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);
        }
        this.hisFlag=hisFlag;
        this.onlyETENG=onlyETENG;
        queryStr = 'SELECT ThisMonthDelivery_Price_IMS__c,Hospital__c,HospitalName__c,ProductModel__c,ThisMonthDeliveryNumber__c,LastShipmentDate__c,Type__c,Key__c FROM TrackConsumableSalesForecast__c WHERE SnapshotMonth__c = :currentMonth ';
        if(this.hisFlag&&this.onlyETENG){
            String recordIdC = Schema.SObjectType.TrackConsumableSalesForecast__c.getRecordTypeInfosByDeveloperName().get('ConsumableCategory3').getRecordTypeId();
            this.recordIdC=recordIdC;
            queryStr+= ' AND RecordTypeId!=:recordIdC';
        }
    }
    //20240308 lwt 出库价,销售状态修改 end
    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> tcsfMap = new Map<String,TrackConsumableSalesForecast__c>();
        for(TrackConsumableSalesForecast__c tcsf : scope){
            tcsf.Status__c = '完成';
            tcsfMap.put(tcsf.Key__c,tcsf);
        }
        //更新过去6月的平均销量及失单预警
        Date sixMonthAgo;
        Date threeMonthAgo;
        Integer year = currentMonth.year();
        Integer month = currentMonth.month();
        if(month <= 2){
            threeMonthAgo = Date.newInstance(year-1, 10+month, 1);
        }else {
            threeMonthAgo = Date.newInstance(year, month-2, 1);
        }
        if(month <= 6){
            sixMonthAgo = Date.newInstance(year-1, 6+month, 1);
        }else {
            sixMonthAgo = Date.newInstance(year, month-6, 1);
        }
        //20240131 lwt 耗材销量分析维度修改
        List<TrackConsumableSalesForecast__c> tcsfSixMonthAgoList = [SELECT ThisMonthDelivery_Price_IMS__c,Hospital__c,HospitalName__c,ProductModel__c,ThisMonthDeliveryNumber__c,LastShipmentDate__c,SnapshotMonth__c,Key__c FROM TrackConsumableSalesForecast__c WHERE SnapshotMonth__c >= :sixMonthAgo AND SnapshotMonth__c < :currentMonth AND Key__c IN :tcsfMap.keySet()];
        System.debug('tcsfSixMonthAgoList:'+tcsfSixMonthAgoList);
        for(String key : tcsfMap.keySet()){
            System.debug('key:'+key);
            Decimal sumThreeMonthSales = 0;
            Decimal sumSixMonthSales = 0;
            Decimal sumSixMonthSalesIMS = 0;//20240131 lwt 耗材销量分析维度修改
            for(TrackConsumableSalesForecast__c tcsf : tcsfSixMonthAgoList){
                if(tcsf.Key__c == key){
                    if(tcsf.SnapshotMonth__c >= threeMonthAgo){
                        sumThreeMonthSales += tcsf.ThisMonthDeliveryNumber__c;
                    }
                    sumSixMonthSales += tcsf.ThisMonthDeliveryNumber__c;
                    //20240131 lwt 耗材销量分析维度修改 start
                    if(tcsf.ThisMonthDelivery_Price_IMS__c!=null){
                        sumSixMonthSalesIMS += tcsf.ThisMonthDelivery_Price_IMS__c;
                    }
                    //20240131 lwt 耗材销量分析维度修改 end
                }
            }
            if(sumThreeMonthSales == 0 && tcsfMap.get(key).ThisMonthDeliveryNumber__c == 0){
                tcsfMap.get(key).BillLostWarning__c = 1;
            }
            if(sumSixMonthSales != 0){
                tcsfMap.get(key).LastSixMonthsAverage__c = sumSixMonthSales/6;
            }else {
                tcsfMap.get(key).LastSixMonthsAverage__c = 0;
            }
            //20240131 lwt 耗材销量分析维度修改 start
            if(sumSixMonthSalesIMS != 0){
                tcsfMap.get(key).LastSixMonthsAverage_Price_IMS__c = sumSixMonthSalesIMS;
            }else {
                tcsfMap.get(key).LastSixMonthsAverage_Price_IMS__c = 0;
            }
            //20240131 lwt 耗材销量分析维度修改 start
        }
        update tcsfMap.values();
    }
 
    global void finish(Database.BatchableContext BC) {
        //20240308 lwt 出库价,销售状态修改 start
        ////20240131 lwt 耗材销量分析维度修改 start
        //if(this.hisFlag){
        //   Database.executeBatch(new TrackConsumableSalesDailyBatch(), 100);
        //    Integer year = currentMonth.year();
        //    Integer month = currentMonth.month();
        //    Date nextMonth;
        //    if(month == 12){
        //        nextMonth = Date.newInstance(year+1, 1, 1);
        //    }else {
        //        nextMonth = Date.newInstance(year, month+1, 1);
        //    }
        //    if(currentMonth < endDate){
        //        Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch1(nextMonth,endDate,this.hisFlag), 200);
        //    }
        ////20240131 lwt 耗材销量分析维度修改 end
        //}else{
        //    Database.executeBatch(new TrackConsumableSalesDailyBatch(), 100);
        //    Integer year = currentMonth.year();
        //    Integer month = currentMonth.month();
        //    Date nextMonth;
        //    if(month == 12){
        //        nextMonth = Date.newInstance(year+1, 1, 1);
        //    }else {
        //        nextMonth = Date.newInstance(year, month+1, 1);
        //    }
        //    if(currentMonth < endDate){
        //        Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch1(nextMonth,endDate), 200);
        //    }
        //}
        
        if(this.onlyETENG){
            Database.executeBatch(new TrackConsumableSalesDailyBatch(), 100);
            Integer year = currentMonth.year();
            Integer month = currentMonth.month();
            Date nextMonth;
            if(month == 12){
                nextMonth = Date.newInstance(year+1, 1, 1);
            }else {
                nextMonth = Date.newInstance(year, month+1, 1);
            }
            if(currentMonth < endDate){
                Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch1(nextMonth,endDate,this.hisFLag,this.onlyETENG), 200);
            }
       
        }else if(this.hisFLag){
            Id batjobId = Database.executeBatch(new ConsumableTargetManageBatch1(currentMonth,endDate,this.hisFLag), 200);
        }else{
            Id batjobId = Database.executeBatch(new ConsumableTargetManageBatch1(currentMonth,endDate,false), 200);
        }
        //20240308 lwt 出库价,销售状态修改 end
        
    }
     
}