public without sharing class PreProductStorageListHandler extends Oly_TriggerHandler {
|
private List<PreProduct_Storage_List__c> oldList;
|
private List<PreProduct_Storage_List__c> newList;
|
private Map<Id,PreProduct_Storage_List__c> oldMap;
|
private Map<Id,PreProduct_Storage_List__c> newMap;
|
|
public PreProductStorageListHandler() {
|
this.newList = (List<PreProduct_Storage_List__c>) Trigger.new;
|
this.oldMap = (Map<Id,PreProduct_Storage_List__c>) Trigger.oldMap;
|
this.newMap = (Map<Id,PreProduct_Storage_List__c>) Trigger.newMap;
|
this.oldList = (List<PreProduct_Storage_List__c>) Trigger.old;
|
}
|
protected override void beforeInsert() {
|
//beforinsert 里面做产品预留减少数量的check,查看是否有足够的数量去减少
|
checkPreProductEnough();
|
}
|
// protected override void afterInsert() {
|
// 做增加产品预留或者减少产品预留的操作 具体看类型
|
// increaseOrReducePreProduct();
|
// }
|
|
|
public void checkPreProductEnough() {
|
//涉及减少的产品预留id
|
List<String> reducePreProductIdList = new List<String>();
|
//涉及减少的产品预留id,增加数量
|
Map<String,Decimal> reducePreProductMap = new Map<String,Decimal>();
|
for(PreProduct_Storage_List__c storageDetails : newList){
|
if(storageDetails.Change_Type__c.equals('调出')){
|
reducePreProductMap.put(storageDetails.PreProduct_Storage__c,storageDetails.Modify_Number__c);
|
reducePreProductIdList.add(storageDetails.PreProduct_Storage__c);
|
}
|
}
|
List<PreProduct_Storage__c> reducePreProductList = new List<PreProduct_Storage__c>();
|
if(reducePreProductIdList.size() > 0){
|
reducePreProductList = [SELECT Id,LeftNumber__c FROM PreProduct_Storage__c WHERE ifValid__c = true AND Id IN:reducePreProductIdList FOR UPDATE];
|
}
|
for(PreProduct_Storage__c storage : reducePreProductList){
|
if(reducePreProductMap.containsKey(storage.Id)){
|
if(storage.LeftNumber__c - reducePreProductMap.get(storage.Id) < 0){
|
// storage.addError('剩余预留数量不足,不能减少');
|
String errorMessage = '剩余预留数量不足,不能提交审批';
|
throw new DmlException(errorMessage);
|
}
|
// storage.Left_Number__c = storage.Left_Number__c - reducePreProductMap.get(storage.Id);
|
}
|
}
|
}
|
|
// public void increaseOrReducePreProduct() {
|
// //涉及修改的产品预留id
|
// List<String> changePreProductIdList = new List<String>();
|
// //涉及修改的产品预留id,产品预留明细对象
|
// Map<String,PreProduct_Storage_List__c> changePreProductMap = new Map<String,PreProduct_Storage_List__c>();
|
// for(PreProduct_Storage_List__c storageDetails : newList){
|
// if(storageDetails.Change_Type__c.equals('调入') || storageDetails.Change_Type__c.equals('调出')){
|
// changePreProductMap.put(storageDetails.PreProduct_Storage__c,storageDetails);
|
// changePreProductIdList.add(storageDetails.PreProduct_Storage__c);
|
// }
|
// }
|
// List<PreProduct_Storage__c> changePreProductList = new List<PreProduct_Storage__c>();
|
// if(changePreProductIdList.size() > 0){
|
// changePreProductList = [SELECT Id,Left_Number__c FROM PreProduct_Storage__c WHERE Id IN:changePreProductIdList FOR UPDATE];
|
// }
|
// for(PreProduct_Storage__c storage : changePreProductList){
|
// if(changePreProductMap.containsKey(storage.Id)){
|
// if(changePreProductMap.get(storage.Id).Change_Type__c =='调入'){
|
// storage.Left_Number__c += changePreProductMap.get(storage.Id).Modify_Number__c;
|
// }
|
// else{
|
// storage.Left_Number__c -= changePreProductMap.get(storage.Id).Modify_Number__c;
|
// }
|
|
// }
|
// }
|
// UPDATE changePreProductList;
|
// }
|
|
}
|