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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
@RestResource(urlMapping='/NFM205/*')
global with sharing class NFM205Rest  {
 
    global class GeDatas {
        public NFMUtil.Monitoring Monitoring;
        public  GeData[] GeData;
    }
 
    global class GeData {
        public String Opportunity_Code;                 // 询价编码
        public String Authorized_DB_No;                 // 授权申请编码
        public String Authorized_Finish_Sales;          // 授权的经销商code
        public String Authorized_Date;                  // 授权日
        public String Autholization_Activated_Date;     // 授权截至日
        //public String Bidding_Company;
        public String Bid_Project_Name;
        public String Bidding_Content;
        public String Bid_Date;
        public String Bidding_No;
        public String Authorized_Status;                // 授权结果 0:提交 1:成功 2:终止 3:解除授权申请的限制
        //报价试算  授权  start
        public String IsFranchised;                     //是否特约授权
        //报价试算  授权  end
    }
 
    @HttpPost
    global static void execute() {
        String strData = RestContext.request.requestBody.toString();
 
        // 取得接口传输内容
        GeDatas ges = (GeDatas) JSON.deserializeStrict(strData, GeDatas.class);
 
        if (ges == null ) {
            return;
        }
 
        NFMUtil.Monitoring Monitoring = ges.Monitoring;
        if (Monitoring == null) {
            return;
        }
 
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, 'NFM205', ges.GeData);
        if (String.isBlank(rowData.Log__c) == false) {
            executefuture(rowData.Id);
        }
 
        // JSONを戻す
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        res.statusCode = 200;
        //String jsonResponse = '{"status": "Success", "Message":'  + gedata + '}';
        String jsonResponse = '{"status": "Success", "Message":""}';
        res.responseBody = blob.valueOf(jsonResponse);
        return;
    }
 
    @future
    global static void executefuture(String rowData_Id) {
        main(rowData_Id);
    }
 
    global static void main (String rowData_Id) {
 
        Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
        BatchIF_Log__c rowData = [Select Id, Name, retry_cnt__c, Log__c, ErrorLog__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c, Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c, MessageGroupNumber__c from BatchIF_Log__c where RowDataFlg__c = true and Id = :rowData_Id];
        String logstr = rowData.MessageGroupNumber__c + ' start\n';
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Type__c = 'NFM205';
        iflog.MessageGroupNumber__c = rowData.MessageGroupNumber__c;
        iflog.Log__c = logstr;
        iflog.ErrorLog__c = '';
        insert iflog;
 
        String rowDataStr = NFMUtil.getRowDataStr(rowData);
        List<GeData> geDataList = (List<GeData>) JSON.deserialize(rowDataStr, List<GeData>.class);
 
        if (geDataList == null || geDataList.size() == 0) {
            return;
        }
 
        Savepoint sp = Database.setSavepoint();
 
        try {
            List<Opportunity> oppUpList = new List<Opportunity>();
            Map<id, Opportunity> oppUpdateMap = new Map<id, Opportunity>();
 
            // 将询价CODE与ID存入MAP
            List<String> oppNoList = new List<String>();
            List<String> agencyList = new List<String>();
 
            for(GeData geData : geDataList) {
                oppNoList.add(geData.Opportunity_Code);
                if (String.isNotBlank(geData.Authorized_Finish_Sales)) {
                    agencyList.add(geData.Authorized_Finish_Sales);
                }
                
            }
 
            List<Opportunity> oppList = [select id, 
                                                Opportunity_No__c, 
                                                Agency1__r.Management_FormulaCode__c, 
                                                Agency1__r.name,
                                                Agency2__r.Management_FormulaCode__c, 
                                                Agency2__r.name
                                            from Opportunity where Opportunity_No__c in :oppNoList];
            Map<String, Opportunity> oppInfoMap = new Map<String, Opportunity>();
            for (Opportunity oppInfo : oppList) {
                oppInfoMap.put(oppInfo.Opportunity_No__c, oppInfo);
            }
 
            List<Account> accList = [select Name, Management_FormulaCode__c from Account where Management_FormulaCode__c in :agencyList];
            Map<String, String> accInfoMap = new Map<String, String>();
            for (Account accInfo : accList) {
                accInfoMap.put(accInfo.Management_FormulaCode__c, accInfo.Name);
            }
 
            // 将XML各数据项更新到报价对象中
            for (GeData geData : geDataList) {
                if (geData.Opportunity_Code == null || geData.Opportunity_Code == '') {
                    // 必須項目がない場合、処理と飛ばす
                    iflog.ErrorLog__c += '询价编码不能为空。\n';
                    continue;
                }
 
                Opportunity opp = oppInfoMap.get(geData.Opportunity_Code);
                
                if (opp == null) {
                    // 如果当前询价编码不存在
                    iflog.ErrorLog__c += geData.Opportunity_Code + ':询价编码不存在。\n';
                    continue;
                }
                
                if (oppUpdateMap.get(opp.Id) == null) {
                    oppUpList.add(opp);
                    oppUpdateMap.put(opp.Id, opp);
                    logstr += opp.Opportunity_No__c + ':';
                } else {
                    opp = oppUpdateMap.get(opp.ID);
                }
 
                // 授权成功的场合
                if (geData.Authorized_Status == '1') {
                    opp.Authorized_Finish_Sales__c = accInfoMap.get(geData.Authorized_Finish_Sales);  // 授权的经销商
                    opp.Authorized_DB_No__c = geData.Authorized_DB_No;                      // 授权申请编码
                    opp.Authorized_Date__c = NFMUtil.parseStr2Date(geData.Authorized_Date); // 授权日
                    opp.Autholization_Activated_Date__c = 
                                NFMUtil.parseStr2Date(geData.Autholization_Activated_Date); // 授权截至日
                    opp.Bidding_Content__c = geData.Bidding_Content;                        // 招标内容
                    opp.Bid_Date__c = NFMUtil.parseStr2Date(geData.Bid_Date);           // 招标日
                    opp.Bidding_No__c = geData.Bidding_No;                               // 招标编码
                    opp.Bidding_Project_Name__c = geData.Bid_Project_Name;               // 招标项目名
                    opp.If_Authorizing_Lock__c = false;
                    // CHAN-BBW576 LHJ 20190508 Start
                    opp.if_Interface_Lock__c = false;
                    // CHAN-BBW576 LHJ 20190508 End
                    //报价试算  授权  start
                    if(String.isNotBlank(geData.IsFranchised)){
                        if(geData.IsFranchised.equals('是')){
                            opp.IsAuthorized__c = true;
                        }else if(geData.IsFranchised.equals('否')){
                            opp.IsAuthorized__c = false;
                        }
                    }
                    //报价试算  授权  end
                    logstr += ' 授权成功。\n';
 
                } else if(geData.Authorized_Status == '0') {
                    // SPO授权提交的场合
                    opp.If_Authorizing_Lock__c = true;
                    logstr += ' 授权申请提交。\n';
 
                } else if(geData.Authorized_Status == '3') {
                    //解除授权申请的限制
                    opp.IF_Submit__c = false;
                    logstr += ' 解除授权申请的限制\n';
 
                } else {
                    opp.If_Authorizing_Lock__c = false;
                    // CHAN-BBW576 LHJ 20190508 Start
                    opp.if_Interface_Lock__c = false;
                    // CHAN-BBW576 LHJ 20190508 End
                    //报价试算 更新是否特约授权 false
                    opp.IsAuthorized__c = false;
                    //报价试算 end
                    logstr += ' 授权中止。\n';
                }
                opp.IF_Submit__c = false;
                // 更新是否授权中状态
                //opp.If_Authorizing__c = false;
            }
 
            if (oppUpList.size() > 0) {
                update oppUpList;
            }
            logstr += '\nend';
            rowData.retry_cnt__c=0;
        } catch(Exception ex) {
            // エラーが発生した場合
            Database.rollback(sp);
            System.debug(Logginglevel.ERROR, 'NFM205_' + rowData.MessageGroupNumber__c + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, 'NFM205_' + rowData.MessageGroupNumber__c + ':' + ex.getStackTraceString());
            logstr += '\n' + ex.getMessage();
            iflog.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + iflog.ErrorLog__c;
            if (rowData.retry_cnt__c == null) rowData.retry_cnt__c = 0;
            if (rowData.retry_cnt__c < batch_retry_max_cnt){
                rowData.retry_cnt__c++;
                LogAutoSendSchedule.assignOneMinute();
            }
            if (rowData.retry_cnt__c >= batch_retry_max_cnt){
                rowData.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + rowData.ErrorLog__c+'错误次数已经超过自动收信设定的最大次数,请手动收信';
            }
        }
 
        update rowData;
        iflog.Log__c = logstr;
        if (iflog.Log__c.length() > 131072) {
            iflog.Log__c = iflog.Log__c.subString(0, 131065) + ' ...';
        }
        if (iflog.ErrorLog__c.length() > 32768) {
            iflog.ErrorLog__c = iflog.ErrorLog__c.subString(0, 32760) + ' ...';
        }
        update iflog;
 
    }
}