liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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;
    // }
 
}