高章伟
2022-02-18 8b5f4c6c281cfa548f92de52c8021e37aa81901e
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
public with sharing class NFM202OppHandler extends Oly_TriggerHandler
{
    private Map<Id, Opportunity> newMap;
    private Map<Id, Opportunity> oldMap;
    private List<Opportunity> newList;
    private List<Opportunity> oldList;
 
    public NFM202OppHandler(){
        this.newMap = (Map<Id, Opportunity>) Trigger.newMap;
        this.oldMap = (Map<Id, Opportunity>) Trigger.oldMap;
        this.newList = (List<Opportunity>) Trigger.new;
        this.oldList = (List<Opportunity>) Trigger.old;
    }
 
    protected override void beforeUpdate() {
        // CHAN-B9R472 <变更用户标识>变更时,记录变更前询价状态 20190305 LHJ Start
        //accChange();
        // CHAN-B9R472 <变更用户标识>变更时,记录变更前询价状态 20190305 LHJ End
 
        if (NFM202Controller.isRunning) {
            return;
        }
        // 调用询价接口ToSPO
        oppToSPO();
    }
    protected override void beforeInsert() {
        separateOpportunity();
    }
    //CHAN-B9L64W 询价拆单时,将授权信息携带到新询价中  XHL Start
    private void separateOpportunity(){
        
        List<String> originalOpportunityIds = new List<String>();
        for (Opportunity nObj : newList) {
            // CHAN-BK96DL 2019-12-28 by vivek start
            // if(Trigger.isInsert && String.isNotBlank(nObj.old_Oppo_No__c) && nObj.SeparateBillMark__c == '1'){
            //     originalOpportunityIds.add(nObj.old_Oppo_No__c);
            // }
            System.debug('======Opportunity_No__c'+nObj.Opportunity_No__c);
            if(Trigger.isInsert && String.isNotBlank(nObj.Old_Opportunity_ID__c) && nObj.SeparateBillMark__c == '1'){
                originalOpportunityIds.add(nObj.Old_Opportunity_ID__c);
            }
            // CHAN-BK96DL 2019-12-28 by vivek end
        }
        // CHAN-BK96DL 2019-12-28 by vivek start
        // List<Opportunity>  opportunitys = [select Id,Opportunity_No__c,Bidding_Project_Name__c,Bidding_No__c,Bidding_Content__c,Autholization_Activated_Date__c,Authorized_Finish_Sales__c,Authorized_DB_No__c,Authorized_Date__c,Bid_Date__c from Opportunity where Opportunity_No__c in :originalOpportunityIds];
        List<Opportunity>  opportunitys = [select Id,Opportunity_No__c,Bidding_Project_Name__c,Bidding_No__c,Bidding_Content__c,Autholization_Activated_Date__c,Authorized_Finish_Sales__c,Authorized_DB_No__c,Authorized_Date__c,Bid_Date__c,RecordType.DeveloperName from Opportunity where id = :originalOpportunityIds];
        // CHAN-BK96DL 2019-12-28 by vivek end
        Map<String,Opportunity> OpportMap = new Map<String,Opportunity>();
        for (Opportunity opp : opportunitys) {
            // CHAN-BK96DL 2019-12-28 by vivek start
            // OpportMap.put(opp.Opportunity_No__c, opp);
            OpportMap.put(opp.Id, opp);
            // CHAN-BK96DL 2019-12-28 by vivek end
        }
        for (Opportunity nObj : newList) { 
             // CHAN-BK96DL 2019-12-28 by vivek start
            // if (OpportMap.containsKey(nObj.old_Oppo_No__c)) {
            //     nObj.Authorized_DB_No__c = OpportMap.get(nObj.old_Oppo_No__c).Authorized_DB_No__c;
            //     nObj.Authorized_Date__c = OpportMap.get(nObj.old_Oppo_No__c).Authorized_Date__c;
            //     nObj.Autholization_Activated_Date__c  = OpportMap.get(nObj.old_Oppo_No__c).Autholization_Activated_Date__c;
            //     nObj.Authorized_Finish_Sales__c  = OpportMap.get(nObj.old_Oppo_No__c).Authorized_Finish_Sales__c;
 
            //     nObj.Bidding_Project_Name__c  = OpportMap.get(nObj.old_Oppo_No__c).Bidding_Project_Name__c;
            //     nObj.Bidding_No__c  = OpportMap.get(nObj.old_Oppo_No__c).Bidding_No__c;
            //     nObj.Bidding_Content__c  = OpportMap.get(nObj.old_Oppo_No__c).Bidding_Content__c;
            if (OpportMap.containsKey(nObj.Old_Opportunity_ID__c)) {
                nObj.Authorized_DB_No__c = OpportMap.get(nObj.Old_Opportunity_ID__c).Authorized_DB_No__c;
                nObj.Authorized_Date__c = OpportMap.get(nObj.Old_Opportunity_ID__c).Authorized_Date__c;
                nObj.Autholization_Activated_Date__c  = OpportMap.get(nObj.Old_Opportunity_ID__c).Autholization_Activated_Date__c;
                nObj.Authorized_Finish_Sales__c  = OpportMap.get(nObj.Old_Opportunity_ID__c).Authorized_Finish_Sales__c;
 
                nObj.Bidding_Project_Name__c  = OpportMap.get(nObj.Old_Opportunity_ID__c).Bidding_Project_Name__c;
                nObj.Bidding_No__c  = OpportMap.get(nObj.Old_Opportunity_ID__c).Bidding_No__c;
                nObj.Bidding_Content__c  = OpportMap.get(nObj.Old_Opportunity_ID__c).Bidding_Content__c;
                // CHAN-BVYAFW XHL
                if ('Opportunity'.equals(OpportMap.get(nObj.Old_Opportunity_ID__c).RecordType.DeveloperName)) {
                    // 普通询价 点击 政府招标拆单 按钮 进行拆单时的标记
                    // 本字段用在业务机会工作流 清空 原询价编码
                    nObj.GeneralInquirySeparateBill__c = true;
                }
                nObj.SeparateBill__c = true;
                nObj.old_Oppo_No__c = OpportMap.get(nObj.Old_Opportunity_ID__c).Opportunity_No__c;
                // CHAN-BVYAFW XHL
 
                System.debug('old_Oppo_No__c======'+nObj.old_Oppo_No__c);
                
                 // CHAN-BK96DL 2019-12-28 by vivek end
                //nObj.Bid_Date__c  = OpportMap.get(nObj.old_Oppo_No__c).Bid_Date__c; 
                // nObj.SeparateBillMark__c = '';
                
 
            }
            nObj.SeparateBillMark__c = '';
        }
    }
    //CHAN-B9L64W 询价拆单时,将授权信息携带到新询价中  XHL End
    
 
    //// CHAN-B9R472 <变更用户标识>变更时,记录变更前询价状态 20190305 LHJ Start
    //protected override void beforeInsert() {
    //    accChange();
    //}
 
    //private void accChange() {
    //    for (Opportunity nObj : newList) {
            
    //        // 如果新建时<变更用户标识>勾选,默认修改前是"引合"
    //        if (Trigger.isInsert && (nObj.If_Account_Change__c == True)) {
    //            nObj.AccChange_Stage_Before__c = '引合'; //oldMap.get(nObj.id).StageName;
            
    //        // 如果更新时<变更用户标识>变成勾选,保存修改前的状态
    //        } else if (Trigger.isUpdate && (nObj.If_Account_Change__c != oldMap.get(nObj.id).If_Account_Change__c)
    //                && (nObj.If_Account_Change__c == True)) {
    //            nObj.AccChange_Stage_Before__c = oldMap.get(nObj.id).StageName;
    //        }
 
    //        // 如果更新时<变更用户标识>变成不勾选,恢复状态
    //        if (Trigger.isUpdate && (nObj.If_Account_Change__c != oldMap.get(nObj.id).If_Account_Change__c)
    //                && (nObj.If_Account_Change__c == False)) {
    //            nObj.StageName = nObj.AccChange_Stage_Before__c;
    //        }
    //    }
    //}
    //// CHAN-B9R472 <变更用户标识>变更时,记录变更前询价状态 20190305 LHJ End
 
 
    private void oppToSPO() {
          
        // 更新対象的询价Id をセット
        List<String> oppIds = new List<String>();
        Map<String, String> transTypeMap = new Map<String, String>(); 
 
        for (Opportunity n : newList) {
            
            Opportunity o = oldMap.get(n.id);
 
            // 经销商授权(是否授权中由否变为是)
            if(n.If_Authorizing__c == true && o.If_Authorizing__c == false) {
                NFM202Controller.isRunning = true;
                oppIds.add(n.id);
                transTypeMap.put(n.id, '1'); 
            }
            // 合同授权
            if (n.Contract_Authorize__c == true && o.Contract_Authorize__c == false ) {
                NFM202Controller.isRunning = true;
                oppIds.add(n.id);
                transTypeMap.put(n.id, '2');
                
            }
        }
 
        if (oppIds.size() > 0) {
            // MessageGroupNumber の採番
            BatchIF_Log__c iflog = new BatchIF_Log__c();
            iflog.Type__c = 'NFM202';
            iflog.Log__c  = 'callout start\n';
            insert iflog;
            iflog = [Select Id, Name from BatchIF_Log__c where Id = :iflog.Id];
            System.debug(Logginglevel.DEBUG, 'NFM202_Opp_' + iflog.Name + ' start');                  // callout の中 end のlogを出します
 
            NFM202Controller.callout(iflog.Id, oppIds, transTypeMap);
        }
 
        for (Opportunity n : newList) {
            Opportunity o = oldMap.get(n.id);
            if (n.If_Authorizing__c == true && o.If_Authorizing__c == false ) {
                n.If_Authorizing__c = false;
            }
            if (n.Contract_Authorize__c == true && o.Contract_Authorize__c == false ) {
                n.Contract_Authorize__c = false;
            }
        }
    }
}