// 预测消费率计算对应型号的过去3年期间有合同的的全国合同修理金额合计 global class ComputeLTYRepairBatch2 implements Database.Batchable { public String query; public String year; public Date timeOne ; public Date timeTwo; public Date timeThr; public Date timeFour; public List targetList= new List(); public List asslist= new List(); global ComputeLTYRepairBatch2() { } global ComputeLTYRepairBatch2(List targetList) { this.query = query; this.targetList = targetList; this.timeTwo = date.newInstance( 2023, 4, 31 ); this.timeThr = date.newInstance( 2020, 4, 1 ); this.timeFour = this.timeTwo.addDays(1); } global Database.QueryLocator start(Database.BatchableContext bc) { system.debug('targetList=='+targetList); //查询符合条件的所有设备 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 (targetList.size()>0 && targetList !=null) { query += ' and id in :targetList'; } return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, list assetList) { // 分类计算,分别赋值,第四分类和产品型号 List assList1 = ComputeLRepair(1,assetList); List 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) { } private List ComputeLRepair(Integer type,list assetList) { List uniqueAssetModelNo = new List(); List uniqueCategories = new List(); 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 mcaList = [select id ,Maintenance_Contract__r.MonthSize__c,Asset__r.product2.Asset_Model_No__c from Maintenance_Contract_Asset__c where ((Maintenance_Contract_StarDate__c>= :this.timeThr and Maintenance_Contract_StarDate__c < :this.timeFour) or (Contract_End_Date__c>= :this.timeThr and Contract_End_Date__c < :this.timeFour)) and Maintenance_Contract__r.MonthSize__c != -1 and Maintenance_Contract__r.MonthSize__c != 0 and Asset__r.product2.Asset_Model_No__c in :uniqueAssetModelNo]; List updateAss = new List(); Map NotoAssSize = new Map(); // 过去3年 for (Maintenance_Contract_Asset__c mca:mcaList) { if (NotoAssSize.containsKey(mca.Asset__r.product2.Asset_Model_No__c)) { Decimal ms = NotoAssSize.get(mca.Asset__r.product2.Asset_Model_No__c); ms += mca.Maintenance_Contract__r.MonthSize__c; NotoAssSize.put(mca.Asset__r.product2.Asset_Model_No__c,ms); }else{ NotoAssSize.put(mca.Asset__r.product2.Asset_Model_No__c,mca.Maintenance_Contract__r.MonthSize__c); } } for (Asset ass : assetList) { // 按照产品型号,更新对应的设备字段(过去3年有合同的修理实绩和,对应型号的设备总数换算为12个月的件数) Asset assOne = new Asset(); assOne.id = ass.id; if (NotoAssSize.get(ass.product2.Asset_Model_No__c) !=null) { assOne.MonthSizeAssetModelNo__c = NotoAssSize.get(ass.product2.Asset_Model_No__c); } updateAss.add(assOne); } return updateAss; //第四分类 }else if (type==1) { List mcaList = [select id ,Maintenance_Contract__r.MonthSize__c,Asset__r.Category4__c from Maintenance_Contract_Asset__c where ((Maintenance_Contract_StarDate__c>= :this.timeThr and Maintenance_Contract_StarDate__c < :this.timeFour) or (Contract_End_Date__c>= :this.timeThr and Contract_End_Date__c < :this.timeFour)) and Maintenance_Contract__r.MonthSize__c != -1 and Maintenance_Contract__r.MonthSize__c != 0 and Asset__r.Category4__c in:uniqueCategories]; List updateAss = new List(); Map NotoAssSize = new Map(); for (Maintenance_Contract_Asset__c mca:mcaList) { if (NotoAssSize.containsKey(mca.Asset__r.Category4__c)) { Decimal ms = NotoAssSize.get(mca.Asset__r.Category4__c); ms += mca.Maintenance_Contract__r.MonthSize__c; NotoAssSize.put(mca.Asset__r.Category4__c,ms); }else{ NotoAssSize.put(mca.Asset__r.Category4__c,mca.Maintenance_Contract__r.MonthSize__c); } } for (Asset ass : assetList) { // 按照第四分类,更新对应的设备字段(过去3年有合同的修理实绩和,对应型号的设备总数换算为12个月的件数) Asset assOne = new Asset(); assOne.id = ass.id; if (NotoAssSize.get(ass.Category4__c) !=null) { assOne.MonthSizeCategory4__c = NotoAssSize.get(ass.Category4__c); } updateAss.add(assOne); } return updateAss; }else{ return null; } } }