高章伟
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
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
/**
 * @url:/services/apexrest/NFM704/execute
 * 
 */
 
@RestResource(urlMapping='/NFM704/*')
global with sharing class NFM704Rest {
 
    global class GeDatas {
        public NFMUtil.Monitoring Monitoring;
        public  GeData[] GeData;
    }
 
    public static final String LOG_TYPE = 'NFM704';
 
    global class GeData {
        public String StaffMCode;   //SFDC人员管理编码
        public String HospitalMCode;     //医院管理编码
        public String OfficeMCode;   //科室管理编码
        public String HcpNo;    //HPC编码
        public String HCPLevel; //HCP等级
        public String Name;    //姓名
        public String Mobile;   //电话
    }
 
    @HttpPost
    global static void doPost() {
        //取得接口传输内容
        String strData = RestContext.request.requestBody.toString();
        GeDatas ges = (GeDatas) JSON.deserializeStrict(strData, GeDatas.class);
    
        if(ges == null) {
            return;
        }
 
        NFMUtil.Monitoring Monitoring = ges.Monitoring;
        if(Monitoring == null ) {
            return;
        }
 
        BatchIF_Log__c rowData = NFMUtil.saveRowData(Monitoring, LOG_TYPE, ges.GeData);
        system.debug('String.isBlank(rowData.Log__c) == false?');
        if(String.isBlank(rowData.Log__c) == false){
            System.debug('进入if方法,成功调用executefuture');
            executefuture(rowData.Id);
        }
 
        // JSONを戻す
        RestResponse res = RestContext.response;
        res.addHeader('Content-Type', 'application/json');
        res.statusCode = 200;
        String jsonResponse = '{"Result":{"Result": "00", "Message":""}}';
        res.responseBody = blob.valueOf(jsonResponse);
        return;
 
    }
 
    @future(callout = true)
    global static void executefuture(String rowData_Id){
        main(rowData_Id);
    }
 
    global static void main(String rowData_Id){
 
        String logstr = 'start\n';
        BatchIF_Log__c iflog = new BatchIF_Log__c();
        iflog.Type__c = LOG_TYPE;
        iflog.ErrorLog__c = '';
        iflog.Log__c = logstr;
        insert iflog;
 
        GeDatas gds = new GeDatas();
        gds.GeData = new List< GeData >();
 
        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, retry_cnt__c from BatchIF_Log__c where RowDataFlg__c = true and Id =: rowData_Id];
 
        String rowDataStr = NFMUtil.getRowDataStr(rowData);
 
        List < GeData > itemMasterList = (List < GeData > ) JSON.deserialize(rowDataStr, List < GeData > .class);
        logstr += '数据总数为:' + itemMasterList.size() + '\n';
        if (itemMasterList == null || itemMasterList.size() == 0) {
            return;
        }
 
        Savepoint sp = Database.setSavepoint();
 
        try{
            //管理编码List
            List<String> DoctorNoList = new List<String>();
 
            //待更新的客户人员List
            List<Contact> updateList = new List<Contact>();
 
            //科室编码List
            List<String> officeMCodeList = new List<String>();
 
            for(GeData ged : itemMasterList){
                String dataComplete = verify(ged);
                if (!String.isBlank(dataComplete)) {
                    logstr += dataComplete;
                    continue;
                }
                DoctorNoList.add(ged.StaffMCode);
                officeMCodeList.add(ged.OfficeMCode);
            }
 
            Map<String, Contact> conListMap = new Map<String, Contact>();
            List<Contact> conList = [select Id,
                                            CManageCode__c,     //人员管理编码
                                            Account.Hospital_Name__c,       //医院名称
                                            Account.Department_Name__c,       //科室名称
                                            Name,       //姓名
                                            Phone,      //电话
                                            HcpNo__c,       //HCP编码
                                            Isactive__c,    //是否有效
                                            UpdateStatus__c            //操作类型
                                            from Contact WHERE CManageCode__c IN:DoctorNoList order by AccountId];
 
            
            for(Contact con : conList ){
                conListMap.put(con.CManageCode__c, con);
            }
 
            //Management_Code__c
            List<Account> accList = [select Id, Name, Management_Code__c from Account where Management_Code__c in :officeMCodeList];
 
            Map<String, Account> accMap = new Map<String, Account>();
 
            for(Account acc : accList){
                accMap.put(acc.Management_Code__c, acc);
            }
 
            for( GeData ged : itemMasterList ){
 
                Contact con = new Contact();
 
                logstr += conListMap.get(ged.StaffMCode).Id;
 
                if(conListMap.containsKey(ged.StaffMCode)){
 
                    logstr += '已存在客户人员\n';
                    con.Id = conListMap.get(ged.StaffMCode).Id;
 
                }else {
                    logstr += '不存在的客户人员\n';
                }
 
                //AccountId
                if( accMap.get(ged.OfficeMCode) != null ){
                    con.AccountId = accMap.get(ged.OfficeMCode).Id;
                }else {
                    logstr += '当前科室id有误或不存在';
                }
 
                con.HcpNo__c = ged.HcpNo;
                con.HCPLevel__c = ged.HCPLevel;
                con.LastName = ged.Name;
                con.MobilePhone = ged.Mobile;
 
                conListMap.put(ged.StaffMCode, con);
                
            }
 
            if(conListMap.size() > 0 ){
                // Update updateList;
                UpSert conListMap.values();
            }
 
            rowData.retry_cnt__c = 0;
            logstr += '\nend';
 
        }catch (Exception ex) {
            Integer batch_retry_max_cnt = Integer.valueOf(System.Label.batch_retry_max_cnt);
            // エラーが発生した場合
            Database.rollback(sp);
            logstr += '\n' + ex.getMessage();
            iflog.ErrorLog__c = ex.getMessage() + '\n' + ex.getStackTraceString() + '\n' + iflog.ErrorLog__c;
            // 异常重发
            rowData = NFMUtil.LogAutoSend(rowData, ex, null);
        }
        iflog.Log__c = logstr;
        upsert iflog;
    }
 
    public static String verify(GeData ged){
        String result = '';
 
        if (ged.StaffMCode == null) {
            result += 'DataError: SFDC人员管理编码 [ StaffMCode ] is null!\n';
        }
        if (ged.HospitalMCode == null) {
            result += 'DataError: 医院管理编码 [ HospitalMCode ] is null!\n';
        }
        if (ged.OfficeMCode == null) {
            result += 'DataError: 科室管理编码 [ OfficeMCode ] is null!\n';
        }
        if (ged.HcpNo == null) {
            result += 'DataError: HCP编码 [ HcpNo ] is null!\n';
        }
        if (ged.HCPLevel == null) {
            result += 'DataError: HCP等级 [ HCPLevel ] is null!\n';
        }
        if (ged.Name == null) {
            result += 'DataError: 姓名 [ Name ] is null!\n';
        }
        if (ged.Mobile == null) {
            result += 'DataError: 电话 [ Mobile ] is null!\n';
        }
 
        return result;
    }
 
}