SiMzService.java 33 KB

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