forked from kidgrow-microservices-platform

zhaoxiaohao
2021-04-15 7748c09b5260b86bc2ad750e3393bbdb85e1b41c
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
package com.kidgrow.usercenter.service.impl;
 
import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kidgrow.common.constant.CommonConstant;
import com.kidgrow.common.model.PageResult;
import com.kidgrow.common.model.ResultBody;
import com.kidgrow.common.model.SysOrganization;
import com.kidgrow.common.model.UserType;
import com.kidgrow.common.service.impl.SuperServiceImpl;
import com.kidgrow.common.utils.DateUtils;
import com.kidgrow.oprationcenter.feign.ProductOrderService;
import com.kidgrow.oprationcenter.model.ProductOrder;
import com.kidgrow.oprationcenter.model.ProductOrderDetail;
import com.kidgrow.usercenter.mapper.SysDepartmentMapper;
import com.kidgrow.usercenter.model.SysDepartment;
import com.kidgrow.usercenter.model.SysHospital;
import com.kidgrow.usercenter.service.ISysDepartmentService;
import com.kidgrow.usercenter.service.ISysHospitalService;
import com.kidgrow.usercenter.service.ISysOrganizationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
 
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020<br>
 *
 * @version 1.0
 * @Description: 科室表<br>
 * @Project: 用户中心<br>
 * @CreateDate: Created in 2020-04-02 14:02:50 <br>
 * @Author: <a href="4345453@kidgrow.com">liuke</a>
 */
@Slf4j
@Service
public class SysDepartmentServiceImpl extends SuperServiceImpl<SysDepartmentMapper, SysDepartment> implements ISysDepartmentService {
 
    @Autowired
    private ISysOrganizationService iSysOrganizationService;
    @Autowired
    private ISysHospitalService iSysHospitalService;
 
    @Autowired
    private ProductOrderService productOrderService;
 
    @Autowired
    private PasswordEncoder passwordEncoder;
 
    @Value("${spring.profiles.active}")
    private String envName;
 
    /**
     * 列表
     *
     * @param params
     * @return
     */
    @Override
    public PageResult<SysDepartment> findList(Map<String, Object> params) {
        Page<SysDepartment> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
        List<SysDepartment> list = baseMapper.findList(page, params);
        return PageResult.<SysDepartment>builder().data(list).code(0).count(page.getTotal()).build();
    }
 
    /**
     * 根据SysDepartment对象当做查询条件进行查询
     *
     * @param sysDepartment
     * @return SysDepartment
     */
    @Override
    public SysDepartment findByObject(SysDepartment sysDepartment) {
        return baseMapper.findByObject(sysDepartment);
    }
 
    @Override
    public boolean updatePay(Long departmentId, boolean isPay) {
        return baseMapper.updatePay(departmentId, isPay);
    }
 
    @Override
    public ResultBody findAll(Map<String, Object> params) {
        return ResultBody.ok().data(baseMapper.selectByMap(params));
    }
 
    @Override
    public ResultBody findListByHospitalId(Map<String, Object> params) {
        //查询组织
        Long id = MapUtils.getLong(params, "id");
        List<SysDepartment> sysDepartments = new ArrayList<>();
        SysHospital byId = iSysHospitalService.getById(id);
        if (byId != null) {
            params = new HashMap<>();
            params.put("org_parent_id", byId.getOrgId());
            List<SysOrganization> sysOrganizations = iSysOrganizationService.listByMap(params);
            if (sysOrganizations.size() > 0) {
                List<Long> collect = sysOrganizations.stream().map(e -> e.getId()).collect(Collectors.toList());
                QueryWrapper<SysDepartment> queryWrapper = new QueryWrapper();
                queryWrapper.in("org_id", collect);
                sysDepartments = baseMapper.selectList(queryWrapper);
            }
        }
        return ResultBody.ok().data(sysDepartments);
    }
 
