gzw
2022-02-15 168114b11da83c5005cd608c1b23a66311717a0f
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// RowDataを残すため、@future execute にします
global class NFM108WebService {
    // Integer batchsize = 200;  考量する必要がないと思います。
    global class Payment {
        webservice NFMUtil.Monitoring Monitoring;
        webservice NFM108WebService.GeneralData[] GeneralData;
    }
    global class GeneralData {
        webservice String SFDCRepairNo;
        webservice String PaymentSource;
        webservice String PaymentAmount;
        webservice String PaymentDate;
        webservice String AccumulativeTotalPaymentAmount;
        webservice String ArrearsBasePaymentTerm;
        webservice String ARAmountBasePaymentTerm;
        webservice String ArrearsBaseInvoice;
        webservice String ARAmountBaseInvoice;
        webservice String ARAmountBaseAverage;
        webservice String AccumulativeARAmountBaseAverage;
        webservice String BaddebtsFlg;
    }
    private class ExtraData {
        public GeneralData iData;
        public String SFDCRepairNo;
        public String PaymentVoucher;
        public ExtraData(GeneralData gData) {
            this.iData = gData;
            this.SFDCRepairNo = gData.SFDCRepairNo;
            if (!(gData.SFDCRepairNo == null || gData.SFDCRepairNo == '')) {
                List<String> splitSFDCRepairNo = gData.SFDCRepairNo.split(':');
                this.SFDCRepairNo = splitSFDCRepairNo[0];
                if (splitSFDCRepairNo.size() > 1) {
                    this.PaymentVoucher = splitSFDCRepairNo[1];
                }
            }
        }
    }
 
    // 非同期を見せかけ、常にreturn void
    webservice static void NFM108(NFM108WebService.Payment Payment) {
        if (Payment == null) {
            return;
        }
        NFMUtil.Monitoring Monitoring = Payment.Monitoring;
        if (Monitoring == null) {
            return;
        }
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, 'NFM108', Payment.GeneralData);
        if (String.isBlank(rowData.Log__c) == false) executefuture(rowData.Id);
    }
    @future
    global static void executefuture(String rowData_Id) {
        execute(rowData_Id);
    }
    global static void execute(String rowData_Id) {
        Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
        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, TransmissionDateTime__c,retry_cnt__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 = 'NFM108';
        iflog.MessageGroupNumber__c = rowData.MessageGroupNumber__c;
        iflog.Log__c = logstr;
        iflog.ErrorLog__c = '';
        insert iflog;
        String rowDataStr = NFMUtil.getRowDataStr(rowData);
        List<GeneralData> generalDataList = (List<GeneralData>) JSON.deserialize(rowDataStr, List<GeneralData>.class);
        if (generalDataList == null || generalDataList.size() == 0) {
            return;
        }
        Savepoint sp = Database.setSavepoint();
        try {
            List<String> receiptNoList = new List<String>();
            List<String> mcManagementCodeList = new List<String>();
            List<ExtraData> gDataList = new List<ExtraData>();
            for (NFM108WebService.GeneralData iData : generalDataList) {
                ExtraData gData = new ExtraData(iData);
                gDataList.add(gData);
                if (iData.SFDCRepairNo == null || iData.SFDCRepairNo == '') continue;
                if (iData.PaymentSource == '维修单')
                    receiptNoList.add(gData.SFDCRepairNo);
                else if (iData.PaymentSource == '维修合同')
                    mcManagementCodeList.add(gData.SFDCRepairNo);
                else continue;
            }
 
            Map<String, Repair__c> recptsMap = new Map<String, Repair__c>();
            for (Repair__c recpt : [
                    Select Id, Name, NFM108_Receipt__r.MessageGroupNumber__c, Received_Date__c, BaddebtsDate__c from Repair__c where Name IN :receiptNoList
            ]) {
                recptsMap.put(recpt.Name, recpt);
            }
 
            Map<String, Maintenance_Contract__c> mcMap = new Map<String, Maintenance_Contract__c>();
            for (Maintenance_Contract__c mc : [
                    Select Id, Maintenance_Contract_No__c, NFM108_Receipt__r.MessageGroupNumber__c, Received_Date__c, BaddebtsDate__c from Maintenance_Contract__c where Maintenance_Contract_No__c IN :mcManagementCodeList
            ]) {
                mcMap.put(mc.Maintenance_Contract_No__c, mc);
            }
 
            Map<String, Repair_receipt__c> receiptLastMap = new Map<String, Repair_receipt__c>();
            Map<String, Repair_receipt__c> receiptUniqueMap = new Map<String, Repair_receipt__c>();
            for (ExtraData gData : gDataList) {
                if (gData.SFDCRepairNo == null || gData.SFDCRepairNo == '') {
                    iflog.ErrorLog__c += 'SFDCRepairNo[' + gData.SFDCRepairNo + ']Required\n';
                    continue;
                }
                if (gData.iData.PaymentSource == '维修单') {
                    Repair__c rpr = recptsMap.get(gData.SFDCRepairNo);
                    if (rpr == null) {
                        iflog.ErrorLog__c += '修理[' + gData.SFDCRepairNo + ']NotExist\n';
                        continue;
                    }
                    logstr += '修理:' + gData.SFDCRepairNo + ' ';
                    Repair_receipt__c rr = new Repair_receipt__c(
                            Name = gData.iData.SFDCRepairNo,
                            UniqueKey__c = gData.iData.SFDCRepairNo,
                            Repair__c = rpr.Id,
                            Maintenance_Contract__c = System.Label.Olympus_Dummy_MC,
                            SFDCRepairNo__c = gData.SFDCRepairNo,
                            PaymentVoucher__c = gData.PaymentVoucher,
                            AccumulativeTotalPaymentAmount__c = String.isBlank(gData.iData.AccumulativeTotalPaymentAmount) ? null : Decimal.valueOf(gData.iData.AccumulativeTotalPaymentAmount),
                            ArrearsBasePaymentTerm__c = String.isBlank(gData.iData.ArrearsBasePaymentTerm) ? null : Decimal.valueOf(gData.iData.ArrearsBasePaymentTerm),
                            ARAmountBasePaymentTerm__c = String.isBlank(gData.iData.ARAmountBasePaymentTerm) ? null : Decimal.valueOf(gData.iData.ARAmountBasePaymentTerm),
                            ArrearsBaseInvoice__c = String.isBlank(gData.iData.ArrearsBaseInvoice) ? null : Decimal.valueOf(gData.iData.ArrearsBaseInvoice),
                            ARAmountBaseInvoice__c = String.isBlank(gData.iData.ARAmountBaseInvoice) ? null : Decimal.valueOf(gData.iData.ARAmountBaseInvoice),
                            ARAmountBaseAverage__c = String.isBlank(gData.iData.ARAmountBaseAverage) ? null : Decimal.valueOf(gData.iData.ARAmountBaseAverage),
                            AccumulativeARAmountBaseAverage__c = String.isBlank(gData.iData.AccumulativeARAmountBaseAverage) ? null : Decimal.valueOf(gData.iData.AccumulativeARAmountBaseAverage),
                            BaddebtsFlg__c = gData.iData.BaddebtsFlg,
                            MessageGroupNumber__c = rowData.MessageGroupNumber__c
                    );
                    if (gData.iData.BaddebtsFlg == 'X') {
                        rr.BaddebtsAmount__c = String.isBlank(gData.iData.PaymentAmount) ? null : Decimal.valueOf(gData.iData.PaymentAmount);
                        rr.BaddebtsDate__c = NFMUtil.parseStr2Date(gData.iData.PaymentDate);
                    } else {
                        rr.PaymentAmount__c = String.isBlank(gData.iData.PaymentAmount) ? null : Decimal.valueOf(gData.iData.PaymentAmount);
                        rr.PaymentDate__c = NFMUtil.parseStr2Date(gData.iData.PaymentDate);
                    }
                    receiptUniqueMap.put(gData.iData.SFDCRepairNo, rr);
                    receiptLastMap.put(rpr.Id, rr);
                }
                else if (gData.iData.PaymentSource == '维修合同' || gData.iData.PaymentSource == ' 维修合同') {
                    Maintenance_Contract__c mc = mcMap.get(gData.SFDCRepairNo);
                    if (mc == null) {
                        iflog.ErrorLog__c += '维修合同[' + gData.SFDCRepairNo + ']NotExist\n';
                        continue;
                    }
                    logstr += '维修合同:' + gData.SFDCRepairNo + ' ';
                    Repair_receipt__c rr = new Repair_receipt__c(
                            Name = gData.iData.SFDCRepairNo,
                            UniqueKey__c = gData.iData.SFDCRepairNo,
                            Repair__c = System.Label.Olympus_Dummy_Repair,
                            Maintenance_Contract__c = mc.Id,
                            SFDCRepairNo__c = gData.SFDCRepairNo,
                            PaymentVoucher__c = gData.PaymentVoucher,
                            AccumulativeTotalPaymentAmount__c = String.isBlank(gData.iData.AccumulativeTotalPaymentAmount) ? null : Decimal.valueOf(gData.iData.AccumulativeTotalPaymentAmount),
                            ArrearsBasePaymentTerm__c = String.isBlank(gData.iData.ArrearsBasePaymentTerm) ? null : Decimal.valueOf(gData.iData.ArrearsBasePaymentTerm),
                            ARAmountBasePaymentTerm__c = String.isBlank(gData.iData.ARAmountBasePaymentTerm) ? null : Decimal.valueOf(gData.iData.ARAmountBasePaymentTerm),
                            ArrearsBaseInvoice__c = String.isBlank(gData.iData.ArrearsBaseInvoice) ? null : Decimal.valueOf(gData.iData.ArrearsBaseInvoice),
                            ARAmountBaseInvoice__c = String.isBlank(gData.iData.ARAmountBaseInvoice) ? null : Decimal.valueOf(gData.iData.ARAmountBaseInvoice),
                            ARAmountBaseAverage__c = String.isBlank(gData.iData.ARAmountBaseAverage) ? null : Decimal.valueOf(gData.iData.ARAmountBaseAverage),
                            AccumulativeARAmountBaseAverage__c = String.isBlank(gData.iData.AccumulativeARAmountBaseAverage) ? null : Decimal.valueOf(gData.iData.AccumulativeARAmountBaseAverage),
                            BaddebtsFlg__c = gData.iData.BaddebtsFlg,
                            MessageGroupNumber__c = rowData.MessageGroupNumber__c
                    );
                    if (gData.iData.BaddebtsFlg == 'X') {
                        rr.BaddebtsAmount__c = String.isBlank(gData.iData.PaymentAmount) ? null : Decimal.valueOf(gData.iData.PaymentAmount);
                        rr.BaddebtsDate__c = NFMUtil.parseStr2Date(gData.iData.PaymentDate);
                    } else {
                        rr.PaymentAmount__c = String.isBlank(gData.iData.PaymentAmount) ? null : Decimal.valueOf(gData.iData.PaymentAmount);
                        rr.PaymentDate__c = NFMUtil.parseStr2Date(gData.iData.PaymentDate);
                    }
                    receiptUniqueMap.put(gData.iData.SFDCRepairNo, rr);
                    receiptLastMap.put(mc.Id, rr);
                }
                else {
                    iflog.ErrorLog__c += 'SFDCRepairNo[' + gData.SFDCRepairNo + ']' + gData.iData.PaymentSource + ':Undefined\n';
                    continue;
                }
            }
            if (receiptUniqueMap.size() > 0) {
                upsert receiptUniqueMap.values() UniqueKey__c;
            }
            // 親の更新
            List<Repair__c> rprList = new List<Repair__c>();
            List<Maintenance_Contract__c> mcList = new List<Maintenance_Contract__c>();
            for (Repair_receipt__c receipt : receiptLastMap.values()) {
                Repair__c rpr = recptsMap.get(receipt.SFDCRepairNo__c);
                Maintenance_Contract__c mc = mcMap.get(receipt.SFDCRepairNo__c);
                if (rpr != null) {
                    if (String.isBlank(rpr.NFM108_Receipt__r.MessageGroupNumber__c) || Integer.valueOf(rpr.NFM108_Receipt__r.MessageGroupNumber__c) <= Integer.valueOf(rowData.MessageGroupNumber__c)) {
                        rpr.NFM108_Receipt__c = receipt.Id;
                        rpr.Paid_Amount__c = receipt.AccumulativeTotalPaymentAmount__c;
                        rpr.Unpaid_Amount_ESAS__c = receipt.ArrearsBaseInvoice__c;
                        rpr.Unpaid_amount_IF__c = receipt.ArrearsBasePaymentTerm__c;
                    }
                    if (receipt.ArrearsBasePaymentTerm__c == 0) {
                        //if (rpr.Received_Date__c == null) {
                            rpr.Received_Date__c = (receipt.PaymentDate__c == null) ? receipt.BaddebtsDate__c : receipt.PaymentDate__c;
                        //} 
                    } else {
                        rpr.Received_Date__c = null;
                    }
                    if (rpr.BaddebtsDate__c == null && receipt.BaddebtsFlg__c == 'X') {
                        rpr.BaddebtsDate__c = receipt.BaddebtsDate__c;
                    }
                    rprList.add(rpr);
                }
                else if (mc != null) {
                    if (String.isBlank(mc.NFM108_Receipt__r.MessageGroupNumber__c) || Integer.valueOf(mc.NFM108_Receipt__r.MessageGroupNumber__c) <= Integer.valueOf(rowData.MessageGroupNumber__c)) {
                        mc.NFM108_Receipt__c = receipt.Id;
                        mc.Recoverable_Amount__c = receipt.AccumulativeTotalPaymentAmount__c;
                        mc.Not_collect_money__c = receipt.ArrearsBaseInvoice__c;
                        mc.Total_not_collect_amount__c = receipt.ArrearsBasePaymentTerm__c;
                    }
                    if (receipt.ArrearsBasePaymentTerm__c == 0) {
                        if (mc.Received_Date__c == null) {
                            mc.Received_Date__c = (receipt.PaymentDate__c == null) ? receipt.BaddebtsDate__c : receipt.PaymentDate__c;
                        }
                    } else {
                        mc.Received_Date__c = null;
                    }
                    if (mc.BaddebtsDate__c == null && receipt.BaddebtsFlg__c == 'X') {
                        mc.BaddebtsDate__c = receipt.BaddebtsDate__c;
                    }
                    mcList.add(mc);
                }
            }
            if (rprList.size() > 0) update rprList;
            if (mcList.size() > 0) update mcList;
            logstr += '\nend';
            rowData.retry_cnt__c=0;
        } catch (Exception ex) {
            // エラーが発生した場合
            Database.rollback(sp);
            System.debug(Logginglevel.ERROR, 'NFM108_' + rowData.MessageGroupNumber__c + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, 'NFM108_' + 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;
    }
}