// 预测消费率计算对应型号的过去3年期间有合同的的全国合同修理金额合计
|
global class ComputeLTYRepairBatch implements Database.Batchable<sObject>,Database.Stateful {
|
public String query;
|
public String year;
|
public Date timeOne ;
|
public Date timeTwo;
|
public Date timeThr;
|
public Date timeFour;
|
private BatchIF_Log__c iflog;
|
public List<String> targetList= new List<String>();
|
public List<String> target2List= new List<String>();
|
public List<String> asslist= new List<String>();
|
global List<String> alist = new List<String>();
|
global ComputeLTYRepairBatch(Date timeOne) {
|
|
this.query = query;
|
this.timeOne = timeOne;
|
// 查询时间=当年-3的4月1日——今年的4月1日
|
this.timeTwo = date.newInstance(
|
this.timeOne.year(),
|
4,
|
1
|
);
|
this.timeThr = date.newInstance(
|
this.timeOne.year()-3,
|
4,
|
1
|
);
|
this.timeFour = this.timeTwo.addDays(1);
|
}
|
global ComputeLTYRepairBatch() {
|
}
|
|
global ComputeLTYRepairBatch(String year) {
|
|
this.query = query;
|
this.year = year;
|
this.timeTwo = date.newInstance(
|
2023,
|
4,
|
1
|
);
|
this.timeThr = date.newInstance(
|
2020,
|
4,
|
1
|
);
|
this.timeFour = this.timeTwo.addDays(1);
|
}
|
global ComputeLTYRepairBatch(List<String> targetList,integer count) {
|
|
this.query = query;
|
if (count ==1) {
|
this.targetList = targetList;
|
}else if(count ==2){
|
this.target2List = targetList;
|
}
|
|
this.timeTwo = date.newInstance(
|
2023,
|
4,
|
1
|
);
|
this.timeThr = date.newInstance(
|
2020,
|
4,
|
1
|
);
|
this.timeFour = this.timeTwo.addDays(1);
|
}
|
global Database.QueryLocator start(Database.BatchableContext bc) {
|
iflog = new BatchIF_Log__c();
|
iflog.Type__c = 'PushNotification';
|
iflog.Log__c = 'EquipmentRenewMoleculeBatch start\n';
|
iflog.ErrorLog__c = '';
|
insert iflog;
|
if (targetList.size()>0 && targetList !=null) {
|
List<Maintenance_Contract_Asset__c> asss = [Select id,name,asset__c from Maintenance_Contract_Asset__c
|
where Maintenance_Contract__c in :targetList];
|
for(Maintenance_Contract_Asset__c a1:asss){
|
asslist.add(a1.asset__c);
|
}
|
}
|
|
//查询符合条件的所有设备
|
query = 'select id,product2.Asset_Model_No__c,Category4__c ';
|
query +=' from asset where IsCompetitorProduct = false AND ( AssetMark__c != \'耗材\' OR Product2.Family != \'ET\') and RecordTypeId !=\'01210000000kOPRAA2\' and Return_Flag__c = false ';
|
if (String.isNotBlank(year)) {
|
query += ' and Shipping_Year__c= :year';
|
}
|
if (asslist.size()>0 && asslist !=null) {
|
query += ' and id in :asslist';
|
}
|
if (target2List.size()>0 && target2List !=null) {
|
query += ' and id in :target2List';
|
}
|
return Database.getQueryLocator(query);
|
}
|
|
global void execute(Database.BatchableContext BC, list<asset> assetList) {
|
for(Asset a1: assetList){
|
this.alist.add(a1.id);
|
}
|
// 分类计算,分别赋值,第四分类和产品型号
|
List<Asset> assList1 = ComputeLRepair(1,assetList);
|
List<Asset> assList2 = ComputeLRepair(2,assetList);
|
try {
|
|
if (assList1.size()>0) {
|
update assList1;
|
}
|
if (assList2.size()>0) {
|
update assList2;
|
}
|
|
|
} catch (Exception e) {
|
system.debug('err:::::' + e.getMessage());
|
}
|
}
|
|
global void finish(Database.BatchableContext BC) {
|
iflog.Log__c += '\nalist== '+alist+'\n';
|
update iflog;
|
Database.executeBatch(new ComputeLTYRepairBatch2(alist),1);
|
}
|
|
// type==1为第四分类 type==2为产品型号
|
private List<Asset> ComputeLRepair(Integer type,list<asset> assetList) {
|
List<String> uniqueAssetModelNo = new List<String>();
|
List<String> uniqueCategories = new List<String>();
|
for (Asset ass: assetList) {
|
// 统计第四分类和 产品分类的集合
|
if (!uniqueCategories.contains(ass.Category4__c) && ass.Category4__c !=null && type==1) {
|
uniqueCategories.add(ass.Category4__c);
|
}
|
if (!uniqueAssetModelNo.contains(ass.product2.Asset_Model_No__c) &&ass.product2.Asset_Model_No__c !=null && type==2) {
|
uniqueAssetModelNo.add(ass.product2.Asset_Model_No__c);
|
}
|
}
|
// 产品型号
|
if (type==2) {
|
List<Repair__c> ThreeyearList = new List<Repair__c>();
|
ThreeyearList = [
|
select
|
Repair_List_Price_formula__c,
|
Delivered_Product__c,
|
Delivered_Product__r.id,
|
Delivered_Product__r.product2.Asset_Model_No__c,
|
Maintenance_Contract__r.MonthSize__c,
|
Maintenance_Contract__r.id
|
from
|
Repair__c
|
where
|
// 满足产品型号
|
Delivered_Product__r.product2.Asset_Model_No__c in:uniqueAssetModelNo
|
// 时间区间
|
and Agreed_Date__c != null
|
and Agreed_Date__c <= :this.timeTwo
|
and Agreed_Date__c >= :this.timeThr
|
and Status2__c !='00.删除'
|
and Status2__c !='00.取消'
|
// 过去三年有合同的
|
and ISPassHaveMc__c = true
|
];
|
|
List<Id> AssList = new List<Id>();
|
List<Asset> updateAss = new List<Asset>();
|
Map<String,Decimal> NotoAssSize = new Map<String,Decimal>();
|
// 过去3年
|
Map<String, Decimal> ThreeYearPriceSumMap = new Map<String, Decimal>();
|
for (Repair__c Rpc : ThreeyearList) {
|
// 修理原价
|
if (ThreeYearPriceSumMap.containsKey(Rpc.Delivered_Product__r.product2.Asset_Model_No__c)) {
|
Decimal priceAssAll = ThreeYearPriceSumMap.get(Rpc.Delivered_Product__r.product2.Asset_Model_No__c);
|
priceAssAll += Rpc.Repair_List_Price_formula__c;
|
ThreeYearPriceSumMap.put(Rpc.Delivered_Product__r.product2.Asset_Model_No__c,priceAssAll);
|
}else{
|
ThreeYearPriceSumMap.put(Rpc.Delivered_Product__r.product2.Asset_Model_No__c,Rpc.Repair_List_Price_formula__c);
|
}
|
// 换算月数
|
// String index = Rpc.Delivered_Product__r.id+':'+Rpc.Maintenance_Contract__r.id;
|
// String[] parts = index.split(':');
|
// String part1 = parts[0]; // 第一部分 设备id
|
// String part2 = parts[1]; // 第二部分 维修合同号
|
|
// if (NotoAssSize.containsKey(Rpc.Delivered_Product__r.product2.Asset_Model_No__c)) {
|
// AssMCData assmc = NotoAssSize.get(Rpc.Delivered_Product__r.product2.Asset_Model_No__c);
|
// if (!assmc.asaMC.contains(index)) {
|
// Double monthSizeAll = assmc.MonthSize;
|
// if(Rpc.Maintenance_Contract__r.MonthSize__c != -1){
|
// monthSizeAll += Rpc.Maintenance_Contract__r.MonthSize__c;
|
// assmc.MonthSize = monthSizeAll;
|
// assmc.asaMC.add(index);
|
// NotoAssSize.put(Rpc.Delivered_Product__r.product2.Asset_Model_No__c,assmc);
|
// }
|
// }
|
// }else{
|
// NotoAssSize.put(Rpc.Delivered_Product__r.product2.Asset_Model_No__c,new AssMCData(index,Rpc.Maintenance_Contract__r.MonthSize__c));
|
// }
|
|
}
|
|
for (Asset ass : assetList) {
|
// 按照产品型号,更新对应的设备字段(过去3年有合同的修理实绩和,对应型号的设备总数换算为12个月的件数)
|
if (ThreeYearPriceSumMap.get(ass.product2.Asset_Model_No__c)!=null) {
|
Asset assOne = new Asset();
|
assOne.id = ass.id;
|
assOne.Three_Years_HasMcAssetModelNo__c = ThreeYearPriceSumMap.get(ass.product2.Asset_Model_No__c);
|
updateAss.add(assOne);
|
}
|
}
|
|
return updateAss;
|
//第四分类
|
}else if (type==1) {
|
List<Id> AssList = new List<Id>();
|
List<Asset> updateAss = new List<Asset>();
|
Map<String,Decimal> NotoAssSize = new Map<String,Decimal>();
|
// 过去3年
|
Map<String, Decimal> ThreeYearPriceSumMap = new Map<String, Decimal>();
|
|
|
List<Repair__c> ThreeyearList = new List<Repair__c>();
|
ThreeyearList = [
|
select
|
Repair_List_Price_formula__c,
|
Delivered_Product__c,
|
Delivered_Product__r.id,
|
Delivered_Product__r.Category4__c,
|
Maintenance_Contract__r.MonthSize__c,
|
Maintenance_Contract__r.id
|
from
|
Repair__c
|
where
|
// 满足产品型号
|
Delivered_Product__r.Category4__c in:uniqueCategories
|
// 时间区间
|
and Agreed_Date__c != null
|
and Agreed_Date__c <= :this.timeTwo
|
and Agreed_Date__c >= :this.timeThr
|
and Status2__c !='00.删除'
|
and Status2__c !='00.取消'
|
// 过去三年有合同的
|
and ISPassHaveMc__c = true
|
];
|
|
|
for (Repair__c Rpc : ThreeyearList) {
|
// 修理原价
|
if (ThreeYearPriceSumMap.containsKey(Rpc.Delivered_Product__r.Category4__c)) {
|
Decimal priceAssAll = ThreeYearPriceSumMap.get(Rpc.Delivered_Product__r.Category4__c);
|
priceAssAll += Rpc.Repair_List_Price_formula__c;
|
ThreeYearPriceSumMap.put(Rpc.Delivered_Product__r.Category4__c,priceAssAll);
|
}else{
|
ThreeYearPriceSumMap.put(Rpc.Delivered_Product__r.Category4__c,Rpc.Repair_List_Price_formula__c);
|
}
|
// 换算月数
|
// String index = Rpc.Delivered_Product__r.id+':'+Rpc.Maintenance_Contract__r.id;
|
// String[] parts = index.split(':');
|
// String part1 = parts[0]; // 第一部分 设备id
|
// String part2 = parts[1]; // 第二部分 维修合同号
|
// if (NotoAssSize.containsKey(Rpc.Delivered_Product__r.Category4__c)) {
|
// AssMCData assmc = NotoAssSize.get(Rpc.Delivered_Product__r.Category4__c);
|
// if (!assmc.asaMC.contains(index)) {
|
// Double monthSizeAll = assmc.MonthSize;
|
// if(Rpc.Maintenance_Contract__r.MonthSize__c != -1){
|
// monthSizeAll += Rpc.Maintenance_Contract__r.MonthSize__c;
|
// assmc.MonthSize = monthSizeAll;
|
// assmc.asaMC.add(index);
|
// NotoAssSize.put(Rpc.Delivered_Product__r.Category4__c,assmc);
|
// }
|
// }
|
// }else{
|
// NotoAssSize.put(Rpc.Delivered_Product__r.Category4__c,new AssMCData(index,Rpc.Maintenance_Contract__r.MonthSize__c));
|
// }
|
}
|
for (Asset ass : assetList) {
|
// 按照第四分类,更新对应的设备字段(过去3年有合同的修理实绩和,对应型号的设备总数换算为12个月的件数)
|
if (ThreeYearPriceSumMap.get(ass.Category4__c)!=null) {
|
Asset assOne = new Asset();
|
assOne.id = ass.id;
|
assOne.Three_Years_HasMcCategory4__c = ThreeYearPriceSumMap.get(ass.Category4__c);
|
updateAss.add(assOne);
|
}
|
}
|
return updateAss;
|
}else{
|
return null;
|
}
|
}
|
|
}
|