高章伟
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
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
/**
 * 
 * @url: /services/apexrest/接口编号/execute
 * 
 * @msg:
 *   1.非赞助会的情况下,审批完成的时候记录审批完成时间
 *   2.补充决裁的情况下,如果决裁和学会绑定,在决裁审批完成后,更新决裁的信息到学会上
 */
@RestResource(urlMapping='/NFM706/*')
global with sharing class NFM706Rest {
 
    global class GeDatas {
        public NFMUtil.Monitoring Monitoring;
        public  GeData[] GeData;
    }
 
    public static final String LOG_TYPE = 'NFM706';
    global class GeData {
        public String MeetingApprovedNo; //会议决裁编码==MeetingApprovedNo__c
        public String Num; //会议编码 == Num__c
        public Integer ProcessState;//OBPM审批状态
    }
    @HttpPost
    global static void doPost() {
        //取得接口传输内容
        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, LOG_TYPE, ges.GeData);
        system.debug('String.isBlank(rowData.Log__c) == false?');
        if(String.isBlank(rowData.Log__c) == false){
            System.debug('进入if方法,成功调用executefuture');
            executefuture(rowData.Id);
        }
 
 
        // JSONを戻す
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        res.statusCode = 200;
        String jsonResponse = '{"Result":{"Result": "00", "Message":""}}';
        res.responseBody = blob.valueOf(jsonResponse);
        return;
 
    }
 
    @future(callout = true)
    global static void executefuture(String rowData_Id){
        main(rowData_Id);
    }
 
    global static void main(String rowData_Id){
 
        Map<String, String> transferMap = new Map<String, String>();
 
        List<BatchIF_Transfer__c> transferList = [select Table__c,
                                    Column__c,
                                    External_value__c,
                                    Internal_value__c
                                    from BatchIF_Transfer__c
                                    where Dropped_Flag__c = false
                                    and (Table__c = 'Application_for_Conference_Adjudication__c')];
 
        for(BatchIF_Transfer__c t : transferList){
            transferMap.put(t.Column__c + t.External_value__c, t.Internal_value__c);
        }
 
        BatchIF_Log__c rowData = [Select Id, Name, 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, retry_cnt__c from BatchIF_Log__c where RowDataFlg__c = true and Id =: rowData_Id];
 
        //匹配
        String logstr = 'start\n';
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Type__c = LOG_TYPE;
        iflog.MessageGroupNumber__c = rowData.MessageGroupNumber__c;
        iflog.Log__c = logstr;
        iflog.ErrorLog__c = '';
        insert iflog;
 
        String rowDataStr = NFMUtil.getRowDataStr(rowData);
 
        Map<String,Id> campignMap = new Map<String,Id>();
 
        List < GeData > itemMasterList = (List < GeData > ) JSON.deserialize(rowDataStr, List < GeData > .class);
        logstr += '数据总数为:' + itemMasterList.size() + '\n';
        if (itemMasterList == null || itemMasterList.size() == 0) {
            return;
        }
        Savepoint sp = Database.setSavepoint();
        try{
            //待更新的裁决List
            List <Application_for_Conference_Adjudication__c> acaList = new List<Application_for_Conference_Adjudication__c>();
            //会议
            List<String> meetingApprovedNoList = new List<String>();
            
            List<String> NumList = new List<String>();
 
            for( GeData ged : itemMasterList){
                //验证数据完整性
                String dataComplete = verify(ged);
                if (!String.isBlank(dataComplete)) {
                    logstr += dataComplete;
                    continue;
                }
                meetingApprovedNoList.add(ged.MeetingApprovedNo);
                NumList.add(ged.Num);
            }
            
            //查询SFDC的会议决裁表
            Map<String,Application_for_Conference_Adjudication__c> oldacfcMap = new Map<String,Application_for_Conference_Adjudication__c>();
            List<Application_for_Conference_Adjudication__c> oldafcaList = [select Id,MeetingApprovedNo__c,    ProcessState__c
                                                                from Application_for_Conference_Adjudication__c
                                                                where MeetingApprovedNo__c IN :meetingApprovedNoList 
                                                                and ProcessState__c != '审批成功'];
            for(Application_for_Conference_Adjudication__c oldafca  : oldafcaList){
                oldacfcMap.put(oldafca.MeetingApprovedNo__c,oldafca);
            }
 
            Map<String,Application_for_Conference_Adjudication__c> updateMap = new Map<String,Application_for_Conference_Adjudication__c>();
 
 
            for(GeData ged : itemMasterList){
                if(oldacfcMap.containsKey(ged.MeetingApprovedNo)){
                    //新会议裁决申请
                    Application_for_Conference_Adjudication__c aca = new Application_for_Conference_Adjudication__c();
                    Application_for_Conference_Adjudication__c aca1 = oldacfcMap.get(ged.MeetingApprovedNo);
                    logstr += '已存在会议决裁编码\n';
                    aca.Id                            = aca1.Id;
                    aca.MeetingApprovedNo__c          = ged.MeetingApprovedNo;
                    // aca.Num__c                        = ged.Num;
                    aca.ProcessState__c               = NFMUtil.getMapValue(transferMap, 'ProcessState__c', string.valueof(ged.ProcessState), iflog);
                    updateMap.put(ged.MeetingApprovedNo, aca);
                
                }else {
                    logstr += '不存在的会议编码';
                }
 
                if( updateMap.size() > 0 ) {
                    logstr += '更新数据\n' + updateMap.values();
                    Update updateMap.values();
                }
 
                rowData.retry_cnt__c = 0;
                logstr += '\nend';
            }
 
 
 
        }catch (Exception ex){
            Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
            // エラーが発生した場合
            Database.rollback(sp);
            logstr += '\n' + ex.getMessage();
            iflog.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + iflog.ErrorLog__c;
            // 异常重发
            rowData = NFMUtil.LogAutoSend(rowData, ex, null);
        }
 
        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) + ' ...';
        }
        upsert iflog;
    }
 
 
    // 必填字段验证
    public static String verify(GeData ged){
        String result = '';
 
        if (ged.MeetingApprovedNo == null) {
            result += 'DataError: 会议决裁编码 [ MeetingApprovedNo ] is null!\n';
        }
        if (ged.Num == null) {
            result += 'DataError: SFDC编码 [ Num ] is null!\n';
        }
        if (ged.ProcessState == null) {
            result += 'DataError: OBPM审批状态 [ ProcessState ] is null!\n';
        }
 
        return result;
    }
 
}