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
global without sharing class TrackConsumableSaleForecastBatch1 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 Integer fyear;
    global TrackConsumableSaleForecastBatch1(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.onlyETENG=false;
        this.fyear=year;
        if(currentMonth.month()>=4){
            this.fyear=year+1;
        }
        queryStr = 'SELECT ';
        for(Integer i=2021;i<=this.fyear;i++){
            queryStr += 'FY_SaleStatus_Batch_'+i+'__c,';
        }
        //20240222 耗材销量分析销售状态修改 end
        queryStr += ' IMS_Flag__c,ThisMonthDelivery_Price_IMS__c,LastMonthDelivery_Price_IMS__c,Sale_OrderName_Set__c,Least_Unit__c,Lastyear_SaleStatus_text__c,Thisyear_SaleStatus_text__c,RecordTypeId,ThisYearDeliveryNumber__c,Consumable_Category3__c,ASP_Price__c,Hospital__c,HospitalName__c,ProductModel__c,ThisMonthDeliveryNumber__c,LastShipmentDate__c,Type__c,Consumable_Category1__c,Consumable_Category2__c,OwnerId FROM TrackConsumableSalesForecast__c WHERE SnapshotMonth__c = :lastMonth';
    }
 
    //20240131 lwt 耗材销量分析维度修改 start
    global TrackConsumableSaleForecastBatch1(Date currentMonth,Date endDate,Boolean hisFLag) {
        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=hisFLag;
        //20240308 lwt 出库价,销售状态修改 start
        this.onlyETENG=false;
        this.fyear=year;
        if(currentMonth.month()>=4){
            this.fyear=year+1;
        }
        queryStr = 'SELECT ';
        for(Integer i=2021;i<=this.fyear;i++){
            queryStr += 'FY_SaleStatus_Batch_'+i+'__c,';
        }
        
         //20240308 lwt 出库价,销售状态修改 end
        //20240222 耗材销量分析销售状态修改 end
        queryStr += ' IMS_Flag__c,ThisMonthDelivery_Price_IMS__c,LastMonthDelivery_Price_IMS__c,Sale_OrderName_Set__c,Least_Unit__c,Lastyear_SaleStatus_text__c,Thisyear_SaleStatus_text__c,RecordTypeId,ThisYearDeliveryNumber__c,Consumable_Category3__c,ASP_Price__c,Hospital__c,HospitalName__c,ProductModel__c,ThisMonthDeliveryNumber__c,LastShipmentDate__c,Type__c,Consumable_Category1__c,Consumable_Category2__c,OwnerId FROM TrackConsumableSalesForecast__c WHERE SnapshotMonth__c = :lastMonth';
        if(hisFLag){
            String recordIdET = Schema.SObjectType.TrackConsumableSalesForecast__c.getRecordTypeInfosByDeveloperName().get('ET').getRecordTypeId();
            String recordIdENG = Schema.SObjectType.TrackConsumableSalesForecast__c.getRecordTypeInfosByDeveloperName().get('ENG').getRecordTypeId();
            this.recordIdENG=recordIdENG;
            this.recordIdET=recordIdET;
            queryStr += '  AND (RecordTypeId != :recordIdET AND RecordTypeId != :recordIdENG)';
        }
    }
    //20240131 lwt 耗材销量分析维度修改 end
 
    
    //20240308 lwt 出库价,销售状态修改 start
    global TrackConsumableSaleForecastBatch1(Date currentMonth,Date endDate,Boolean hisFLag,Boolean onlyETENG) {
        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=hisFLag;
        this.onlyETENG=onlyETENG;
        this.fyear=year;
        if(currentMonth.month()>=4){
            this.fyear=year+1;
        }
        queryStr = 'SELECT ';
        for(Integer i=2021;i<=this.fyear;i++){
            queryStr += 'FY_SaleStatus_Batch_'+i+'__c,';
        }
        //20240222 耗材销量分析销售状态修改 end
        queryStr += ' IMS_Flag__c,ThisMonthDelivery_Price_IMS__c,LastMonthDelivery_Price_IMS__c,Sale_OrderName_Set__c,Least_Unit__c,Lastyear_SaleStatus_text__c,Thisyear_SaleStatus_text__c,RecordTypeId,ThisYearDeliveryNumber__c,Consumable_Category3__c,ASP_Price__c,Hospital__c,HospitalName__c,ProductModel__c,ThisMonthDeliveryNumber__c,LastShipmentDate__c,Type__c,Consumable_Category1__c,Consumable_Category2__c,OwnerId FROM TrackConsumableSalesForecast__c WHERE SnapshotMonth__c = :lastMonth';
        if(hisFLag&&onlyETENG){
            String recordIdET = Schema.SObjectType.TrackConsumableSalesForecast__c.getRecordTypeInfosByDeveloperName().get('ET').getRecordTypeId();
            String recordIdENG = Schema.SObjectType.TrackConsumableSalesForecast__c.getRecordTypeInfosByDeveloperName().get('ENG').getRecordTypeId();
            this.recordIdENG=recordIdENG;
            this.recordIdET=recordIdET;
            queryStr += '  AND (RecordTypeId = :recordIdET OR RecordTypeId = :recordIdENG)';
        }
    }
    //20240308 lwt 出库价,销售状态修改 end
 
    global Database.QueryLocator start(Database.BatchableContext BC) {
        return Database.getQueryLocator(queryStr);
    }
 
    global void execute(Database.BatchableContext BC, List<TrackConsumableSalesForecast__c> scope){
        List<TrackConsumableSalesForecast__c> newTcsfList = new List<TrackConsumableSalesForecast__c>();
        for(TrackConsumableSalesForecast__c tcsf : scope){
            TrackConsumableSalesForecast__c newTcsf = new TrackConsumableSalesForecast__c();
            newTcsf.Hospital__c = tcsf.Hospital__c;
            newTcsf.HospitalName__c = tcsf.HospitalName__c;
            newTcsf.ProductModel__c = tcsf.ProductModel__c;
            newTcsf.LastMonthDeliveryNumber__c = tcsf.ThisMonthDeliveryNumber__c != null ? tcsf.ThisMonthDeliveryNumber__c : 0;
            newTcsf.ThisMonthDeliveryNumber__c = 0;
            newTcsf.LastShipmentDate__c = tcsf.LastShipmentDate__c;
            newTcsf.Type__c = tcsf.Type__c;
            // 20231211 hql 课题  耗材【销售状态】查看权限 DB202311574810 start
            newTcsf.OwnerId = tcsf.OwnerId;
            // 20231211 hql 课题  耗材【销售状态】查看权限 DB202311574810 end
            //20231225  DB202312609378【IMS/TMS】ASP价格上传--平均价格,平均月销量 lwt start
            newTcsf.ASP_Price__c = tcsf.ASP_Price__c;
            newTcsf.Consumable_Category3__c = tcsf.Consumable_Category3__c;
            //20231225  DB202312609378【IMS/TMS】ASP价格上传--平均价格,平均月销量 lwt end
            newTcsf.SnapshotMonth__c = currentMonth;
            newTcsf.Status__c = '复制';
            newTcsf.Consumable_Category1__c = tcsf.Consumable_Category1__c;
            newTcsf.Consumable_Category2__c = tcsf.Consumable_Category2__c;
            //20240131 lwt 耗材销量分析维度修改 start
            newTcsf.RecordTypeId = tcsf.RecordTypeId;
            newTcsf.LastMonthDelivery_Price_IMS__c = tcsf.ThisMonthDelivery_Price_IMS__c != null ? tcsf.ThisMonthDelivery_Price_IMS__c : 0;
            newTcsf.ThisMonthDelivery_Price_IMS__c = 0;
 
 
 
 
            //20240222 耗材销量分析销售状态修改 start
 
            for(Integer i=2021;i<=this.fyear;i++){
                newTcsf.put('FY_SaleStatus_Batch_'+i+'__c',tcsf.get('FY_SaleStatus_Batch_'+i+'__c'));
            }
            //20240222 耗材销量分析销售状态修改 end
 
            if(currentMonth.month()==5){
                newTcsf.ThisYearDeliveryNumber__c = 0;
                newTcsf.Least_Unit__c = 0;
                newTcsf.Sale_OrderName_Set__c = '';
                newTcsf.IMS_Flag__c=false;
            }else{
                newTcsf.ThisYearDeliveryNumber__c = tcsf.ThisYearDeliveryNumber__c;
                newTcsf.Least_Unit__c = tcsf.Least_Unit__c;
                newTcsf.Sale_OrderName_Set__c = tcsf.Sale_OrderName_Set__c;
                newTcsf.IMS_Flag__c=tcsf.IMS_Flag__c;
            }
            //20240131 lwt 耗材销量分析维度修改 end
 
            newTcsfList.add(newTcsf);
        }
        if(!newTcsfList.isEmpty()){
            insert newTcsfList;
        }       
    }
 
    global void finish(Database.BatchableContext BC) {
        
        //20240308 lwt 出库价,销售状态修改 start
        //20240131 lwt 耗材销量分析维度修改 start
        // Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch2(currentMonth,endDate), 200);
        if(this.onlyETENG){
            Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch2(currentMonth,endDate,this.onlyETENG), 200);
        }else if(this.hisFLag){
            Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch2C(currentMonth,endDate,this.hisFLag), 200);
        }else{
            Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch2(currentMonth,endDate), 200);
        }
         //20240131 lwt 耗材销量分析维度修改 end 
        //20240308 lwt 出库价,销售状态修改 end  
    }
}