buli
2022-04-14 ca54382924f5a27063eabbc5b2a81b3d44496b58
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
global class SBG017WebService {
 
    global class GeDatas {
        webservice NFMUtil.Monitoring Monitoring;
        webservice SBG017WebService.GeData[] GeData;
    }
    global class GeData {
        webservice String  OpportunityCode;
        webservice String  SAPQuotationCode;
        webservice String  StockAnswer;
        webservice String  Other1;
        webservice String  Other2;
        webservice String  Other3;
    }
    webservice static void SBG017(GeDatas geDatas) {
        if (geDatas == null) {
            return;
        }
        NFMUtil.Monitoring Monitoring = GeDatas.Monitoring;
        if (Monitoring == null) {
            return;
        }
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, 'SBG017', GeDatas.GeData);
        if (String.isBlank(rowData.Log__c) == false) 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 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 = 'SBG017';
        iflog.MessageGroupNumber__c = rowData.MessageGroupNumber__c;
        iflog.Log__c = logstr;
        iflog.ErrorLog__c = '';
        insert iflog;
        String rowDataStr = NFMUtil.getRowDataStr(rowData);
        List<GeData> achievementList = (List<GeData>) JSON.deserialize(rowDataStr, List<GeData>.class);
        if (achievementList == null || achievementList.size() == 0) {
            return;
        }
 
        Savepoint sp = Database.setSavepoint();
        try {
            // 更新対応配列とMapなどをセット
            List<String> oppNoList = new List<String>();
            for (SBG017WebService.GeData GeData : achievementList) {
                if (GeData.OpportunityCode == null || GeData.OpportunityCode == '' ||
                    GeData.SAPQuotationCode == null || GeData.SAPQuotationCode == '' ||
                    GeData.StockAnswer == null || GeData.StockAnswer == ''
                ) {
                    // 必須項目がない場合、処理と飛ばす
                    continue;
                }
                oppNoList.add(GeData.OpportunityCode);
            }
            List<Opportunity> oppList = [select Id, SAP_QuotationCode__c,InquiryNumber__c,Stock_Answer__c
                                           from Opportunity
                                          where InquiryNumber__c in :oppNoList];
            Map<String, Opportunity> oppsMap = new Map<String, Opportunity>();
            for (Opportunity opp : oppList) {
                oppsMap.put(opp.InquiryNumber__c, opp);
            }
 
            // 更新対応配列をセット
            Map<Id, Opportunity> oppUpdateMap = new Map<Id, Opportunity>();
            for (SBG017WebService.GeData GeData : achievementList) {
                if (GeData.OpportunityCode == null || GeData.OpportunityCode == '') {
                    // 必須項目がない場合、処理と飛ばす
                    iflog.ErrorLog__c += 'OpportunityCode is required\n';
                    continue;
                }
                if (GeData.SAPQuotationCode == null || GeData.SAPQuotationCode == '') {
                    // 必須項目がない場合、処理と飛ばす
                    iflog.ErrorLog__c += 'SAPQuotationCode is required\n';
                    continue;
                }
                if (GeData.StockAnswer == null || GeData.StockAnswer == '') {
                    // 必須項目がない場合、処理と飛ばす
                    iflog.ErrorLog__c += 'StockAnswer is required\n';
                    continue;
                }
                Opportunity opp = oppsMap.get(GeData.OpportunityCode);
                if (opp == null) {
                    // 更新対象商談がない場合
                    iflog.ErrorLog__c += '[' + GeData.OpportunityCode + ']Opportunity[' + GeData.SAPQuotationCode + ']NotExist\n';
                    continue;
                }
                // 項目セット
                //opp.InquiryNumber__c = GeData.OpportunityCode;
                opp.SAP_QuotationCode__c = GeData.SAPQuotationCode;
                opp.Stock_Answer__c            = GeData.StockAnswer;
 
                oppUpdateMap.put(opp.Id, opp);
            }
            if (oppUpdateMap.size() > 0) {
                update oppUpdateMap.values();
            }
            logstr += '\nend';
            rowData.retry_cnt__c=0;
        } catch(Exception ex) {
            // エラーが発生した場合
            Database.rollback(sp);
            System.debug(Logginglevel.ERROR, 'SBG017_' + rowData.MessageGroupNumber__c + ':' + ex.getMessage());
            System.debug(Logginglevel.ERROR, 'SBG017_' + 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;
    }
}