forked from kidgrow-microservices-platform

luliqiang
2021-02-19 e613dc170308423742325db4a8bd9a3e01659add
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package com.kidgrow.oprationcenter.service.impl;
 
import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kidgrow.common.constant.SecurityConstants;
import com.kidgrow.common.model.PageResult;
import com.kidgrow.common.model.ResultBody;
import com.kidgrow.common.service.impl.SuperServiceImpl;
import com.kidgrow.common.utils.EncryptUtils;
import com.kidgrow.oprationcenter.mapper.PartnerDockingInfoMapper;
import com.kidgrow.oprationcenter.model.PartnerDockingInfo;
import com.kidgrow.oprationcenter.model.PartnerInfo;
import com.kidgrow.oprationcenter.model.ProductOrder;
import com.kidgrow.oprationcenter.model.ProductOrderRecord;
import com.kidgrow.oprationcenter.service.IPartnerDockingInfoService;
import com.kidgrow.oprationcenter.service.IPartnerInfoService;
import com.kidgrow.oprationcenter.service.IProductOrderRecordService;
import com.kidgrow.oprationcenter.service.IProductOrderService;
import com.kidgrow.usercenter.feign.SysDepartmentService;
import com.kidgrow.usercenter.feign.SysDoctorService;
import com.kidgrow.usercenter.feign.SysHospitalService;
import com.kidgrow.usercenter.model.SysDoctor;
import com.kidgrow.usercenter.model.SysHospital;
import io.undertow.util.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
 
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
 *
 * @version 1.0
 * @Description: 合作商客户对接信息表<br>
 * @Project: 运营中心<br>
 * @CreateDate: Created in 2020-12-08 14:35:33 <br>
 * @Author: <a href="1024030301@kidgrow.com">LuLiQiang</a>
 */
@Slf4j
@Service
public class PartnerDockingInfoServiceImpl extends SuperServiceImpl<PartnerDockingInfoMapper, PartnerDockingInfo> implements IPartnerDockingInfoService {
 
    @Autowired
    ResourceLoader resourceLoader;
 
    @Autowired
    SysHospitalService sysHospitalService;
 
    @Autowired
    SysDepartmentService sysDepartmentService;
 
    @Autowired
    SysDoctorService sysDoctorService;
 
    @Autowired
    IPartnerInfoService partnerInfoService;
 
    @Autowired
    IProductOrderService productOrderService;
 
    @Autowired
    IProductOrderRecordService productOrderRecordService;
 
    /**
     * 列表
     *
     * @param params
     * @return
     */
    @Override
    public PageResult<PartnerDockingInfo> findList(Map<String, Object> params) {
        Page<PartnerDockingInfo> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
        List<PartnerDockingInfo> list = baseMapper.findList(page, params);
        return PageResult.<PartnerDockingInfo>builder().data(list).code(0).count(page.getTotal()).build();
    }
 