    @Override
    public String checkDepartmentName(Long hosId, String departmentName) {
        String departName = baseMapper.checkDepartmentName(hosId, departmentName);
        return departName;
    }
 
    @Override
    public ResultBody getHealth(SysDepartment sysDepartment) {
        SysDepartment department = baseMapper.selectById(sysDepartment.getId());
        if (department == null) {
            return ResultBody.failed("该数据为空");
        } else {
            if (department.getIsHealth()) {
                Date now = new Date();
                Date endDate = DateUtils.addDays(department.getHealthEndTime(), 1);
                if (now.getTime() < department.getHealthBeginTime().getTime()) {
                    department.setIsHealth(false);
                    return ResultBody.ok().data(department);
                } else if (department.getHealthBeginTime().getTime() <= now.getTime() && now.getTime() <= endDate.getTime()) {
                    return ResultBody.ok().data(department);
                } else if (endDate.getTime() < now.getTime()) {
                    department.setIsHealth(false);
                    return ResultBody.ok().data(department);
                }
                return ResultBody.ok();
            } else {
                return ResultBody.ok().data(false);
            }
        }
    }
 
    /**
     * 初始化私有云本地数据库
     *
     * @param hospitalId 医院ID
     * @param departmentId 科室ID
     * @return java.lang.String 私有云数据库初始化SQL
     */
    public String initPrivateData(Long hospitalId, Long departmentId) {
        String initSQL = "";            // 初始化数据SQL
        String organizationSQL = "";    // 组织数据SQL
        String hospitalSQL = "";        // 医院数据SQL
        String departmentSQL = "";      // 科室数据SQL
        String doctorSQL = "";           // 医生数据SQL
        String userSQL = "";            // 用户数据SQL
        String userOrgSQL = "";         // 组织用户关系数据SQL
        String roleUserSQL = "";           // 角色用户关系数据SQL
        String productOrderSQL = "";    // 合同信息数据SQL
        String productOrderDetailSQL = "";  // 合同明细(套餐)信息数据SQL
        String productOrderRecordSQL = "";  // 合同充值记录表SQL
 
        // 时间转换格式
        SimpleDateFormat dateTimeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
 
        String createTime = "";
        String updateTime = "";
 
        // 1、根据hospitalId从sys_hospital中查询医院信息
        SysHospital hospitalInfo = iSysHospitalService.getById(hospitalId);
        if (hospitalInfo != null) {
            createTime = "'" + dateTimeFormat.format(hospitalInfo.getCreateTime()) + "'";
            updateTime = (hospitalInfo.getUpdateTime() == null) ? "NULL" : "'" + dateTimeFormat.format(hospitalInfo.getUpdateTime()) + "'";
            hospitalSQL = " INSERT INTO user_center_pri.sys_hospital  VALUES (" +
                    hospitalInfo.getId() + ", " +
                    "'" + hospitalInfo.getHospitalName() + "', " +
                    hospitalInfo.getOrgId() + ", " +
                    "'" + 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() + "', " +
                    "'http://123.kidgrow.cloud/OtherImage/2020-08-12/logo.jpg'," +
                    "NULL," +
                    "NULL," +
                    "'" + hospitalInfo.getHospitalAbout() + "', " +
                    "0, 0, " +
                    hospitalInfo.getHospitalState() + ", " +
                    hospitalInfo.getEnabled() + ", " +
                    "'" + hospitalInfo.getTenantId() + "', " +
                    hospitalInfo.getIsDel() + ", " +
                    hospitalInfo.getCreateUserId() + ", " +
                    "'" + hospitalInfo.getCreateUserName() + "', " +
                    hospitalInfo.getUpdateUserId() + ", " +
                    "'" + hospitalInfo.getUpdateUserName() + "', " +
                    createTime + ", " +
                    updateTime + ", " +
                    "'" + hospitalInfo.getCreateUserOrgCode() + "' " +
                    ");\n\r";
        } else {
            log.error("【{}】医院信息读取失败",hospitalId);
            return "医院信息读取失败,请重新生成SQL!";
        }
        // 2、根据departmentId从sys_department中查询科室信息
        SysDepartment department = baseMapper.selectById(departmentId);
        if (department != null) {
            createTime = "'" + dateTimeFormat.format(department.getCreateTime()) + "'";
            updateTime = (department.getUpdateTime() == null) ? "NULL" : "'" + dateTimeFormat.format(department.getUpdateTime()) + "'";
            departmentSQL = " INSERT INTO user_center_pri.sys_department  VALUES (" +
                    department.getId() + ", " +
                    "'" + department.getDepartmentName() + "', " +
                    department.getOrgId() + ", " +
                    department.getServerUserId() + ", " +
                    "'" + department.getServerUserName() + "', " +
                    "'" + department.getServerUserTel() + "', " +
                    department.getIsDel() + ", " +
                    department.getEnabled() + ", " +
                    department.getCreateUserId() + ", " +
                    "'" + department.getCreateUserName() + "', " +
                    department.getUpdateUserId() + ", " +
                    "'" + department.getUpdateUserName() + "', " +
                    createTime + ", " +
                    updateTime + ", " +
                    department.getSaleUserId() + ", " +
                    "'" + department.getSaleUserName() + "', " +
                    "'" + department.getSaleUserTel() + "', " +
                    department.getAccountsCount() + ", " +
                    "0,0,NULL,NULL, " +
                    "'" + department.getDepartmentLink() + "', " +
                    "'" + department.getDepartmentTel() + "', " +
                    "0,0,1," +
                    "'" + department.getPrivateServerGuuid() + "' " +
                    ");\n\r";
        } else {
            log.error("【{}】科室信息读取失败",departmentId);
            return "科室信息读取失败,请重新生成SQL!";
        }
        // 3、根据医院和科室表中的org_id查询sys_organization中查询组织信息
        List<SysOrganization> organizationList = iSysOrganizationService.list(
                new QueryWrapper<SysOrganization>()
                        .in("id", hospitalInfo.getOrgId(), department.getOrgId())
                        .eq("is_del", 0)
                        .eq("enabled", 1)
        );
        if ((organizationList != null) && (organizationList.size() > 0)) {
            for (int i = 0; i < organizationList.size(); i++) {
                SysOrganization organization = organizationList.get(i);
                createTime = "'" + dateTimeFormat.format(organization.getCreateTime()) + "'";
                updateTime = (organization.getUpdateTime() == null) ? "NULL" : "'" + dateTimeFormat.format(organization.getUpdateTime()) + "'";
                organizationSQL += " INSERT INTO user_center_pri.sys_organization  VALUES (" +
                        organization.getId() + ", " +
                        organization.getOrgLevel() + ", " +
                        organization.getOrgAttr() + ", " +
                        organization.getOrgParentId() + ", " +
                        "'" + organization.getOrgName() + "', " +
                        organization.getOrgOrder() + ", " +
                        organization.getIsDel() + ", " +
                        organization.getEnabled() + ", " +
                        organization.getCreateUserId() + ", " +
                        "'" + organization.getCreateUserName() + "', " +
                        organization.getUpdateUserId() + ", " +
                        "'" + organization.getUpdateUserName() + "', " +
                        createTime + ", " +
                        updateTime + ", " +
                        "'" + organization.getCreateUserOrgCode() + "', " +
                        "'" + organization.getOrgCode() + "' " +
                        ");\n\r";
            }
        }
        // 4、给科室创建管理员账号
        // 4.1 sys_user创建账号【userName:admin, password:123456,Mobile:admin,Type:DOCTOR,tenantId:】
        Long userId = IdWorker.getId();
 
        userSQL = " INSERT INTO user_center_pri.sys_user  VALUES (" +
                userId.toString() + ", " +
                "'admin', " +
                "'" + passwordEncoder.encode("123456") + "', " +
                "'admin', " +
                "NULL, " +
                "'admin', " +
                "0, 1, " +
                "'"+ UserType.DOCTOR.name()+"', " +
                "'" + dateTimeFormat.format(DateTime.now()) + "', " +
                "NULL, NULL, NULL, 0, " +
                "'" +CommonConstant.H_TENANT + "', " +
                CommonConstant.ADMIN_USER_ID.toString() +", " +
                "'" + CommonConstant.ADMIN_USER_NAME + "', " +
                "NULL, 0, " +
                "NULL, "+ // create_user_org_code
                "1" +
                ");\n\r";
        // 4.2、 sys_user_org创建记录
        //      用户与医院的关系数据
        userOrgSQL = " INSERT INTO user_center_pri.sys_user_org  VALUES (" +
                IdWorker.getId() + ", " +
                userId.toString() + ", " +
                hospitalInfo.getOrgId().toString() + ", " +
                hospitalId.toString() + ", " +
                CommonConstant.SYSTEM_ORG_HOS_LEVEL.toString() + ", " +
                "1, 0, " +
                CommonConstant.ADMIN_USER_ID.toString() +", " +
                "'" + CommonConstant.ADMIN_USER_NAME + "', " +
                "NULL, NULL, " +
                "'" + dateTimeFormat.format(DateTime.now()) + "', " +
                "NULL, "+
                "NULL " + // create_user_org_code
                ");\n\r";
        //      用户与科室的关系数据
        userOrgSQL += " INSERT INTO user_center_pri.sys_user_org  VALUES (" +
                IdWorker.getId() + ", " +
                userId.toString() + ", " +
                department.getOrgId().toString() + ", " +
                departmentId.toString() + ", " +
                CommonConstant.SYSTEM_ORG_DEP_LEVEL.toString() + ", " +
                "1, 0, " +
                CommonConstant.ADMIN_USER_ID.toString() +", " +
                "'" + CommonConstant.ADMIN_USER_NAME + "', " +
                "NULL, NULL, " +
                "'" + dateTimeFormat.format(DateTime.now()) + "', " +
                "NULL, "+
                "NULL " + // create_user_org_code
                ");\n\r";
        // 4.3、 sys_role_user创建记录
        roleUserSQL = " INSERT INTO user_center_pri.sys_role_user  VALUES ( " +
                userId.toString() + ", "+
                CommonConstant.HOSPITAL_DOCTOR_ID +" );\n\r" +  // 普通医生
                    " INSERT INTO user_center_pri.sys_role_user  VALUES ( " +
                userId.toString() + ", "+
                CommonConstant.HOSPITAL_ADMIN_ID +");\n\r" ;   // 医院管理员
        // 4.4、 sys_doctor创建记录
        doctorSQL = " INSERT INTO user_center_pri.sys_doctor  VALUES (" +
                IdWorker.getId() + ", " +
                userId.toString() + ", " +
                hospitalInfo.getId().toString() + ", " +
                "'" + hospitalInfo.getHospitalName() + "', " +
                department.getId().toString() + ", " +
                "'" + department.getDepartmentName()  + "', " +
                "'管理员', NULL, NULL, 'admin', " +
                "NULL, NULL, NULL, NULL, NULL,  " +
                "0, 0, 0, 0, 1, 1, 1, " +
                CommonConstant.ADMIN_USER_ID.toString() +", " +
                "'" + CommonConstant.ADMIN_USER_NAME + "', " +
                "NULL, NULL, " +
                "'" +dateTimeFormat.format(DateTime.now()) + "', " +
                "NULL, " +
                department.getSaleUserId().toString() + ", " +
                "'" + department.getSaleUserName() + "', " +
                "1, " +
                "'" + CommonConstant.ADMIN_USER_ORG_CODE + "'" +
                ");\n\r";
        // 5、根据hospitalId和departmentId从opration_center.product_order中查询合同信息
        Map<String, Object> orderMap = new HashMap<>();
        orderMap.put("hospital_id", hospitalId);
        orderMap.put("department_id", departmentId);
        orderMap.put("enabled", 1);
        orderMap.put("is_del", 0);
        orderMap.put("sql_is_downloaded",0);
        ResultBody orderResult = productOrderService.findProductOrderListByMap(orderMap);
        if (orderResult.isOk()) {
            //List<ProductOrder> productOrderList = (List<ProductOrder>) orderResult.getData();
            List<ProductOrder> productOrderList = JSON.parseArray(JSON.toJSONString(orderResult.getData()), ProductOrder.class);
            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()) + "'";
                    productOrderSQL += "INSERT INTO opration_center_pri.product_order VALUES(" +
                            productOrder.getId() + ", " +
                            productOrder.getHospitalId() + ", " +
                            "'" + productOrder.getHospitalName() + "', " +
                            productOrder.getDepartmentId() + ", " +
                            "'" + productOrder.getDepartmentName() + "', " +
                            "'" + productOrder.getContractNo() + "', " +
                            "'" + productOrder.getContractTitle() + "', " +
                            productOrder.getContractNum() + ", " +
                            "'" + dateFormat.format(productOrder.getContractBeginTime()) + "', " +
                            "'" + dateFormat.format(productOrder.getContractEndTime()) + "', " +
                            "'" + dateTimeFormat.format(productOrder.getContractTime()) + "', " +
                            productOrder.getIsDel() + ", " +
                            productOrder.getEnabled() + ", " +
                            "1, " +
                            updateTime + ", " +
                            productOrder.getCreateUserId() + ", " +
                            "'" + productOrder.getCreateUserName() + "', " +
                            productOrder.getUpdateUserId() + ", " +
                            "'" + productOrder.getUpdateUserName() + "', " +
                            createTime + ", " +
                            "NULL, "+ // tenant_id
                            "'" + productOrder.getCreateUserOrgCode() + "' " +
                            ");\n\r";
 
                    // 6、根据order_id从opration_center.product_order_detail中查询合同中的套餐信息
                    orderMap.clear();
                    orderMap.put("order_id", productOrder.getId());
                    orderMap.put("enabled", 1);
                    orderMap.put("is_del", 0);
                    ResultBody detailResult = productOrderService.findProductOrderDetailListByMap(orderMap);
 
                    if (detailResult.isOk()) {
                        //List<ProductOrderDetail> productOrderDetailList = (List<ProductOrderDetail>) detailResult.getData();
                        List<ProductOrderDetail> productOrderDetailList = JSON.parseArray(JSON.toJSONString(detailResult.getData()), ProductOrderDetail.class);
 
                        if ((productOrderDetailList != null) && (productOrderDetailList.size() > 0)) {
 
                            for (int j = 0; j < productOrderDetailList.size(); j++) {
                                ProductOrderDetail productOrderDetail = productOrderDetailList.get(j);
                                createTime = "'" + dateTimeFormat.format(productOrderDetail.getCreateTime()) + "'";
                                updateTime = (productOrderDetail.getUpdateTime() == null) ? "NULL" : "'" + dateTimeFormat.format(productOrderDetail.getUpdateTime()) + "'";
                                productOrderDetailSQL += "INSERT INTO opration_center_pri.product_order_detail VALUES( " +
                                        productOrderDetail.getId() + ", " +
                                        productOrderDetail.getOrderId() + ", " +
                                        productOrderDetail.getProId() + ", " +
                                        productOrderDetail.getProType() + ", " +
                                        productOrderDetail.getTermType() + ", " +
                                        "'" + productOrderDetail.getProName() + "', " +
                                        productOrderDetail.getIsShare() + ", " +
                                        productOrderDetail.getIsDel() + ", " +
                                        productOrderDetail.getOrderAilightCount() + ", " +
                                        productOrderDetail.getOrderRecordCount() + ", " +
                                        productOrderDetail.getAilightCount() + ", " +
                                        "'" + productOrderDetail.getAilightCountEncryption() + "', " +
                                        productOrderDetail.getRecordCount() + ", " +
                                        "'" + dateFormat.format(productOrderDetail.getProBegintime()) + "', " +
                                        "'" + productOrderDetail.getProBegintimeEncryption() + "', " +
                                        "'" + dateFormat.format(productOrderDetail.getProEndtime()) + "', " +
                                        "'" + productOrderDetail.getProEndtimeEncryption() + "', " +
                                        productOrderDetail.getEnabled() + ", " +
                                        productOrderDetail.getCreateUserId() + ", " +
                                        "'" + productOrderDetail.getCreateUserName() + "', " +
                                        productOrderDetail.getUpdateUserId() + ", " +
                                        updateTime + ", " +
                                        "'" + productOrderDetail.getUpdateUserName() + "', " +
                                        createTime + ", " +
                                        "Null, " + // tenant_id
                                        "'" + productOrderDetail.getCreateUserOrgCode() + "' " +
                                        ");\n\r";
                                productOrderRecordSQL +=  "INSERT INTO opration_center_pri.product_order_record VALUES( " +
                                        productOrderDetail.getId() + ", " +
                                        productOrderDetail.getOrderId() + ", " +
                                        productOrderDetail.getProId() + ", " +
                                        productOrderDetail.getProType() + ", " +
                                        "'" + productOrderDetail.getProName() + "', " +
                                        productOrderDetail.getIsShare() + ", " +
                                        productOrderDetail.getIsDel() + ", " +
                                        productOrderDetail.getAilightCount() + ", " +
                                        productOrderDetail.getRecordCount() + ", " +
                                        "'" + dateFormat.format(productOrderDetail.getProBegintime()) + "', " +
                                        "'" + dateFormat.format(productOrderDetail.getProEndtime()) + "', " +
                                        productOrderDetail.getEnabled() + ", " +
                                        productOrderDetail.getCreateUserId() + ", " +
                                        "'" + productOrderDetail.getCreateUserName() + "', " +
                                        productOrderDetail.getUpdateUserId() + ", " +
                                        updateTime + ", " +
                                        "'" + productOrderDetail.getUpdateUserName() + "', " +
                                        createTime + ", " +
                                        "Null, " + // tenant_id
                                        "'" + productOrderDetail.getCreateUserOrgCode() + "' " +
                                        ");\n\r";
                            }
                        } else {
                            log.error("此合同【{}】没有合同明细(套餐)",productOrder.getId());
                            // return "没有已开通的合同明细(套餐),请先充值,再生成SQL文";
                        }
 
                    } else {
                        log.error(detailResult.getMsg());
                        return "没有已开通的合同明细(套餐),请先充值,再生成SQL文";
                    }
                    // 更新合同表中的是否下载字段,为已下载(true)
                    productOrder.setSqlIsDownloaded(true);
                    ResultBody updateResult = productOrderService.downLoadProductOrderOver(productOrder);
                    if (!updateResult.isOk()) {
                        log.error("合同表下载状态更新失败!");
                    }
 
                }
            } else {
 
                log.error("此科室【hospital_id:{},department_id:{}】没有已开通的合同记录",hospitalId,departmentId);
                return "没有已开通的合同记录,请先充值,再生成SQL文";
            }
        } else {
            log.error(orderResult.getMsg());
            return "没有已开通的合同记录,请先充值,再生成SQL文";
 
        }
 
        if ((productOrderSQL.isEmpty()) || (productOrderDetailSQL.isEmpty())){
            log.error("此科室【hospital_id:{},department_id:{}】没有已开通的合同记录",hospitalId,departmentId);
            return "没有已开通的合同记录,请先充值,再生成SQL文";
        }
 
        initSQL = organizationSQL + hospitalSQL + departmentSQL +
                userSQL + userOrgSQL + roleUserSQL + doctorSQL +
                productOrderSQL + productOrderDetailSQL + productOrderRecordSQL;
 
        return initSQL;
    }
}