zjh
2025-03-20 e191ab24286eb091db9966c9227c68d1af717b46
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
package com.ltkj.web.controller.pacs;
 
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.XmlUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ltkj.common.core.domain.AjaxResult;
import com.ltkj.common.core.domain.entity.SysDept;
import com.ltkj.common.utils.AgeResult;
import com.ltkj.common.utils.DateUtils;
import com.ltkj.hosp.domain.*;
import com.ltkj.hosp.hisDto.OutpintestapplyDetailsDto;
import com.ltkj.hosp.pacsDto.SavePacsApply;
import com.ltkj.hosp.service.*;
import com.ltkj.hosp.sqlDomain.LtkjExamJcsqd;
import com.ltkj.system.service.ISysConfigService;
import com.ltkj.system.service.ISysDeptService;
import com.ltkj.web.controller.his.HisApiGetMethodService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;
import javax.xml.soap.*;
 
/**
 * @Company: 西安路泰科技有限公司
 * @Author: zhaowenxuan
 * @Date: 2024/9/18 10:55
 */
@Slf4j
@Component
public class PacsApiMethodService {
    @Autowired
    private HisApiGetMethodService getMethodService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ITjProjectService projectService;
    @Autowired
    private LtkjJfzhglmxjfxmzdService ltkjJfzhglmxjfxmzdService;
    @Autowired
    private LtkjMxjfxmzdService ltkjMxjfxmzdService;
    @Autowired
    private LtkjEjjfxmzdService ltkjEjjfxmzdService;
    @Autowired
    private LtkjJianchajianyanTreeService ltkjJianchajianyanTreeService;
    @Autowired
    private ISysConfigService configService;
    @Autowired
    private ITbTransitionService transitionService;
    @Autowired
    private ITjOrderService tjOrderService;
    @Autowired
    private LtkjExamJcsqdService ltkjExamJcsqdService;
 
 
    /**
     * pas 检查申请信息保存
     *
     * @param savePacsApply
     * @return
     */
    private AjaxResult savePacsApply(SavePacsApply savePacsApply) {
        Map<String, Object> map = BeanUtil.beanToMap(savePacsApply);
        return getMethodService.getPacsData("savePacsApply", map);
    }
 
    /**
     * pas 结果获取
     *
     * @param regNo 检查申请单号
     * @return
     */
    public AjaxResult getPacsReport(String regNo) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("regNo", regNo);
        return getMethodService.getPacsData("getPacsReport", hashMap);
    }
 
    /**
     * pas 检查申请信息作废
     *
     * @param regNo  检查申请单号
     * @param examNo 体检号
     * @param name   病人姓名
     * @return
     */
    public AjaxResult updatePacsApply(String regNo, String examNo, String name) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("regNo", regNo);
        hashMap.put("examNo", examNo);
        hashMap.put("name", name);
        log.info("作废检查 ->{}", JSONUtil.toJsonStr(hashMap));
        return getMethodService.getPacsData("updatePacsApply", hashMap);
