global class ConsumptionRateWebService2 { @AuraEnabled WebService static String ToConsumptionRate(String mceId){ try{ Decimal Totalnumerator = 0; Date CreateTime; Integer AssCount = 0; Decimal EstimateAssetCnt; Decimal Contract_Range; Decimal Consumption_rate_Forecast_fenzi; Boolean CategoryOfNumberAsset; // >=100为true ,<100为false List mcaeList = [SELECT Id,Asset__c,Asset__r.id,Asset__r.CurrentContract_F__c,Asset__r.CurrentContract_F__r.Contract_Range__c, Asset__r.InstallDate,Asset__r.Asset_Year_Txt__c, Asset__r.CurrentContract_F__r.Contract_Start_Date__c, Asset__r.CurrentContract_F__r.Contract_End_Date__c, Asset__r.LastSFDCArankRepairReturn_day__c,Maintenance_Contract_Estimate__r.Contract_Range__c ,Maintenance_Contract_Estimate__r.CreatedDate ,Asset__r.Category4__c, Asset__r.product2Id,Asset__r.product2.Asset_Model_No__c,Asset__r.product2.Category4__c, Maintenance_Contract_Estimate__r.CategoryOfNumberAsset__c, Maintenance_Contract_Estimate__r.Consumption_rate_Forecast_fenzi__c, Maintenance_Contract_Estimate__r.Estimate_Asset_Cnt__c,Asset__r.AccountId,Asset__r.Hospital__c, Product_Manual__r.id,Product_Manual__r.Asset_Model_No__c,Product_Manual__r.Category4__c FROM Maintenance_Contract_Asset_Estimate__c WHERE Maintenance_Contract_Estimate__c = :mceId]; Maintenance_Contract__c mcOne =[select id from Maintenance_Contract__c where Estimation_Id__c=:mceId and Estimation_Id_status__c = '批准' limit 1]; Double TotalPriceInMc = 0; Decimal TotalPrice = 0; Map updateAss = new Map(); Map updatePro = new Map(); List updateMCAELi = new List(); List hasNoMcAssets = new List(); List hasMcAssets = new List(); List checkHasmcAss = new List(); Set ProAssetModelNo = new Set(); Set ProCategory4 = new Set(); Map assToMcMap = new Map(); Map assToDay = new Map(); Map assTofeni = new Map(); Map assTotalPriceInMcMap = new Map(); Map mcInfo = new Map(); Map assInfo = new Map(); Map asstoRepair = new Map(); Map proInfo = new Map(); if (mcOne !=null) { List mcaList = [select id,asset__r.id ,LastMContract__c,LastMContract__r.Contract_Start_Date__c ,LastMContract__r.Contract_End_Date__c,LastMContract__r.Contract_Range__c from Maintenance_Contract_Asset__c where Maintenance_Contract__c = :mcOne.id ]; for (Maintenance_Contract_Asset__c mca:mcaList) { if (mca.LastMContract__c!=null) { assToMcMap.put(mca.Asset__r.id,mca.LastMContract__c); assTotalPriceInMcMap.put(mca.Asset__r.id,TotalPriceInMc); hasMcAssets.add(mca.Asset__r.id); AssCount++; }else{ if (mca.Asset__r.id !=null) { hasNoMcAssets.add(mca.Asset__r.id); } } // 维修合同信息 if (!mcInfo.containsKey(mca.LastMContract__c)) { Maintenance_Contract__c mc1 = new Maintenance_Contract__c(); mc1.id = mca.LastMContract__c; mc1.Contract_Start_Date__c = mca.LastMContract__r.Contract_Start_Date__c; mc1.Contract_End_Date__c = mca.LastMContract__r.Contract_End_Date__c; mc1.Contract_Range__c = mca.LastMContract__r.Contract_Range__c; mcInfo.put(mc1.id,mc1); } } } for (Maintenance_Contract_Asset_Estimate__c mcae: mcaeList) { if (CreateTime==null) { CreateTime = mcae.Maintenance_Contract_Estimate__r.CreatedDate.date(); } if (EstimateAssetCnt==null) { EstimateAssetCnt = mcae.Maintenance_Contract_Estimate__r.Estimate_Asset_Cnt__c; } if (Contract_Range==null) { Contract_Range = mcae.Maintenance_Contract_Estimate__r.Contract_Range__c; } // if (Consumption_rate_Forecast_fenzi==null) { // Consumption_rate_Forecast_fenzi = mcae.Maintenance_Contract_Estimate__r.Consumption_rate_Forecast_fenzi__c; // } if (CategoryOfNumberAsset==null) { if (mcae.Maintenance_Contract_Estimate__r.CategoryOfNumberAsset__c == '按第四分类') { CategoryOfNumberAsset = false; } if (mcae.Maintenance_Contract_Estimate__r.CategoryOfNumberAsset__c == '按产品型号') { CategoryOfNumberAsset = true; } } // if (mcae.Asset__r.CurrentContract_F__c!=null) { // assToMcMap.put(mcae.Asset__r.id,mcae.Asset__r.CurrentContract_F__c); // assTotalPriceInMcMap.put(mca.Asset__r.id,TotalPriceInMc); // hasMcAssets.add(mcae.Asset__r.id); // AssCount++; // }else{ // if (mcae.Asset__r.id !=null) { // hasNoMcAssets.add(mcae.Asset__r.id); // } // if (mcae.Product_Manual__r.id !=null) { // hasNoMcAssets.add(mcae.Product_Manual__r.id); // ProAssetModelNo.add(mcae.Product_Manual__r.Asset_Model_No__c); // ProCategory4.add(mcae.Product_Manual__r.Category4__c); // } // } // 保有设备信息 if (!assInfo.containsKey(mcae.Asset__r.id)) { Asset ass = new Asset(); Product2 Pro2 = new Product2(); pro2.id = mcae.Asset__r.product2Id; pro2.Asset_Model_No__c = mcae.Asset__r.product2.Asset_Model_No__c; pro2.Category4__c = mcae.Asset__r.product2.Category4__c; ass.id = mcae.Asset__r.id; ass.InstallDate = mcae.Asset__r.InstallDate; ass.Asset_Year_Txt__c = mcae.Asset__r.Asset_Year_Txt__c; ass.LastSFDCArankRepairReturn_day__c = mcae.Asset__r.LastSFDCArankRepairReturn_day__c; ass.product2 = pro2; ass.AccountId = mcae.Asset__r.AccountId; ass.Hospital__c = mcae.Asset__r.Hospital__c; assInfo.put(ass.id,ass); } // 产品信息 if (!proInfo.containsKey(mcae.Product_Manual__r.id)) { Product2 pro2 = new Product2(); pro2.id = mcae.Product_Manual__r.id; pro2.Asset_Model_No__c = mcae.Product_Manual__r.Asset_Model_No__c; pro2.Category4__c = mcae.Product_Manual__r.Category4__c; proInfo.put(pro2.id,pro2); } } // List assmodelList = [select id,MonthSizeAssetModelNo__c,Three_Years_HasMcAssetModelNo__c from asset where product2.Asset_Model_No__c in :ProAssetModelNo group by product2.Asset_Model_No__c]; if (assToMcMap.keySet()!=null) { List reps = [SELECT id,Failure_Occurrence_Date__c,Repair_List_Price_formula__c,Maintenance_Contract__c,Maintenance_Contract__r.id,Delivered_Product__c, Maintenance_Contract__r.Contract_Start_Date__c,Maintenance_Contract__r.Contract_Range__c,Maintenance_Contract__r.Contract_End_Date__c FROM Repair__c WHERE Delivered_Product__c in :assTotalPriceInMcMap.keySet() and Status2__c !='00.删除' and Status2__c !='00.取消' and Failure_Occurrence_Date__c >= :CreateTime.addYears(-3) order by Delivered_Product__c,Failure_Occurrence_Date__c desc]; assTotalPriceInMcMap.clear(); Set assetIdsWithRelatedRepairs = new Set(); for (Repair__c rep :reps) { if (rep.Delivered_Product__c=='02i10000000fo9U') { system.debug('上一期合同=='+assToMcMap.get(rep.Delivered_Product__c)); system.debug('修理合同=='+rep.Maintenance_Contract__c); } if (rep.Maintenance_Contract__c == assToMcMap.get(rep.Delivered_Product__c)) { if (!asstoRepair.containsKey(rep.Delivered_Product__c) && rep.Maintenance_Contract__r.Contract_Start_Date__c !=null) { asstoRepair.put(rep.Delivered_Product__c,rep.Maintenance_Contract__r.Contract_Start_Date__c); } assetIdsWithRelatedRepairs.add(rep.Delivered_Product__c); if (rep.Delivered_Product__c=='02i10000000fo9U') { system.debug('修理=='+rep.id); system.debug('设备=='+rep.Delivered_Product__c); } Boolean IsCanJoin = true; Date AssetDate; Id assId = rep.Delivered_Product__c; Maintenance_Contract__c mc = mcInfo.get(rep.Maintenance_Contract__c); Asset ass = assInfo.get(assId); if (ass !=null) { if (ass.InstallDate!=null) { AssetDate = ass.InstallDate; }else{ if (ass.Asset_Year_Txt__c!=null) { AssetDate = Date.valueOf(ass.Asset_Year_Txt__c); } } if (AssetDate!=null) { //如果参保设备发生的合同修理是在安装日(有安装日取安装日,无安装日取【设备日期】)1年以内 if (rep.Failure_Occurrence_Date__c>=AssetDate && rep.Failure_Occurrence_Date__c<=AssetDate.addyears(1)) { IsCanJoin = false; assTotalPriceInMcMap.remove(assId); } //如果合同修理正好只有1个小于等于一年期的合同,且合同修理和安装日期之后的一年和合同期间重合了半年以上,【有效年度的合同修理合计】的情况等同于【否】 if (rep.Maintenance_Contract__r.Contract_Range__c !=null && rep.Maintenance_Contract__r.Contract_Range__c<=12) { if (areIntervalsOverlapHalfYear(rep.Maintenance_Contract__r.Contract_Start_Date__c,rep.Maintenance_Contract__r.Contract_End_Date__c,AssetDate.addyears(1),rep.Failure_Occurrence_Date__c)) { if(!hasNoMcAssets.contains(assId)){ hasNoMcAssets.add(assId); } IsCanJoin = false; assTotalPriceInMcMap.remove(assId); // system.debug('【有效年度的合同修理合计】的情况等同于【否】'+assId); // system.debug('【有效年度的合同修理合计】的情况等同于【否】的型号'+ass.product2.Asset_Model_No__c); } } } if (rep.Delivered_Product__c=='02i10000000fo9U') { system.debug('【IsCanJoin】'+IsCanJoin); } //【有效年度的合同修理合计】计算 if (IsCanJoin) { if (assTotalPriceInMcMap.containsKey(assId)) { Double price1 = assTotalPriceInMcMap.get(assId); price1 += rep.Repair_List_Price_formula__c; if (assId=='02i10000000fo9U') { system.debug('【IsCanJoin=true进入集合】'+assId); } assTotalPriceInMcMap.put(assId,price1); }else{ if (assId=='02i10000000fo9U') { system.debug('修理添加'+rep.Repair_List_Price_formula__c); } assTotalPriceInMcMap.put(assId,rep.Repair_List_Price_formula__c); } // 经历天数 Double ElapsedDays; Date start = asstoRepair.get(assId); if (start!=null) { Double time1 = (Double)(start.daysBetween(CreateTime)); if (time1 >0) { if (time1>=1095) { system.debug('大于3年'); ElapsedDays = 1095.0; }else{ ElapsedDays = time1; } if (assId=='02i10000000fo9U') { system.debug('ElapsedDays=='+ElapsedDays); system.debug('start=='+start); system.debug('CreateTime=='+CreateTime); } String mcassStr = assId+':'+rep.Maintenance_Contract__r.Id; if (!assToDay.containsKey(assId)) { assToDay.put(assId,ElapsedDays); checkHasmcAss.add(mcassStr); }else{ if (!checkHasmcAss.contains(mcassStr)) { Double days = assToDay.get(assId); days += ElapsedDays; assToDay.put(assId,days); checkHasmcAss.add(mcassStr); } } }else{ hasNoMcAssets.add(assId); } } } } } } for (String assetid : hasMcAssets) { if (!assetIdsWithRelatedRepairs.contains(assetid)) { if (assetid=='02i10000000fo9U') { system.debug('assetid加入hasNoMcAssets=='+assetid); } hasNoMcAssets.add(assetid); } } for (Id assid:assTotalPriceInMcMap.keySet()) { // 消费率单个分子 Decimal numerator=0; Id mcId = assToMcMap.get(assid); // Maintenance_Contract__c mc = mcInfo.get(mcId); // 经历天数 if (Contract_Range!=null) { Asset ass = new Asset(); ass.id = assid; Decimal price = assTotalPriceInMcMap.get(assid); Double ElapsedDay; if (assToDay.containsKey(assid)) { ass.ExperienceDays__c = assToDay.get(assid); ElapsedDay = assToDay.get(assid); } if (ElapsedDay!=null && ElapsedDay!=0) { numerator = (price/ElapsedDay*30*Contract_Range); } Totalnumerator += numerator; ass.TotalPriceInMc__c = price; if (assid=='02i10000000fo9U') { system.debug('设备经历天数='+ElapsedDay); system.debug('有效年度的修理合计='+price); system.debug('合同月数='+Contract_Range); system.debug('情况等同于【是】加入'+assid); system.debug('预测消费率分子='+numerator); } if (updateAss.get(ass.id) == null) { updateAss.put(ass.id,ass); } assTofeni.put(ass.id,numerator); } } } system.debug('参保设备总数=='+EstimateAssetCnt); system.debug('有合同的设备总数=='+AssCount); // if(CategoryOfNumberAsset == null){ if (AssCount<100) { CategoryOfNumberAsset = false; } if (AssCount>=100) { CategoryOfNumberAsset = true; } // } TotalPrice += Totalnumerator; List MDMOrCategory4List = new List(); Map AccIdMap = new Map(); Map IsIn3YearMap = new Map(); Map Category4Map = new Map(); Map Category4MapNum = new Map(); Map MDMMap = new Map(); Map MDMMapNum = new Map(); for (Id assid : hasNoMcAssets) { Asset ass = assInfo.get(assid); if ( ass!=null && ass.Hospital__c !=null) { AccIdMap.put(ass.id,ass.Hospital__c); } } // ComputeLTYRepair(hasNoMcAssets,mceId,MDMOrCategory4List,CategoryOfNumberAsset); List mcaeList3 = [SELECT id,Asset__r.product2.Category4__c,Asset__r.product2.Asset_Model_No__c ,Asset__r.Three_Years_HasMcAssetModelNo__c ,Asset__r.Three_Years_HasMcCategory4__c ,Asset__r.MonthSizeAssetModelNo__c ,Asset__r.MonthSizeCategory4__c FROM Maintenance_Contract_Asset_Estimate__c WHERE Maintenance_Contract_Estimate__c = :mceId ]; List mcList = [SELECT min(Contract_Start_Date__c) minStartDate ,Hospital__c FROM Maintenance_Contract__c WHERE Hospital__c IN :AccIdMap.values() group by Hospital__c]; for (AggregateResult Rpc : mcList) { id hpid = String.valueOf(Rpc.get('Hospital__c')); Date contractStartDate = (Date)Rpc.get('minStartDate'); Date today = Date.today(); //所在医院在我司累计签约达到了3年以上 if (today.addyears(-3)>contractStartDate && hpid!=null) { IsIn3YearMap.put(hpid,true); }else{ IsIn3YearMap.put(hpid,false); } } system.debug('是否按产品略称=='+CategoryOfNumberAsset); //按第四分类计算 // if (!CategoryOfNumberAsset) { for (Maintenance_Contract_Asset_Estimate__c mcae :mcaeList3) { // Double mcNumAll; if (!Category4Map.containsKey(mcae.Asset__r.product2.Category4__c)) { Category4Map.put(mcae.Asset__r.product2.Category4__c,mcae.Asset__r.Three_Years_HasMcCategory4__c); } if (!Category4MapNum.containsKey(mcae.Asset__r.product2.Category4__c)) { Category4MapNum.put(mcae.Asset__r.product2.Category4__c,mcae.Asset__r.MonthSizeCategory4__c ); } } // } //按产品略称 产品型号(MDM) if (CategoryOfNumberAsset) { for (Maintenance_Contract_Asset_Estimate__c mcae :mcaeList3) { // Double mcNumAll; if (!MDMMap.containsKey(mcae.Asset__r.product2.Asset_Model_No__c)) { MDMMap.put(mcae.Asset__r.product2.Asset_Model_No__c,mcae.Asset__r.Three_Years_HasMcAssetModelNo__c); } if (!MDMMapNum.containsKey(mcae.Asset__r.product2.Asset_Model_No__c)) { MDMMapNum.put(mcae.Asset__r.product2.Asset_Model_No__c,mcae.Asset__r.MonthSizeAssetModelNo__c); } } } system.debug('按第四分类金额=='+Category4Map); system.debug('按第四分类月份=='+Category4MapNum); // for(String sn :MDMMap.keySet()){ // system.debug('按产品略称='+sn+'***金额为=='+MDMMap.get(sn)); // } // for(String sn :MDMMapNum.keySet()){ // system.debug('按产品略称='+sn+'***换算为12个月件数为=='+MDMMapNum.get(sn)); // } Decimal hasNoMcAssetsPriceAll=0; for (Id assid : hasNoMcAssets) { Asset ass = assInfo.get(assid); Product2 pro = proInfo.get(assid); if (assid=='02i100000029zknAAA') { system.debug('计算开始=='+assid); } Decimal AverageContractRepair=0; Decimal hasNoMcAssetsPrice=0; Boolean toBreak = false; Boolean toBreak2 = false; if (ass !=null) { //申请合同的设备参保件数大于100件以上,且所在医院在我司累计签约达到了3年以上,计算出按第四分类的每个分类平均修理金额, if (EstimateAssetCnt>100) { Id accId = AccIdMap.get(assid); if (IsIn3YearMap.containskey(accId)) { Boolean IsIn3Year = IsIn3YearMap.get(accId); if (IsIn3Year) { system.debug('ass.product2.Category4__c=='+ass.product2.Category4__c); if (Category4Map.get(ass.product2.Category4__c) !=null && Category4MapNum.get(ass.product2.Category4__c) !=null) { AverageContractRepair = Category4Map.get(ass.product2.Category4__c)/Category4MapNum.get(ass.product2.Category4__c); system.debug('按签约达到了3年以上计算=='+ass.product2.Category4__c); } } } toBreak = true; } if (!toBreak) { //参保设备的上次大中修理日期是在报价制作日之前1年以内,用0赋值,如果设备是安装在1年之内,用0赋值。 if ((ass.LastSFDCArankRepairReturn_day__c!=null&&CreateTime.addyears(-1)ass.LastSFDCArankRepairReturn_day__c) ||(ass.InstallDate!=null&&CreateTime.addyears(-1)ass.InstallDate)) { AverageContractRepair = 0; toBreak2 = true; system.debug('参保设备的上次大中修理日期是在报价制作日之前1年以内计算=='+ass.product2.Asset_Model_No__c+':'+ass.id); } if (!toBreak2) { //如果有合同的设备总数小于100,则按第四分类计算,取对应型号的过去3年期间有合同的的全国合同修理金额合计/对应型号的设备总数换算为12个月的件数 if (!CategoryOfNumberAsset) { if (Category4Map.get(ass.product2.Category4__c) !=null && Category4MapNum.get(ass.product2.Category4__c) !=null) { AverageContractRepair = Category4Map.get(ass.product2.Category4__c)/Category4MapNum.get(ass.product2.Category4__c); } if (assid=='02i100000029zknAAA') { system.debug('第四分类分子=='+Category4Map.get(ass.product2.Category4__c)); system.debug('第四分类分母=='+Category4MapNum.get(ass.product2.Category4__c)); system.debug('按第四分类计算=='+ass.product2.Category4__c); } } //如果有合同的设备总数大于等于100,则按产品略称,取对应型号的过去3年期间的有合同的的全国合同修理金额合计/对应型号的设备总数换算为12个月的件数 if (CategoryOfNumberAsset) { if (MDMMap.get(ass.product2.Asset_Model_No__c) !=null && MDMMapNum.get(ass.product2.Asset_Model_No__c) !=null) { AverageContractRepair = MDMMap.get(ass.product2.Asset_Model_No__c)/MDMMapNum.get(ass.product2.Asset_Model_No__c); } if (assid=='02i100000029zknAAA') { system.debug('产品略称分子=='+MDMMap.get(ass.product2.Asset_Model_No__c)); system.debug('产品略称分母=='+MDMMapNum.get(ass.product2.Asset_Model_No__c)); system.debug('按产品略称计算=='+ass.product2.Asset_Model_No__c); } } } } // system.debug('合同月数=='+Contract_Range); if (Contract_Range!=0 && Contract_Range!=null) { hasNoMcAssetsPrice = AverageContractRepair*Contract_Range; if (!assTofeni.containsKey(assid)) { assTofeni.put(assid,hasNoMcAssetsPrice); } if (assid=='02i100000029zknAAA') { system.debug('设备进入assTofeni==>'+assid); } }else{ system.debug('此设备的没有进assTofeni==>'+assid); system.debug('此设备的==Contract_Range>'+Contract_Range); } if (assid=='02i100000029zknAAA') { system.debug('此设备的hasNoMcAssetsPrice=='+hasNoMcAssetsPrice); } hasNoMcAssetsPriceAll += hasNoMcAssetsPrice; Double MonthSize=0; Double MonthSize2=0; Asset ass1 = new Asset(); ass1.id = assid; // if (CategoryOfNumberAsset && MDMMap.get(ass.product2.Asset_Model_No__c) !=null && MDMMapNum.get(ass.product2.Asset_Model_No__c) !=null) { // ass1.AverageContractRepair__c = MDMMap.get(ass.product2.Asset_Model_No__c)/MDMMapNum.get(ass.product2.Asset_Model_No__c); // } // if (!CategoryOfNumberAsset && Category4Map.get(ass.product2.Category4__c) !=null && Category4MapNum.get(ass.product2.Category4__c) !=null) { // ass1.AverageContractRepair__c = Category4Map.get(ass.product2.Category4__c)/Category4MapNum.get(ass.product2.Category4__c); // } ass1.AverageContractRepair__c = AverageContractRepair; ass1.TotalPriceInMc__c = 0; ass1.ExperienceDays__c = 0; // if (!CategoryOfNumberAsset && Category4MapNum.get(ass.product2.Category4__c)!=null) { // MonthSize = Category4MapNum.get(ass.product2.Category4__c); // } // if (CategoryOfNumberAsset && MDMMapNum.get(ass.product2.Asset_Model_No__c)!=null) { // MonthSize2 = MDMMapNum.get(ass.product2.Asset_Model_No__c); // } // if (MonthSize != 0 ) { // ass1.MonthSizeCategory4__c = MonthSize; // } // if (MonthSize2 != 0 ) { // ass1.MonthSizeAssetModelNo__c = MonthSize2; // } // system.debug('情况等同于【否】加入'+assid); if (updateAss.get(ass1.id) == null) { updateAss.put(ass1.id,ass1); } } if (pro !=null) { //如果有合同的设备总数小于100,则按第四分类计算,取对应型号的过去3年期间有合同的的全国合同修理金额合计/对应型号的设备总数换算为12个月的件数 if (!CategoryOfNumberAsset) { if (Category4Map.get(pro.Category4__c) !=null && Category4MapNum.get(pro.Category4__c) !=null) { AverageContractRepair = Category4Map.get(pro.Category4__c)/Category4MapNum.get(pro.Category4__c); } if (pro.id=='02i100000029zknAAA') { system.debug('第四分类分子=='+Category4Map.get(pro.Category4__c)); system.debug('第四分类分母=='+Category4MapNum.get(pro.Category4__c)); system.debug('按第四分类计算=='+pro.Category4__c); } } //如果有合同的设备总数大于等于100,则按产品略称,取对应型号的过去3年期间的有合同的的全国合同修理金额合计/对应型号的设备总数换算为12个月的件数 if (CategoryOfNumberAsset) { if (MDMMap.get(pro.Asset_Model_No__c) !=null && MDMMapNum.get(pro.Asset_Model_No__c) !=null) { AverageContractRepair = MDMMap.get(pro.Asset_Model_No__c)/MDMMapNum.get(pro.Asset_Model_No__c); } if (pro.id=='02i100000029zknAAA') { system.debug('产品略称分子=='+MDMMap.get(pro.Asset_Model_No__c)); system.debug('产品略称分母=='+MDMMapNum.get(pro.Asset_Model_No__c)); system.debug('按产品略称计算=='+pro.Asset_Model_No__c); } } // system.debug('合同月数=='+Contract_Range); if (Contract_Range!=0 && Contract_Range!=null) { hasNoMcAssetsPrice = AverageContractRepair/12*Contract_Range; assTofeni.put(pro.id,hasNoMcAssetsPrice); if (pro.id=='02i100000029zknAAA') { system.debug('产品进入assTofeni==>'+pro.id); } } if (pro.id=='02i100000029zknAAA') { system.debug('此产品的hasNoMcAssetsPrice=='+hasNoMcAssetsPrice); } hasNoMcAssetsPriceAll += hasNoMcAssetsPrice; product2 pro1 = new product2(); pro1.id = pro.id; pro1.AverageContractRepair__c = AverageContractRepair; if (updatePro.get(pro1.id) == null) { updatePro.put(pro1.id,pro1); } } } TotalPrice += hasNoMcAssetsPriceAll; Maintenance_Contract_Estimate__c mce = new Maintenance_Contract_Estimate__c(); mce.id = mceId; system.debug('以往报价分子为:'+Consumption_rate_Forecast_fenzi); // if (Consumption_rate_Forecast_fenzi!=null) { // mce.Consumption_rate_Forecast_fenzi__c = TotalPrice+Consumption_rate_Forecast_fenzi; // }else{ // mce.Consumption_rate_Forecast_fenzi__c = TotalPrice; // } if (CategoryOfNumberAsset) { mce.CategoryOfNumberAsset__c = '按产品型号'; } if (!CategoryOfNumberAsset) { mce.CategoryOfNumberAsset__c = '按第4分类'; } List McaeLi = new List(); for (Maintenance_Contract_Asset_Estimate__c mcae: mcaeList) { if (mcae.Asset__r.id=='02i100000029zknAAA') { system.debug('是否存在=='+assTofeni.containsKey(mcae.Asset__r.id)+mcae.Asset__r.id); } if (assTofeni.containsKey(mcae.Asset__r.id)) { Maintenance_Contract_Asset_Estimate__c mcae1= new Maintenance_Contract_Asset_Estimate__c(); mcae1.id = mcae.id; mcae1.Consumption_rate_Forecast_fenzi__c = assTofeni.get(mcae.Asset__r.id); if (!McaeLi.contains(mcae1)) { McaeLi.add(mcae1); } } if (assTofeni.containsKey(mcae.Product_Manual__r.id)) { Maintenance_Contract_Asset_Estimate__c mcae1= new Maintenance_Contract_Asset_Estimate__c(); mcae1.id = mcae.id; mcae1.Consumption_rate_Forecast_fenzi__c = assTofeni.get(mcae.Product_Manual__r.id); if (!McaeLi.contains(mcae1)) { McaeLi.add(mcae1); } } } system.debug('更新报价为=='+mce); update mce; if (updateAss.size()>0) { // system.debug('updateAss:::::' + updateAss); update updateAss.values(); } if (updatePro.size()>0) { // system.debug('updateAss:::::' + updateAss); update updatePro.values(); } if (McaeLi.size()>0) { system.debug('McaeLi:::::' + McaeLi); update McaeLi; } return '计算完成'; }catch(Exception e){ if (e.getMessage().contains(',') && e.getMessage().contains('[')){ String eMessage = e.getMessage(); Integer left = eMessage .indexof(',') + 1; Integer right = eMessage.lastIndexof('[')-2; return eMessage.substring(left,right); }else { return e.getMessage(); } } } public ConsumptionRateWebService2() {} // 计算是否重合半年 public static Boolean areIntervalsOverlapHalfYear(Date timeA, Date timeB, Date timeC, Date timeD) { // // 将日期转换为日期时间,时间部分设为最早或最晚的时间 // DateTime dateTimeA = DateTime.newInstance( timeA.year(), // timeA.month(), // timeA.day()); // DateTime dateTimeB = DateTime.newInstance(timeB.year(), // timeB.month(), // timeB.day()); // DateTime dateTimeC = DateTime.newInstance(timeC.year(), // timeC.month(), // timeC.day()); // DateTime dateTimeD = DateTime.newInstance(timeD.year(), // timeD.month(), // timeD.day()); // // 确保时间区间按照从小到大的顺序排列 // if (dateTimeA.getTime() > dateTimeB.getTime()) { // DateTime temp = dateTimeA; // dateTimeA = dateTimeB; // dateTimeB = temp; // } // if (dateTimeC.getTime() > dateTimeD.getTime()) { // DateTime temp = dateTimeC; // dateTimeC = dateTimeD; // dateTimeD = temp; // } // // 计算时间区间重叠部分的起始时间和结束时间 // DateTime overlapStart = dateTimeA.getTime() > dateTimeC.getTime() ? dateTimeA : dateTimeC; // DateTime overlapEnd = dateTimeB.getTime() < dateTimeD.getTime() ? dateTimeB : dateTimeD; // // 计算时间区间重叠部分的月份数 // Integer months = monthDifference(overlapStart, overlapEnd); // // 判断重叠的月份数是否超过半年 // return months >= 6; // 确保时间区间按照从小到大的顺序排列 if (timeA> timeB) { Date temp = timeA; timeA = timeB; timeB = temp; } if (timeC > timeD) { Date temp = timeC; timeC = timeD; timeD = temp; } // 计算时间区间重叠部分的起始时间和结束时间 Date overlapStart = timeA > timeC ? timeA : timeC; Date overlapEnd = timeB < timeD ? timeB : timeD; // 计算时间区间重叠部分的月份数 Integer days = overlapStart.daysBetween(overlapEnd); // 判断重叠的月份数是否超过半年 return days >= 180; } private static Integer monthDifference(Datetime startDate, Datetime endDate) { Integer startYear = startDate.year(); Integer startMonth = startDate.month(); Integer endYear = endDate.year(); Integer endMonth = endDate.month(); return (endYear - startYear) * 12 + (endMonth - startMonth); } // public static void ComputeLTYRepair(List hasNoMcAssets,String targetHospitalId,List MDMOrCategory4List, Boolean CategoryOfNumberAsset) { // system.debug('ComputeLTYRepair=====Start'); // List McaeList = new List(); // McaeList = [select id, asset__c,asset__r.product2.Asset_Model_No__c,asset__r.product2.Category4__c,Three_Years_Repair_Cost_HasMc_Text__c // from Maintenance_Contract_Asset_Estimate__c // where Maintenance_Contract_Estimate__c = :targetHospitalId and asset__c IN :hasNoMcAssets]; // // List AsList = new List(); // // Map McaecToAsset = new Map(); // // for (Maintenance_Contract_Asset_Estimate__c Mcaes : McaeList) { // // AsList.add(Mcaes.asset__c); // // McaecToAsset.put(Mcaes.id, Mcaes.asset__c); // // } // Maintenance_Contract_Estimate__c mcec = [select id, createdDate, Submit_quotation_day__c from Maintenance_Contract_Estimate__c where id = :targetHospitalId]; // //1年前维修实绩 // Date today = null; // Date LastThirdYearDate = null; // Decimal LastYearPriceForMCAEC = 0; // if (mcec.Submit_quotation_day__c != null && String.valueOf(mcec.Submit_quotation_day__c) != '') { // if (mcec.Submit_quotation_day__c.month() <4) { // today = mcec.Submit_quotation_day__c; // today = today.addYears(-2); // LastThirdYearDate = mcec.Submit_quotation_day__c; // LastThirdYearDate = LastThirdYearDate.addYears(-2); // }else{ // today = mcec.Submit_quotation_day__c; // today = today.addYears(-1); // LastThirdYearDate = mcec.Submit_quotation_day__c; // LastThirdYearDate = LastThirdYearDate.addYears(-1); // } // } else { // if (Date.valueOf(mcec.createdDate).month() < 4) { // today = Date.valueOf(mcec.createdDate); // today = today.addYears(-2); // LastThirdYearDate = Date.valueOf(mcec.createdDate); // LastThirdYearDate = LastThirdYearDate.addYears(-2); // }else{ // today = Date.valueOf(mcec.createdDate); // today = today.addYears(-1); // LastThirdYearDate = Date.valueOf(mcec.createdDate); // LastThirdYearDate = LastThirdYearDate.addYears(-1); // } // } // LastThirdYearDate = LastThirdYearDate.addYears(-3); // List ThreeyearList = new List(); // for(String mdm :MDMOrCategory4List){ // system.debug('本次有的产品略称有='+mdm); // } // // 过去3年修理实绩 // if (CategoryOfNumberAsset) { // ThreeyearList = [ // select // sum(Discount_Price_formula__c) SumPrice, // sum(Repair_Quotation_Id__r.sales_discount__c) sales_discount, // sum(Repair_Quotation_Id__r.Contract_target__c) contract_target, // sum(Repair_Quotation_Id__r.Loaner_repair__c) loaner_repair, // sum(Repair_Quotation_Id__r.long_term_insurance__c) long_term_insurance, // sum(Repair_Quotation_Id__r.Set_discount__c) set_discount, // sum(Repair_Quotation_Id__r.Servince_contract_discount_amount__c) sercince, // sum(Repair_Quotation_Id__r.long_term_insurance_MD__c) long_term_insuranceMD, // sum(Repair_Quotation_Id__r.Delivery_compensation__c) delivery, // sum(Repair_Quotation_Id__r.Other_discount__c) other, // AVG(Delivered_Product__r.Last_Years_Repair_Month__c) threeYearM, // Delivered_Product__c // from // Repair__c // where // Delivered_Product__r.product2.Asset_Model_No__c in:MDMOrCategory4List // and Agreed_Date__c != null // and Agreed_Date__c <= :today // and Agreed_Date__c > :LastThirdYearDate // and Status2__c !='00.删除' // and Status2__c !='00.取消' // and ISPassHaveMc__c = true // group by Delivered_Product__c // ]; // } // if (!CategoryOfNumberAsset) { // ThreeyearList = [ // select // sum(Discount_Price_formula__c) SumPrice, // sum(Repair_Quotation_Id__r.sales_discount__c) sales_discount, // sum(Repair_Quotation_Id__r.Contract_target__c) contract_target, // sum(Repair_Quotation_Id__r.Loaner_repair__c) loaner_repair, // sum(Repair_Quotation_Id__r.long_term_insurance__c) long_term_insurance, // sum(Repair_Quotation_Id__r.Set_discount__c) set_discount, // sum(Repair_Quotation_Id__r.Servince_contract_discount_amount__c) sercince, // sum(Repair_Quotation_Id__r.long_term_insurance_MD__c) long_term_insuranceMD, // sum(Repair_Quotation_Id__r.Delivery_compensation__c) delivery, // sum(Repair_Quotation_Id__r.Other_discount__c) other, // AVG(Delivered_Product__r.Last_Years_Repair_Month__c) threeYearM, // Delivered_Product__c // from // Repair__c // where // Delivered_Product__r.Category4__c in:MDMOrCategory4List // and Agreed_Date__c != null // and Agreed_Date__c <= :today // and Agreed_Date__c > :LastThirdYearDate // and Status2__c !='00.删除' // and Status2__c !='00.取消' // and ISPassHaveMc__c = true // group by Delivered_Product__c // ]; // } // List AssList = new List(); // Map NotoAss = new Map(); // Map NotoAssSize = new Map(); // // 过去3年 // Map ThreeYearPriceSumMap = new Map(); // for (AggregateResult Rpc : ThreeyearList) { // Id idf = String.valueOf(Rpc.get('Delivered_Product__c')); // AssList.add(idf); // Decimal Defir = sumPrice1(Rpc); // system.debug('idf=='+idf); // system.debug('过去3年有合同的修理实绩=='+Defir); // ThreeYearPriceSumMap.put(idf, Defir); // } // List assList2 = [select id,product2.Asset_Model_No__c,product2.Category4__c,CurrentContract_F__r.Contract_Range__c from Asset where id in :AssList]; // for (Asset ass : assList2) { // if (!CategoryOfNumberAsset) { // if (ThreeYearPriceSumMap.get(ass.id) != null) { // if (!NotoAss.containsKey(ass.product2.Category4__c)) { // NotoAss.put(ass.product2.Category4__c,ThreeYearPriceSumMap.get(ass.id)); // }else{ // Decimal priceAssAll = NotoAss.get(ass.product2.Category4__c); // priceAssAll += ThreeYearPriceSumMap.get(ass.id); // NotoAss.put(ass.product2.Category4__c,priceAssAll); // } // if (ass.CurrentContract_F__r.Contract_Range__c!=null) { // Decimal contractRange = ass.CurrentContract_F__r.Contract_Range__c; // double divided = contractRange / 12; // double rounded; // // 判断小数位是否为 0.5,如果是,则不参与四舍五入 // if (String.valueOf(divided).endsWith('.5')) { // rounded = divided; // } else { // rounded = Math.round(divided); // } // if (!NotoAssSize.containsKey(ass.product2.Category4__c)) { // NotoAssSize.put(ass.product2.Category4__c,rounded); // }else{ // if (NotoAssSize.get(ass.product2.Category4__c)!=null) { // Double mcNumAll = NotoAssSize.get(ass.product2.Category4__c); // mcNumAll += rounded; // NotoAssSize.put(ass.product2.Category4__c,mcNumAll); // } // } // } // } // } // if (CategoryOfNumberAsset) { // if (ThreeYearPriceSumMap.get(ass.id) != null) { // if (!NotoAss.containsKey(ass.product2.Asset_Model_No__c)) { // NotoAss.put(ass.product2.Asset_Model_No__c,ThreeYearPriceSumMap.get(ass.id)); // }else{ // Decimal priceAssAll = NotoAss.get(ass.product2.Asset_Model_No__c); // priceAssAll += ThreeYearPriceSumMap.get(ass.id); // NotoAss.put(ass.product2.Asset_Model_No__c,priceAssAll); // } // if (ass.CurrentContract_F__r.Contract_Range__c!=null) { // Decimal contractRange = ass.CurrentContract_F__r.Contract_Range__c; // double divided = contractRange / 12; // double rounded; // // 判断小数位是否为 0.5,如果是,则不参与四舍五入 // if (String.valueOf(divided).endsWith('.5')) { // rounded = divided; // } else { // rounded = Math.round(divided); // } // if (!NotoAssSize.containsKey(ass.product2.Asset_Model_No__c)) { // NotoAssSize.put(ass.product2.Asset_Model_No__c,rounded); // }else{ // if (NotoAssSize.get(ass.product2.Asset_Model_No__c)!=null) { // Double mcNumAll = NotoAssSize.get(ass.product2.Asset_Model_No__c); // mcNumAll += rounded; // NotoAssSize.put(ass.product2.Asset_Model_No__c,mcNumAll); // } // } // } // } // } // } // for (Maintenance_Contract_Asset_Estimate__c Mca : McaeList) { // if (CategoryOfNumberAsset && NotoAss.get(Mca.asset__r.product2.Asset_Model_No__c)!=null) { // Mca.Three_Years_Repair_Cost_HasMc_Text__c = NotoAss.get(Mca.asset__r.product2.Asset_Model_No__c); // mca.ToMonthSize__c = NotoAssSize.get(Mca.asset__r.product2.Asset_Model_No__c); // } // if (!CategoryOfNumberAsset && NotoAss.get(Mca.asset__r.product2.Category4__c)!=null) { // Mca.Three_Years_Repair_Cost_HasMc_Text__c = NotoAss.get(Mca.asset__r.product2.Category4__c); // mca.ToMonthSize__c = NotoAssSize.get(Mca.asset__r.product2.Category4__c); // } // } // try { // system.debug('McaeList:::::' + McaeList); // update McaeList; // } catch (Exception e) { // } // } // private static Decimal sumPrice1(AggregateResult rpc) { // Decimal SumPrice = Decimal.valueOf(rpc.get('SumPrice') + ''); // Decimal sales_discount = Decimal.valueOf((rpc.get('sales_discount') == null ? 0 : rpc.get('sales_discount')) + '') * -1; // Decimal contract_target = Decimal.valueOf((rpc.get('contract_target') == null ? 0 : rpc.get('contract_target')) + '') * -1; // Decimal loaner_repair = Decimal.valueOf((rpc.get('loaner_repair') == null ? 0 : rpc.get('loaner_repair')) + '') * -1; // Decimal long_term_insurance = Decimal.valueOf((rpc.get('long_term_insurance') == null ? 0 : rpc.get('long_term_insurance')) + '') * -1; // Decimal set_discount = Decimal.valueOf((rpc.get('set_discount') == null ? 0 : rpc.get('set_discount')) + '') * -1; // Decimal sercince = Decimal.valueOf((rpc.get('sercince') == null ? 0 : rpc.get('sercince')) + '') * -1; // Decimal long_term_insuranceMD = Decimal.valueOf((rpc.get('long_term_insuranceMD') == null ? 0 : rpc.get('long_term_insuranceMD')) + '') * -1; // Decimal delivery = Decimal.valueOf((rpc.get('delivery') == null ? 0 : rpc.get('delivery')) + '') * -1; // Decimal other = Decimal.valueOf((rpc.get('other') == null ? 0 : rpc.get('other')) + '') * -1; // // system.debug(rpc.get('SumPrice') + '--' + rpc.get('sales_discount') + '--' + rpc.get('contract_target') + '--' + // // rpc.get('loaner_repair') + '--' + rpc.get('long_term_insurance') + '--' + rpc.get('set_discount') + '--' + rpc.get('long_term_insuranceMD') // // + '--' + rpc.get('delivery') + '--' + rpc.get('other') + '--'); // return SumPrice + sales_discount + contract_target + loaner_repair + long_term_insurance + sercince + set_discount + long_term_insuranceMD + delivery + other; // } public class RepairInfo { private Date mcDate; private Date FailureOccurrenceDate; public RepairInfo(Date mcDate,Date FailureOccurrenceDate) { this.mcDate = mcDate; this.FailureOccurrenceDate = FailureOccurrenceDate; } } }