高章伟
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
public with sharing class NFM115Controller {
    public static String status;
    public class Samples {
        public NFMUtil.Monitoring Monitoring;
        public Sample[] Sample;
    }
    //样本订货单
    public class Sample {
        public String AgentCode;
        public String DeliveryAddress;
        public String DeliveryContact;
        public String DeliveryPhone;
        public String AreaCode;
        public Detail[] Detail;
    }
    //样本订货单明细
    public class Detail {
        public String ItemCode;
        public String ItemQuantity;
    }
 
    /**
     * NFM115の送信処理
     * 
     * @param iflog_Id     //日志表的ID
     * @param samIds       //发送样本订货单
     */
    @future (callout=true)
    public static void callout(String iflog_Id, List<String> samIds) {
        if (samIds == null || samIds.size() == 0) {
            return;
        }
        //MessageGroupNumber的获取
        List<BatchIF_Log__c> iflogList = [Select Id, Name, Log__c, ErrorLog__c from BatchIF_Log__c where Id = :iflog_Id];
        BatchIF_Log__c iflog = null;
        if (iflogList.size() > 0) {
            iflog = iflogList.get(0);
            iflog.ErrorLog__c = '';
        } else {
            //没有取得数据,就是被rollback了
            return;
        }
        //Monitoring的设定
        String logstr = iflog.Log__c + '\nNumberOfRecord=' + samIds.size();
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmm');
        Samples samples = new Samples();
        samples.Monitoring = new NFMUtil.Monitoring();
        samples.Monitoring.TransmissionDateTime = nowStr;
        samples.Monitoring.Text                 = ''; 
        samples.Monitoring.Tag                  = 'MSGH';
        samples.Monitoring.Sender               = 'SFDC';
        samples.Monitoring.Receiver             = 'SAP';
        samples.Monitoring.NumberOfRecord       = '' + samIds.size();
        samples.Monitoring.MessageType          = 'NFM115';
        samples.Monitoring.MessageGroupNumber   = iflog.Name;
 
        BatchIF_Log__c rowData = null;
        try {
            List<Sample_order_list__c> samList = [select Id, AgentCode__c, DeliveryAddress__c, DeliveryContact__c, DeliveryPhone__c, Account__c, 
                                                    Account__r.City_Master__r.Level2_Sys_No__c 
                                                    from Sample_order_list__c where Id IN:samIds];
            List<Sample_order_list_detail__c> samDetailList = [select id, ItemCode__c, ItemQuantity__c, Sample_order_list__c 
                                                                from Sample_order_list_detail__c where Sample_order_list__c IN:samIds];
            
            samples.Sample = new List<Sample>();
            for(Sample_order_list__c sam : samList){
                Sample sample = new Sample();
                sample.AgentCode       = sam.AgentCode__c;
                sample.DeliveryAddress = sam.DeliveryAddress__c;
                sample.DeliveryContact = sam.DeliveryContact__c;
                sample.DeliveryPhone   = sam.DeliveryPhone__c;
                sample.AreaCode        = sam.Account__r.City_Master__r.Level2_Sys_No__c;
                sample.Detail   = new List<Detail>();
                samples.Sample.add(sample);
                for(Sample_order_list_detail__c samDetail : samDetailList){
                    if(samDetail.Sample_order_list__c == sam.Id){
                        Detail Detail = new Detail();
                        Detail.ItemCode     = samDetail.ItemCode__c;
                        Detail.ItemQuantity = String.valueOf(samDetail.ItemQuantity__c);
                        sample.Detail.add(Detail);
                    }
                }
            }
            if(samples.Sample.size() > 0){
                NFMUtil.Monitoring Monitoring   = new NFMUtil.Monitoring();
                Monitoring.Tag                  = samples.Monitoring.Tag;
                Monitoring.Sender               = samples.Monitoring.Sender;
                Monitoring.Receiver             = samples.Monitoring.Receiver;
                Monitoring.MessageType          = samples.Monitoring.MessageType;
                Monitoring.MessageGroupNumber   = samples.Monitoring.MessageGroupNumber;
                Monitoring.NumberOfRecord       = samples.Monitoring.NumberOfRecord;
                Monitoring.TransmissionDateTime = samples.Monitoring.TransmissionDateTime;
                Monitoring.Text = '';
                
                rowData = NFMUtil.makeRowData(Monitoring, 'NFM115', samples);
                execute(rowData, iflog);
            }
            logstr += '\nend';
        } catch(Exception ex) {
            //发生错误的情况
            System.debug(Logginglevel.ERROR, 'NFM115_' + iflog.Name + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, 'NFM115_' + iflog.Name + ':' + ex.getStackTraceString());
            logstr += ex.getMessage();
            iflog.ErrorLog__c += ex.getMessage() + '\n';
            iflog.ErrorLog__c += ex.getStackTraceString() + '\n';
        }
        System.debug('thhrowData--->'+rowData);
        if (rowData != null) {
            upsert rowData;
        }
 
        iflog.Log__c = logstr;
        upsert iflog;
    }
 
    public static void execute(BatchIF_Log__c rowData, BatchIF_Log__c iflog) {
        Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
        String rowDataStr = NFMUtil.getRowDataStr(rowData);
        Samples samples = (Samples) JSON.deserialize(rowDataStr, Samples.class);
        String logstr = samples.Monitoring.MessageGroupNumber + ' start\n';
        Boolean needUpdateIflog = false;
        if (iflog == null) {
            needUpdateIflog = true;
            iflog = new BatchIF_Log__c();
            iflog.Type__c = 'NFM115';
            iflog.MessageGroupNumber__c = samples.Monitoring.MessageGroupNumber;
            iflog.Log__c = logstr;
            iflog.ErrorLog__c = '';
        } else {
            logstr = iflog.Log__c;
        }
 
        try{
            //发送接口
            status = NFMUtil.sendToSapRet(rowDataStr, NFMUtil.NFM115_ENDPOINT);
            System.debug('NFM115Log--status->' + status);
            if (status == 'Accepted') {
                logstr += status + '\n';
                rowData.retry_cnt__c = 0;
            } else {
                logstr += status + '\n';
                rowData = NFMUtil.LogAutoSend(rowData, null, status);
            }    
        } catch (Exception ex) {
            //发生错误的情况
            System.debug(Logginglevel.ERROR, 'NFM115_' + iflog.Name + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, 'NFM115_' + iflog.Name + ':' + ex.getStackTraceString());
            logstr += ex.getMessage();
            iflog.ErrorLog__c += ex.getMessage() + '\n';
            iflog.ErrorLog__c += ex.getStackTraceString() + '\n';
 
            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+'错误次数已经超过自动送信设定的最大次数,请手动送信';
            }
        }
        iflog.Log__c = logstr;
 
        if (needUpdateIflog) {
            upsert iflog;
            upsert rowData;
        }
    }
 
}