    /**
     * 根据PartnerDockingInfo对象当做查询条件进行查询
     *
     * @param partnerDockingInfo
     * @return PartnerDockingInfo
     */
    @Override
    public PartnerDockingInfo findByObject(PartnerDockingInfo partnerDockingInfo) {
        return baseMapper.findByObject(partnerDockingInfo);
    }
 
 
    /**
     * 根据对接类型版本等信息,创建客户私有云数据库脚本
     *
     * @param partnerDockingInfo
     * @return
     */
    @Override
    public String createSQL(PartnerDockingInfo partnerDockingInfo) {
        // 模板SQL文
        String dbSql = "";
        // 医院信息SQL文
        String hospitalSQL = "";
        // 科室信息SQL文
        String departmentSQL = "";
        // 医生信息SQL文
        String doctorSQL = "";
        // 合作商信息SQL文
        String partnerSQL = "";
        // 服务器信息SQL文
        String serverSQL = "";
        // 服务期限和数量的SQL文
        String serviceSQL = "";
 
        InputStream fileImput = null;
        // 时间转换格式
        SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 
        String createTime = "";
        String updateTime = "";
        try {
            // 1、读取基础表结构SQL
            Resource resource = resourceLoader.getResource("classpath:template//imageServer_template.sql");
            fileImput = resource.getInputStream();  //new FileInputStream("./template/imageServer_template.sql");
            dbSql = FileUtils.readFile(fileImput);
 
            // 2、读取医院信息表Hospital_Info,插入SQL文中
            Map<String, Object> hospitalMap = new HashMap<>();
            hospitalMap.put("id", partnerDockingInfo.getHospitalId());
            ResultBody hospitalResult = sysHospitalService.findAllByMap(hospitalMap);
 
            if (hospitalResult.isOk()) {
                String jsonData = JSONObject.toJSONString(hospitalResult.getData());
                List<SysHospital> hospitalLists = JSONObject.parseArray(jsonData, SysHospital.class);
                if (hospitalLists.size() > 0) {
                    SysHospital hospitalInfo = hospitalLists.get(0);
                    createTime = "'" + dateTimeFormat.format(hospitalInfo.getCreateTime()) + "'";
                    updateTime = (hospitalInfo.getUpdateTime() == null) ? "NULL" : "'" + dateTimeFormat.format(hospitalInfo.getUpdateTime())+ "'";
                    hospitalSQL = " INSERT INTO sys_hospital  VALUES (" +
                            hospitalInfo.getId() + ", " +
                            "'" + hospitalInfo.getHospitalName() + "', " +
                            "'" + hospitalInfo.getHospitalShortName() + "', " +
                            "'" + hospitalInfo.getHospitalCode() + "', " +
                            "'" + hospitalInfo.getHospitalTypeId() + "', " +
                            "'" + hospitalInfo.getHospitalTypeName() + "', " +
                            "'" + hospitalInfo.getHospitalTel() + "', " +
                            "'" + hospitalInfo.getHospitalProvince() + "', " +
                            "'" + hospitalInfo.getHospitalCity() + "', " +
                            "'" + hospitalInfo.getHospitalArea() + "', " +
                            "'" + hospitalInfo.getAreaCode() + "', " +
                            "'" + hospitalInfo.getHospitalLink() + "', " +
                            "'" + hospitalInfo.getHospitalAdress() + "', " +
                            "'" + hospitalInfo.getLatitude() + "', " +
                            "'" + hospitalInfo.getLongitude() + "', " +
                            "'" + hospitalInfo.getHospitalQualifiedId() + "', " +
                            "'" + hospitalInfo.getHospitalQualifiedName() + "', " +
                            "NULL," +
                            "NULL," +
                            "NULL," +
                            "'" + hospitalInfo.getHospitalAbout() + "', " +
                            hospitalInfo.getHospitalState() + ", " +
                            hospitalInfo.getEnabled() + ", " +
                            hospitalInfo.getIsDel() + ", " +
                            hospitalInfo.getCreateUserId() + ", " +
                            "'" + hospitalInfo.getCreateUserName() + "', " +
                            hospitalInfo.getUpdateUserId() + ", " +
                            "'" + hospitalInfo.getUpdateUserName() + "', " +
                            createTime + ", " +
                            updateTime + " " +
                            ");\n\r";
                } else {
                    return "医院信息读取失败,请重新生成SQL!";
                }
            } else {
                return "医院信息读取失败,请重新生成SQL!";
            }
 
            // 3、读取科室信息表Department_Info,生成SQL文
            departmentSQL = "INSERT INTO sys_department VALUES (" +
                    +partnerDockingInfo.getDepartmentId() + ", " +
                    "'" + partnerDockingInfo.getDepartmentName() + "', " +
                    +partnerDockingInfo.getHospitalId() + ", " +
                    "0, 1, 0, '喜高科技', 0 , '喜高科技', " +
                    "'" + DateTime.now().toString() + "', " +
                    "'" + DateTime.now().toString() + "', " +
                    "6 " +
                    ");\n\r";
            // 4、读取医生信息表Doctor_Info,生成SQL文
            Map<String, Object> doctorMap = new HashMap<>();
            doctorMap.put("hospital_id", partnerDockingInfo.getHospitalId());
            doctorMap.put("department_id", partnerDockingInfo.getDepartmentId());
            doctorMap.put("is_admin_user", 1);
            ResultBody doctorResult = sysDoctorService.getListByMap(doctorMap);
 
            if (doctorResult.isOk()) {
                String jsonData = JSONObject.toJSONString(doctorResult.getData());
                List<SysDoctor> doctorList = JSONObject.parseArray(jsonData, SysDoctor.class);
                if (doctorList.size() > 0) {
                    SysDoctor doctorInfo = doctorList.get(0);
                    doctorSQL = "INSERT INTO sys_doctor VALUES(" +
                            doctorInfo.getId() + ", " +
                            +doctorInfo.getHospitalId() + ", " +
                            "'" + doctorInfo.getHospitalName() + "', " +
                            +doctorInfo.getDepartmentId() + ", " +
                            "'" + doctorInfo.getDepartmentName() + "', " +
                            "'" + doctorInfo.getDoctorName() + "', " +
                            "NULL, NULL, " +
                            "'" + doctorInfo.getDoctorTel() + "', " +
                            "'" + DigestUtils.md5DigestAsHex("123456".getBytes()).replace("-", "") + "', " +
                            "'" + doctorInfo.getDoctorRankId() + "', " +
                            "'" + doctorInfo.getDoctorRank() + "', " +
                            "'" + doctorInfo.getDoctorOtherLink() + "', " +
                            "'" + doctorInfo.getDoctorEmail() + "', " +
                            "'" + doctorInfo.getDoctorAbout() + "', " +
                            "0, " +
                            doctorInfo.getDoctorState() + ", " +
                            doctorInfo.getDoctorType() + ", " +
                            "1, 0, '喜高科技', 0 , '喜高科技', " +
                            "'" + DateTime.now().toString() + "', " +
                            "'" + DateTime.now().toString() + "', " +
                            "1" +
                            ");\n\r";
                } else {
                    return "医生信息读取失败,请先创建管理员,再重新生成SQL!";
                }
            } else {
                return "医生信息读取失败,请先创建管理员,再重新生成SQL!";
            }
 
            // 5、读取合作商信息表Partner_Info,生成SQL文
            PartnerInfo partnerInfo = partnerInfoService.getById(partnerDockingInfo.getPartnerId());
            if (partnerInfo != null) {
                createTime = "'" + dateTimeFormat.format(partnerInfo.getCreateTime()) + "'";
                updateTime = (partnerInfo.getUpdateTime() == null) ? "NULL" : "'" + dateTimeFormat.format(partnerInfo.getUpdateTime()) + "'";
 
                partnerSQL = "INSERT INTO partner_info VALUES (" +
                        partnerInfo.getId() + ", " +
                        "'" + partnerInfo.getPartnerName() + "', " +
                        +partnerInfo.getPartnerType() + ", " +
                        "'" + partnerInfo.getPartnerAddress() + "', " +
                        "'" + partnerInfo.getPartnerProvince() + "', " +
                        "'" + partnerInfo.getPartnerCity() + "', " +
                        "'" + partnerInfo.getPartnerArea() + "', " +
                        "'" + partnerInfo.getPartnerLinkMan() + "', " +
                        "'" + partnerInfo.getPartnerLinkTelephone() + "', " +
                        "'" + partnerInfo.getPartnerBussinessArea() + "', " +
                        "'" + partnerInfo.getPartnerIntroduce() + "', " +
                        "'" + partnerInfo.getPartnerUniqueCode() + "', " +
                        "0, 1, " + partnerInfo.getCreateUserId() + ", " +
                        "'" + partnerInfo.getCreateUserName() + "', " +
                        createTime + ", " +
                        +partnerInfo.getUpdateUserId() + ", " +
                        "'" + partnerInfo.getUpdateUserName() + "', " +
                        updateTime +
                        " );\n\r";
            } else {
                return "合作商信息读取失败,请重新生成SQL!";
            }
            // 6、读取服务器信息表Server_Info,生成SQL文
            createTime = "'"+ dateTimeFormat.format(partnerDockingInfo.getCreateTime())+"'";
            updateTime = (partnerDockingInfo.getUpdateTime() == null) ? "NULL" : "'"+ dateTimeFormat.format(partnerDockingInfo.getUpdateTime())+"'";
 
            serverSQL = "INSERT INTO server_info VALUES(" +
                    partnerDockingInfo.getId() + ", " +
                    partnerDockingInfo.getPartnerId() + ", " +
                    partnerDockingInfo.getHospitalId() + ", " +
                    partnerDockingInfo.getDepartmentId() + ", " +
                    "NULL, NULL, NULL, NULL, ";
            // 判断机器码是否为空,拼接不同的字符串。
            if ((partnerDockingInfo.getServerMachineCode() == null) || (partnerDockingInfo.getServerMachineCode().isEmpty())) {
                serverSQL += "NULL, ";
            } else {
                serverSQL += "'" + partnerDockingInfo.getServerMachineCode() + "', ";
            }
            serverSQL += "NULL, NULL, " +
                    "'" + partnerDockingInfo.getAccessKey() + "', " +
                    "0, 1, " +
                    partnerDockingInfo.getCreateUserId() + ", " +
                    "'" + partnerDockingInfo.getCreateUserName() + "', " +
                    createTime + ", " +
                    partnerDockingInfo.getUpdateUserId() + ", " +
                    "'" + partnerDockingInfo.getUpdateUserName() + "', " +
                    updateTime + " " +
                    ");\n\r";
 
            // 7、订单、订单明细(服务期限和数量)的数据生成
            // 7.1 订单数据
            Map<String, Object> orderMap = new HashMap<>();
            orderMap.put("hospital_id", partnerDockingInfo.getHospitalId());
            orderMap.put("department_id", partnerDockingInfo.getDepartmentId());
            orderMap.put("enabled", 1);
            orderMap.put("is_del", 0);
            List<ProductOrder> productOrderList = productOrderService.listByMap(orderMap);
            if ((productOrderList != null) && (productOrderList.size() > 0)) {
                for (int i = 0; i < productOrderList.size(); i++) {
                    ProductOrder productOrder = productOrderList.get(i);
                    createTime = "'"+dateTimeFormat.format(productOrder.getCreateTime()) +"'";
                    updateTime = (productOrder.getUpdateTime() == null) ? "NULL" : "'"+dateTimeFormat.format(productOrder.getUpdateTime())+"'";
                    serviceSQL += "INSERT INTO order_record VALUES(" +
                            productOrder.getId() + ", " +
                            productOrder.getHospitalId() + ", " +
                            "'" + productOrder.getHospitalName() + "', " +
                            productOrder.getDepartmentId() + ", " +
                            "'" + productOrder.getDepartmentName() + "', " +
                            "'" + productOrder.getContractNo() + "', " +
                            "'" + productOrder.getContractTitle() + "', " +
                            "'" + dateFormat.format(productOrder.getContractBeginTime()) + "', " +
                            "'" + dateFormat.format(productOrder.getContractEndTime()) + "', " +
                            "'" + dateTimeFormat.format(productOrder.getContractTime()) + "', " +
                            productOrder.getIsDel() + ", " +
                            productOrder.getEnabled() + ", " +
                            createTime + ", " +
                            productOrder.getCreateUserId() + ", " +
                            "'" + productOrder.getCreateUserName() + "', " +
                            updateTime + ", " +
                            productOrder.getUpdateUserId() + ", " +
                            "'" + productOrder.getUpdateUserName() + "' " +
                            ");\n\r";
 
                    // 7.2 订单明细数据
                    orderMap.clear();
                    orderMap.put("order_id", productOrder.getId());
                    orderMap.put("enabled", 1);
                    orderMap.put("is_del", 0);
                    List<ProductOrderRecord> productOrderRecordList = productOrderRecordService.listByMap(orderMap);
 
                    if ((productOrderRecordList != null) && (productOrderRecordList.size() > 0)) {
 
                        for (i = 0; i < productOrderRecordList.size(); i++) {
                            ProductOrderRecord productOrderRecord = productOrderRecordList.get(i);
                            createTime = "'"+dateTimeFormat.format(productOrderRecord.getCreateTime())+"'";
                            updateTime = (productOrderRecord.getUpdateTime() == null) ? "NULL" : "'"+dateTimeFormat.format(productOrderRecord.getUpdateTime())+"'";
                            serviceSQL += "INSERT INTO order_detail VALUES( " +
                                    productOrderRecord.getId() + ", " +
                                    productOrderRecord.getOrderId() + ", " +
                                    productOrderRecord.getProId() + ", " +
                                    productOrderRecord.getProType() + ", " +
                                    "'" + productOrderRecord.getProName() + "', " +
                                    productOrderRecord.getAilightCount() + ", " +
                                    productOrderRecord.getRecordCount() + ", " +
                                    "'" + dateFormat.format(productOrderRecord.getProBegintime()) + "', " +
                                    "'" + dateFormat.format(productOrderRecord.getProEndtime()) + "', " +
                                    productOrderRecord.getIsShare() + ", " +
                                    "0, 0, " +
                                    // 服务验证字符串【合作商ID、医院ID、科室ID、产品ID、开始日期、结束日期、读片数量、报告数量、已使用读片数量、已使用报告数量】
                                    "'" + EncryptUtils.encryptAES((partnerDockingInfo.getPartnerId().toString() + "|" +
                                    partnerDockingInfo.getHospitalId().toString() + "|" +
                                    partnerDockingInfo.getDepartmentId().toString() + "|" +
                                    productOrderRecord.getProId().toString() + "|" +
                                    dateFormat.format(productOrderRecord.getProBegintime()) + "|" +
                                    dateFormat.format(productOrderRecord.getProEndtime()) + "|" +
                                    productOrderRecord.getAilightCount().toString() + "|" +
                                    productOrderRecord.getRecordCount() + "|" +
                                    "0|0"), SecurityConstants.AES_KEY) + "', " +
                                    productOrderRecord.getIsDel() + ", " +
                                    productOrderRecord.getEnabled() + ", " +
                                    createTime + ", " +
                                    productOrderRecord.getCreateUserId() + ", " +
                                    "'" + productOrderRecord.getCreateUserName() + "', " +
                                    updateTime + ", " +
                                    productOrderRecord.getUpdateUserId() + ", " +
                                    "'" + productOrderRecord.getUpdateUserName() + "' " +
                                    ");\n\r";
                        }
                    } else {
                        return "没有已开通的服务记录,请先充值,再生成SQL文";
                    }
                }
            } else {
                return "没有已开通的服务记录,请先充值,再生成SQL文";
            }
 
 
            // 8、所有SQL文合并入基本的SQL中
            dbSql += hospitalSQL + departmentSQL + doctorSQL + partnerSQL + serverSQL + serviceSQL;
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                fileImput.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 
 
        return dbSql;
    }
 
    ;
}