高章伟
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
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
public with sharing class OpportunityFileTriggerHandler  extends Oly_TriggerHandler {
    //CHAN-BCNCRB XHL 
 
    private Map<Id, Opportunity_File__c> newMap;
    private Map<Id, Opportunity_File__c> oldMap;
    private List<Opportunity_File__c> newList;
    private List<Opportunity_File__c> oldList;
    public OpportunityFileTriggerHandler() {
        this.newMap = (Map<Id, Opportunity_File__c>) Trigger.newMap;
        this.oldMap = (Map<Id, Opportunity_File__c>) Trigger.oldMap;
        this.newList = (List<Opportunity_File__c>) Trigger.new;
        this.oldList = (List<Opportunity_File__c>) Trigger.old;
    }
 
    protected override void beforeInsert(){
        beforeInsertValue();
    }
 
    protected override void beforeUpdate(){
        beforeSetValue();
    }
 
    protected override void beforeDelete(){
        beforeDeleteSetValue();
    }
    //CHAN-BER3N8; 一个询价只能有一个类型为"G.T1清单"的询价文件
    private void beforeInsertValue(){
        List<String> oppIds = new List<String>();
        List<Opportunity_File__c> opportunityFiles = new List<Opportunity_File__c>();
 
        for( Opportunity_File__c ofc :newList ){
 
            if ( ofc.Oppor_File_Stage__c == 'G' ) {
 
                oppIds.add(ofc.Opportunity__c);
                opportunityFiles.add(ofc);
            }
        }
 
        List<Opportunity_File__c> opportunity_FileS = [select Id,Oppor_File_Stage__c,Opportunity__c,
                                                        Opportunity__r.Opportunity_No__c 
                                                        from Opportunity_File__c
                                                        where Opportunity__c In :oppIds
                                                        And Oppor_File_Stage__c = 'G'];
        if ( opportunity_FileS.size() > 0) {
            for (Opportunity_File__c ofc : opportunityFiles) {
                ofc.addError('该询价已含有类型为G.T1清单的询价文件,不可重复创建 ');
                continue;
            }
        }
 
    }
    //CHAN-BER3N8; 询价做win之后,不可删除类型为"G.T1清单"的询价文件
    private void beforeDeleteSetValue () {
        List<String> oppIds = new List<String>();
        List<Opportunity_File__c>  opportunity_FileS = new List<Opportunity_File__c>();
        List<String> idNowList  = new List<String>();
        List<Opportunity> opportunitys  = new List<Opportunity>();
    
        for( Opportunity_File__c ofc :oldList ){
 
            if ( ofc.Oppor_File_Stage__c == 'G' && ofc.If_Opportunity_win__c == true){
                
                ofc.addError('询价已SAP上传(WIN),不能删除该询价文件');
                continue;
            } else  if (ofc.Oppor_File_Stage__c == 'G'){
                oppIds.add(ofc.Opportunity__c);
                idNowList.add(ofc.Id);
            }
        }
 
        if (oppIds.size() > 0) {
            opportunity_FileS = [select Id,Oppor_File_Stage__c,Opportunity__c 
                                    from Opportunity_File__c
                                    where Opportunity__c In :oppIds
                                          And Id not In :idNowList
                                          And Oppor_File_Stage__c = 'G'];
            if (opportunity_FileS.size() <= 0) {
                for (String oppId : oppIds ) {
                    Opportunity opp = new Opportunity();
                    opp.Id = oppId;
                    opp.If_UploadT1Detailed__c = false;
                    opportunitys.add(opp);
                }
            } 
 
            if (opportunitys.size() > 0) {
                update opportunitys;
            }                             
        }
    }
    //询价win之后,不可修改类型"G.T1清单"的询价文件
    private void beforeSetValue() { 
        List<String> oppIds = new List<String>();
        List<Opportunity> opportunitys  = new List<Opportunity>();
        for( Opportunity_File__c ofc :newList ){
            //CHAN-BER3N8;Start
            if (ofc.Oppor_File_Stage__c == 'G' && ofc.If_Opportunity_win__c == true) {
                ofc.addError('询价已SAP上传(WIN),不能修改该询价文件');
                continue;
            }
            //CHAN-BER3N8;End
            if (ofc.Oppor_File_Stage__c == 'G' && ofc.Last_upload_time__c != null) {
                oppIds.add(ofc.Opportunity__c);
            }
        }
 
        if (oppIds.size() > 0) {
 
            for (String oppId : oppIds ) {
                Opportunity opp = new Opportunity();
                opp.Id = oppId;
                opp.If_UploadT1Detailed__c = true;
                opportunitys.add(opp);
            }
        }
 
        if (opportunitys.size() > 0) {
            update opportunitys;
        }
    }
}