//        JSONObject object = JSONUtil.createObj();
//        object.putOpt("ResultCode","1");
//        return AjaxResult.success().put("data",JSONUtil.toJsonStr(object));
    }
 
    public Boolean save(TjOrder tjOrder, TjCustomer tjCustomer, List<TbTransition> detailList) {
//        if (true)
//            return true;
        JSONArray array = JSONUtil.createArray();
        ArrayList<SavePacsApply> errorPacsList = new ArrayList<>();
        ArrayList<SavePacsApply> successPacsList = new ArrayList<>();
        int allTotal = 0;
        if (detailList != null && !detailList.isEmpty()) {
            LambdaQueryWrapper<TjProject> wq = new LambdaQueryWrapper<>();
            wq.in(TjProject::getProId, detailList.stream().map(TbTransition::getParentProId).collect(Collectors.toList()));
            wq.in(TjProject::getDeptId, deptService.getJcDeptIds());
            List<TjProject> projects = projectService.list(wq);
            try {
                allTotal = saveProjects(tjOrder, tjCustomer, projects, array, successPacsList, errorPacsList);
            } catch (Exception e) {
                log.error(e.getMessage());
                for (Object object : array) {
                    JSONObject jsonObject = (JSONObject) object;
                    updatePacsApply(jsonObject.getStr("regNo"), jsonObject.getStr("cardId"), jsonObject.getStr("name"));
                }
                return false;
            }
        }
        log.info("检查总计 ->{}个", allTotal);
        if (!errorPacsList.isEmpty()) {
            log.info("成功 ->{}", successPacsList);
            log.error("失败 ->{}", errorPacsList);
        }
        return true;
    }
 
    public int saveProjects(TjOrder tjOrder, TjCustomer tjCustomer, List<TjProject> projects, JSONArray array, ArrayList<SavePacsApply> successPacsList, ArrayList<SavePacsApply> errorPacsList) {
        int allTotal = 0;
        if (projects != null && !projects.isEmpty()) {
            String dept = configService.selectConfigByKey("request_default_dept");
            String ysbm = configService.selectConfigByKey("request_default_ysbm");
            for (TjProject project : projects) {
                if (0 == project.getProParentId() && project.getProPrice().compareTo(BigDecimal.ZERO) != 0) {
                    if (null != project.getHisXmbm()) {
                        SavePacsApply savePacsApply = new SavePacsApply();
                        savePacsApply.setExamNo(tjOrder.getCardId());
                        savePacsApply.setName(tjCustomer.getCusName());
                        savePacsApply.setSex(tjCustomer.getCusSex() == 1 ? "2" : "1");
                        AgeResult ageResult = DateUtils.calculateAge(tjCustomer.getCusBrithday());
                        savePacsApply.setAge(String.valueOf(ageResult.getAge()));
                        savePacsApply.setAgeUnit(String.valueOf(ageResult.getUnit()));
                        savePacsApply.setBirthday(DateFormatUtils.format(tjCustomer.getCusBrithday(), "yyyy-MM-dd"));
                        savePacsApply.setCardNumber(tjCustomer.getCusIdcard());
                        savePacsApply.setTel(tjCustomer.getCusPhone());
                        Date date = new Date();
                        String regNo = "M_jk" + tjOrder.getCardId() +"_" + project.getHisXmbm();
                        savePacsApply.setRegNo(regNo);
                        JSONObject jsonObject = JSONUtil.createObj();
                        jsonObject.putOpt("cardId", tjOrder.getCardId());
                        jsonObject.putOpt("regNo", regNo);
                        jsonObject.putOpt("name", tjCustomer.getCusName());
                        array.add(jsonObject);
                        savePacsApply.setCheckItemID(project.getHisXmbm());
                        savePacsApply.setCheckItem(project.getHisXmmc());
                        // TODO 检查部位
                        savePacsApply.setCheckPosition("-");
                        savePacsApply.setAmount(project.getProPrice());
                        savePacsApply.setApplyDate(DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"));
                        savePacsApply.setApplyDepment(dept);
                        savePacsApply.setApplyDepmentName("体检中心");
                        savePacsApply.setApplyDoctor(ysbm);
                        savePacsApply.setApplyDoctorName("王健萍");
                        savePacsApply.setExecDepment(dept);
                        savePacsApply.setExecDepmentName("体检中心");
                        savePacsApply.setCreateTime(savePacsApply.getApplyDate());
                        // TODO 检查设备
                        savePacsApply.setDevice("-");
                        // TODO 检查设备类型
                        savePacsApply.setDeviceType("-");
                        log.info("PACS检查申请 ->{}", JSONUtil.toJsonStr(savePacsApply));
                        AjaxResult ajaxResult = savePacsApply(savePacsApply);
                        log.info("返回 ->{}",JSONUtil.toJsonStr(ajaxResult));
                        String result = ajaxResult.get("data").toString();
                        JSONObject object = JSONUtil.parseObj(JSONUtil.parseObj(result));
                        log.info("PACS请求返回 ->{}", object);
                        String code = object.getStr("ResultCode");
                        allTotal++;
                        if (code.equals("1")) {
                            successPacsList.add(savePacsApply);
                            LtkjJianchajianyanTree tree = new LtkjJianchajianyanTree();
                            tree.setCardId(tjOrder.getCardId());
                            tree.setPationid(tjCustomer.getPationId());
                            tree.setSqdh(regNo);
                            tree.setFyhj(String.valueOf(project.getProPrice()));
                            tree.setHisXmdm(project.getHisXmbm());
                            tree.setType("检查");
                            tree.setService("pacs");
                            tree.setTjPrice(String.valueOf(project.getProPrice()));
                            LambdaQueryWrapper<LtkjJfzhglmxjfxmzd> wrapper1 = new LambdaQueryWrapper<>();
                            if (project.getSfzhfy().equals("Y"))
                                wrapper1.eq(LtkjJfzhglmxjfxmzd::getZhxmdm, project.getHisXmbm());
                            else wrapper1.eq(LtkjJfzhglmxjfxmzd::getXmdm, project.getHisXmbm());
                            List<LtkjJfzhglmxjfxmzd> ltkjJfzhglmxjfxmzds = ltkjJfzhglmxjfxmzdService.list(wrapper1);
                            if (ltkjJfzhglmxjfxmzds != null && !ltkjJfzhglmxjfxmzds.isEmpty()) {
                                String xmdm = ltkjJfzhglmxjfxmzds.get(0).getXmdm();
                                LambdaQueryWrapper<LtkjMxjfxmzd> wrapper2 = new LambdaQueryWrapper<>();
                                wrapper2.eq(LtkjMxjfxmzd::getXmdm, xmdm);
                                LtkjMxjfxmzd mxjfxmzd = ltkjMxjfxmzdService.getOne(wrapper2);
                                LambdaQueryWrapper<LtkjEjjfxmzd> wrapper3 = new LambdaQueryWrapper<>();
                                wrapper3.eq(LtkjEjjfxmzd::getEjkmdm, mxjfxmzd.getEjkmdm());
                                LtkjEjjfxmzd ejjfxmzd = ltkjEjjfxmzdService.getOne(wrapper3);
                                tree.setXmfl(ejjfxmzd.getYjkmdm());
                            }
                            tree.setCreateTime(DateUtil.date());
                            ltkjJianchajianyanTreeService.save(tree);
                        } else {
                            errorPacsList.add(savePacsApply);
                        }
                    }
                }
            }
        }
        return allTotal;
    }
 
    /*         查询检查结果、更新查询状态           */
    // 调用返回检验结果:
    // exec usp_tjjk_pacsjg ‘体检号’
    // 调用更新状态:
    // exec usp_tjjk_pacszt ‘体检号’
 
 
    /**
     * 调用pacs申请单推送
     * xml传递
     */
    public boolean OrderAdd(TjCustomer customer,TjOrder tjOrder){
        String hospbm = configService.selectConfigByKey("common_api_service_hospbm");
 
        List<TbTransition> detailList = transitionService.getTbTransitionListByCusId(customer.getCusIdcard(), tjOrder.getCardId());
        LambdaQueryWrapper<TjProject> wq = new LambdaQueryWrapper<>();
        wq.in(TjProject::getProId, detailList.stream().map(TbTransition::getParentProId).collect(Collectors.toList()));
        wq.in(TjProject::getDeptId, deptService.getJcDeptIds());
        List<TjProject> projects = projectService.list(wq);
        HashMap<String, Object> root = new HashMap<>();
        HashMap<String, Object> hashMap = new HashMap<>();
 
        String xmlRequest = "";
 
        MessageFactory messageFactory = null;
        try {
            // 创建SOAP消息
            messageFactory = MessageFactory.newInstance();
            SOAPMessage soapMessage = messageFactory.createMessage();
            // 创建SOAP Envelope
            SOAPPart soapPart = soapMessage.getSOAPPart();
            SOAPEnvelope envelope = soapPart.getEnvelope();
            envelope.addNamespaceDeclaration("xmlns", "http://tempuri.org/");
 
            // 创建SOAP Body
            SOAPBody soapBody = envelope.getBody();
            SOAPElement rootElement = soapBody.addChildElement("OrderAdd", "xmlns");
            rootElement.addChildElement("dataSourceName").addTextNode("hisDataSource");
 
            // PatientInfor 部分
            SOAPElement patientInfor = rootElement.addChildElement("PatientInfor");
            if (customer.getCusSex() == 0) patientInfor.addChildElement("PatientSex").addTextNode("M");
            else if (customer.getCusSex() == 1) patientInfor.addChildElement("PatientSex").addTextNode("F");
            else patientInfor.addChildElement("PatientSex").addTextNode("O");
            patientInfor.addChildElement("PatBirthday").addTextNode(new SimpleDateFormat("yyyy-MM-dd").format(customer.getCusBrithday()));
            patientInfor.addChildElement("VisitNo").addTextNode(tjOrder.getTjNumber());
            patientInfor.addChildElement("PatientId").addTextNode(String.valueOf(customer.getCusId()));
            patientInfor.addChildElement("HospitalID").addTextNode("shanxiqinxamjyy");
            patientInfor.addChildElement("IdCard").addTextNode(customer.getCusIdcard());
            patientInfor.addChildElement("PatientName").addTextNode(customer.getCusName());
            patientInfor.addChildElement("PatType").addTextNode("P");
 
            // ExamItemList 部分
            SOAPElement examItemList = rootElement.addChildElement("ExamItemList");
 
            String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            for (TjProject project : projects) {
                SOAPElement examItem = examItemList.addChildElement("ExamItem");
                examItem.addChildElement("HisID").addTextNode(tjOrder.getTjNumber() + String.valueOf(project.getProId()).substring(String.valueOf(project.getProId()).length() - 4));
                examItem.addChildElement("EmergencyFlag").addTextNode("0");
                examItem.addChildElement("ExamItemID").addTextNode(project.getHisXmbm());
                examItem.addChildElement("ExamItemName").addTextNode(project.getHisXmmc());
                examItem.addChildElement("OrderDT").addTextNode(date);
                SysDept dept = deptService.getById(project.getDeptId());
                examItem.addChildElement("Modality").addTextNode(dept.getModality());
                examItem.addChildElement("ChargeFlag").addTextNode("1");
            }
 
            // 保存SOAP消息
            soapMessage.saveChanges();
 
            // 打印生成的SOAP请求
            System.out.println("生成的SOAP请求内容:");
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            soapMessage.writeTo(byteArrayOutputStream);
            xmlRequest = byteArrayOutputStream.toString("UTF-8");
        } catch (SOAPException | IOException e) {
            log.error("SOAP异常 ->{}",String.valueOf(e));
        }
 
 
//        HashMap<String, Object> patientInfo = new HashMap<>();
//        patientInfo.put("PatientId",customer.getCusId());
//        patientInfo.put("VisitNo",tjOrder.getTjNumber());
//        patientInfo.put("PatType","P");
//        patientInfo.put("PatientName",customer.getCusName());
//        if (customer.getCusSex() == 0) patientInfo.put("PatientSex","M");
//        else if (customer.getCusSex() == 1) patientInfo.put("PatientSex","F");
//        else patientInfo.put("PatientSex","O");
//        patientInfo.put("PatBirthday",new SimpleDateFormat("yyyy-MM-dd").format(customer.getCusBrithday()));
//        patientInfo.put("IdCard",customer.getCusIdcard());
//        patientInfo.put("hospitalid","chkwyy");
//        patientInfo.put("HospitalID",hospbm);
//
//        HashMap<String, Object> examItemList = new HashMap<>();
//        List<Map<String, Object>> examItemsList = new ArrayList<>();
        String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//
//        for (TjProject project : projects) {
//            HashMap<String, Object> examItem = new HashMap<>();
//            // 申请单号规则:体检号_project.getProId()后四位
//            examItem.put("HisID", tjOrder.getTjNumber() + String.valueOf(project.getProId()).substring(String.valueOf(project.getProId()).length() - 4));
//            examItem.put("EmergencyFlag", "0");
//            examItem.put("ExamItemID", project.getHisXmbm());
//            examItem.put("ExamItemName", project.getHisXmmc());
//            examItem.put("OrderDT", date);
//
//            SysDept dept = deptService.getById(project.getDeptId());
//            examItem.put("Modality", dept.getModality());
//            // 收费标识:1 是收费,0 未收费
//            examItem.put("ChargeFlag", "1");
//
//            examItemsList.add(examItem); // 将每个ExamItem添加到集合
//        }
//
//        // 构造最终的ExamItemList
//        examItemList.put("ExamItem", examItemsList);
//        hashMap.put("ExamItemList", examItemList);
//
//        hashMap.put("PatientInfor", patientInfo);
//
//        // 将数据封装到根节点
//        root.put("Root", hashMap);
//
//        // 使用Hutool将Map转换为XML字符串
//        String xmlRequest = XmlUtil.mapToXmlStr(root);
 
        String result = "1";
        log.info("调用pacs传递参数的值为: "+xmlRequest);
        Map<String, Object> map = XmlUtil.xmlToMap(result);
        if (map.get("Code").toString().equals("1")){
            for (TjProject project : projects) {
                LtkjJianchajianyanTree tree = new LtkjJianchajianyanTree();
                tree.setCardId(tjOrder.getTjNumber());
                tree.setPationid(String.valueOf(customer.getCusId()));
                tree.setSqdh(tjOrder.getTjNumber() + String.valueOf(project.getProId()).substring(String.valueOf(project.getProId()).length() - 4));
                tree.setFyhj(String.valueOf(project.getProPrice()));
                tree.setHisXmdm(project.getHisXmbm());
                tree.setType("检查");
                tree.setService("pacs");
                tree.setTjPrice(String.valueOf(project.getProPrice()));
                LambdaQueryWrapper<LtkjJfzhglmxjfxmzd> wrapper1 = new LambdaQueryWrapper<>();
                if (project.getSfzhfy().equals("Y"))
                    wrapper1.eq(LtkjJfzhglmxjfxmzd::getZhxmdm, project.getHisXmbm());
                else wrapper1.eq(LtkjJfzhglmxjfxmzd::getXmdm, project.getHisXmbm());
                List<LtkjJfzhglmxjfxmzd> ltkjJfzhglmxjfxmzds = ltkjJfzhglmxjfxmzdService.list(wrapper1);
                if (ltkjJfzhglmxjfxmzds != null && !ltkjJfzhglmxjfxmzds.isEmpty()) {
                    String xmdm = ltkjJfzhglmxjfxmzds.get(0).getXmdm();
                    LambdaQueryWrapper<LtkjMxjfxmzd> wrapper2 = new LambdaQueryWrapper<>();
                    wrapper2.eq(LtkjMxjfxmzd::getXmdm, xmdm);
                    LtkjMxjfxmzd mxjfxmzd = ltkjMxjfxmzdService.getOne(wrapper2);
                    LambdaQueryWrapper<LtkjEjjfxmzd> wrapper3 = new LambdaQueryWrapper<>();
                    wrapper3.eq(LtkjEjjfxmzd::getEjkmdm, mxjfxmzd.getEjkmdm());
                    LtkjEjjfxmzd ejjfxmzd = ltkjEjjfxmzdService.getOne(wrapper3);
                    tree.setXmfl(ejjfxmzd.getYjkmdm());
                }
                tree.setCreateTime(DateUtil.date());
                ltkjJianchajianyanTreeService.save(tree);
 
 
                LtkjExamJcsqd jcsqd = new LtkjExamJcsqd();
                jcsqd.setTjh(tjOrder.getTjNumber());
                jcsqd.setTmh(tree.getSqdh());
                jcsqd.setJcxmid(tree.getHisXmdm());
                jcsqd.setJcxmmc(project.getHisXmmc());
                jcsqd.setSqsj(date);
                ltkjExamJcsqdService.save(jcsqd);
            }
            return true;
        }
        return false;
    }
 
    /**
     * 调用pacs申请单作废
     * @param sqdh 申请单号
     * @param czrid 操作人编码
     * @return
     */
    public boolean OrderDelete(String sqdh,String czrid){
        HashMap<String, Object> root = new HashMap<>();
        HashMap<String, Object> hashMap = new HashMap<>();
        LtkjJianchajianyanTree tree = ltkjJianchajianyanTreeService.getOne(new LambdaQueryWrapper<LtkjJianchajianyanTree>().eq(LtkjJianchajianyanTree::getSqdh, sqdh));
        String pationid = tree.getPationid();
        hashMap.put("PatientId",pationid);
        hashMap.put("PatType","P");
        hashMap.put("Operator",czrid);
        hashMap.put("HisID",sqdh);
        root.put("Root",hashMap);
        String result = HttpRequest.post("").body(XmlUtil.mapToXmlStr(root)).contentType("application/xml;charset:utf-8;").execute().body();
        Map<String, Object> map = XmlUtil.xmlToMap(result);
        if (map.get("Code").toString().equals("1")){
            ltkjJianchajianyanTreeService.removeById(tree);
            return true;
        }
        return false;
    }
}