public with sharing class SetContractEstimatePriceHandler extends Oly_TriggerHandler {
|
private Map<Id, Maintenance_Contract_Asset__c> newMap;
|
private Map<Id, Maintenance_Contract_Asset__c> oldMap;
|
private List<Maintenance_Contract_Asset__c> newList;
|
private List<Maintenance_Contract_Asset__c> oldList;
|
|
public SetContractEstimatePriceHandler() {
|
this.newMap = (Map<Id, Maintenance_Contract_Asset__c>) Trigger.newMap;
|
this.oldMap = (Map<Id, Maintenance_Contract_Asset__c>) Trigger.oldMap;
|
this.newList = (List<Maintenance_Contract_Asset__c>) Trigger.new;
|
this.oldList = (List<Maintenance_Contract_Asset__c>) 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<String> mcIds = new List<String>();
|
List<String> assetIds = new List<String>();
|
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<Asset> assetList = [select Id, Product2Id, Product2.Asset_Model_No__c from Asset where Id IN:assetIds];
|
Map<String,String> asProMap = new Map<String,String>();
|
Map<String,String> asModelMap = new Map<String,String>();
|
for (Asset a: assetList) {
|
asProMap.put(a.Id, a.Product2Id);
|
asModelMap.put(a.Id, a.Product2.Asset_Model_No__c);
|
}
|
|
// 取出 维修合同/保有设备 中维修合同当前的报价Id
|
List<Maintenance_Contract__c> mcList = [select Id, Estimation_Id__c from Maintenance_Contract__c where Id IN : mcIds];
|
List<String> mceIds = new List<String>();
|
for (Maintenance_Contract__c mc : mcList) {
|
if (!String.isBlank(mc.Estimation_Id__c)) {
|
mceIds.add(mc.Estimation_Id__c);
|
}
|
}
|
|
// 取出 维修合同/保有设备 中维修合同当前的报价 中的 维修合同报价/保有设备
|
List<Maintenance_Contract_Asset_Estimate__c> mcaeList = new List<Maintenance_Contract_Asset_Estimate__c> ();
|
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<String, Maintenance_Contract_Asset_Estimate__c> productMap = new Map<String, Maintenance_Contract_Asset_Estimate__c>();
|
Map<String, Maintenance_Contract_Asset_Estimate__c> assetMap = new Map<String, Maintenance_Contract_Asset_Estimate__c>();
|
Map<String, Maintenance_Contract_Asset_Estimate__c> modelMap = new Map<String, Maintenance_Contract_Asset_Estimate__c>();
|
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 ****************************
|
}
|