public with sharing class SetContractEstimatePriceHandler extends Oly_TriggerHandler { private Map newMap; private Map oldMap; private List newList; private List oldList; public SetContractEstimatePriceHandler() { this.newMap = (Map) Trigger.newMap; this.oldMap = (Map) Trigger.oldMap; this.newList = (List) Trigger.new; this.oldList = (List) Trigger.old; } protected override void beforeInsert() { beforeSetValue(); } protected override void beforeUpdate() { beforeSetValue(); } // ************** HWAG-AXB9TN ******** START ************************** // 维修合同报价中手动产品在【维修合同/保有设备】中显示维修合同价格 // 2018-03-30 chen private void beforeSetValue() { // 维修合同/保有设备 中存在 维修合同报价/保有设备 为空的记录,取合同Id 和 设备Id List mcIds = new List(); List assetIds = new List(); for(Maintenance_Contract_Asset__c mca: newList) { if (String.isBlank(mca.Maintenance_Contract_Asset_Estimate__c)) { mcIds.add(mca.Maintenance_Contract__c); assetIds.add(mca.Asset__c); } } if (mcIds.size() > 0) { // 取出 维修合同/保有设备 中保有设备对应的 产品Id List assetList = [select Id, Product2Id, Product2.Asset_Model_No__c from Asset where Id IN:assetIds]; Map asProMap = new Map(); Map asModelMap = new Map(); for (Asset a: assetList) { asProMap.put(a.Id, a.Product2Id); asModelMap.put(a.Id, a.Product2.Asset_Model_No__c); } // 取出 维修合同/保有设备 中维修合同当前的报价Id List mcList = [select Id, Estimation_Id__c from Maintenance_Contract__c where Id IN : mcIds]; List mceIds = new List(); for (Maintenance_Contract__c mc : mcList) { if (!String.isBlank(mc.Estimation_Id__c)) { mceIds.add(mc.Estimation_Id__c); } } // 取出 维修合同/保有设备 中维修合同当前的报价 中的 维修合同报价/保有设备 List mcaeList = new List (); if (mceIds.size() > 0) { mcaeList = [select Id, Maintenance_Contract_Estimate__r.Maintenance_Contract__c, Product_Manual__c, IsNew__c, Estimate_List_Price_All__c, Asset__c, Product_Manual__r.Asset_Model_No__c from Maintenance_Contract_Asset_Estimate__c where Maintenance_Contract_Estimate__c IN:mceIds]; // 将 维修合同报价/保有设备 以 产品Id 为Key值放入Map中 Map productMap = new Map(); Map assetMap = new Map(); Map modelMap = new Map(); if (mcaeList.size() > 0) { for (Maintenance_Contract_Asset_Estimate__c mcae : mcaeList) { if (mcae.Product_Manual__c == null) { assetMap.put('' + mcae.Asset__c + mcae.Maintenance_Contract_Estimate__r.Maintenance_Contract__c, mcae); } else { productMap.put('' + mcae.Product_Manual__c + mcae.Maintenance_Contract_Estimate__r.Maintenance_Contract__c, mcae); modelMap.put('' + mcae.Product_Manual__r.Asset_Model_No__c + mcae.Maintenance_Contract_Estimate__r.Maintenance_Contract__c, mcae); } } } // 将取到的 维修合同报价/保有设备 中的报价价格 及 是否新品 更新到对应 的 维修合同/保有设备 中 for(Maintenance_Contract_Asset__c newMca: newList) { if (String.isBlank(newMca.Maintenance_Contract_Asset_Estimate__c)) { Maintenance_Contract_Asset_Estimate__c assetVal = assetMap.get('' + newMca.Asset__c + newMca.Maintenance_Contract__c); Maintenance_Contract_Asset_Estimate__c productVal = productMap.get(asProMap.get(newMca.Asset__c) + newMca.Maintenance_Contract__c); Maintenance_Contract_Asset_Estimate__c modelVal = modelMap.get(asModelMap.get(newMca.Asset__c) + newMca.Maintenance_Contract__c); if (assetVal != null) { newMca.Maintenance_Contract_Asset_Estimate__c = assetVal.Id; } else if (productVal != null) { newMca.Maintenance_Contract_Asset_Estimate__c = productVal.Id; } else if (modelVal != null) { newMca.Maintenance_Contract_Asset_Estimate__c = modelVal.Id; } } } } } } // ************** HWAG-AXB9TN ******** END **************************** }