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
public without sharing class MaintanceContractPackEstimateHandler extends Oly_TriggerHandler{
    private Map<Id, MaintanceContractPackEstimate__c> newMap;
    private Map<Id, MaintanceContractPackEstimate__c> oldMap;
    private List<MaintanceContractPackEstimate__c> newList;
    private List<MaintanceContractPackEstimate__c> oldList;
 
    public MaintanceContractPackEstimateHandler(){
        this.newMap =  (Map<Id, MaintanceContractPackEstimate__c>) Trigger.newMap;
        this.oldMap =  (Map<Id, MaintanceContractPackEstimate__c>) Trigger.oldMap;
        this.newList = (List<MaintanceContractPackEstimate__c>) Trigger.new;
        this.oldList = (List<MaintanceContractPackEstimate__c>) Trigger.old;
        
    }
    protected override void afterUpdate() {
        List<Maintenance_Contract_Estimate__c> mceUpdateLists= new List<Maintenance_Contract_Estimate__c>();
        List<String> mcpIds = new List<string>();
        for(MaintanceContractPackEstimate__c mcpe: newList){
            mcpIds.add(mcpe.MaintanceContractPack__c);
        }
        List<Maintenance_Contract_Estimate__c> mceList = [Select Id,Name,Process_Status__c,
                    Maintenance_Contract__r.Id 
                    from Maintenance_Contract_Estimate__c 
                    where Maintenance_Contract__c in (Select Id from Maintenance_Contract__c  where MaintanceContractPack__c In :mcpIds) order by Maintenance_Contract__r.Id];
 
 
        for(MaintanceContractPackEstimate__c mcpe: newList){
            //打包合同报价提交审批,同步更新合同报价相关字段的值
            MaintanceContractPackEstimate__c oldMcpe = oldMap.get(mcpe.Id);
            // List<MaintanceContractPackEstimate__c> mcpeSelect = [Select Maintenance_Contract_Estimate__c from MaintanceContractPackEstimate__c where Id = :mcpe.Id ];
            // if(mcpeSelect.size()>0) {
            //     List<String> mceIds = mcpeSelect[0].Maintenance_Contract_Estimate__c.split(',');
            //     mceList = [Select Id,Name,Process_Status__c from Maintenance_Contract_Estimate__c where Name in :mceIds];
            // }
 
            //提交
            if (mcpe.Process_Status__c == '申请中' && oldMcpe.Process_Status__c != '申请中') {
                for(Maintenance_Contract_Estimate__c mce : mceList) {
                    //已经批准的合同报价不能再进行操作
                    if(mce.Process_Status__c != '批准' && mcpe.Maintenance_Contract_Estimate__c.contains(mce.Name)) {
                        mce.Process_Status__c = '申请中';
                        mce.Submit_quotation_day__c = System.today();
                        Approval.lock(mce.Id);
                        mceUpdateLists.add(mce);
                    }
                }
            }
            //撤回
            if (mcpe.Process_Status__c == '草案中' && oldMcpe.Process_Status__c != '草案中') {
                for(Maintenance_Contract_Estimate__c mce : mceList) {
                    if(mce.Process_Status__c != '批准' && mcpe.Maintenance_Contract_Estimate__c.contains(mce.Name)) {
                        mce.Process_Status__c = '草案中';
                        mce.Submit_quotation_day__c = null;
                        mceUpdateLists.add(mce);
                    }
                    
                }
                                                    
            }
            //拒绝
            if (mcpe.Process_Status__c == '不批准' && oldMcpe.Process_Status__c != '不批准') {
                for(Maintenance_Contract_Estimate__c mce : mceList) {
                    if(mce.Process_Status__c != '批准' && mcpe.Maintenance_Contract_Estimate__c.contains(mce.Name)) {
                        mce.Process_Status__c = '不批准';
                        mceUpdateLists.add(mce);
                    }
                    
                }
                                                    
            }
            //批准
            if (mcpe.Process_Status__c == '批准' && oldMcpe.Process_Status__c != '批准') {
                for(Maintenance_Contract_Estimate__c mce : mceList) {
                    System.debug('------MaintanceContractPackEstimateHandler contains:' +mcpe.Maintenance_Contract_Estimate__c +','+mce.Name+','+mcpe.Maintenance_Contract_Estimate__c.contains(mce.Name));
 
                    if(mce.Process_Status__c != '批准' && mcpe.Maintenance_Contract_Estimate__c.contains(mce.Name)) {
                        mce.Process_Status__c = '批准';
                        mce.Approved_quotation_day__c = System.today();
                        Approval.lock(mce.Id);
                        mceUpdateLists.add(mce);
                    }
                    
                }
                                                    
            }
            if (!mceUpdateLists.isEmpty()) {
                update mceUpdateLists;
            }
 
        }          
 
    }
}