public with sharing class OpportunityLineItemTrigger {
|
public static void setOpportunityAfter(List<OpportunityLineItem> newList,
|
List<OpportunityLineItem> oldList, Map<Id, OpportunityLineItem> newMap, Map<Id, OpportunityLineItem> oldMap) {
|
List<String> selIds = new List<String>();
|
Map<String, OpportunityLineItem> deleteOpportunityLineItemMap = new Map<String, OpportunityLineItem>();
|
if (Trigger.isInsert || Trigger.isUpdate) {
|
// OpportunityLineItemのOpportunityIdを集める
|
for (OpportunityLineItem oppli : newList) {
|
if (oppli.OpportunityId != null) {
|
selIds.add(oppli.OpportunityId);
|
}
|
}
|
} else if (Trigger.isDelete) {
|
// OpportunityLineItemのOpportunityIdを集める
|
for (OpportunityLineItem oppli : oldList) {
|
if (oppli.OpportunityId != null) {
|
selIds.add(oppli.OpportunityId);
|
deleteOpportunityLineItemMap.put(oppli.Id, oppli);
|
}
|
}
|
}
|
|
List<OpportunityLineItem> retList =
|
[Select Id, OpportunityId, Category3__c, Product2.MDM_Model_No__c, Opportunity.STMS_Package_Sale__c
|
, Opportunity.State_Owner__c, Product2.Family, Opportunity.EnergyHost__c, Product2.Asset_Model_No__c
|
from OpportunityLineItem
|
where OpportunityId in: selIds
|
order by OpportunityId];
|
|
system.debug('===retList==='+retList);
|
if (retList.size() > 0) {
|
|
Map<String, Map<String, OpportunityLineItem>> opportunityMap = new Map<String, Map<String, OpportunityLineItem>>();
|
|
for (OpportunityLineItem oli : retList) {
|
// 排除待删除的数据 防止对现有数据的干扰
|
if (deleteOpportunityLineItemMap.size() > 0 && deleteOpportunityLineItemMap.containsKey(oli.Id)) {
|
continue;
|
}
|
|
Map<String, OpportunityLineItem> familyMap = new Map<String, OpportunityLineItem>();
|
// 产品的第3分类 是 主机 或 能量主机
|
if (String.isNotBlank(oli.Category3__c) && oli.Category3__c.contains('主机')) {
|
string mapkey = oli.Product2.Family + '_' + oli.Category3__c;
|
if (opportunityMap.containsKey(oli.OpportunityId)) {
|
familyMap = opportunityMap.get(oli.OpportunityId);
|
}
|
familyMap.put(mapkey, oli);
|
system.debug('==familyMap==='+familyMap);
|
opportunityMap.put(oli.OpportunityId, familyMap);
|
}
|
}
|
Map<String, Opportunity> updateOpportunityMap = new Map<String, Opportunity>();
|
Boolean flg_usg =false;
|
Boolean flg_usg1 =false;
|
for (OpportunityLineItem oli1 : retList) {
|
system.debug('==oli1==='+oli1.id+'======='+oli1.Product2.Asset_Model_No__c);
|
if(oli1.Product2.Asset_Model_No__c != null && oli1.Product2.Asset_Model_No__c.contains('USG-400')){
|
system.debug('==jinlai==='+oli1.id);
|
flg_usg1 = true;
|
break;
|
}
|
}
|
for (OpportunityLineItem oli : retList) {
|
// 获取原字段值
|
String oppId = oli.OpportunityId;
|
//能量打包
|
Boolean getOldStmsPackageSale = oli.Opportunity.STMS_Package_Sale__c;
|
//能量主机
|
Boolean getOldEnergyHost = oli.Opportunity.EnergyHost__c;
|
Opportunity updateOpportunity = new Opportunity();
|
updateOpportunity.Id = oppId;
|
// 判断 询价产品中是否含有主机产品
|
if (!updateOpportunityMap.containsKey(oppId)) {
|
if (opportunityMap.containsKey(oppId) ) {
|
Map<String, OpportunityLineItem> familyMap = opportunityMap.get(oppId);
|
// 判断 询价产品是否含有 ENG 主机 产品
|
string mapkey1 = 'ENG_主机';
|
string mapkey2 = 'ENG_能量主机';
|
//if (oli.Product2.Asset_Model_No__c != null && oli.Product2.Asset_Model_No__c.contains('USG-400')) {
|
// flg_usg = true;
|
// }
|
system.debug('==jinlai==familyMap==='+familyMap);
|
if (familyMap.containsKey(mapkey1) || familyMap.containsKey(mapkey2)) {
|
OpportunityLineItem FamMap1 = familyMap.get(mapkey1);
|
OpportunityLineItem FamMap2 = familyMap.get(mapkey2);
|
if((FamMap1 !=null && FamMap1.Product2.Asset_Model_No__c != null && FamMap1.Product2.Asset_Model_No__c.contains('USG-400')) || (FamMap2 !=null &&FamMap2.Product2.Asset_Model_No__c != null && FamMap2.Product2.Asset_Model_No__c.contains('USG-400'))){
|
flg_usg = true;
|
}
|
system.debug('flg_usg=='+flg_usg+'========='+familyMap.size());
|
// 只含有 ENG 主机产品
|
if (familyMap.size() == 1 ) {
|
//能量打包 False
|
//能量主机 True
|
//if (getOldStmsPackageSale || !getOldEnergyHost) {
|
updateOpportunity.STMS_Package_Sale__c = false;
|
//updateOpportunity.EnergyHost__c = true;
|
if(flg_usg || flg_usg1){
|
updateOpportunity.EnergyHost__c = true;
|
}else{
|
updateOpportunity.EnergyHost__c = false;
|
}
|
updateOpportunityMap.put(oppId, updateOpportunity);
|
//}
|
} else {
|
//判断是否都是主机
|
Boolean principalMachine = true;
|
//判断是否都是能量主机
|
Boolean energyHost = true;
|
Boolean notContains = true;
|
Boolean principalMachinENG = false;
|
|
for(String key :familyMap.keySet()) {
|
|
|
OpportunityLineItem mapOli = familyMap.get(key);
|
System.debug('mapOli--->'+mapOli);
|
if (!'能量主机'.equals(mapOli.Category3__c)) {
|
energyHost = false;
|
System.debug('test--1-->');
|
if(!'ENG'.equals(mapOli.Product2.Family)){
|
principalMachine = false;
|
}
|
else {
|
principalMachinENG = true;
|
}
|
}
|
|
}
|
system.debug(energyHost+'===='+notContains+'===='+principalMachinENG);
|
|
system.debug('==='+flg_usg1);
|
if (energyHost ) {
|
// 都是能量主机
|
//能量打包 False
|
//能量主机 True
|
system.debug('能量打包=='+getOldStmsPackageSale+'==能量主机=='+getOldEnergyHost);
|
//if (getOldStmsPackageSale || !getOldEnergyHost) {
|
updateOpportunity.STMS_Package_Sale__c = false;
|
//updateOpportunity.EnergyHost__c = true;
|
if(flg_usg1){
|
updateOpportunity.EnergyHost__c = true;
|
}else{
|
updateOpportunity.EnergyHost__c = false;
|
}
|
updateOpportunityMap.put(oppId, updateOpportunity);
|
//}
|
|
|
} else {
|
if (principalMachine) {
|
// 只有 ENG 主机
|
//能量打包 False
|
//能量主机 True
|
//if (getOldStmsPackageSale || !getOldEnergyHost) {
|
updateOpportunity.STMS_Package_Sale__c = false;
|
//updateOpportunity.EnergyHost__c = true;
|
if(flg_usg1){
|
updateOpportunity.EnergyHost__c = true;
|
}else{
|
updateOpportunity.EnergyHost__c = false;
|
}
|
updateOpportunityMap.put(oppId, updateOpportunity);
|
//}
|
} else {
|
if (principalMachinENG ) {
|
// 能量打包 True
|
// 能量主机 False
|
//if (!getOldStmsPackageSale || getOldEnergyHost) {
|
updateOpportunity.STMS_Package_Sale__c = true;
|
//updateOpportunity.EnergyHost__c = false;
|
if(flg_usg1){
|
updateOpportunity.EnergyHost__c = true;
|
}else{
|
updateOpportunity.EnergyHost__c = false;
|
}
|
updateOpportunityMap.put(oppId, updateOpportunity);
|
//}
|
} else {
|
//if (getOldStmsPackageSale || getOldEnergyHost) {
|
updateOpportunity.STMS_Package_Sale__c = false;
|
//updateOpportunity.EnergyHost__c = false;
|
if(flg_usg1){
|
updateOpportunity.EnergyHost__c = true;
|
}else{
|
updateOpportunity.EnergyHost__c = false;
|
}
|
updateOpportunityMap.put(oppId, updateOpportunity);
|
|
//}
|
}
|
}
|
}
|
/*
|
// 含有 多种 主机 产品
|
// 能量打包 True
|
// 能量主机 False
|
if (!getOldStmsPackageSale || getOldEnergyHost) {
|
updateOpportunity.STMS_Package_Sale__c = true;
|
updateOpportunity.EnergyHost__c = false;
|
updateOpportunityMap.put(oppId, updateOpportunity);
|
}
|
*/
|
}
|
} else {
|
// 不含有 ENG 主机 产品
|
// 能量打包 False
|
// 能量主机 False
|
if (getOldStmsPackageSale || getOldEnergyHost) {
|
updateOpportunity.STMS_Package_Sale__c = false;
|
updateOpportunity.EnergyHost__c = false;
|
updateOpportunityMap.put(oppId, updateOpportunity);
|
}
|
}
|
} else {
|
// 不含有 主机 产品
|
// 能量打包 False
|
// 能量主机 False
|
if (getOldStmsPackageSale || getOldEnergyHost) {
|
updateOpportunity.STMS_Package_Sale__c = false;
|
updateOpportunity.EnergyHost__c = false;
|
updateOpportunityMap.put(oppId, updateOpportunity);
|
|
}
|
|
}
|
}
|
}
|
|
|
if (updateOpportunityMap.size() > 0) {
|
update updateOpportunityMap.values();
|
}
|
|
}
|
|
// String opportunityId = null;
|
// // STMS打包销售
|
// boolean flg_category3 = false;
|
// boolean flg_mdm = false;
|
// boolean stms_package_sale = false;
|
// boolean oldStmsPackageSale = false;
|
// //boolean state_Owner = false;
|
// List<Opportunity> oppUpd = new List<Opportunity>();
|
// //項目判断
|
// for (OpportunityLineItem oli : retList) {
|
// if (opportunityId == null) {
|
// opportunityId = oli.OpportunityId;
|
// oldStmsPackageSale = oli.Opportunity.STMS_Package_Sale__c;
|
// }
|
|
// if (opportunityId != oli.OpportunityId) {
|
// //stms_package_sale = ((flg_category3 == true && flg_mdm == true) || state_Owner == true) ? true : false;
|
// stms_package_sale = (flg_category3 == true && flg_mdm == true) ? true : false;
|
// if (stms_package_sale != oldStmsPackageSale) {
|
// Opportunity opportunity = new Opportunity(
|
// Id = opportunityId,
|
// STMS_Package_Sale__c = stms_package_sale
|
// );
|
// oppUpd.add(opportunity);
|
// }
|
// opportunityId = oli.OpportunityId;
|
// oldStmsPackageSale = oli.Opportunity.STMS_Package_Sale__c;
|
// flg_category3 = false;
|
// flg_mdm = false;
|
// stms_package_sale = false;
|
// }
|
|
// if (oli.Category3__c != null && oli.Category3__c.contains('主机')) { //zh 2020/09/15
|
// flg_category3 = true;
|
// }
|
// if (oli.Product2.MDM_Model_No__c != null && oli.Product2.MDM_Model_No__c.contains('USG-400')) {
|
// flg_mdm = true;
|
// }
|
// //if (oli.Opportunity.State_Owner__c == '能量') {
|
// // state_Owner = true;
|
// //}
|
// }
|
|
// //stms_package_sale = ((flg_category3 == true && flg_mdm == true) || state_Owner == true) ? true : false;
|
// stms_package_sale = (flg_category3 == true && flg_mdm == true) ? true : false;
|
// if (opportunityId != null && stms_package_sale != oldStmsPackageSale) {
|
// Opportunity opportunity = new Opportunity(
|
// Id = opportunityId,
|
// STMS_Package_Sale__c = stms_package_sale
|
// );
|
// oppUpd.add(opportunity);
|
// }
|
|
// if (oppUpd.size() > 0) {
|
// update oppUpd;
|
// }
|
|
//List<Opportunity> oppProductEPT1 = new List<Opportunity>();
|
//CHAN-BCNCRB ---XHL----Start-----
|
//Map<String,String> appMap = new Map<String,String>();
|
//for (OpportunityLineItem oli : retList) {
|
// appMap.put(oli.OpportunityId,oli.OpportunityId);
|
//}
|
//Map<String,Opportunity> oppProductEPT1Map = new Map<String,Opportunity>();
|
//for (OpportunityLineItem oli : retList) {
|
|
// Opportunity opportunity = new Opportunity();
|
// opportunity.Id = oli.OpportunityId;
|
// if (oli.Product2.MDM_Model_No__c != null && (oli.Product2.MDM_Model_No__c.contains('EP-T1(A)') ||
|
// oli.Product2.MDM_Model_No__c.contains('EP-T1(B)'))) {
|
// opportunity.If_Have_EP_T1__c = true;
|
// oppProductEPT1Map.put(oli.OpportunityId,opportunity);
|
// } else {
|
// opportunity.If_Have_EP_T1__c = false;
|
// if (!oppProductEPT1Map.containsKey(oli.OpportunityId)) {
|
// oppProductEPT1Map.put(oli.OpportunityId,opportunity);
|
// }
|
// }
|
// //oppProductEPT1Map.put(oli.OpportunityId,opportunity);
|
//}
|
//if (oppProductEPT1Map.size() > 0) {
|
// update oppProductEPT1Map.values();
|
//}
|
//if (oppProductEPT1.size() > 0) {
|
// update oppProductEPT1;
|
//}
|
//CHAN-BCNCRB ---XHL----End-----
|
|
}
|
|
}
|