global without sharing class TrackConsumableSaleForecastBatch2 implements Database.Batchable<sObject>{
|
|
public Date currentMonth;
|
public Date endDate;
|
public Date lastMonth;
|
public String queryStr;
|
//20240308 lwt 出库价,销售状态修改 start
|
public Boolean onlyETENG=false;
|
|
//20240308 lwt 出库价,销售状态修改 end
|
global TrackConsumableSaleForecastBatch2(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);
|
}
|
this.onlyETENG=false;
|
//20231225 DB202312609378【IMS/TMS】ASP价格上传--平均价格,平均月销量 lwt start
|
//20240131 lwt 耗材销量分析维度修改
|
//20240308 lwt 出库价,销售状态修改
|
queryStr = 'SELECT Intra_Trade_KRMB__c,Sale_orderName__c,Detail_Count__c,ProductPacking_list_manual__c,Hospital_ID__c,Consumable_product__r.Product2__r.ConsumCategory2__c,Consumable_product__r.Product2__r.ConsumCategory3__c,Consumable_product__r.Product2__r.ASP_Price__c,Consumable_Sale_order__c,Consumable_Shipment_order__c,Consumable_Sale_order__r.Order_ForHospital__c,Consumable_Sale_order__r.Order_ForHospital__r.ET_owner__r.IsActive,Consumable_Sale_order__r.Order_ForDealer__c,Consumable_Sale_order__r.Order_ForDealer__r.ET_owner__r.IsActive,Consumable_Shipment_order__r.Order_ForHospital__c,Consumable_Shipment_order__r.Order_ForHospital__r.ET_owner__r.IsActive,Consumable_Shipment_order__r.Order_ForDealer__c,Consumable_Shipment_order__r.Order_ForDealer__r.ET_owner__r.IsActive,ShipmentAccount__c,Asset_Model_No__c,Product_OutDate__c,Product_Type__c,Consumable_Category1__c,Consumable_Category2__c FROM Consumable_order_details2__c WHERE Product_Type__c != \'\' AND Asset_Model_No__c != \'\' AND (Inventory_Status__c = \'直销到医院\' OR Inventory_Status__c = \'给二级到医院\') AND ShipmentAccount__c != \'\' AND Product_OutDate__c >= :lastMonth AND Product_OutDate__c < :currentMonth';
|
}
|
|
//20240308 lwt 出库价,销售状态修改 start
|
global TrackConsumableSaleForecastBatch2(Date currentMonth,Date endDate,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.onlyETENG=onlyETENG;
|
//20231225 DB202312609378【IMS/TMS】ASP价格上传--平均价格,平均月销量 lwt start
|
//20240131 lwt 耗材销量分析维度修改
|
queryStr = 'SELECT Intra_Trade_KRMB__c,Sale_orderName__c,Detail_Count__c,ProductPacking_list_manual__c,Hospital_ID__c,Consumable_product__r.Product2__r.ConsumCategory2__c,Consumable_product__r.Product2__r.ConsumCategory3__c,Consumable_product__r.Product2__r.ASP_Price__c,Consumable_Sale_order__c,Consumable_Shipment_order__c,Consumable_Sale_order__r.Order_ForHospital__c,Consumable_Sale_order__r.Order_ForHospital__r.ET_owner__r.IsActive,Consumable_Sale_order__r.Order_ForDealer__c,Consumable_Sale_order__r.Order_ForDealer__r.ET_owner__r.IsActive,Consumable_Shipment_order__r.Order_ForHospital__c,Consumable_Shipment_order__r.Order_ForHospital__r.ET_owner__r.IsActive,Consumable_Shipment_order__r.Order_ForDealer__c,Consumable_Shipment_order__r.Order_ForDealer__r.ET_owner__r.IsActive,ShipmentAccount__c,Asset_Model_No__c,Product_OutDate__c,Product_Type__c,Consumable_Category1__c,Consumable_Category2__c FROM Consumable_order_details2__c WHERE Product_Type__c != \'\' AND Asset_Model_No__c != \'\' AND (Inventory_Status__c = \'直销到医院\' OR Inventory_Status__c = \'给二级到医院\') AND ShipmentAccount__c != \'\' AND Product_OutDate__c >= :lastMonth AND Product_OutDate__c < :currentMonth';
|
}
|
//20240308 lwt 出库价,销售状态修改 end
|
global Database.QueryLocator start(Database.BatchableContext BC) {
|
return Database.getQueryLocator(queryStr);
|
}
|
|
global void execute(Database.BatchableContext BC, List<Consumable_order_details2__c> scope){
|
Map<String,List<Consumable_order_details2__c>> coMap = new Map<String,List<Consumable_order_details2__c>>();
|
//根据医院和产品对本月所有出库的消耗品明细2进行分类
|
for(Consumable_order_details2__c coDetail : scope){
|
String key = coDetail.ShipmentAccount__c + '-' + coDetail.Asset_Model_No__c;
|
if(coMap.containsKey(key)){
|
coMap.get(key).add(coDetail);
|
}else {
|
List<Consumable_order_details2__c> coList = new List<Consumable_order_details2__c>();
|
coList.add(coDetail);
|
coMap.put(key,coList);
|
}
|
}
|
//取到从上月复制到本月的销量表数据
|
Map<String,TrackConsumableSalesForecast__c> tcsfMap = new Map<String,TrackConsumableSalesForecast__c>();
|
//20240131 lwt 耗材销量分析维度修改
|
String recordIdC = Schema.SObjectType.TrackConsumableSalesForecast__c.getRecordTypeInfosByDeveloperName().get('ConsumableCategory3').getRecordTypeId();
|
List<TrackConsumableSalesForecast__c> lastMonthTcsfList = [SELECT ThisMonthDelivery_Price_IMS__c,Id,Consumable_Category3__c,ASP_Price__c,Hospital__c,HospitalName__c,ProductModel__c,ThisMonthDeliveryNumber__c,LastShipmentDate__c,Key__c FROM TrackConsumableSalesForecast__c WHERE SnapshotMonth__c = :currentMonth AND Key__c IN :coMap.keySet() AND RecordTypeId!=:recordIdC ];
|
for(TrackConsumableSalesForecast__c tcsf : lastMonthTcsfList){
|
tcsfMap.put(tcsf.Key__c,tcsf);
|
}
|
//更新本月的销量表数据
|
Map<String,TrackConsumableSalesForecast__c> tcsfMapNewOrUpdate = new Map<String,TrackConsumableSalesForecast__c>();
|
for(String key : coMap.keySet()){
|
List<Consumable_order_details2__c> coList;
|
if(coMap.get(key) != null){
|
coList = coMap.get(key);
|
}else {
|
coList = new List<Consumable_order_details2__c>();
|
}
|
TrackConsumableSalesForecast__c tcsf;
|
//最近出库日
|
Date lastShipmentDate = Date.newInstance(1998, 1, 1);
|
Decimal touRMB=0;
|
for(Consumable_order_details2__c cod : coList){
|
if(cod.Product_OutDate__c > lastShipmentDate){
|
lastShipmentDate = cod.Product_OutDate__c;
|
}
|
//20240308 lwt 出库价,销售状态修改 start
|
if(cod.Intra_Trade_KRMB__c!=null && cod.Detail_Count__c!=null){
|
touRMB+=cod.Intra_Trade_KRMB__c*cod.Detail_Count__c;
|
}
|
//20240308 lwt 出库价,销售状态修改 end
|
|
}
|
System.debug('lastShipmentDate:'+lastShipmentDate);
|
if(tcsfMap.containsKey(key)){
|
tcsf = tcsfMap.get(key);
|
tcsf.SnapshotMonth__c = currentMonth;
|
tcsf.ThisMonthDeliveryNumber__c += coList.size();
|
//20231225 DB202312609378【IMS/TMS】ASP价格上传--平均价格,平均月销量 lwt start
|
if(coList.size()>0&&coList[0].Consumable_product__c!=null&&coList[0].Consumable_product__r.Product2__c!=null){
|
tcsf.ASP_Price__c = coList[0].Consumable_product__r.Product2__r.ASP_Price__c;
|
tcsf.Consumable_Category3__c = coList[0].Consumable_product__r.Product2__r.ConsumCategory3__c;
|
}
|
//20231225 DB202312609378【IMS/TMS】ASP价格上传--平均价格,平均月销量 lwt end
|
if(tcsf.LastShipmentDate__c < lastShipmentDate){
|
tcsf.LastShipmentDate__c = lastShipmentDate;
|
}
|
//20240131 lwt 耗材销量分析维度修改
|
tcsf.ThisMonthDelivery_Price_IMS__c+=touRMB;
|
tcsf.Status__c = '销量计算';
|
}else{
|
tcsf = new TrackConsumableSalesForecast__c();
|
//取到医院
|
if(coList[0].Consumable_Sale_order__c != null){
|
if(coList[0].Consumable_Sale_order__r.Order_ForHospital__c != null){
|
tcsf.Hospital__c = coList[0].Consumable_Sale_order__r.Order_ForHospital__c;
|
// 20231211 hql 课题 耗材【销售状态】查看权限 DB202311574810 start
|
if(coList[0].Consumable_Sale_order__r.Order_ForHospital__r.ET_owner__r.IsActive == true){
|
tcsf.OwnerId = coList[0].Consumable_Sale_order__r.Order_ForHospital__r.ET_owner__c;
|
}
|
}else{
|
tcsf.Hospital__c = coList[0].Consumable_Sale_order__r.Order_ForDealer__c;
|
if(coList[0].Consumable_Sale_order__r.Order_ForDealer__r.ET_owner__r.IsActive == true){
|
tcsf.OwnerId = coList[0].Consumable_Sale_order__r.Order_ForDealer__r.ET_owner__c;
|
}
|
}
|
}else if(coList[0].Consumable_Shipment_order__c != null){
|
if(coList[0].Consumable_Shipment_order__r.Order_ForHospital__c != null){
|
tcsf.Hospital__c = coList[0].Consumable_Shipment_order__r.Order_ForHospital__c;
|
if(coList[0].Consumable_Shipment_order__r.Order_ForHospital__r.ET_owner__r.IsActive == true){
|
tcsf.OwnerId = coList[0].Consumable_Shipment_order__r.Order_ForHospital__r.ET_owner__c;
|
}
|
}else{
|
tcsf.Hospital__c = coList[0].Consumable_Shipment_order__r.Order_ForDealer__c;
|
if(coList[0].Consumable_Shipment_order__r.Order_ForDealer__r.ET_owner__r.IsActive == true){
|
tcsf.OwnerId = coList[0].Consumable_Shipment_order__r.Order_ForDealer__r.ET_owner__c;
|
}
|
}
|
}
|
// 20231211 hql 课题 耗材【销售状态】查看权限 DB202311574810 end
|
//20231225 DB202312609378【IMS/TMS】ASP价格上传--平均价格,平均月销量 lwt start
|
if(coList.size()>0&&coList[0].Consumable_product__c!=null&&coList[0].Consumable_product__r.Product2__c!=null){
|
String ASPPrice;
|
String ConsumCategory3;
|
tcsf.ASP_Price__c = coList[0].Consumable_product__r.Product2__r.ASP_Price__c;
|
if(coList[0].Consumable_product__r.Product2__r.ConsumCategory3__c!=null){
|
tcsf.Consumable_Category3__c = coList[0].Consumable_product__r.Product2__r.ConsumCategory3__c;
|
}
|
}
|
//20231225 DB202312609378【IMS/TMS】ASP价格上传--平均价格,平均月销量 lwt end
|
tcsf.HospitalName__c = coList[0].ShipmentAccount__c;
|
tcsf.ProductModel__c = coList[0].Asset_Model_No__c;
|
tcsf.SnapshotMonth__c = currentMonth;
|
tcsf.ThisMonthDeliveryNumber__c = coList.size();
|
tcsf.LastShipmentDate__c = lastShipmentDate;
|
tcsf.Type__c = coList[0].Product_Type__c ;
|
tcsf.Consumable_Category1__c = coList[0].Consumable_Category1__c;
|
tcsf.Consumable_Category2__c = coList[0].Consumable_Category2__c;
|
//20240131 lwt 耗材销量分析维度修改
|
tcsf.ThisMonthDelivery_Price_IMS__c=touRMB;
|
tcsf.Status__c = '销量计算';
|
}
|
tcsfMapNewOrUpdate.put(key,tcsf);
|
}
|
System.debug('all1:'+tcsfMapNewOrUpdate.values());
|
//更新过去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);
|
// }
|
//获取过去6个月的销量数据
|
// List<TrackConsumableSalesForecast__c> tcsfSixMonthAgoList = [SELECT 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 :coMap.keySet()];
|
// System.debug('tcsfSixMonthAgoList:'+tcsfSixMonthAgoList);
|
// for(String key : coMap.keySet()){
|
// System.debug('key:'+key);
|
// Decimal sumThreeMonthSales = 0;
|
// Decimal sumSixMonthSales = 0;
|
// for(TrackConsumableSalesForecast__c tcsf : tcsfSixMonthAgoList){
|
// if(tcsf.Key__c == key){
|
// if(tcsf.SnapshotMonth__c >= threeMonthAgo){
|
// sumThreeMonthSales += tcsf.ThisMonthDeliveryNumber__c;
|
// }
|
// sumSixMonthSales += tcsf.ThisMonthDeliveryNumber__c;
|
// }
|
// }
|
// //失单预警
|
// if(sumThreeMonthSales == 0 && tcsfMapNewOrUpdate.get(key).ThisMonthDeliveryNumber__c == 0){
|
// tcsfMapNewOrUpdate.get(key).BillLostWarning__c = 1;
|
// }
|
// //过去6月平均销量
|
// if(sumSixMonthSales != 0){
|
// tcsfMapNewOrUpdate.get(key).LastSixMonthsAverage__c = sumSixMonthSales/6;
|
// }else {
|
// tcsfMapNewOrUpdate.get(key).LastSixMonthsAverage__c = 0;
|
// }
|
// }
|
//更新或者插入销量表
|
Database.upsert(tcsfMapNewOrUpdate.values());
|
}
|
|
global void finish(Database.BatchableContext BC) {
|
//20240308 lwt 出库价,销售状态修改 start
|
//20240131 lwt 耗材销量分析维度修改 start
|
if(this.onlyETENG){
|
Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch3(currentMonth,endDate,true,this.onlyETENG), 200);
|
}else{
|
Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch2C(currentMonth,endDate), 200);
|
}
|
//20240308 lwt 出库价,销售状态修改 end
|
// Id batjobId = Database.executeBatch(new TrackConsumableSaleForecastBatch3(currentMonth,endDate), 200);
|
//20240131 lwt 耗材销量分析维度修改 end
|
}
|
}
|