SiMzFeeService.java 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. package thyyxxk.simzfeeoprnsystm.service;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import thyyxxk.simzfeeoprnsystm.dao.SiLogDao;
  6. import thyyxxk.simzfeeoprnsystm.dao.SiMzDao;
  7. import thyyxxk.simzfeeoprnsystm.dao.SiSetldetailDao;
  8. import thyyxxk.simzfeeoprnsystm.dao.SiSetlinfoDao;
  9. import thyyxxk.simzfeeoprnsystm.dicts.*;
  10. import thyyxxk.simzfeeoprnsystm.external.WebHisSrvc;
  11. import thyyxxk.simzfeeoprnsystm.pojo.*;
  12. import thyyxxk.simzfeeoprnsystm.pojo.ResultVo;
  13. import thyyxxk.simzfeeoprnsystm.pojo.SiPatInfo;
  14. import thyyxxk.simzfeeoprnsystm.utils.*;
  15. import lombok.extern.slf4j.Slf4j;
  16. import org.springframework.beans.factory.annotation.Autowired;
  17. import org.springframework.stereotype.Service;
  18. import java.math.BigDecimal;
  19. import java.math.RoundingMode;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Objects;
  24. import java.util.concurrent.TimeUnit;
  25. @Slf4j
  26. @Service
  27. public class SiMzFeeService {
  28. private final SiMzDao mzDao;
  29. private final SiSetlinfoDao setlinfoDao;
  30. private final SiSetldetailDao setldetailDao;
  31. private final ExecService exec;
  32. private final WebHisSrvc webHisSrvc;
  33. private final MariadbService mariadbService;
  34. private static final String RESULT_CODE = "infcode";
  35. private static final String ERROR_MESSAGE = "err_msg";
  36. private static final String OUTPUT = "output";
  37. private final SiLogDao logDao;
  38. @Value("${web-his-url}")
  39. private String webHisUrl;
  40. @Autowired
  41. public SiMzFeeService(SiMzDao mzDao, SiSetlinfoDao setlinfoDao,
  42. SiSetldetailDao setldetailDao, ExecService exec, WebHisSrvc webHisSrvc, MariadbService mariadbService, SiLogDao logDao) {
  43. this.mzDao = mzDao;
  44. this.setlinfoDao = setlinfoDao;
  45. this.setldetailDao = setldetailDao;
  46. this.exec = exec;
  47. this.webHisSrvc = webHisSrvc;
  48. this.mariadbService = mariadbService;
  49. this.logDao = logDao;
  50. }
  51. public ResultVo<String> outpatientRegistration(MzPatientInfo p) {
  52. // if (p.getInsuplcAdmdvs().equals("430182") && !Objects.equals("11", p.getMedType())) {
  53. // return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "宁乡参保地区的患者我院只能办理普门医保业务,其他类别的业务暂不支持。");
  54. // }
  55. Regstrtn regstrtn = mzDao.selectRegstrtn(p.getPatNo(), p.getTimes());
  56. if (null == regstrtn) {
  57. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "mz_visit_table患者信息为空,请联系医生重开处方。");
  58. }
  59. ReadCardBizType readCardBizType = ReadCardBizType.get(p.getReadCardBizType());
  60. if (readCardBizType != ReadCardBizType.REGISTRATION && !p.getInsuplcAdmdvs().startsWith("43")) {
  61. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "省外异地的患者请读社保卡或医保电子凭证登记!");
  62. }
  63. if (StringUtil.notBlank(p.getMedType())) {
  64. mzDao.updateMedtype(p.getMedType(), p.getPatNo(), p.getTimes());
  65. }
  66. if (readCardBizType == ReadCardBizType.REGISTRATION) {
  67. MdtrtCertType mdtrtCertType = MdtrtCertType.getByLabel(p.getMdtrtCertType());
  68. regstrtn.setMdtrtCertType(mdtrtCertType.getCode());
  69. if (mdtrtCertType == MdtrtCertType.SOCIAL_SECURITY_CARD) {
  70. String[] out = p.getReadCardResult().split("\\|");
  71. regstrtn.setMdtrtCertNo(out[2]);
  72. regstrtn.setCardSn(out[3]);
  73. } else if (mdtrtCertType == MdtrtCertType.MEDICAL_INSURANCE_ELECTRONIC_VOUCHER) {
  74. JSONObject qrinfo = JSONObject.parseObject(p.getReadCardResult());
  75. regstrtn.setMdtrtCertNo(qrinfo.getString("ecToken"));
  76. }
  77. }
  78. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.OUTPATIENT_REGISTRATION,
  79. p.getInsuplcAdmdvs(), p.getStaffId());
  80. regstrtn.setInsutype(p.getInsutype());
  81. regstrtn.setPsnType(p.getPsnType());
  82. regstrtn.setIptOtpNo(p.getPatNo());
  83. String ref = JSONObject.toJSONStringWithDateFormat(regstrtn, "yyyy-MM-dd HH:mm:ss");
  84. input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
  85. JSONObject result = exec.executeTrade(input, SiFunction.OUTPATIENT_REGISTRATION);
  86. log.info("【操作员:{}】,门诊挂号:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
  87. if (null == result) {
  88. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  89. }
  90. Integer infcode = result.getInteger(RESULT_CODE);
  91. logDao.insert(new SiLog(input, result, p.getPatNo(), p.getTimes(), infcode, regstrtn.getPsnNo()));
  92. if (null == infcode) {
  93. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  94. }
  95. if (infcode == 0) {
  96. JSONObject output = result.getJSONObject(OUTPUT).getJSONObject("data");
  97. p.setMdtrtId(output.getString("mdtrt_id"));
  98. p.setVisitDate(regstrtn.getBegntime());
  99. mzDao.afterRegistrtn(p);
  100. return ResultVoUtil.success("挂号成功。");
  101. }
  102. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  103. }
  104. public ResultVo<String> revokeOutpatientRegistration(MzPatientInfo p) {
  105. if (null == p.getTimes()) {
  106. p.setTimes(mzDao.selectMaxTimes(p.getPatNo()));
  107. }
  108. SiPatInfo siPatInfo = mzDao.selectSiPatInfoForMz(p.getPatNo(), p.getTimes());
  109. if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getMdtrtId())) {
  110. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保挂号信息。");
  111. }
  112. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_OUTPATIENT_REGISTRATION,
  113. siPatInfo.getInsuplcAdmdvs(), p.getStaffId());
  114. JSONObject data = new JSONObject();
  115. data.put("psn_no", siPatInfo.getPsnNo());
  116. data.put("mdtrt_id", siPatInfo.getMdtrtId());
  117. data.put("ipt_otp_no", p.getPatNo());
  118. input.getJSONObject("input").put("data", data);
  119. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_OUTPATIENT_REGISTRATION);
  120. log.info("【操作员:{}】,取消门诊挂号:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
  121. if (null == result) {
  122. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  123. }
  124. Integer infcode = result.getInteger(RESULT_CODE);
  125. logDao.insert(new SiLog(input, result, p.getPatNo(), p.getTimes(), infcode, siPatInfo.getPsnNo()));
  126. if (null == infcode) {
  127. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  128. }
  129. if (infcode == 0) {
  130. mzDao.clearMdtrtIdForMz(p.getPatNo(), p.getTimes(), null);
  131. return ResultVoUtil.success("取消门诊挂号成功。");
  132. }
  133. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  134. }
  135. private ResultVo<String> uploadOutpatientInfo(SpcChrDiseAcct spcChrDiseAcct, SiPatInfo siPatInfo) {
  136. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.UPLOAD_OUTPATIENT_INFO,
  137. siPatInfo.getInsuplcAdmdvs(), spcChrDiseAcct.getStaffId());
  138. JSONObject mdtrtinfo = new JSONObject();
  139. List<Diagnoses> diagnosesList = new ArrayList<>();
  140. String icdCodeNew = mzDao.selectIcdCodeNew(siPatInfo.getPatNo(), siPatInfo.getTimes());
  141. if (StringUtil.isBlank(icdCodeNew)) {
  142. diagnosesList = mzDao.selectMzDiags(siPatInfo.getPatNo(), siPatInfo.getTimes());
  143. if (null == diagnosesList || diagnosesList.isEmpty()) {
  144. log.info("【{}】 上传就诊信息失败,门诊诊断为空。", siPatInfo.getPatNo());
  145. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "患者的门诊诊断为空,请联系医生填写。");
  146. }
  147. } else {
  148. Diagnoses base = mzDao.selectDiseinfo(siPatInfo.getPatNo(), siPatInfo.getTimes());
  149. String[] icds = icdCodeNew.split(",");
  150. for (int i = 0; i < icds.length; i++) {
  151. String icd = icds[i];
  152. String icdTextNew = mzDao.selectIcdTextNew(icd);
  153. Diagnoses diag = new Diagnoses();
  154. diag.setDiagCode(icd);
  155. diag.setDiagName(icdTextNew);
  156. diag.setDiagSrtNo(i + 1);
  157. diag.setDiagType("1");
  158. diag.setValiFlag("1");
  159. diag.setDiagDept(base.getDiagDept());
  160. diag.setDiagTime(base.getDiagTime());
  161. diag.setDiseDorNo(base.getDiseDorNo());
  162. diag.setDiseDorName(base.getDiseDorName());
  163. diagnosesList.add(diag);
  164. }
  165. }
  166. if (siPatInfo.getMedType().equals("14") || siPatInfo.getMedType().equals("51")) {
  167. if (StringUtil.isBlank(spcChrDiseAcct.getOpspDiseCode())) {
  168. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "生育门诊与慢特病门诊的病种不能为空 !");
  169. }
  170. }
  171. mdtrtinfo.put("mdtrt_id", siPatInfo.getMdtrtId());
  172. mdtrtinfo.put("psn_no", siPatInfo.getPsnNo());
  173. mdtrtinfo.put("med_type", siPatInfo.getMedType());
  174. mdtrtinfo.put("begntime", DateUtil.formatDatetime(siPatInfo.getVisitDatetime()));
  175. mdtrtinfo.put("main_cond_dscr", spcChrDiseAcct.getOpspDiseName());
  176. mdtrtinfo.put("dise_codg", spcChrDiseAcct.getOpspDiseCode());
  177. mdtrtinfo.put("dise_name", spcChrDiseAcct.getOpspDiseName());
  178. mdtrtinfo.put("birctrl_type", "");
  179. mdtrtinfo.put("birctrl_matn_date", "");
  180. mdtrtinfo.put("exp_content", spcChrDiseAcct.getExpContent());
  181. String ref = JSONObject.toJSONString(diagnosesList);
  182. input.getJSONObject("input").put("mdtrtinfo", mdtrtinfo);
  183. input.getJSONObject("input").put("diseinfo", JSONArray.parse(ref));
  184. JSONObject result = exec.executeTrade(input, SiFunction.UPLOAD_OUTPATIENT_INFO);
  185. log.info("【操作员:{}】门诊就诊信息上传:\n参数:{},\n结果:{}", spcChrDiseAcct.getStaffId(), input, result);
  186. Integer infcode = result.getInteger(RESULT_CODE);
  187. logDao.insert(new SiLog(input, result, siPatInfo.getPatNo(), siPatInfo.getTimes(), infcode, siPatInfo.getPsnNo()));
  188. if (result.getIntValue(RESULT_CODE) == 0) {
  189. Diagnoses main = diagnosesList.get(0);
  190. mzDao.updatePatDiseinfo(siPatInfo.getPatNo(), siPatInfo.getTimes(), main.getDiagCode(), main.getDiagName());
  191. return ResultVoUtil.success("门诊就诊信息上传成功。");
  192. }
  193. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  194. }
  195. public ResultVo<SiPatInfo> uploadOutpatientFeeDetails(SpcChrDiseAcct p) {
  196. String patNo = p.getPatNo();
  197. if (null == p.getTimes()) {
  198. p.setTimes(mzDao.selectMaxTimes(patNo));
  199. }
  200. SiPatInfo siPatInfo = mzDao.selectSiPatInfoForMz(patNo, p.getTimes());
  201. if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getMdtrtId())) {
  202. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "【门诊号:" + patNo +
  203. "】未找到此患者的医保挂号信息,请核实。");
  204. }
  205. if (!p.getFromDirectReg()) {
  206. ResultVo<String> uplRes = uploadOutpatientInfo(p, siPatInfo);
  207. if (null != uplRes && uplRes.getCode() != ExceptionEnum.SUCCESS.getCode()) {
  208. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, uplRes.getMessage());
  209. }
  210. }
  211. List<FeeDetail> feeDetails = mzDao.selectOutpatientFees(patNo, p.getTimes());
  212. if (null == feeDetails || feeDetails.isEmpty()) {
  213. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有需要上传的费用。");
  214. }
  215. MzVisit mzVisit = mzDao.selectMzVisitInfo(patNo, p.getTimes());
  216. if (null == mzVisit || StringUtil.isBlank(mzVisit.getVisitDeptCode())) {
  217. log.info("【{},{}】就诊科室没有匹配医保码,无法使用门诊统筹。", patNo, p.getTimes());
  218. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "就诊科室没有匹配医保码,无法使用门诊统筹。");
  219. }
  220. String batchNo = patNo + "_" + p.getTimes();
  221. feeDetails.forEach(item -> {
  222. item.setMdtrtId(siPatInfo.getMdtrtId());
  223. item.setPsnNo(siPatInfo.getPsnNo());
  224. item.setChrgBchno(batchNo);
  225. item.setRxCircFlag(YesOrNo.NO.getCodeStr());
  226. item.setHospApprFlag(YesOrNo.YES.getCodeStr());
  227. item.setBilgDeptCodg(mzVisit.getVisitDeptCode());
  228. item.setBilgDeptName(mzVisit.getVisitDeptName());
  229. if (siPatInfo.getMedType().equals("51")) {
  230. item.setMatnFeeFlag("1");
  231. }
  232. });
  233. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.UPLOAD_OUTPATIENT_FEE_DETAILS,
  234. siPatInfo.getInsuplcAdmdvs(), p.getStaffId());
  235. String ref = JSONArray.toJSONStringWithDateFormat(feeDetails, "yyyy-MM-dd HH:mm:ss");
  236. JSONArray feedetail = JSONArray.parseArray(ref);
  237. input.getJSONObject("input").put("feedetail", feedetail);
  238. JSONObject result = exec.executeTrade(input, SiFunction.UPLOAD_OUTPATIENT_FEE_DETAILS);
  239. log.info("【操作员:{}】门诊费用明细信息上传:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
  240. if (null == result) {
  241. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  242. }
  243. Integer infcode = result.getInteger(RESULT_CODE);
  244. logDao.insert(new SiLog(input, result, p.getPatNo(), p.getTimes(), infcode, siPatInfo.getPsnNo()));
  245. if (null == infcode) {
  246. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  247. }
  248. if (infcode == 0) {
  249. JSONArray feeRes = result.getJSONObject(OUTPUT).getJSONArray("result");
  250. double fulamtOwnpayAmt = 0d;
  251. double overlmtAmt = 0d;
  252. double preselfpayAmt = 0d;
  253. double inscpScpAmt = 0d;
  254. for (int i = 0; i < feeRes.size(); i++) {
  255. JSONObject fee = feeRes.getJSONObject(i);
  256. fulamtOwnpayAmt += fee.getDoubleValue("fulamt_ownpay_amt");
  257. overlmtAmt += fee.getDoubleValue("overlmt_amt");
  258. preselfpayAmt += fee.getDoubleValue("preselfpay_amt");
  259. inscpScpAmt += fee.getDoubleValue("inscp_scp_amt");
  260. String detlSn = fee.getString("feedetl_sn");
  261. String chrgLv = fee.getString("chrgitm_lv");
  262. String chrgType = fee.getString("med_chrgitm_type");
  263. String[] sns = detlSn.split("_");
  264. int times = Integer.parseInt(sns[1]);
  265. int recNo = Integer.parseInt(sns[2]);
  266. int odrNo = Integer.parseInt(sns[3]);
  267. int itmNo = Integer.parseInt(sns[4]);
  268. mzDao.afterUploadFees(patNo, times, recNo, odrNo, itmNo, chrgLv, chrgType);
  269. }
  270. siPatInfo.setFulamtOwnpayAmt(fulamtOwnpayAmt);
  271. siPatInfo.setOverlmtAmt(overlmtAmt);
  272. siPatInfo.setPreselfpayAmt(preselfpayAmt);
  273. siPatInfo.setInscpScpAmt(inscpScpAmt);
  274. mzDao.updateSortOfAmt(siPatInfo);
  275. return ResultVoUtil.success(siPatInfo);
  276. }
  277. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  278. }
  279. public ResultVo<String> revokeOutpatientFeeDetails(MzPatientInfo p) {
  280. if (null == p.getTimes()) {
  281. p.setTimes(mzDao.selectMaxTimes(p.getPatNo()));
  282. }
  283. SiPatInfo siPatInfo = mzDao.selectSiPatInfoForMz(p.getPatNo(), p.getTimes());
  284. if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getMdtrtId())) {
  285. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "没有此患者的医保挂号信息!");
  286. }
  287. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_OUTPATIENT_FEE_DETAILS,
  288. siPatInfo.getInsuplcAdmdvs(), p.getStaffId());
  289. JSONObject data = new JSONObject();
  290. data.put("mdtrt_id", siPatInfo.getMdtrtId());
  291. data.put("psn_no", siPatInfo.getPsnNo());
  292. data.put("chrg_bchno", "0000");
  293. input.getJSONObject("input").put("data", data);
  294. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_OUTPATIENT_FEE_DETAILS);
  295. log.info("【操作员:{}】门诊费用明细信息撤销,参数:{},结果:{}", p.getStaffId(), input, result);
  296. if (null == result) {
  297. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  298. }
  299. Integer infcode = result.getInteger(RESULT_CODE);
  300. logDao.insert(new SiLog(input, result, p.getPatNo(), p.getTimes(), infcode, siPatInfo.getPsnNo()));
  301. if (null == infcode) {
  302. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  303. }
  304. if (infcode == 0) {
  305. mzDao.afterRevokeFees(p.getPatNo(), p.getTimes());
  306. return ResultVoUtil.success("门诊费用明细信息撤销成功。");
  307. }
  308. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  309. }
  310. public ResultVo<FundDetail> outpatientPreSettlement(MzPatientInfo p) {
  311. while (MzgjUtil.inProcessing(p.getPatNo())) {
  312. try {
  313. TimeUnit.SECONDS.sleep(1);
  314. } catch (InterruptedException e) {
  315. throw new RuntimeException(e);
  316. }
  317. }
  318. if (null == p.getTimes()) {
  319. p.setTimes(mzDao.selectMaxTimes(p.getPatNo()));
  320. }
  321. ResultVo<FundDetail> setlfund = getFundDetailByPatientInfo(p.getPatNo(), p.getTimes());
  322. if (setlfund != null) {
  323. return setlfund;
  324. }
  325. MzPreSetlmt mzPreSetlmt = mzDao.selectPreSetlmt(p.getPatNo(), p.getTimes());
  326. if (null == mzPreSetlmt || null == mzPreSetlmt.getMedfeeSumamt()) {
  327. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "患者没有已上传的费用。");
  328. }
  329. ReadCardBizType readCardBizType = ReadCardBizType.get(p.getReadCardBizType());
  330. if (readCardBizType == ReadCardBizType.SETTLEMENT) {
  331. MdtrtCertType mdtrtCertType = MdtrtCertType.getByLabel(p.getMdtrtCertType());
  332. mzPreSetlmt.setMdtrtCertType(mdtrtCertType.getCode());
  333. if (mdtrtCertType == MdtrtCertType.SOCIAL_SECURITY_CARD) {
  334. String[] out = p.getReadCardResult().split("\\|");
  335. mzPreSetlmt.setMdtrtCertNo(out[2]);
  336. mzPreSetlmt.setCardSn(out[3]);
  337. } else if (mdtrtCertType == MdtrtCertType.MEDICAL_INSURANCE_ELECTRONIC_VOUCHER) {
  338. JSONObject qrinfo = JSONObject.parseObject(p.getReadCardResult());
  339. if (!p.getSocialNo().trim().equals(qrinfo.getString("idNo").trim())) {
  340. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "电子医保凭证身份证与HIS身份证不一致,请确认是否人证相符。");
  341. }
  342. mzPreSetlmt.setMdtrtCertNo(qrinfo.getString("ecToken"));
  343. }
  344. }
  345. mzPreSetlmt.setMedfeeSumamt(mzPreSetlmt.getMedfeeSumamt().setScale(2, RoundingMode.HALF_UP));
  346. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.OUTPATIENT_PRE_SETTLEMENT,
  347. mzPreSetlmt.getInsuplcAdmdvs(), p.getStaffId());
  348. mzPreSetlmt.setPsnSetlway(PsnSetlWay.SETTLE_BY_ITEMS.getCode());
  349. mzPreSetlmt.setAcctUsedFlag(p.getAcctUsedFlag());
  350. mzPreSetlmt.setChrgBchno(p.getPatNo() + "_" + p.getTimes());
  351. mzPreSetlmt.setExpContent(p.getExpContent());
  352. String ref = JSONObject.toJSONString(mzPreSetlmt);
  353. input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
  354. JSONObject result = exec.executeTrade(input, SiFunction.OUTPATIENT_PRE_SETTLEMENT);
  355. log.info("【操作员:{}】门诊预结算:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
  356. if (null == result) {
  357. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  358. }
  359. Integer infcode = result.getInteger(RESULT_CODE);
  360. logDao.insert(new SiLog(input, result, p.getPatNo(), p.getTimes(), infcode, mzPreSetlmt.getPsnNo()));
  361. if (null == infcode) {
  362. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  363. }
  364. if (infcode == 0) {
  365. JSONObject setlinfo = result.getJSONObject(OUTPUT).getJSONObject("setlinfo");
  366. FundDetail fundDetail = new FundDetail();
  367. fundDetail.setTotalCost(setlinfo.getString("medfee_sumamt"));
  368. fundDetail.setFundPay(setlinfo.getString("fund_pay_sumamt"));
  369. fundDetail.setCashPay(setlinfo.getString("psn_cash_pay"));
  370. fundDetail.setAcctPay(setlinfo.getString("acct_pay"));
  371. fundDetail.setSelfPay(setlinfo.getString("psn_part_amt"));
  372. fundDetail.setCardType(getCardType(mzPreSetlmt.getInsuplcAdmdvs()));
  373. return ResultVoUtil.success(fundDetail);
  374. }
  375. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  376. }
  377. private ResultVo<FundDetail> getFundDetailByPatientInfo(String patientId, int times) {
  378. SiSetlinfo sisetlinfo = mzDao.selectSettledInfo(patientId, times, 0);
  379. if (null != sisetlinfo && null != sisetlinfo.getSetlId()) {
  380. return getFundDetailFromSetlinfo(sisetlinfo);
  381. }
  382. return null;
  383. }
  384. private ResultVo<FundDetail> getFundDetailFromSetlinfo(SiSetlinfo sisetl) {
  385. FundDetail fundDetail = new FundDetail();
  386. fundDetail.setCardType(getCardType(sisetl.getInsuplcAdmdvs()));
  387. fundDetail.setTotalCost(String.valueOf(sisetl.getMedfeeSumamt()));
  388. fundDetail.setFundPay(String.valueOf(sisetl.getFundPaySumamt()));
  389. fundDetail.setCashPay(String.valueOf(sisetl.getPsnCashPay()));
  390. fundDetail.setAcctPay(String.valueOf(sisetl.getAcctPay()));
  391. fundDetail.setSelfPay(String.valueOf(sisetl.getPsnPartAmt()));
  392. if (sisetl.getRevoked() == 1) {
  393. fundDetail.reverseAll();
  394. }
  395. return ResultVoUtil.success(fundDetail);
  396. }
  397. private Integer getCardType(String insplc) {
  398. if (insplc.startsWith("4301")) {
  399. return 1;
  400. } else if (insplc.equals("439900")) {
  401. return 2;
  402. }
  403. return 3;
  404. }
  405. public ResultVo<FundDetail> outpatientSettlement(MzPatientInfo p) {
  406. ResultVo<FundDetail> setlfund = getFundDetailByPatientInfo(p.getPatNo(), p.getTimes());
  407. if (setlfund != null) {
  408. return setlfund;
  409. }
  410. Setlmt setlmt = mzDao.selectSetlmt(p.getPatNo(), p.getTimes());
  411. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.OUTPATIENT_SETTLEMENT,
  412. setlmt.getInsuplcAdmdvs(), p.getStaffId());
  413. ReadCardBizType readCardBizType = ReadCardBizType.get(p.getReadCardBizType());
  414. if (readCardBizType == ReadCardBizType.SETTLEMENT) {
  415. MdtrtCertType mdtrtCertType = MdtrtCertType.getByLabel(p.getMdtrtCertType());
  416. setlmt.setMdtrtCertType(mdtrtCertType.getCode());
  417. if (mdtrtCertType == MdtrtCertType.SOCIAL_SECURITY_CARD) {
  418. String[] out = p.getReadCardResult().split("\\|");
  419. setlmt.setMdtrtCertNo(out[2]);
  420. setlmt.setCardSn(out[3]);
  421. } else if (mdtrtCertType == MdtrtCertType.MEDICAL_INSURANCE_ELECTRONIC_VOUCHER) {
  422. JSONObject qrinfo = JSONObject.parseObject(p.getReadCardResult());
  423. if (!p.getSocialNo().trim().equals(qrinfo.getString("idNo").trim())) {
  424. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "电子医保凭证身份证与HIS身份证不一致,请确认是否人证相符。");
  425. }
  426. setlmt.setMdtrtCertNo(qrinfo.getString("ecToken"));
  427. }
  428. }
  429. setlmt.setMedfeeSumamt(setlmt.getMedfeeSumamt().setScale(2, RoundingMode.HALF_UP));
  430. setlmt.setPsnSetlway(PsnSetlWay.SETTLE_BY_ITEMS.getCode());
  431. setlmt.setChrgBchno(p.getPatNo() + "_" + p.getTimes());
  432. setlmt.setAcctUsedFlag(p.getAcctUsedFlag());
  433. setlmt.setInvono(SnowFlakeId.getInstance().nextId());
  434. setlmt.setExpContent(p.getExpContent());
  435. String ref = JSONObject.toJSONString(setlmt);
  436. input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
  437. JSONObject result = exec.executeTrade(input, SiFunction.OUTPATIENT_SETTLEMENT);
  438. log.info("【操作员:{}】门诊结算:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
  439. if (null == result) {
  440. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR, "与医保中心的连接出现网络异常!");
  441. }
  442. Integer infcode = result.getInteger(RESULT_CODE);
  443. logDao.insert(new SiLog(input, result, p.getPatNo(), p.getTimes(), infcode, setlmt.getPsnNo()));
  444. if (null == infcode) {
  445. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  446. }
  447. if (infcode == 0) {
  448. JSONObject setlinfo = result.getJSONObject(OUTPUT).getJSONObject("setlinfo");
  449. SiSetlinfo setlEntity = JSONObject.parseObject(setlinfo.toJSONString(), SiSetlinfo.class);
  450. setlEntity.setPatNo(p.getPatNo());
  451. setlEntity.setTimes(p.getTimes());
  452. setlEntity.setLedgerSn(0);
  453. setlEntity.setStaffId(p.getStaffId());
  454. setlEntity.setRevoked(YesOrNo.NO.getCode());
  455. setlEntity.setSetlType(ClrType.OUTPATIENT.getCode());
  456. setlEntity.setInsuplcAdmdvs(setlmt.getInsuplcAdmdvs());
  457. setlEntity.setBegntime(mzDao.selectBegntime(p.getPatNo(), p.getTimes()));
  458. setlEntity.setEndtime(setlEntity.getSetlTime());
  459. setlEntity.setMzSaved(p.getSaved());
  460. BigDecimal hospitalPart = BigDecimal.ZERO;
  461. JSONArray setldetail = result.getJSONObject(OUTPUT).getJSONArray("setldetail");
  462. for (int i = 0; i < setldetail.size(); i++) {
  463. SiSetldetail setldetailEntity = JSONObject.parseObject(setldetail.getJSONObject(i).toJSONString(),
  464. SiSetldetail.class);
  465. setldetailEntity.setPatNo(p.getPatNo());
  466. setldetailEntity.setTimes(p.getTimes());
  467. setldetailEntity.setLedgerSn(0);
  468. if (setldetailEntity.getFundPayType().equals("999996") || setldetailEntity.getSetlProcInfo().equals("999996")) {
  469. hospitalPart = hospitalPart.add(BigDecimal.valueOf(setldetailEntity.getFundPayamt()));
  470. }
  471. setldetailDao.insert(setldetailEntity);
  472. }
  473. setlEntity.setHospPartAmt(hospitalPart.doubleValue());
  474. setlinfoDao.insert(setlEntity);
  475. setlinfoDao.updateSiZyInfoSetlId(p.getPatNo(), p.getTimes(), 0, setlEntity.getSetlId(),
  476. setlEntity.getMedinsSetlId(), input.getString("msgid"));
  477. webHisSrvc.saveCumInfo(webHisUrl, setlEntity);
  478. return getFundDetailFromSetlinfo(setlEntity);
  479. }
  480. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  481. }
  482. public ResultVo<FundDetail> revokeOutpatientSettlement(MzPatientInfo p) {
  483. if (null == p.getTimes()) {
  484. p.setTimes(mzDao.selectMaxTimes(p.getPatNo()));
  485. }
  486. SiSetlinfo setlinfo = mzDao.selectSettledInfo(p.getPatNo(), p.getTimes(), 0);
  487. if (null == setlinfo) {
  488. setlinfo = mzDao.selectSettledInfo(p.getPatNo(), p.getTimes(), 1);
  489. if (null == setlinfo) {
  490. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "没有此患者的已结算数据。");
  491. }
  492. return getFundDetailFromSetlinfo(setlinfo);
  493. }
  494. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_OUTPATIENT_SETTLEMENT,
  495. mzDao.selectAdmdvs(setlinfo.getPatNo(), setlinfo.getTimes(), setlinfo.getLedgerSn()), p.getStaffId());
  496. JSONObject data = new JSONObject();
  497. data.put("setl_id", setlinfo.getSetlId());
  498. data.put("mdtrt_id", setlinfo.getMdtrtId());
  499. data.put("psn_no", setlinfo.getPsnNo());
  500. input.getJSONObject("input").put("data", data);
  501. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_OUTPATIENT_SETTLEMENT);
  502. log.info("【操作员:{}】门诊结算撤销:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
  503. if (null == result) {
  504. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  505. }
  506. Integer infcode = result.getInteger(RESULT_CODE);
  507. logDao.insert(new SiLog(input, result, p.getPatNo(), p.getTimes(), infcode, setlinfo.getPsnNo()));
  508. if (null == infcode) {
  509. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  510. }
  511. if (infcode == 0) {
  512. afterRevokeSetl(p.getPatNo(), p.getTimes(), input.getString("msgid"));
  513. if (null != p.getNeedRevokeRegistration() && p.getNeedRevokeRegistration() == 1) {
  514. revokeOutpatientFeeDetails(p);
  515. revokeOutpatientRegistration(p);
  516. }
  517. return getFundDetailFromSetlinfo(setlinfo);
  518. }
  519. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  520. }
  521. public void revokeOutpatientSettlementForTask(SiSetlinfo setlinfo) {
  522. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_OUTPATIENT_SETTLEMENT,
  523. mzDao.selectAdmdvs(setlinfo.getPatNo(), setlinfo.getTimes(), setlinfo.getLedgerSn()), "99999");
  524. JSONObject data = new JSONObject();
  525. data.put("setl_id", setlinfo.getSetlId());
  526. data.put("mdtrt_id", setlinfo.getMdtrtId());
  527. data.put("psn_no", setlinfo.getPsnNo());
  528. input.getJSONObject("input").put("data", data);
  529. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_OUTPATIENT_SETTLEMENT);
  530. log.info("【操作员:99999】门诊结算撤销:\n参数:{},\n结果:{}", input, result);
  531. Integer infcode = result.getInteger(RESULT_CODE);
  532. logDao.insert(new SiLog(input, result, setlinfo.getPatNo(), setlinfo.getTimes(), infcode, setlinfo.getPsnNo()));
  533. if (null != infcode && infcode == 0) {
  534. afterRevokeSetl(setlinfo.getPatNo(), setlinfo.getTimes(), input.getString("msgid"));
  535. MzPatientInfo mzPatientInfo = new MzPatientInfo();
  536. mzPatientInfo.setPatNo(setlinfo.getPatNo());
  537. mzPatientInfo.setTimes(setlinfo.getTimes());
  538. mzPatientInfo.setStaffId("99999");
  539. revokeOutpatientFeeDetails(mzPatientInfo);
  540. revokeOutpatientRegistration(mzPatientInfo);
  541. }
  542. }
  543. public void afterRevokeSetl(String patNo, int times, String msgId) {
  544. try {
  545. mzDao.deleteSetlInfo(patNo, times);
  546. mzDao.deleteSetlDetail(patNo, times);
  547. mzDao.updateRvkSetlMsgid(patNo, times, msgId);
  548. } catch (Exception e) {
  549. String sql1 = String.format("update t_si_setlinfo set revoked=1,mz_saved=0 where pat_no='%s' and times=%d;\n", patNo, times);
  550. String sql2 = String.format("delete from t_si_setldetail where pat_no='%s' and times=%d;\n", patNo, times);
  551. String sql3 = String.format("update t_si_pat_info set rvk_setl_msgid='%s' where pat_no='%s' and times=%d;",
  552. msgId, patNo, times);
  553. String sql = sql1 + sql2 + sql3;
  554. mariadbService.insertNewRecord(sql);
  555. }
  556. }
  557. public ResultVo<FundDetail> directRegistration(DirectionRegParam param) {
  558. log.info("门诊共济:{}", param);
  559. if (MzgjUtil.inProcessing(param.getPatientId())) {
  560. log.info("【{}】此患者上一次的共济流程尚未结束,请稍后再试。", param.getPatientId());
  561. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者上一次的共济流程尚未结束,请稍后再试。");
  562. }
  563. ResultVo<FundDetail> setlfund = getFundDetailByPatientInfo(param.getPatientId(), param.getTimes());
  564. if (setlfund != null) {
  565. return setlfund;
  566. }
  567. MzgjUtil.addProcess(param.getPatientId());
  568. try {
  569. return executeMzgj(param);
  570. } catch (Exception e) {
  571. log.error("门诊共济出错:", e);
  572. return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, e.getMessage());
  573. } finally {
  574. MzgjUtil.finishProcessing(param.getPatientId());
  575. }
  576. }
  577. private ResultVo<FundDetail> executeMzgj(DirectionRegParam param) {
  578. if (mzDao.selectExistCount(param.getPatientId(), param.getTimes()) == 0) {
  579. int update = mzDao.updateMzTimes(param.getPatientId(), param.getTimes());
  580. if (update == 0) {
  581. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR,
  582. "不存在此患者第[" + param.getTimes() + "]次的就诊信息,请重新查询。");
  583. }
  584. }
  585. mzDao.deleteTempPatinfo(param.getPatientId(), param.getTimes());
  586. MzPatientInfo mzptnt = new MzPatientInfo();
  587. mzptnt.setStaffId(param.getStaffId());
  588. mzptnt.setPatNo(param.getPatientId());
  589. mzptnt.setTimes(param.getTimes());
  590. mzptnt.setMedType("11");
  591. mzptnt.setAcctUsedFlag(String.valueOf(param.getAcctUsedFlag()));
  592. SiPatInfo siPatInfo = mzDao.selectSiPatInfoForMz(mzptnt.getPatNo(), mzptnt.getTimes());
  593. mzptnt.setInsuplcAdmdvs(siPatInfo.getInsuplcAdmdvs());
  594. mzptnt.setInsutype(siPatInfo.getInsutype());
  595. mzptnt.setPsnType(siPatInfo.getPsnType());
  596. mzptnt.setName(siPatInfo.getPsnName());
  597. mzptnt.setSocialNo(siPatInfo.getCertno());
  598. if (StringUtil.notBlank(param.getReadCardResult())) {
  599. MdtrtCertType mdtrtCertType = MdtrtCertType.get(param.getReadCardType());
  600. mzptnt.setMdtrtCertType(mdtrtCertType.getLabel());
  601. mzptnt.setReadCardBizType(ReadCardBizType.REGISTRATION.getCode());
  602. mzptnt.setReadCardResult(param.getReadCardResult());
  603. }
  604. ResultVo<String> regres = outpatientRegistration(mzptnt);
  605. if (null == regres) {
  606. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  607. }
  608. if (regres.getCode() != ExceptionEnum.SUCCESS.getCode()) {
  609. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, regres.getMessage());
  610. }
  611. // 挂号登记成功后上传就诊信息
  612. siPatInfo = mzDao.selectSiPatInfoForMz(mzptnt.getPatNo(), mzptnt.getTimes());
  613. siPatInfo.setFromDirectReg(true);
  614. SpcChrDiseAcct spcChrDiseAcct = new SpcChrDiseAcct();
  615. spcChrDiseAcct.setStaffId(param.getStaffId());
  616. spcChrDiseAcct.setExpContent(param.getExpContent());
  617. ResultVo<String> upldMdtrtRes = uploadOutpatientInfo(spcChrDiseAcct, siPatInfo);
  618. if (null == upldMdtrtRes) {
  619. revokeOutpatientRegistration(mzptnt);
  620. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  621. }
  622. if (upldMdtrtRes.getCode() != ExceptionEnum.SUCCESS.getCode()) {
  623. revokeOutpatientRegistration(mzptnt);
  624. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, upldMdtrtRes.getMessage());
  625. }
  626. // 上传就诊信息成功后生成门诊医保费用
  627. ResultVo<List<Map<String, Object>>> getMzRcptRes = webHisSrvc.getMzReceipts(webHisUrl, mzptnt);
  628. if (null == getMzRcptRes) {
  629. revokeOutpatientRegistration(mzptnt);
  630. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  631. }
  632. if (getMzRcptRes.getCode() != ExceptionEnum.SUCCESS.getCode()) {
  633. revokeOutpatientRegistration(mzptnt);
  634. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, getMzRcptRes.getMessage());
  635. }
  636. List<MzReceipt> mzReceipts = new ArrayList<>();
  637. for (Map<String, Object> item : getMzRcptRes.getData()) {
  638. int times = (int) item.get("times");
  639. if (times != mzptnt.getTimes()) {
  640. continue;
  641. }
  642. mzDao.deleteAllReceipts(param.getPatientId(), times);
  643. Map<Integer, List<MzReceipt>> orderReceiptsMap = FilterUtil.cast(item.get("mzReceipts"));
  644. for (Map.Entry<Integer, List<MzReceipt>> entry : orderReceiptsMap.entrySet()) {
  645. mzReceipts.addAll(entry.getValue());
  646. }
  647. }
  648. if (mzReceipts.isEmpty()) {
  649. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "没有可以上传到医保的费用。");
  650. }
  651. ResultVo<String> insertSiMzFeeRes = webHisSrvc.insertSiMzFees(webHisUrl, mzReceipts);
  652. if (null == insertSiMzFeeRes) {
  653. revokeOutpatientRegistration(mzptnt);
  654. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  655. }
  656. if (insertSiMzFeeRes.getCode() != ExceptionEnum.SUCCESS.getCode()) {
  657. revokeOutpatientRegistration(mzptnt);
  658. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, insertSiMzFeeRes.getMessage());
  659. }
  660. // 生成费用成功后上传到医保
  661. spcChrDiseAcct.setStaffId(param.getStaffId());
  662. spcChrDiseAcct.setPatNo(mzptnt.getPatNo());
  663. spcChrDiseAcct.setTimes(mzptnt.getTimes());
  664. spcChrDiseAcct.setFromDirectReg(true);
  665. ResultVo<SiPatInfo> upldFeeRes = uploadOutpatientFeeDetails(spcChrDiseAcct);
  666. if (null == upldFeeRes) {
  667. revokeOutpatientRegistration(mzptnt);
  668. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  669. }
  670. if (upldFeeRes.getCode() != ExceptionEnum.SUCCESS.getCode()) {
  671. revokeOutpatientRegistration(mzptnt);
  672. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, upldFeeRes.getMessage());
  673. }
  674. // 上传完成之后结算并返回结算结果
  675. mzptnt.setReadCardBizType(ReadCardBizType.SETTLEMENT.getCode());
  676. return outpatientSettlement(mzptnt);
  677. }
  678. public ResultVo<Integer> isPatientDuringSiSettle(String patientId) {
  679. int result = MzgjUtil.inProcessing(patientId) ? 1 : 0;
  680. return ResultVoUtil.success(result);
  681. }
  682. }