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
@RestResource(urlMapping='/NFM403/*')
global with sharing class NFM403Rest {
 
    //11111111111111111
    // global class GeDatasRest {
    //  public GeDatas GeDatas;
    // }
 
    global class GeDatas {
        // public NFMUtil.Monitoring Monitoring;
        public GeData[] GeData;
    }
 
// 对象名  RSA_master_data
    global class GeData {
        public String RSS_CODE;
        public String RSS_CODE_DESC;
        public String RSS_CODE_FULL_DESC;
        public String PRODUCT_LINES;
        public String IS_INTAKE_ESTIMATION;
        public String IS_PAE;
        public String IS_INACTIVE;
        public String REVISION;
        public String COMMENT;
        public String LAST_MODIFIED_DATE; //DateTime
        public String RSS_CODE_PRODUCT_LINES; //主键(唯一)
        //public String PARENT_RSS_CODE;
        //public String PRODUCT_LINE_DESC;
    }
 
 
 
    @HttpPost
    global static void execute() {
 
        // 取得接口传输内容
        String strData = RestContext.request.requestBody.toString();
        GeDatas ges = (GeDatas) JSON.deserializeStrict(strData, GeDatas.class);
 
        // // 取得接口传输内容
        // String strData = RestContext.request.requestBody.toString();
        // system.debug('strData等于'+strData);
        // GeDatasRest ges = (GeDatasRest) JSON.deserializeStrict(strData, GeDatasRest.class);
 
        if (ges == null ) {
            return;
        }
 
        NFMUtil.Monitoring Monitoring = new NFMUtil.Monitoring();
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmmss');
        // if (Monitoring == null) {
            Monitoring.Tag='MSGH';
            Monitoring.Sender='SPO';
            Monitoring.Receiver='SFDC';
            Monitoring.MessageType='NFM403';
            Monitoring.MessageGroupNumber=nowStr+ges.GeData.size();
            Monitoring.NumberOfRecord=''+ges.GeData.size();
            Monitoring.TransmissionDateTime=nowStr;
            Monitoring.Text='';
            // return;
        // }
 
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, 'NFM403', 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 = 'NFM403';
        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;
        }
 
        // 传入数据去重
        Map<String,GeData> RSAmasterdataMap=new Map<String,GeData>();
        for (GeData geData : geDataList) {
            RSAmasterdataMap.put(geData.RSS_CODE_PRODUCT_LINES, geData);
        }
 
        // 数据回滚
        Savepoint sp = Database.setSavepoint();
        
        try {
            // 接收的数据存到数据表 RSA_master_data__c 里
            List<RSA_master_data__c> getDataList=new List<RSA_master_data__c>();
 
            for (String cOMMENT : RSAmasterdataMap.keySet()) {
                GeData geData = RSAmasterdataMap.get(cOMMENT);
 
                RSA_master_data__c insertGetDate=new RSA_master_data__c();
                insertGetDate.RSS_CODE_PRODUCT_LINES__c=geData.RSS_CODE_PRODUCT_LINES;
                insertGetDate.COMMENT__C=geData.COMMENT;
                insertGetDate.IS_INACTIVE__C=geData.IS_INACTIVE;
                insertGetDate.IS_PAE__c=geData.IS_PAE;
                insertGetDate.IS_INTAKE_ESTIMATION__C=geData.IS_INTAKE_ESTIMATION;
                insertGetDate.LAST_MODIFIED_DATE__C=Datetime.valueOf(geData.LAST_MODIFIED_DATE);
                insertGetDate.PRODUCT_LINES__C=geData.PRODUCT_LINES;
                insertGetDate.REVISION__C=geData.REVISION;
                insertGetDate.RSS_CODE__C=geData.RSS_CODE;
                insertGetDate.RSS_CODE_DESC__C=geData.RSS_CODE_DESC;
                insertGetDate.RSS_CODE_FULL_DESC__C=geData.RSS_CODE_FULL_DESC;
                //insertGetDate.PARENT_RSS_CODE__C=geData.PARENT_RSS_CODE;
                //insertGetDate.PRODUCT_LINE_DESC__c=geData.PRODUCT_LINE_DESC;
                getDataList.add(insertGetDate);
            }
            // upsert
            if (getDataList.size() > 0) {
                upsert getDataList RSA_master_data__r.RSS_CODE_PRODUCT_LINES__c;
            }
            logstr += '\nend';
            rowData.retry_cnt__c=0;
        } catch(Exception ex) {
            // エラーが発生した場合
            Database.rollback(sp);
            System.debug(Logginglevel.ERROR, 'NFM403_' + rowData.MessageGroupNumber__c + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, 'NFM403_' + 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();
            }
            system.debug('retry_cnt__c等于'+rowData.retry_cnt__c);
            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;
    }
}