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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
public without sharing class PreProductStorageAdjustHandler extends Oly_TriggerHandler {
    private List<PreProduct_StorageAdjust__c> oldList;
    private List<PreProduct_StorageAdjust__c> newList;
    private Map<Id,PreProduct_StorageAdjust__c> oldMap;
    private Map<Id,PreProduct_StorageAdjust__c> newMap;
    //当前时间
    private DateTime currentTime = System.now();
 
    public PreProductStorageAdjustHandler() {
        this.newList = (List<PreProduct_StorageAdjust__c>) Trigger.new;
        this.oldMap = (Map<Id,PreProduct_StorageAdjust__c>) Trigger.oldMap;
        this.newMap = (Map<Id,PreProduct_StorageAdjust__c>) Trigger.newMap;
        this.oldList = (List<PreProduct_StorageAdjust__c>) Trigger.old;
    }
 
    protected override void beforeInsert() {
        //给送货方所有人赋值  审批流程需要
        setColumn();
    }
    protected override void beforeUpdate() {
        // check产品预留的本部是否为'其他'||'市场本部',等于时不能更新
        // checkPreProductDepar();
        //调整状态为申请中时,增加一条预留产品明细数据,类型为调出,(减少送货方预留产品数量以及check)这个应该在产品明细的触发器里面写
        checkBeforApproval();
    }
 
    protected override void afterUpdate() {   
        //审批批准生成一条预留产品明细,类型为调入.(增加收货方预留产品数量)这个应该在产品明细的触发器里面写
        adjustPreproductNumber();
        //如果审批不通过,状态变为驳回时,得在减少的产品预留上增加一条调入的明细,将数量变回
        //增加一条调入的明细
        rejectSubmitApproval();
        //如果审批撤回,同上
        recallSubmitApproval();
    }
 
 
    public void setColumn() {
        //存放送货方产品预留id
        List<String> givePreProductIdList = new List<String>();
        for( PreProduct_StorageAdjust__c  adjust :  newList){
            givePreProductIdList.add(adjust.PreProduct_StorageGive__c);
            System.debug(adjust.PreProduct_StorageGive__c);
        }
        //送货方产品预留对象
        List<PreProduct_Storage__c>  givePreProductList = [SELECT OwnerId,CreatedById,ManyImportOwnerId__c
                                                                FROM PreProduct_Storage__c 
                                                                    WHERE ifValid__c = true AND  Id  IN : givePreProductIdList];
        //产品预留id,所有人id
        Map<String,PreProduct_Storage__c> preproductAndOwnMap  = new Map<String,PreProduct_Storage__c>();
        for(PreProduct_Storage__c product : givePreProductList){
            preproductAndOwnMap.put(product.Id,product);
        }
 
        for( PreProduct_StorageAdjust__c  adjust :  newList){
            if(preproductAndOwnMap.containskey(adjust.PreProduct_StorageGive__c)){
                //拿到正确的所有人,产品预留有可能是通过批量导入的,批量导入给ManyImportOwnerId__c赋值原本的所有人
                if(preproductAndOwnMap.get(adjust.PreProduct_StorageGive__c).ManyImportOwnerId__c == null ){
                    adjust.GivePreProductOwnPerson__c = preproductAndOwnMap.get(adjust.PreProduct_StorageGive__c).OwnerId;       
                }
                else{
                    adjust.GivePreProductOwnPerson__c = preproductAndOwnMap.get(adjust.PreProduct_StorageGive__c).ManyImportOwnerId__c;
                }
            }
            //给接收方的本部窗口、省份助理  以及 调出方的本部窗口以及省份助理赋值
            adjust.AcceptDepartWindow__c = adjust.AcceptDepartWindow_F__c;
            adjust.AcceptProvinceAssistance__c = adjust.AcceptProvinceAssis_F__c;
            adjust.AcceptProvinceSPAssistance__c = adjust.AcceptProvinceAssis_F__c;
            adjust.GiveDepartWindow__c = adjust.GiveDepartWindow_F__c;
            adjust.GiveProvinceAssistance__c = adjust.GiveProvinceAssist_F__c;
            adjust.GiveProvinceSPAssist__c = adjust.GiveProvinceSPAssist_F__c;
        }
    }
 
 
    public void adjustPreproductNumber(){
        //调入的预留产品明细的list
        List<PreProduct_Storage_List__c>  storageList = new List<PreProduct_Storage_List__c>();
        for( PreProduct_StorageAdjust__c  adjust :  newList){
            if(adjust.Status__c != null && adjust.Status__c.equals('批准')){
                PreProduct_Storage_List__c  storageDetails = new PreProduct_Storage_List__c();
                storageDetails.PreProduct_Storage__c = adjust.PreProduct_Storage__c;
                storageDetails.Modify_Number__c = adjust.Quantity__c;
                storageDetails.Change_Type__c = '调入';
                storageDetails.Modified_By__c = adjust.CreatedById;     
                storageDetails.Modified_Time__c   =  currentTime;
                storageDetails.AddReduceNumber__c = +adjust.Quantity__c;
                storageDetails.PreProductAdjust__c = adjust.Id;
                storageList.add(storageDetails)  ;
            }
        }
        if(storageList.size() > 0){
            INSERT storageList;
        }
    }
 
    public void checkBeforApproval(){
        //调出的预留产品明细的list
        List<PreProduct_Storage_List__c>  storageList = new List<PreProduct_Storage_List__c>();
        for( PreProduct_StorageAdjust__c  adjust :  newList){
            if(adjust.Status__c != null && adjust.Status__c.equals('申请中')){
                PreProduct_Storage_List__c  storageDetails = new PreProduct_Storage_List__c();
                storageDetails.PreProduct_Storage__c = adjust.PreProduct_StorageGive__c;
                storageDetails.Modify_Number__c = adjust.Quantity__c;
                storageDetails.Change_Type__c = '调出';
                storageDetails.Modified_By__c = adjust.CreatedById;  
                storageDetails.Modified_Time__c   =  currentTime;
                storageDetails.AddReduceNumber__c = -adjust.Quantity__c;
                storageDetails.PreProductAdjust__c = adjust.Id;
                storageList.add(storageDetails); 
            }
            
        }
        if(storageList.size() > 0){
            INSERT storageList;    
        }
 
    }
 
    // public void checkPreProductDepar(){
    //     //查找设计的预留产品
    //     Set<String> changeNumberProductIdSet = new Set<String>();
    //     for( PreProduct_StorageAdjust__c  adjust :  newList){
    //         changeNumberProductIdSet.add(adjust.PreProduct_StorageGive__c);
    //         changeNumberProductIdSet.add(adjust.PreProduct_Storage__c);        
    //     }
    //     if(changeNumberProductIdSet.size() > 0){
    //         List<PreProduct_Storage__c>  changeNumberPreProductList = [SELECT OwnerId,CreatedById,Department__c
    //                                                                 FROM PreProduct_Storage__c 
    //                                                                     WHERE ifValid__c = true AND Id  IN : changeNumberProductIdSet];
    //         //是否有涉及的市场本部|其他
    //         for(PreProduct_Storage__c storage : changeNumberPreProductList){
    //             if(storage.Department__c.equals('其他') || storage.Department__c.equals('市场') ){
    //                 // storage.Department__c.addError('所属本部为其他或市场本部时,不允许更新调整申请');
    //                 String errorMessage = '所属本部为其他或市场时,不允许更新调整申请';
    //                 throw new DmlException(errorMessage);
    //             }
    //         }
    //     }
        
    // }
    public void rejectSubmitApproval(){
        //驳回自动生成调入的明细,将调出减少的数据还原
        //调入的预留产品明细的list
        List<PreProduct_Storage_List__c>  storageList = new List<PreProduct_Storage_List__c>();
        for( PreProduct_StorageAdjust__c  adjust :  newList){
            if(adjust.Status__c != null && adjust.Status__c.equals('驳回') && oldMap.get(adjust.Id).Status__c =='申请中'){
                PreProduct_Storage_List__c  storageDetails = new PreProduct_Storage_List__c();
                storageDetails.PreProduct_Storage__c = adjust.PreProduct_StorageGive__c;
                storageDetails.Modify_Number__c = adjust.Quantity__c;
                storageDetails.Change_Type__c = '调入';
                storageDetails.Modified_By__c = adjust.CreatedById;    
                storageDetails.Modified_Time__c   =  currentTime; 
                storageDetails.AddReduceNumber__c = +adjust.Quantity__c;
                storageDetails.PreProductAdjust__c = adjust.Id;
                storageList.add(storageDetails)  ;
            }
        }
        if(storageList.size() > 0){
            INSERT storageList;
        }
    }
    public void recallSubmitApproval(){
        //撤回自动生成调入的明细,将调出减少的数据还原
        //调入的预留产品明细的list
        List<PreProduct_Storage_List__c>  storageList = new List<PreProduct_Storage_List__c>();
        for( PreProduct_StorageAdjust__c  adjust :  newList){
            if(adjust.Status__c != null && adjust.Status__c.equals('审批撤回') && oldMap.get(adjust.Id).Status__c =='申请中'){
                PreProduct_Storage_List__c  storageDetails = new PreProduct_Storage_List__c();
                storageDetails.PreProduct_Storage__c = adjust.PreProduct_StorageGive__c;
                storageDetails.Modify_Number__c = adjust.Quantity__c;
                storageDetails.Change_Type__c = '调入';
                storageDetails.Modified_By__c = adjust.CreatedById;  
                storageDetails.Modified_Time__c   =  currentTime;   
                storageDetails.AddReduceNumber__c = +adjust.Quantity__c;
                storageDetails.PreProductAdjust__c = adjust.Id;
                storageList.add(storageDetails)  ;
            }
        }
        if(storageList.size() > 0){
            INSERT storageList;
        }  
    }
}