高章伟
2022-02-24 2aa8da8af66aa8ae00f25831aed6bb0364176e7b
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
public with sharing class OpportunityFileOrderTriggerHandler  extends Oly_TriggerHandler {
 
    private Map<Id, OpportunityFileOrder__c> newMap;
    private Map<Id, OpportunityFileOrder__c> oldMap;
    private List<OpportunityFileOrder__c> newList;
    private List<OpportunityFileOrder__c> oldList;
 
    public OpportunityFileOrderTriggerHandler() {
        this.newMap = (Map<Id, OpportunityFileOrder__c>) Trigger.newMap;
        this.oldMap = (Map<Id, OpportunityFileOrder__c>) Trigger.oldMap;
        this.newList = (List<OpportunityFileOrder__c>) Trigger.new;
        this.oldList = (List<OpportunityFileOrder__c>) Trigger.old;
    }
 
    protected override void beforeInsert(){
        beforeInsertValue();
    }
 
    protected override void beforeUpdate(){
        beforeSetValue();
    }
 
 
    private void beforeInsertValue(){
 
        List<String> statuIds = new List<String>();
        List<OpportunityFileOrder__c> statuOFiles = new List<OpportunityFileOrder__c>();
        for( OpportunityFileOrder__c ofc :newList ){
            
            if ( ofc.OpporFileStage__c == 'G' ) {
 
                statuIds.add(ofc.StatuAchievements__c);
                statuOFiles.add(ofc);
            }
        }
 
        if (statuIds.size() > 0) {
            List<OpportunityFileOrder__c> opportunity_FileS = [select Id,OpporFileStage__c,StatuAchievements__c
                                                            from OpportunityFileOrder__c
                                                            where StatuAchievements__c In :statuIds
                                                            And OpporFileStage__c = 'G'];
            if (opportunity_FileS.size() > 0) {
 
                for (OpportunityFileOrder__c ofc : statuOFiles) {
                    ofc.addError('该注残已含有类型为G.T1清单的询价文件,不可重复创建 ');
                    continue;
                }    
            }
        }
 
    }
 
 
    private void beforeSetValue() { 
        List<String> statuIds = new List<String>();
        Map<String,Boolean> statuIdMap = new Map<String,Boolean>();
        List<Statu_Achievements__c> statuAList = new List<Statu_Achievements__c>();
 
        for( OpportunityFileOrder__c ofo :newList ){
 
            if (ofo.OpporFileStage__c == 'G' && ofo.LatestUploadTime__c != null) {
 
                statuIds.add(ofo.StatuAchievements__c);
                statuIdMap.put(ofo.StatuAchievements__c,true);
            } else if (ofo.OpporFileStage__c == 'G' && ofo.LatestUploadTime__c == null) {
                statuIdMap.put(ofo.StatuAchievements__c,false);
            }
        }
 
        if (statuIdMap.size() > 0) {
 
            for (String statuId : statuIdMap.keySet() ) {
                Statu_Achievements__c statuA = new Statu_Achievements__c();
                statuA.Id = statuId;
                //statuA.If_UploadT1Detailed__c = true;
                statuA.If_UploadT1Detailed__c = statuIdMap.get(statuId);
 
                statuAList.add(statuA);
            }
        }
 
        if (statuAList.size() > 0) {
            update statuAList;
        }
    }
}