高章伟
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
public without sharing class NFM605Controller {
    private static final String LOG_TYPE = 'NFM605';
    private static final String API = '/admin/api/scd/save';
 
    //服务合同数据推送接口
    public class ContractInfo {
        public String contractCd; //合同编码
        public String hospitalCd; //医院编码
        public String strategicDepartmenCd; //战略科室编码
        public String departmentCd; //科室编码
        public String contractStartDate; //合同起始日期
        public String contractEndDate; //合同结束日期
        public String contractEquipQty; //合同设备量
        public String reportSeason; //报告季度
        public String reportSeasonStartDate; //报告季度开始时间
        public String reportSeasonEndDate; //报告季度结束时间
        public String reportSendTime; //报告发放时间
        public Decimal CurDistributionTimes; //目前应发放次数
        public Decimal ActDistributionTimes; //实际发放次数
    }
    //正常调用
    @future(callout = true)
    public static void callout(String iflog_Id, List < Id > reportIdList) {
        executeNotFuture(iflog_Id, reportIdList);
    }
    //Batch调用
    public static void executeNotFuture(String iflog_Id, List < Id > reportIdList) {
 
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        Datetime nowDT = Datetime.now();
        String nowStr = nowDT.format('yyyyMMddHHmmss');
        if (String.isNotBlank(iflog_Id)) {
            iflog = [Select Id, Name, Log__c, ErrorLog__c from BatchIF_Log__c where Id = : iflog_Id];  
            if (iflog == null) {
                iflog = new BatchIF_Log__c();
            } 
        }
        iflog.Type__c = LOG_TYPE;
        iflog.ErrorLog__c = '';
        iflog.Log__c = 'callout start\n';
        iflog.MessageGroupNumber__c = nowStr;
 
 
 
 
        List < ContractInfo > contrs = new List < ContractInfo > ();
        BatchIF_Log__c rowData = null;
        List < NewMaintenanceReport_Task__c > nmtreportList = [select id,
            NewMaintenance_Contract__r.Maintenance_Contract_No__c,
            NewMaintenance_Contract__r.Hospital__r.Management_Code__c,
            NewMaintenance_Contract__r.Department_Class__r.Management_Code__c,
            NewMaintenance_Contract__r.Department_Class__r.Management_Code_Auto__c,
            NewMaintenance_Contract__r.Department__r.Management_Code__c,
            NewMaintenance_Contract__r.Contract_Start_Date__c,
            NewMaintenance_Contract__r.Contract_End_Date__c,
            NewMaintenance_Contract__r.New_equipment_quantity1__c,
            NewMaintenance_Contract__r.Estimate_Asset_Cnt__c,
            Distribution_Quarter__c, Distribution_Start_Date__c,
            Distribution_End_Date__c,
            CorrespondingPeriod__c,
            NewMaintenance_Contract__r.NumberOfPlan_Today__c,
            NewMaintenance_Contract__r.ActualFrequency_Formula__c
            from NewMaintenanceReport_Task__c where id in : reportIdList
        ];
        String logstr = iflog.Log__c + ' ' + 'NumberOfRecord=' + nmtreportList.size() + '\n';
        try {
            for (NewMaintenanceReport_Task__c n: nmtreportList) {
                ContractInfo cont = new ContractInfo();
                cont.contractCd = n.NewMaintenance_Contract__r.Maintenance_Contract_No__c; //合同编码
                cont.hospitalCd = n.NewMaintenance_Contract__r.Hospital__r.Management_Code__c; //医院编码
                cont.strategicDepartmenCd = n.NewMaintenance_Contract__r.Department_Class__r.Management_Code__c; //战略科室编码
                
                cont.departmentCd = n.NewMaintenance_Contract__r.Department__r.Management_Code__c; //科室编码
                cont.contractStartDate = NFMUtil.formatDate2StrDateTime(n.NewMaintenance_Contract__r.Contract_Start_Date__c); //合同起始日期
                cont.contractEndDate = NFMUtil.formatDate2StrDateTime(n.NewMaintenance_Contract__r.Contract_End_Date__c); //合同结束日期
                cont.contractEquipQty = n.NewMaintenance_Contract__r.Estimate_Asset_Cnt__c + ''; //合同设备量
                cont.reportSeason = n.Distribution_Quarter__c; //报告季度
                cont.reportSeasonStartDate = NFMUtil.formatDate2StrDateTime(n.Distribution_Start_Date__c); //报告季度开始时间
                cont.reportSeasonEndDate = NFMUtil.formatDate2StrDateTime(n.Distribution_End_Date__c); //报告季度结束时间
                cont.reportSendTime = NFMUtil.formatDate2StrDateTime(n.CorrespondingPeriod__c); //报告发放时间
                cont.CurDistributionTimes = n.NewMaintenance_Contract__r.NumberOfPlan_Today__c; //目前应发放次数
                cont.ActDistributionTimes = n.NewMaintenance_Contract__r.ActualFrequency_Formula__c; //实际发放次数
                contrs.add(cont);
            }
            logstr += '\nend';
            if (contrs.size() > 0) {
                //生成BatchLog日志,记录发送情况
                rowData = NFMUtil.makeRowData(iflog, LOG_TYPE, contrs);
                rowData.MessageGroupNumber__c = nowStr;
                //insert rowData;
                execute(rowData, iflog);
            }
            if(Test.isRunningTest()){
                Integer num = Integer.valueOf('TestError');
            }
        } catch (Exception ex) {
            System.debug(LoggingLevel.ERROR, 'NFM_SA_SERVICECONTRACTDATE' + iflog.Name + ':' + ex.getMessage());
            System.debug(LoggingLevel.ERROR, 'NFM_SA_SERVICECONTRACTDATE' + iflog.Name + ':' + ex.getStackTraceString());
            logstr += ex.getMessage();
            iflog.ErrorLog__c += ex.getMessage() + '\n';
            iflog.ErrorLog__c += ex.getStackTraceString() + '\n';
        }
        if (rowData != null) {
            upsert rowData;
        }
        iflog.Log__c += logstr;
        upsert iflog;
    }
    /**
     * 接口发送失败 根据BatchLogId进行手动发送
     * @Author   
     * @DateTime 
     * @param    rowDataId  [BatchLogId]
     */
    public static void ManualExecute(String rowDataId) {
        List < BatchIF_Log__c > row = [select id, name, MessageGroupNumber__c, retry_cnt__c,
            RowDataFlg__c, ErrorLog__c, Type__c,
            Log__c, Log2__c, Log3__c, Log4__c, Log5__c, Log6__c,
            Log7__c, Log8__c, Log9__c, Log10__c, Log11__c, Log12__c
            from BatchIF_Log__c
            where id = : rowDataId
        ];
        if (row.size() > 0) execute(row[0], null);
    }
    /**
     * 逻辑正常执行发送
     * @Author   
     * @DateTime 
     * @param    rowDataSFDC [BatchLogId]
     * @param    iflog       [执行情况记录]
     */
    public static void execute(BatchIF_Log__c rowDataSFDC, BatchIF_Log__c iflog) {
        Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
        String logstr = rowDataSFDC.MessageGroupNumber__c + ' start\n';
        if (iflog == null) {
            iflog = new BatchIF_Log__c();
        } else {
            
            logstr += iflog.Log__c;
        }
        iflog.Type__c = LOG_TYPE;
        iflog.MessageGroupNumber__c = rowDataSFDC.MessageGroupNumber__c;
        iflog.ErrorLog__c = '';
        try {
            String data = NFMUtil.getRowDataStr(rowDataSFDC);
            String status = NFMUtil.sendToAWS(data, API);
            System.debug('NFM605Log--status->'+ status);
 
            if ('OK'.equals(status)) {
                logstr += status + '\n';
                rowDataSFDC.retry_cnt__c = 0;
            } else {
                rowDataSFDC = NFMUtil.LogAutoSend(rowDataSFDC, null, status);
            }
        } catch (Exception ex) {
            // TODO IOException
            
            iflog.ErrorLog__c += ex.getMessage() + '\n';
            iflog.ErrorLog__c += ex.getStackTraceString() + '\n';
            rowDataSFDC = NFMUtil.LogAutoSend(rowDataSFDC, ex, null);
        }
        upsert rowDataSFDC;
        iflog.Log__c +=  logstr;
        upsert iflog;
    }
}