SiMzService.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. package thyyxxk.webserver.service.medicalinsurance;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.alibaba.fastjson.JSONObject;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.stereotype.Service;
  8. import thyyxxk.webserver.config.exception.BizException;
  9. import thyyxxk.webserver.config.exception.ExceptionEnum;
  10. import thyyxxk.webserver.constants.Capacity;
  11. import thyyxxk.webserver.constants.sidicts.*;
  12. import thyyxxk.webserver.dao.his.medicalinsurance.DigitalReceiptDao;
  13. import thyyxxk.webserver.dao.his.medicalinsurance.SiMzDao;
  14. import thyyxxk.webserver.entity.ResultVo;
  15. import thyyxxk.webserver.entity.markmtfees.*;
  16. import thyyxxk.webserver.entity.medicalinsurance.digitalreceipt.*;
  17. import thyyxxk.webserver.entity.medicalinsurance.digitalreceipt.request.*;
  18. import thyyxxk.webserver.entity.medicalinsurance.manage.clinicinfo.RevokeRegRequest;
  19. import thyyxxk.webserver.entity.medicalinsurance.outpatient.*;
  20. import thyyxxk.webserver.entity.medicalinsurance.query.InsuInfo;
  21. import thyyxxk.webserver.entity.medicalinsurance.query.PsnBaseInfo;
  22. import thyyxxk.webserver.entity.medicalinsurance.query.QryPsnBsInfo;
  23. import thyyxxk.webserver.entity.medicalinsurance.query.SiPatInfo;
  24. import thyyxxk.webserver.entity.medicalinsurance.setlinfo.FundDetail;
  25. import thyyxxk.webserver.entity.outpatient.medicalinfo.MzBlRecord;
  26. import thyyxxk.webserver.service.externalhttp.SiMzSrvc;
  27. import thyyxxk.webserver.service.externalhttp.ThmzSystem;
  28. import thyyxxk.webserver.utils.*;
  29. import java.math.BigDecimal;
  30. import java.math.RoundingMode;
  31. import java.nio.charset.StandardCharsets;
  32. import java.util.*;
  33. /**
  34. * @description: 门诊医保交易
  35. * @author: DingJie
  36. * @create: 2021-05-28 16:11:19
  37. **/
  38. @Slf4j
  39. @Service
  40. public class SiMzService {
  41. private final SiMzDao dao;
  42. private final DigitalReceiptDao rxDao;
  43. private final ThmzSystem thmz;
  44. private final SiMzSrvc mzSrvc;
  45. private final SiQueryService qryService;
  46. @Value("${si-mz-fee-url}")
  47. private String siMzFeeUrl;
  48. @Value("${thmz-api-url}")
  49. private String thmzApiUrl;
  50. @Autowired
  51. public SiMzService(SiMzDao dao, DigitalReceiptDao rxDao, ThmzSystem thmz, SiMzSrvc mzSrvc,
  52. SiQueryService qryService) {
  53. this.dao = dao;
  54. this.rxDao = rxDao;
  55. this.thmz = thmz;
  56. this.mzSrvc = mzSrvc;
  57. this.qryService = qryService;
  58. }
  59. public ResultVo<String> outpatientRegistration(MzPatientInfo p) {
  60. p.setStaffId(TokenUtil.getInstance().getTokenUserId());
  61. String hisOrdNum = p.getPatNo() + "_" + p.getTimes() + "_" + 1;
  62. int mipCount = dao.selectMipayCount(hisOrdNum);
  63. if (mipCount > 0) {
  64. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "患者已进行移动医保支付,请勿重复登记。");
  65. }
  66. return mzSrvc.outpatientRegistration(siMzFeeUrl, p);
  67. }
  68. public ResultVo<String> revokeOutpatientRegistration(MzPatientInfo p) {
  69. p.setStaffId(TokenUtil.getInstance().getTokenUserId());
  70. return mzSrvc.revokeOutpatientRegistration(siMzFeeUrl, p);
  71. }
  72. public ResultVo<String> executeOutpatientRegRevoke(RevokeRegRequest request) {
  73. request.setStaffId(TokenUtil.getInstance().getTokenUserId());
  74. return mzSrvc.executeOutpatientRegRevoke(siMzFeeUrl, request);
  75. }
  76. public ResultVo<List<Map<String, Object>>> getMzReceipts(MzPatientInfo p) {
  77. String patNo = p.getPatNo();
  78. JSONObject queryMzChargeListParam = new JSONObject();
  79. queryMzChargeListParam.put("patCardType", 21);
  80. queryMzChargeListParam.put("patCardNo", patNo);
  81. queryMzChargeListParam.put("hisOrdNum", "");
  82. queryMzChargeListParam.put("ybRequest", 1);
  83. Map<String, Object> mzChargeListMap = thmz.getMzChargeDetailForUnPaid(thmzApiUrl, queryMzChargeListParam);
  84. if (null == mzChargeListMap) {
  85. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  86. }
  87. if (0 != (int) mzChargeListMap.get("resultCode")) {
  88. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, mzChargeListMap.get("resultMessage").toString());
  89. }
  90. List<Map<String, Object>> finalResult = new ArrayList<>();
  91. List<Map<String, String>> mzChargeList = FilterUtil.cast(mzChargeListMap.get("data"));
  92. for (Map<String, String> item : mzChargeList) {
  93. String hisOrdNum = item.get("hisOrdNum");
  94. if (null != hisOrdNum) {
  95. String[] hisOrdNumParts = hisOrdNum.split("_");
  96. int tempTimes = Integer.parseInt(hisOrdNumParts[1]);
  97. MzVisit mzVisit = dao.selectMzVisit(patNo, tempTimes);
  98. if (null == mzVisit) {
  99. continue;
  100. }
  101. if (IdCardUtil.isValidatedIdCard(mzVisit.getSocialNo())) {
  102. mzVisit.setAge(IdCardUtil.getAgeByIdCard(mzVisit.getSocialNo()));
  103. }
  104. String vipFlag = String.valueOf(item.get("vipFlag"));
  105. mzVisit.setReceiptNo(Integer.parseInt(hisOrdNumParts[2]));
  106. mzVisit.setTableClinicReceiptNo(0);
  107. mzVisit.setSettledFlag(calSettleFlag(mzVisit));
  108. mzVisit.setVipFlag(vipFlag);
  109. Map<String, Object> childResult = new HashMap<>(Capacity.FIVE);
  110. childResult.put("times", tempTimes);
  111. childResult.put("mzVisit", mzVisit);
  112. JSONObject queryMzChargeDetailParam = new JSONObject();
  113. queryMzChargeDetailParam.put("patientId", patNo);
  114. queryMzChargeDetailParam.put("times", tempTimes);
  115. queryMzChargeDetailParam.put("receiptNo", hisOrdNumParts[2]);
  116. Map<String, Object> mzChargeDetailMap = thmz.unPaidToFullChargeDetail(thmzApiUrl, queryMzChargeDetailParam);
  117. if (null != mzChargeDetailMap && 0 == (int) mzChargeDetailMap.get("resultCode")) {
  118. List<Map<String, Object>> mzChargeDetailList = FilterUtil.cast(mzChargeDetailMap.get("data"));
  119. mzChargeDetailList.removeIf(detail -> "TC".equals(detail.get("billItemCode")) ||
  120. !"5".equals(detail.get("payMark")) || "BILL99".equals(detail.get("chargeItemCode")) ||
  121. "四舍五入".equals(detail.get("tcName")));
  122. if (mzChargeDetailList.isEmpty()) {
  123. continue;
  124. }
  125. Map<Integer, List<MzReceipt>> orderReceiptsMap = new HashMap<>(Capacity.DEFAULT);
  126. String doctorName = dao.selectDoctorName(mzChargeDetailList.get(0).get("doctorCode").toString());
  127. mzChargeDetailList.forEach(detail -> {
  128. MzReceipt receipt = fillMzReceipt(detail);
  129. receipt.setPatientId(patNo);
  130. receipt.setTimes(tempTimes);
  131. receipt.setDoctorName(doctorName);
  132. if (!orderReceiptsMap.containsKey(receipt.getOrderNo())) {
  133. List<MzReceipt> list = new ArrayList<>();
  134. list.add(receipt);
  135. orderReceiptsMap.put(receipt.getOrderNo(), list);
  136. } else {
  137. orderReceiptsMap.get(receipt.getOrderNo()).add(receipt);
  138. }
  139. });
  140. List<OrderNo> orderNos = getOrderNos(patNo, tempTimes, mzVisit.getReceiptNo(), orderReceiptsMap);
  141. childResult.put("orderNos", orderNos);
  142. childResult.put("mzReceipts", orderReceiptsMap);
  143. childResult.put("zgmztczf", mzSrvc.getZgmztczf(mzVisit).getData());
  144. finalResult.add(childResult);
  145. }
  146. }
  147. }
  148. return ResultVoUtil.success(finalResult);
  149. }
  150. public ResultVo<List<MzDepositFile>> getHistoryMzReceipts(String patNo, String start, String end) {
  151. List<MzDepositFile> mzChargeList = dao.selectMzDepositFiles(patNo, start, end);
  152. if (null == mzChargeList || mzChargeList.isEmpty()) {
  153. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有找到历史处方!");
  154. }
  155. for (MzDepositFile depositFile : mzChargeList) {
  156. MzVisit mzVisit = new MzVisit();
  157. mzVisit.setPatientId(patNo);
  158. mzVisit.setTimes(depositFile.getTimes());
  159. mzVisit.setReceiptNo(depositFile.getReceiptNo());
  160. depositFile.setZgmztczf(mzSrvc.getZgmztczf(mzVisit).getData());
  161. }
  162. return ResultVoUtil.success(mzChargeList);
  163. }
  164. public Map<String, Object> getHistoryReceiptDetail(MzDepositFile mzDepositFile) {
  165. MzVisit mzVisit = dao.selectMzVisit(mzDepositFile.getPatNo(), mzDepositFile.getTimes());
  166. if (null == mzVisit) {
  167. throw new BizException(ExceptionEnum.NULL_POINTER, "没有找到患者的就诊信息。");
  168. }
  169. if (IdCardUtil.isValidatedIdCard(mzVisit.getSocialNo())) {
  170. mzVisit.setAge(IdCardUtil.getAgeByIdCard(mzVisit.getSocialNo()));
  171. }
  172. mzVisit.setSettledFlag(calSettleFlag(mzVisit));
  173. List<MzReceipt> mzReceipts = dao.selectMzCharge(mzDepositFile.getPatNo(),
  174. mzDepositFile.getTimes(), mzDepositFile.getReceiptNo());
  175. mzReceipts.forEach(itm -> itm.setChecked(StringUtil.notBlank(itm.getNationalCode())));
  176. Map<String, Object> map = new HashMap<>();
  177. map.put("mzVisit", mzVisit);
  178. map.put("mzReceipts", mzReceipts);
  179. return map;
  180. }
  181. public MzDepositFile mergeReceipts(List<MzDepositFile> list) {
  182. MzDepositFile minTimesVisit = list.get(0);
  183. List<MzReceipt> mergedReceipts = new ArrayList<>();
  184. for (MzDepositFile mzDepositFile : list) {
  185. if (mzDepositFile.getTimes() < minTimesVisit.getTimes()) {
  186. minTimesVisit = mzDepositFile;
  187. }
  188. List<MzReceipt> mzReceipts = dao.selectMzCharge(mzDepositFile.getPatNo(),
  189. mzDepositFile.getTimes(), mzDepositFile.getReceiptNo());
  190. mzReceipts.removeIf(item -> StringUtil.isBlank(item.getNationalCode()));
  191. mergedReceipts.addAll(mzReceipts);
  192. }
  193. String hasCovidExamFees = covidExamFeesDetect(mergedReceipts);
  194. if (StringUtil.notBlank(hasCovidExamFees)) {
  195. throw new BizException(ExceptionEnum.LOGICAL_ERROR, hasCovidExamFees);
  196. }
  197. for (int i = 0; i < mergedReceipts.size(); i++) {
  198. MzReceipt receipt = mergedReceipts.get(i);
  199. receipt.setTimes(minTimesVisit.getTimes());
  200. receipt.setItemNo(i + 1);
  201. }
  202. dao.deleteAllReceipts(list.get(0).getPatNo(), minTimesVisit.getTimes());
  203. insertBatchedMtReceipts(mergedReceipts);
  204. String message = "合并处方成功,请使用【第 " + minTimesVisit.getTimes() + " 次就诊】办理医保业务。";
  205. minTimesVisit.setMessage(message);
  206. return minTimesVisit;
  207. }
  208. private void insertBatchedMtReceipts(List<MzReceipt> receipts) {
  209. List<MzReceipt> tempList = new ArrayList<>();
  210. for (MzReceipt item : receipts) {
  211. tempList.add(item);
  212. if (tempList.size() == 50) {
  213. dao.insertMtReceipts(tempList);
  214. tempList.clear();
  215. }
  216. }
  217. if (!tempList.isEmpty()) {
  218. dao.insertMtReceipts(tempList);
  219. }
  220. }
  221. private int calSettleFlag(MzVisit f) {
  222. int siCount = dao.selectSiCount(f.getPatientId(), f.getTimes());
  223. if (siCount > 0) {
  224. return 1;
  225. }
  226. String hisOrdNum = f.getPatientId() + "_" + f.getTimes() + "_" + f.getReceiptNo();
  227. int mipay = dao.selectMipayCount(hisOrdNum);
  228. return mipay == 0 ? 0 : 2;
  229. }
  230. private MzReceipt fillMzReceipt(Map<String, Object> detail) {
  231. MzReceipt receipt = new MzReceipt();
  232. receipt.setReceiptNo((int) detail.get("receiptNo"));
  233. receipt.setOrderNo((int) detail.get("orderNo"));
  234. receipt.setItemNo((int) detail.get("itemNo"));
  235. receipt.setDrugName(detail.get("tcName").toString());
  236. receipt.setChargeItemCode(detail.get("chargeItemCode").toString());
  237. receipt.setPriceTime(DateUtil.formatPriceTime(detail.get("priceTime").toString()));
  238. receipt.setUnitPrice(new BigDecimal(detail.get("unitPrice").toString()).setScale(4, RoundingMode.HALF_UP));
  239. receipt.setQuantity(Double.valueOf(detail.get("quantity").toString()));
  240. receipt.setDrugWin((int) detail.get("drugWin"));
  241. receipt.setDoctorCode(detail.get("doctorCode").toString());
  242. receipt.setGroupNo(detail.get("groupNo").toString());
  243. receipt.setChargeDate(detail.get("priceTime").toString().split("\\+")[0].replace("T", " "));
  244. receipt.setBillItemCode(detail.get("billItemCode").toString());
  245. receipt.setChargeBillCode(detail.get("chargeBillCode").toString());
  246. String table = receipt.getGroupNo().trim().equals("00") ? "zd_charge_item" : "yp_zd_dict";
  247. receipt.setNationalCode(dao.selectNationalCode(receipt.getChargeItemCode(), table));
  248. receipt.setChecked(StringUtil.notBlank(receipt.getNationalCode()));
  249. if (null != detail.get("hospApprFlag")) {
  250. receipt.setHospApprFlag(detail.get("hospApprFlag").toString());
  251. }
  252. if (null != detail.get("serialNo")) {
  253. receipt.setSerialNo(detail.get("serialNo").toString());
  254. }
  255. if (null != detail.get("instructionText")) {
  256. receipt.setInstructionText(detail.get("instructionText").toString());
  257. }
  258. if (null != detail.get("specification")) {
  259. receipt.setSpecification(detail.get("specification").toString());
  260. }
  261. if (null != detail.get("frequency")) {
  262. receipt.setFrequency(detail.get("frequency").toString());
  263. }
  264. if (null != detail.get("drugQuan")) {
  265. receipt.setDrugQuan(Double.valueOf(detail.get("drugQuan").toString()));
  266. }
  267. if (null != detail.get("orderDays")) {
  268. receipt.setOrderDays((Integer) detail.get("orderDays"));
  269. }
  270. // groupNo:00-项目;其他-药品
  271. if ("00".equals(receipt.getGroupNo())) {
  272. receipt.setDrugUnit(dao.selectXmChargeUnit(receipt.getChargeItemCode()));
  273. } else {
  274. receipt.setYbComment(dao.selectYbComment(receipt.getChargeItemCode()));
  275. if (null != detail.get("serial")) {
  276. receipt.setSerial(detail.get("serial").toString());
  277. receipt.setSpecification(dao.selectSpecification(receipt.getChargeItemCode(), receipt.getSerial()));
  278. }
  279. if (null != detail.get("supplyCode")) {
  280. receipt.setSupplyCode(dao.selectSupplyName(detail.get("supplyCode").toString()));
  281. }
  282. if (null != detail.get("drugUnit")) {
  283. receipt.setDrugUnit(dao.selectDrugUnit(detail.get("drugUnit").toString()));
  284. }
  285. }
  286. return receipt;
  287. }
  288. private List<OrderNo> getOrderNos(String patientId, int times, int receiptNo, Map<Integer, List<MzReceipt>> orderReceiptsMap) {
  289. List<OrderNo> orderNos = new ArrayList<>();
  290. for (Map.Entry<Integer, List<MzReceipt>> entry : orderReceiptsMap.entrySet()) {
  291. OrderNo orderNo = new OrderNo();
  292. orderNo.setPatientId(patientId);
  293. orderNo.setTimes(times);
  294. orderNo.setReceiptNo(entry.getValue().get(0).getReceiptNo());
  295. orderNo.setOrderNo(entry.getKey());
  296. BigDecimal total = new BigDecimal("0.00");
  297. for (MzReceipt receipt : entry.getValue()) {
  298. total = total.add(receipt.getChargeFee());
  299. }
  300. orderNo.setTotalFee(total);
  301. int count = dao.selectFeeCount(patientId, times, receiptNo, entry.getKey());
  302. orderNo.setStatus(count > 0);
  303. orderNos.add(orderNo);
  304. }
  305. return orderNos;
  306. }
  307. public ResultVo<String> insertSiMzFees(List<MzReceipt> receipts) {
  308. receipts.removeIf(item -> !item.getChecked());
  309. if (receipts.isEmpty()) {
  310. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "患者没有可以报销的项目!");
  311. }
  312. String hasCovidExamFees = covidExamFeesDetect(receipts);
  313. if (StringUtil.notBlank(hasCovidExamFees)) {
  314. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, hasCovidExamFees);
  315. }
  316. dao.insertMtReceipts(receipts);
  317. return ResultVoUtil.success();
  318. }
  319. private String covidExamFeesDetect(List<MzReceipt> receipts) {
  320. StringBuilder message = new StringBuilder();
  321. for (MzReceipt receipt : receipts) {
  322. if (receipt.getChargeItemCode().trim().equals("019180")) {
  323. message.append("【019180,新型冠状病毒核酸检测】");
  324. }
  325. if (receipt.getChargeItemCode().trim().equals("019110")) {
  326. message.append("【019110,新型冠状病毒2019-ncov核酸检测试剂盒(荧光)】");
  327. }
  328. }
  329. if (message.length() > 0) {
  330. message.append("无法上传至医保中心,请单独开具处方。");
  331. return "因政策原因," + message;
  332. }
  333. return null;
  334. }
  335. public ResultVo<String> insertSiMzFeesForSupplement(List<MzReceipt> receipts) {
  336. receipts.removeIf(item -> !item.getChecked() || item.getChargeItemCode().equals("019110")
  337. || item.getChargeItemCode().equals("019180"));
  338. if (receipts.isEmpty()) {
  339. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "患者没有可以报销的项目!");
  340. }
  341. MzReceipt receipt = receipts.get(0);
  342. List<Integer> receiptNos = dao.selectGeneratedReceiptNos(receipt.getPatientId(), receipt.getTimes());
  343. for (int receiptNo : receiptNos) {
  344. if (dao.isReceiptPayed(receipt.getPatientId(), receipt.getTimes(), receiptNo) == 0) {
  345. dao.deleteCertainReceipt(receipt.getPatientId(), receipt.getTimes(), receiptNo);
  346. }
  347. }
  348. dao.insertMtReceipts(receipts);
  349. return ResultVoUtil.success();
  350. }
  351. public ResultVo<String> deleteMzReceipt(OrderNo param) {
  352. dao.deleteMzReceipt(param);
  353. return ResultVoUtil.success();
  354. }
  355. public ResultVo<String> deleteAllMzReceipts(MzPatientInfo p) {
  356. if (null == p.getTimes()) {
  357. p.setTimes(dao.selectMaxTimes(p.getPatNo()));
  358. }
  359. log.info("【操作员:{}】删除所有门特处方:门诊号:{},门诊次数:{}",
  360. TokenUtil.getInstance().getTokenUserId(), p.getPatNo(), p.getTimes());
  361. dao.deleteAllReceipts(p.getPatNo(), p.getTimes());
  362. return ResultVoUtil.success("删除成功。");
  363. }
  364. public ResultVo<SiPatInfo> uploadOutpatientFeeDetails(SpcChrDiseAcct p) {
  365. p.setStaffId(TokenUtil.getInstance().getTokenUserId());
  366. return mzSrvc.uploadOutpatientFeeDetails(siMzFeeUrl, p);
  367. }
  368. public ResultVo<String> revokeOutpatientFeeDetails(MzPatientInfo p) {
  369. p.setStaffId(TokenUtil.getInstance().getTokenUserId());
  370. return mzSrvc.revokeOutpatientFeeDetails(siMzFeeUrl, p);
  371. }
  372. public ResultVo<FundDetail> outpatientPreSettlement(MzPatientInfo p) {
  373. if (null == p.getStaffId()) {
  374. p.setStaffId(TokenUtil.getInstance().getTokenUserId());
  375. }
  376. return mzSrvc.outpatientPreSettlement(siMzFeeUrl, p);
  377. }
  378. public ResultVo<FundDetail> outpatientSettlement(MzPatientInfo p) {
  379. if (null == p.getStaffId()) {
  380. p.setStaffId(TokenUtil.getInstance().getTokenUserId());
  381. }
  382. return mzSrvc.outpatientSettlement(siMzFeeUrl, p);
  383. }
  384. public ResultVo<FundDetail> timeoutSetl(MzPatientInfo p) {
  385. if (null == p.getStaffId()) {
  386. p.setStaffId(TokenUtil.getInstance().getTokenUserId());
  387. }
  388. String logContent = dao.getLogContent(p.getMsgid());
  389. if (!logContent.contains("服务提供者后端服务响应超时")) {
  390. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR,
  391. "该笔交易不符合超时结算条件,无法进行超时结算(结算失败的原因为“服务提供者后端服务响应超时”时,方可进行超时结算)。");
  392. }
  393. return mzSrvc.timeoutSetl(siMzFeeUrl, p);
  394. }
  395. public boolean receiptAlreadyPrinted(MzPatientInfo p) {
  396. String receiptBill = dao.selectReceiptBill(p.getPatNo(), p.getTimes());
  397. return StringUtil.notBlank(receiptBill);
  398. }
  399. public ResultVo<FundDetail> revokeOutpatientSettlement(MzPatientInfo p) {
  400. if (null == p.getStaffId()) {
  401. p.setStaffId(TokenUtil.getInstance().getTokenUserId());
  402. }
  403. return mzSrvc.revokeOutpatientSettlement(siMzFeeUrl, p);
  404. }
  405. public void insertSiPatInfo(MzPatientInfo mz) {
  406. int count = dao.selectSiMzInfoCount(mz.getPatNo(), -1);
  407. if (count == 0) {
  408. PsnBaseInfo patinfo = dao.selectPsnBaseinfo(mz.getPatNo(), mz.getTimes());
  409. if (null != patinfo) {
  410. patinfo.setTimes(-1);
  411. dao.insertSiMzInfoPsnNo(patinfo);
  412. }
  413. }
  414. }
  415. public ResultVo<String> saveSiMzDiags(List<SiMzDiag> diags) {
  416. if (ListUtil.isBlank(diags)) {
  417. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有要保存的诊断!");
  418. }
  419. SiMzDiag diag = diags.get(0);
  420. dao.deleteMzDiags(diag.getPatNo(), diag.getTimes());
  421. String staffId = TokenUtil.getInstance().getTokenUserId();
  422. for (SiMzDiag itm : diags) {
  423. itm.setRealOpter(staffId);
  424. dao.insertNewMzDiag(itm);
  425. }
  426. return ResultVoUtil.success("门诊诊断保存成功。");
  427. }
  428. public Map<String, Object> queryInsuinfo(SiMzBusinessParams param) {
  429. Map<String, Object> result = new HashMap<>();
  430. QryPsnBsInfo mzptnt = new QryPsnBsInfo();
  431. mzptnt.setStaffId(param.getStaffId());
  432. mzptnt.setPatNo(param.getPatientId());
  433. mzptnt.setMedType(MedType.GENERAL_CLINIC.getCode());
  434. mzptnt.setOnlyQry(1);
  435. mzptnt.setTimes(-1);
  436. if (StringUtil.notBlank(param.getAdmdvs())) {
  437. mzptnt.setAdmdvs(param.getAdmdvs());
  438. mzptnt.setSocialNo(param.getSocialNo());
  439. }
  440. ResultVo<PsnBaseInfo> baseInfo = qryService.obtainBasicPersonInfo(mzptnt);
  441. if (null == baseInfo) {
  442. result.put("code", -2);
  443. result.put("msg", "网络异常。");
  444. return result;
  445. }
  446. if (!Objects.equals(baseInfo.getCode(), ExceptionEnum.SUCCESS.getCode())) {
  447. result.put("code", -2);
  448. result.put("msg", baseInfo.getMessage());
  449. return result;
  450. }
  451. // 如果有职工保险,就入库
  452. PsnBaseInfo psninfo = baseInfo.getData();
  453. InsuInfo insuInfo = getResidantOrEmployeeInsuInfo(psninfo.getInsuinfo());
  454. if (null == insuInfo) {
  455. result.put("code", -1);
  456. result.put("msg", "患者没有职工医保或居民医保,无法报销。");
  457. return result;
  458. }
  459. psninfo.setInsutype(insuInfo.getInsutype());
  460. int count = dao.selectSiMzInfoCount(mzptnt.getPatNo(), mzptnt.getTimes());
  461. if (count == 0) {
  462. psninfo.setMedType(MedType.GENERAL_CLINIC.getCode());
  463. psninfo.setInsuplc(insuInfo.getInsuplcAdmdvs());
  464. psninfo.setInsuplcAdmdvsName(insuInfo.getInsuplcAdmdvsName());
  465. psninfo.setLedgerSn(0);
  466. psninfo.setPsnType(insuInfo.getPsnType());
  467. psninfo.setEmpName(insuInfo.getEmpName());
  468. psninfo.setBalc(insuInfo.getBalc());
  469. dao.insertSiMzInfoPsnNo(psninfo);
  470. } else {
  471. dao.updateSiZyInfoPsnNo(mzptnt.getPatNo(), mzptnt.getTimes(), psninfo.getInsutype(),
  472. insuInfo.getBalc(), insuInfo.getInsuplcAdmdvs(), insuInfo.getInsuplcAdmdvsName());
  473. }
  474. result.put("code", 0);
  475. result.put("expContentRequired", expContentRequired(insuInfo.getInsuplcAdmdvs()));
  476. result.put("msg", "查询参保信息成功。");
  477. return result;
  478. }
  479. public ResultVo<PsnBaseInfo> queryInsuinfoDetail(SiMzBusinessParams param) {
  480. QryPsnBsInfo mzptnt = new QryPsnBsInfo();
  481. mzptnt.setStaffId(param.getStaffId());
  482. mzptnt.setPatNo(param.getPatientId());
  483. mzptnt.setMedType(MedType.GENERAL_CLINIC.getCode());
  484. mzptnt.setOnlyQry(1);
  485. mzptnt.setTimes(-1);
  486. if (StringUtil.notBlank(param.getAdmdvs())) {
  487. mzptnt.setAdmdvs(param.getAdmdvs());
  488. }
  489. ResultVo<PsnBaseInfo> baseInfo = qryService.obtainBasicPersonInfo(mzptnt);
  490. if (null == baseInfo) {
  491. throw new BizException(ExceptionEnum.NETWORK_ERROR);
  492. }
  493. if (!Objects.equals(baseInfo.getCode(), ExceptionEnum.SUCCESS.getCode())) {
  494. throw new BizException(ExceptionEnum.NETWORK_ERROR, baseInfo.getMessage());
  495. }
  496. return baseInfo;
  497. }
  498. private InsuInfo getResidantOrEmployeeInsuInfo(List<InsuInfo> list) {
  499. for (InsuInfo info : list) {
  500. Insutype insutype = Insutype.get(info.getInsutype());
  501. if (null == insutype) {
  502. continue;
  503. }
  504. if (insutype == Insutype.BASIC_MEDICAL_INSURANCE_FOR_EMPLOYEES ||
  505. insutype == Insutype.BASIC_MEDICAL_INSURANCE_FOR_RURAL_RESIDENTS) {
  506. return info;
  507. }
  508. }
  509. return null;
  510. }
  511. private int expContentRequired(String insuplc) {
  512. return (insuplc.startsWith("4301") || insuplc.equals("439900")) ? 0 : 1;
  513. }
  514. public ResultVo<FundDetail> directRegistration(SiMzBusinessParams param) {
  515. return mzSrvc.directRegistration(siMzFeeUrl, param);
  516. }
  517. public ResultVo<FundDetail> mzgjAfterModifyReceipt(SiMzBusinessParams param) {
  518. PsnBaseInfo psnBaseInfo = dao.selectLatestPsnInfo(param.getPatientId());
  519. if (null == psnBaseInfo) {
  520. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "无法办理,没有找到参保信息。");
  521. }
  522. dao.deleteAllReceipts(param.getPatientId(), param.getTimes());
  523. param.setStraitSettle(true);
  524. return mzSrvc.directRegistration(siMzFeeUrl, param);
  525. }
  526. public ResultVo<Integer> isPatientDuringSiSettle(String patientId) {
  527. return mzSrvc.isPatientDuringSiSettle(siMzFeeUrl, patientId);
  528. }
  529. public String queryModifyPermission(MzPatientInfo info) {
  530. int pre = dao.selectPresettleCount(info.getPatNo(), info.getTimes());
  531. if (pre > 0) {
  532. return "此患者的处方已上传医保,无法修改。";
  533. }
  534. String setlStaff = dao.selectSetlStaff(info.getPatNo(), info.getTimes());
  535. if (StrUtil.isEmpty(setlStaff)) {
  536. return "OK";
  537. }
  538. if (Objects.equals(info.getStaffId(), setlStaff)) {
  539. return "OK";
  540. }
  541. return "DENIED";
  542. }
  543. public Map<String, Object> modifyFundPayAmount(ModifyFundAmt amt) {
  544. amt.fixNullVal();
  545. int res = dao.modifyFundAmt(amt);
  546. Map<String, Object> resultMap = new HashMap<>();
  547. if (res > 0) {
  548. resultMap.put("code", 0);
  549. resultMap.put("msg", "更新成功,有" + res + "条数据受影响。");
  550. } else {
  551. resultMap.put("code", -1);
  552. resultMap.put("msg", "更新失败,有0条数据受影响。");
  553. }
  554. log.info("THMZ修改报销数据:\n参数:{}\n结果:{}", amt, resultMap);
  555. return resultMap;
  556. }
  557. public MzBlRecord getMzBlRecord(String patientId, Integer times) {
  558. return dao.selectMzBlRecord(patientId, times);
  559. }
  560. public ResultVo<FundDetail> ybRegForRx(SiMzBusinessParams params) {
  561. return mzSrvc.directRegistration(siMzFeeUrl, params);
  562. }
  563. public List<RxIndex> getRxIndex(IndexInquiry inquiry) {
  564. if (null == inquiry.getBegintime()) {
  565. inquiry.setBegintime(DateUtil.parse("2024-01-01", "yyyy-MM-dd"));
  566. }
  567. if (null == inquiry.getEndtime()) {
  568. inquiry.setEndtime(new Date());
  569. }
  570. List<RxIndex> list = rxDao.getRxIndex(inquiry);
  571. if (inquiry.getState() != RxState.ALL) {
  572. list.removeIf(item -> item.getState() != inquiry.getState());
  573. }
  574. return list;
  575. }
  576. public String getRxDetail(String hospRxno) {
  577. String rxFile = rxDao.getRxFile(hospRxno);
  578. if (StringUtil.notBlank(rxFile)) {
  579. return rxFile;
  580. }
  581. RxPreCheckRequest request = new RxPreCheckRequest();
  582. request.setHospRxno(hospRxno);
  583. request.setStaffId(TokenUtil.getInstance().getTokenUserId());
  584. ResultVo<String> pdfVo = mzSrvc.createRxPdf(siMzFeeUrl, makePreCheckRequest(request));
  585. if (!Objects.equals(pdfVo.getCode(), ExceptionEnum.SUCCESS.getCode())) {
  586. throw new BizException(ExceptionEnum.LOGICAL_ERROR, pdfVo.getMessage());
  587. }
  588. return pdfVo.getData();
  589. }
  590. public ResultVo<JSONObject> rxPreCheck(RxPreCheckRequest request) {
  591. makePreCheckRequest(request);
  592. if (StringUtil.isBlank(request.getRxMdtrtinfo().getMdtrtId())) {
  593. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "患者未做医保登记,无法进行预核验!");
  594. }
  595. return mzSrvc.rxPreCheck(siMzFeeUrl, request);
  596. }
  597. private RxPreCheckRequest makePreCheckRequest(RxPreCheckRequest request) {
  598. RxMdtrtInfo rxMdtrtInfo = rxDao.getRxMdtrtinfo(request.getHospRxno());
  599. RxPreCheck rxPreCheck = rxDao.getRxReceiptinfo(request.getHospRxno());
  600. fillMdtrtCertNo(request, rxPreCheck);
  601. rxPreCheck.setBizTypeCode("01");
  602. List<RxDrugDetail> rxDrugDetailList = rxDao.getRxDruginfo(request.getHospRxno());
  603. for (RxDrugDetail item : rxDrugDetailList) {
  604. String spec = item.getDrugSpec();
  605. String unt = String.valueOf(spec.charAt(spec.length() - 1));
  606. item.setDrugTotlcntEmp(unt);
  607. }
  608. List<RxDiseInfo> rxDiseInfoList = rxDao.getRxDiseinfo(request.getHospRxno());
  609. request.setRxPreCheck(rxPreCheck);
  610. request.setRxMdtrtinfo(rxMdtrtInfo);
  611. request.setRxDrugDetailList(rxDrugDetailList);
  612. request.setRxDiseInfoList(rxDiseInfoList);
  613. return request;
  614. }
  615. private void fillMdtrtCertNo(RxPreCheckRequest request, RxPreCheck rxPreCheck) {
  616. ReadCardType readCardType = request.getReadCardType();
  617. rxPreCheck.setMdtrtCertType(readCardType.getCode());
  618. if (readCardType == ReadCardType.SFZ) {
  619. return;
  620. }
  621. String readCardResult = request.getReadCardResult();
  622. if (StringUtil.isBlank(readCardResult)) {
  623. throw new BizException("没有获取到读卡信息,请稍后重试。");
  624. }
  625. if (readCardType == ReadCardType.DZPZ) {
  626. JSONObject qrinfo = JSONObject.parseObject(readCardResult);
  627. String ecToken = qrinfo.getString("ecToken");
  628. rxPreCheck.setMdtrtCertNo(ecToken);
  629. rxPreCheck.setEcToken(ecToken);
  630. } else if (readCardType == ReadCardType.SBK) {
  631. String[] out = readCardResult.split("\\|");
  632. rxPreCheck.setMdtrtCertNo(out[2]);
  633. rxPreCheck.setCardSn(out[3]);
  634. }
  635. }
  636. public ResultVo<JSONObject> rxSign(RxSignRequest request) {
  637. RxUpload pharInfo = rxDao.selectPharInfo(request.getStaffId());
  638. pharInfo.setHospRxno(request.getHospRxno());
  639. rxDao.updatePharInfo(pharInfo);
  640. RxUpload rxUpload = rxDao.selectRxUploadForSign(request.getHospRxno());
  641. RxSign rxSign = new RxSign();
  642. String j = JSONObject.toJSONStringWithDateFormat(rxUpload, "yyyy-MM-dd HH:mm:ss");
  643. rxSign.setOriginalValue(Base64.getEncoder().encodeToString(j.getBytes(StandardCharsets.UTF_8)));
  644. rxSign.setOriginalRxFile(rxDao.selectOriginRxFile(request.getHospRxno()));
  645. request.setRxSign(rxSign);
  646. return mzSrvc.rxSign(siMzFeeUrl, request);
  647. }
  648. public ResultVo<JSONObject> rxUpload(RxUploadRequest request) {
  649. RxUpload rxUpload = rxDao.selectFullRxUploadInfo(request.getHospRxno());
  650. request.setRxUpload(rxUpload);
  651. return mzSrvc.rxUpload(siMzFeeUrl, request);
  652. }
  653. public ResultVo<JSONObject> rxRevoke(RxRevokeRequest request) {
  654. RxRevoke rxRevoke = rxDao.selectRxRevoke(request);
  655. request.setRxRevoke(rxRevoke);
  656. return mzSrvc.rxRevoke(siMzFeeUrl, request);
  657. }
  658. public ResultVo<JSONObject> rxInfoQuery(RxInfoQueryRequest request) {
  659. RxInfoQuery rxInfoQuery = rxDao.selectRxInfoQuery(request.getHospRxno());
  660. request.setRxInfoQuery(rxInfoQuery);
  661. return mzSrvc.rxInfoQuery(siMzFeeUrl, request);
  662. }
  663. public ResultVo<JSONObject> rxAuditingQuery(RxAuditingQueryRequest request) {
  664. RxInfoQuery rxInfoQuery = rxDao.selectRxInfoQuery(request.getHospRxno());
  665. request.setRxAuditingQuery(rxInfoQuery);
  666. return mzSrvc.rxAuditingQuery(siMzFeeUrl, request);
  667. }
  668. public ResultVo<JSONObject> rxSetlQuery(RxSetlQueryRequest request) {
  669. RxInfoQuery rxInfoQuery = rxDao.selectRxInfoQuery(request.getHospRxno());
  670. request.setRxSetlQuery(rxInfoQuery);
  671. return mzSrvc.rxSetlQuery(siMzFeeUrl, request);
  672. }
  673. public ResultVo<String> regRevoke(RxRevokeRequest request) {
  674. MzPatientInfo patinfo = rxDao.getBriefPatInfo(request.getHospRxno());
  675. patinfo.setStaffId(request.getStaffId());
  676. return mzSrvc.revokeOutpatientRegistration(siMzFeeUrl, patinfo);
  677. }
  678. public ResultVo<JSONObject> medicineInquiry(MedicineInquiry inquiry) throws Exception {
  679. return mzSrvc.medicineInquiry(siMzFeeUrl, inquiry);
  680. }
  681. }