SiMzFeeService.java 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. package thyyxxk.simzfeeoprnsystm.service;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.alibaba.fastjson2.JSON;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.web.client.RestTemplate;
  7. import thyyxxk.simzfeeoprnsystm.dao.*;
  8. import thyyxxk.simzfeeoprnsystm.dicts.*;
  9. import thyyxxk.simzfeeoprnsystm.external.WebHisService;
  10. import thyyxxk.simzfeeoprnsystm.pojo.*;
  11. import thyyxxk.simzfeeoprnsystm.pojo.ResultVo;
  12. import thyyxxk.simzfeeoprnsystm.pojo.SiPatInfo;
  13. import thyyxxk.simzfeeoprnsystm.utils.*;
  14. import lombok.extern.slf4j.Slf4j;
  15. import org.springframework.beans.factory.annotation.Autowired;
  16. import org.springframework.stereotype.Service;
  17. import java.math.BigDecimal;
  18. import java.math.RoundingMode;
  19. import java.util.*;
  20. import java.util.concurrent.TimeUnit;
  21. @Slf4j
  22. @Service
  23. public class SiMzFeeService {
  24. private final SiMzDao mzDao;
  25. private final SiSetlinfoDao setlinfoDao;
  26. private final SiSetldetailDao setldetailDao;
  27. private final ScheduledDao scheduledDao;
  28. private final ExecService exec;
  29. private final WebHisService webHisService;
  30. private final MariadbService mariadbService;
  31. private static final String RESULT_CODE = "infcode";
  32. private static final String ERROR_MESSAGE = "err_msg";
  33. private static final String OUTPUT = "output";
  34. private final SiLogDao logDao;
  35. @Value("${web-his-url}")
  36. private String webHisUrl;
  37. @Value("${thmz-url}")
  38. private String thmzUrl;
  39. @Autowired
  40. public SiMzFeeService(SiMzDao mzDao, SiSetlinfoDao setlinfoDao,
  41. SiSetldetailDao setldetailDao, ScheduledDao scheduledDao, ExecService exec,
  42. WebHisService webHisService, MariadbService mariadbService,
  43. SiLogDao logDao) {
  44. this.mzDao = mzDao;
  45. this.setlinfoDao = setlinfoDao;
  46. this.setldetailDao = setldetailDao;
  47. this.scheduledDao = scheduledDao;
  48. this.exec = exec;
  49. this.webHisService = webHisService;
  50. this.mariadbService = mariadbService;
  51. this.logDao = logDao;
  52. }
  53. public ResultVo<String> outpatientRegistration(MzPatientInfo p) {
  54. Regstrtn regstrtn;
  55. if (null != p.getVisitDate() && StringUtil.notBlank(p.getDeptCode())
  56. && StringUtil.notBlank(p.getDoctorCode())) {
  57. regstrtn = mzDao.selectRegstrtn2(p);
  58. if (null != regstrtn) {
  59. regstrtn.setBegntime(p.getVisitDate());
  60. regstrtn.setDeptCode(p.getDeptCode());
  61. }
  62. } else {
  63. regstrtn = mzDao.selectRegstrtn(p.getPatNo(), p.getTimes());
  64. }
  65. if (null == regstrtn) {
  66. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR,
  67. "mz_visit_table或t_si_pat_info 信息为空,请联系医生重开处方。");
  68. }
  69. if (StringUtil.notBlank(regstrtn.getMdtrtId())) {
  70. return ResultVoUtil.success("本次就诊已有医保登记[mdtrtId:" + regstrtn.getMdtrtId() + "],请勿重复办理。");
  71. }
  72. if (!p.abortBeforeUploadFees()) {
  73. List<String> chargeCodeList = mzDao.selectHisChargeCodes(p.getPatNo(), p.getTimes());
  74. if (chargeCodeList.isEmpty()) {
  75. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "未找到收费明细,请先生成医保处方。");
  76. }
  77. // 查询处方是否有折扣
  78. String discount = queryDiscount(p.getPatNo(), p.getTimes(), chargeCodeList);
  79. if (!discount.equals("OK")) {
  80. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, discount);
  81. }
  82. }
  83. if (null == p.getInsutype()) {
  84. SiPatInfo siPatInfo = mzDao.selectSiPatInfoForMz(p.getPatNo(), p.getTimes());
  85. p.setInsuplcAdmdvs(siPatInfo.getInsuplcAdmdvs());
  86. p.setInsutype(siPatInfo.getInsutype());
  87. p.setPsnType(siPatInfo.getPsnType());
  88. p.setName(siPatInfo.getPsnName());
  89. p.setSocialNo(siPatInfo.getCertno());
  90. }
  91. ReadCardBizType readCardBizType = ReadCardBizType.get(p.getReadCardBizType());
  92. if (readCardBizType != ReadCardBizType.REGISTRATION && !p.getInsuplcAdmdvs().startsWith("43")) {
  93. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "省外异地的患者请读社保卡或医保电子凭证登记!");
  94. }
  95. if (existSettledButNotPay(p.getPatNo(), p.getTimes())) {
  96. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR,
  97. "有HIS未缴费的医保结算,无法办理医保登记。请先完成门诊缴费再重新办理。");
  98. }
  99. if (StringUtil.notBlank(p.getMedType())) {
  100. mzDao.updateMedtype(p.getMedType(), p.getPatNo(), p.getTimes());
  101. }
  102. if (readCardBizType == ReadCardBizType.REGISTRATION) {
  103. MdtrtCertType mdtrtCertType = MdtrtCertType.getByLabel(p.getMdtrtCertType());
  104. regstrtn.setMdtrtCertType(mdtrtCertType.getCode());
  105. if (mdtrtCertType.getCode().equals(MdtrtCertType.SOCIAL_SECURITY_CARD.getCode())) {
  106. String[] out = p.getReadCardResult().split("\\|");
  107. regstrtn.setMdtrtCertNo(out[2]);
  108. regstrtn.setCardSn(out[3]);
  109. } else if (mdtrtCertType.getCode().equals(MdtrtCertType.ELECTRONIC_VOUCHER.getCode())) {
  110. JSONObject qrinfo = JSONObject.parseObject(p.getReadCardResult());
  111. JSONObject qrdata = qrinfo.getJSONObject("data");
  112. if (null != qrdata) {
  113. qrinfo = qrdata;
  114. }
  115. regstrtn.setMdtrtCertNo(qrinfo.getString("ecToken"));
  116. }
  117. }
  118. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.OUTPATIENT_REGISTRATION,
  119. p.getInsuplcAdmdvs(), p.getStaffId());
  120. regstrtn.setInsutype(p.getInsutype());
  121. regstrtn.setPsnType(p.getPsnType());
  122. regstrtn.setIptOtpNo(p.getPatNo());
  123. String ref = JSONObject.toJSONStringWithDateFormat(regstrtn, "yyyy-MM-dd HH:mm:ss");
  124. input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
  125. JSONObject result = exec.executeTrade(input, SiFunction.OUTPATIENT_REGISTRATION);
  126. log.info("【操作员:{}】,门诊挂号:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
  127. Integer infcode = result.getInteger(RESULT_CODE);
  128. logDao.insert(new SiLog(input, result, p.getPatNo(), p.getTimes(), infcode, regstrtn.getPsnNo()));
  129. if (infcode == 0) {
  130. setlinfoDao.deletePreSettleInfo(p.getPatNo(), p.getTimes());
  131. JSONObject output = result.getJSONObject(OUTPUT).getJSONObject("data");
  132. p.setMdtrtId(output.getString("mdtrt_id"));
  133. p.setVisitDate(regstrtn.getBegntime());
  134. mzDao.afterRegistrtn(p);
  135. return ResultVoUtil.success(p.getMdtrtId());
  136. }
  137. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  138. }
  139. public ResultVo<String> revokeOutpatientRegistration(MzPatientInfo p) {
  140. if (null == p.getTimes()) {
  141. p.setTimes(mzDao.selectMaxTimes(p.getPatNo()));
  142. }
  143. SiPatInfo siPatInfo = mzDao.selectSiPatInfoForMz(p.getPatNo(), p.getTimes());
  144. if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getMdtrtId())) {
  145. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保挂号信息。");
  146. }
  147. MzgjUtil.finishProcessing(p.getPatNo());
  148. RevokeRegRequest request = new RevokeRegRequest();
  149. request.setInsuplcAdmdvs(siPatInfo.getInsuplcAdmdvs()); ;
  150. request.setStaffId(p.getStaffId());
  151. request.setPsnNo(siPatInfo.getPsnNo());
  152. request.setMdtrtId(siPatInfo.getMdtrtId());
  153. request.setPatNo(p.getPatNo());
  154. request.setTimes(p.getTimes());
  155. return executeOutpatientRegRevoke(request);
  156. }
  157. public ResultVo<String> executeOutpatientRegRevoke(RevokeRegRequest request) {
  158. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_OUTPATIENT_REGISTRATION,
  159. request.getInsuplcAdmdvs(), request.getStaffId());
  160. JSONObject data = new JSONObject();
  161. data.put("psn_no", request.getPsnNo());
  162. data.put("mdtrt_id", request.getMdtrtId());
  163. data.put("ipt_otp_no", request.getPatNo());
  164. input.getJSONObject("input").put("data", data);
  165. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_OUTPATIENT_REGISTRATION);
  166. log.info("【操作员:{}】,取消门诊挂号:\n参数:{},\n结果:{}", request.getStaffId(), input, result);
  167. Integer infcode = result.getInteger(RESULT_CODE);
  168. logDao.insert(new SiLog(input, result, request.getPatNo(), request.getTimes(), infcode, request.getPsnNo()));
  169. if (infcode == 0) {
  170. mzDao.clearMdtrtIdForMz(request.getPatNo(), request.getTimes(), null);
  171. setlinfoDao.deletePreSettleInfo(request.getPatNo(), request.getTimes());
  172. return ResultVoUtil.success("取消门诊挂号成功。");
  173. }
  174. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  175. }
  176. public ResultVo<String> uploadOutpatientInfo(MzPatientInfo mzptnt, SpcChrDiseAcct spcChrDiseAcct, SiPatInfo siPatInfo) {
  177. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.UPLOAD_OUTPATIENT_INFO,
  178. siPatInfo.getInsuplcAdmdvs(), spcChrDiseAcct.getStaffId());
  179. JSONObject mdtrtinfo = new JSONObject();
  180. List<Diagnoses> diagnosesList = new ArrayList<>();
  181. String icdCodeNew = mzDao.selectIcdCodeNew(siPatInfo.getPatNo(), siPatInfo.getTimes());
  182. if (StringUtil.isBlank(icdCodeNew) && null != mzptnt) {
  183. icdCodeNew = mzptnt.getIcdCodeNew();
  184. }
  185. if (StringUtil.notBlank(icdCodeNew)) {
  186. Diagnoses base = mzDao.selectDiseinfo(siPatInfo.getPatNo(), siPatInfo.getTimes());
  187. if (null == base && null != mzptnt) {
  188. CodeName dor = mzDao.getDorCodeAndName(mzptnt.getDoctorCode());
  189. base = new Diagnoses();
  190. base.setDiagDept(mzptnt.getDeptCode());
  191. base.setDiagTime(mzptnt.getVisitDate());
  192. base.setDiseDorNo(dor.getCode());
  193. base.setDiseDorName(dor.getName());
  194. }
  195. String[] icds = icdCodeNew.split(",");
  196. for (int i = 0; i < icds.length; i++) {
  197. String icd = icds[i];
  198. String icdTextNew = mzDao.selectIcdTextNew(icd);
  199. Diagnoses diag = new Diagnoses();
  200. diag.setDiagCode(icd);
  201. diag.setDiagName(icdTextNew);
  202. diag.setDiagSrtNo(i + 1);
  203. diag.setDiagType("1");
  204. diag.setValiFlag("1");
  205. diag.setDiagDept(base.getDiagDept());
  206. diag.setDiagTime(base.getDiagTime());
  207. diag.setDiseDorNo(base.getDiseDorNo());
  208. diag.setDiseDorName(base.getDiseDorName());
  209. diagnosesList.add(diag);
  210. }
  211. } else {
  212. diagnosesList = mzDao.selectMzDiags(siPatInfo.getPatNo(), siPatInfo.getTimes());
  213. if (null == diagnosesList || diagnosesList.isEmpty()) {
  214. log.info("【{}】 上传就诊信息失败,门诊诊断为空。", siPatInfo.getPatNo());
  215. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "患者的门诊诊断为空,请联系医生填写。");
  216. }
  217. }
  218. if (siPatInfo.getMedType().equals("14") || siPatInfo.getMedType().equals("51")) {
  219. if (StringUtil.isBlank(spcChrDiseAcct.getOpspDiseCode())) {
  220. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "生育门诊与慢特病门诊的病种不能为空 !");
  221. }
  222. }
  223. mdtrtinfo.put("mdtrt_id", siPatInfo.getMdtrtId());
  224. mdtrtinfo.put("psn_no", siPatInfo.getPsnNo());
  225. mdtrtinfo.put("med_type", siPatInfo.getMedType());
  226. mdtrtinfo.put("begntime", DateUtil.formatDatetime(siPatInfo.getVisitDatetime()));
  227. mdtrtinfo.put("main_cond_dscr", spcChrDiseAcct.getOpspDiseName());
  228. mdtrtinfo.put("dise_codg", spcChrDiseAcct.getOpspDiseCode());
  229. mdtrtinfo.put("dise_name", spcChrDiseAcct.getOpspDiseName());
  230. mdtrtinfo.put("birctrl_type", "");
  231. mdtrtinfo.put("birctrl_matn_date", "");
  232. mdtrtinfo.put("exp_content", spcChrDiseAcct.getExpContent());
  233. String ref = JSONObject.toJSONString(diagnosesList);
  234. input.getJSONObject("input").put("mdtrtinfo", mdtrtinfo);
  235. input.getJSONObject("input").put("diseinfo", JSONArray.parse(ref));
  236. JSONObject result = exec.executeTrade(input, SiFunction.UPLOAD_OUTPATIENT_INFO);
  237. log.info("【操作员:{}】门诊就诊信息上传:\n参数:{},\n结果:{}", spcChrDiseAcct.getStaffId(), input, result);
  238. Integer infcode = result.getInteger(RESULT_CODE);
  239. logDao.insert(new SiLog(input, result, siPatInfo.getPatNo(), siPatInfo.getTimes(), infcode, siPatInfo.getPsnNo()));
  240. if (result.getIntValue(RESULT_CODE) == 0) {
  241. Diagnoses main = diagnosesList.get(0);
  242. mzDao.updatePatDiseinfo(siPatInfo.getPatNo(), siPatInfo.getTimes(), main.getDiagCode(), main.getDiagName());
  243. return ResultVoUtil.success("门诊就诊信息上传成功。");
  244. }
  245. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  246. }
  247. public ResultVo<SiPatInfo> uploadOutpatientFeeDetails(SpcChrDiseAcct p) {
  248. String patNo = p.getPatNo();
  249. if (null == p.getTimes()) {
  250. p.setTimes(mzDao.selectMaxTimes(patNo));
  251. }
  252. SiPatInfo siPatInfo = mzDao.selectSiPatInfoForMz(patNo, p.getTimes());
  253. if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getMdtrtId())) {
  254. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "【门诊号:" + patNo +
  255. "】未找到此患者的医保挂号信息,请核实。");
  256. }
  257. if (!p.getFromDirectReg()) {
  258. ResultVo<String> uplRes = uploadOutpatientInfo(null, p, siPatInfo);
  259. if (null != uplRes && uplRes.getCode() != ExceptionEnum.SUCCESS.getCode()) {
  260. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, uplRes.getMessage());
  261. }
  262. }
  263. List<FeeDetail> feeDetails = mzDao.selectOutpatientFees(patNo, p.getTimes());
  264. if (null == feeDetails || feeDetails.isEmpty()) {
  265. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有需要上传的费用。");
  266. }
  267. MzVisit mzVisit = mzDao.selectMzVisitInfo(patNo, p.getTimes());
  268. if (null == mzVisit || StringUtil.isBlank(mzVisit.getVisitDeptCode())) {
  269. log.info("【{},{}】就诊科室没有匹配医保码,无法使用门诊统筹。", patNo, p.getTimes());
  270. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "就诊科室没有匹配医保码,无法使用门诊统筹。");
  271. }
  272. String batchNo = patNo + "_" + p.getTimes();
  273. feeDetails.forEach(item -> {
  274. item.setMdtrtId(siPatInfo.getMdtrtId());
  275. item.setPsnNo(siPatInfo.getPsnNo());
  276. item.setChrgBchno(batchNo);
  277. item.setRxCircFlag(YesOrNo.NO.getCodeStr());
  278. item.setBilgDeptCodg(mzVisit.getVisitDeptCode());
  279. item.setBilgDeptName(mzVisit.getVisitDeptName());
  280. if (siPatInfo.getMedType().equals("51")) {
  281. item.setMatnFeeFlag("1");
  282. }
  283. });
  284. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.UPLOAD_OUTPATIENT_FEE_DETAILS,
  285. siPatInfo.getInsuplcAdmdvs(), p.getStaffId());
  286. String ref = JSONArray.toJSONStringWithDateFormat(feeDetails, "yyyy-MM-dd HH:mm:ss");
  287. JSONArray feedetail = JSONArray.parseArray(ref);
  288. input.getJSONObject("input").put("feedetail", feedetail);
  289. JSONObject result = exec.executeTrade(input, SiFunction.UPLOAD_OUTPATIENT_FEE_DETAILS);
  290. log.info("【操作员:{}】门诊费用明细信息上传:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
  291. Integer infcode = result.getInteger(RESULT_CODE);
  292. logDao.insert(new SiLog(input, result, p.getPatNo(), p.getTimes(), infcode, siPatInfo.getPsnNo()));
  293. if (infcode == 0) {
  294. JSONArray feeRes = result.getJSONObject(OUTPUT).getJSONArray("result");
  295. double fulamtOwnpayAmt = 0d;
  296. double overlmtAmt = 0d;
  297. double preselfpayAmt = 0d;
  298. double inscpScpAmt = 0d;
  299. for (int i = 0; i < feeRes.size(); i++) {
  300. JSONObject fee = feeRes.getJSONObject(i);
  301. fulamtOwnpayAmt += fee.getDoubleValue("fulamt_ownpay_amt");
  302. overlmtAmt += fee.getDoubleValue("overlmt_amt");
  303. preselfpayAmt += fee.getDoubleValue("preselfpay_amt");
  304. inscpScpAmt += fee.getDoubleValue("inscp_scp_amt");
  305. String detlSn = fee.getString("feedetl_sn");
  306. String chrgLv = fee.getString("chrgitm_lv");
  307. String chrgType = fee.getString("med_chrgitm_type");
  308. String[] sns = detlSn.split("_");
  309. int times = Integer.parseInt(sns[1]);
  310. int recNo = Integer.parseInt(sns[2]);
  311. int odrNo = Integer.parseInt(sns[3]);
  312. int itmNo = Integer.parseInt(sns[4]);
  313. mzDao.afterUploadFees(patNo, times, recNo, odrNo, itmNo, chrgLv, chrgType);
  314. }
  315. siPatInfo.setFulamtOwnpayAmt(fulamtOwnpayAmt);
  316. siPatInfo.setOverlmtAmt(overlmtAmt);
  317. siPatInfo.setPreselfpayAmt(preselfpayAmt);
  318. siPatInfo.setInscpScpAmt(inscpScpAmt);
  319. mzDao.updateSortOfAmt(siPatInfo);
  320. setlinfoDao.deletePreSettleInfo(p.getPatNo(), p.getTimes());
  321. return ResultVoUtil.success(siPatInfo);
  322. }
  323. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  324. }
  325. public ResultVo<String> revokeOutpatientFeeDetails(MzPatientInfo p) {
  326. if (null == p.getTimes()) {
  327. p.setTimes(mzDao.selectMaxTimes(p.getPatNo()));
  328. }
  329. SiPatInfo siPatInfo = mzDao.selectSiPatInfoForMz(p.getPatNo(), p.getTimes());
  330. if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getMdtrtId())) {
  331. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "没有此患者的医保挂号信息!");
  332. }
  333. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_OUTPATIENT_FEE_DETAILS,
  334. siPatInfo.getInsuplcAdmdvs(), p.getStaffId());
  335. JSONObject data = new JSONObject();
  336. data.put("mdtrt_id", siPatInfo.getMdtrtId());
  337. data.put("psn_no", siPatInfo.getPsnNo());
  338. data.put("chrg_bchno", "0000");
  339. input.getJSONObject("input").put("data", data);
  340. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_OUTPATIENT_FEE_DETAILS);
  341. log.info("【操作员:{}】门诊费用明细信息撤销,参数:{},结果:{}", p.getStaffId(), input, result);
  342. Integer infcode = result.getInteger(RESULT_CODE);
  343. logDao.insert(new SiLog(input, result, p.getPatNo(), p.getTimes(), infcode, siPatInfo.getPsnNo()));
  344. if (infcode == 0) {
  345. mzDao.afterRevokeFees(p.getPatNo(), p.getTimes());
  346. setlinfoDao.deletePreSettleInfo(p.getPatNo(), p.getTimes());
  347. return ResultVoUtil.success("门诊费用明细信息撤销成功。");
  348. }
  349. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  350. }
  351. public ResultVo<FundDetail> outpatientPreSettlement(MzPatientInfo p) {
  352. if (null == p.getTimes()) {
  353. p.setTimes(mzDao.selectMaxTimes(p.getPatNo()));
  354. }
  355. ResultVo<FundDetail> setlfund = getFundDetailByPatientInfo(p.getPatNo(), p.getTimes(), false);
  356. if (setlfund != null) {
  357. return setlfund;
  358. }
  359. MzPreSetlmt mzPreSetlmt = mzDao.selectPreSetlmt(p.getPatNo(), p.getTimes());
  360. if (null == mzPreSetlmt || null == mzPreSetlmt.getMedfeeSumamt()) {
  361. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "患者没有已上传的费用。");
  362. }
  363. ReadCardBizType readCardBizType = ReadCardBizType.get(p.getReadCardBizType());
  364. if (readCardBizType == ReadCardBizType.SETTLEMENT) {
  365. MdtrtCertType mdtrtCertType = MdtrtCertType.getByLabel(p.getMdtrtCertType());
  366. mzPreSetlmt.setMdtrtCertType(mdtrtCertType.getCode());
  367. if (mdtrtCertType.getCode().equals(MdtrtCertType.SOCIAL_SECURITY_CARD.getCode())) {
  368. String[] out = p.getReadCardResult().split("\\|");
  369. mzPreSetlmt.setMdtrtCertNo(out[2]);
  370. mzPreSetlmt.setCardSn(out[3]);
  371. } else if (mdtrtCertType.getCode().equals(MdtrtCertType.ELECTRONIC_VOUCHER.getCode())) {
  372. String socialNo = mzDao.selectSocialNo(p.getPatNo());
  373. JSONObject qrinfo = JSONObject.parseObject(p.getReadCardResult());
  374. JSONObject qrdata = qrinfo.getJSONObject("data");
  375. if (null != qrdata) {
  376. qrinfo = qrdata;
  377. }
  378. if (!Objects.equals(socialNo, qrinfo.getString("idNo").trim())) {
  379. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "电子医保凭证身份证与HIS身份证不一致,请确认是否人证相符。");
  380. }
  381. mzPreSetlmt.setMdtrtCertNo(qrinfo.getString("ecToken"));
  382. }
  383. }
  384. mzPreSetlmt.setMedfeeSumamt(mzPreSetlmt.getMedfeeSumamt().setScale(2, RoundingMode.HALF_UP));
  385. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.OUTPATIENT_PRE_SETTLEMENT,
  386. mzPreSetlmt.getInsuplcAdmdvs(), p.getStaffId());
  387. mzPreSetlmt.setPsnSetlway(PsnSetlWay.SETTLE_BY_ITEMS.getCode());
  388. mzPreSetlmt.setAcctUsedFlag(p.getAcctUsedFlag());
  389. mzPreSetlmt.setChrgBchno(p.getPatNo() + "_" + p.getTimes());
  390. mzPreSetlmt.setExpContent(p.getExpContent());
  391. String ref = JSONObject.toJSONString(mzPreSetlmt);
  392. input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
  393. JSONObject result = exec.executeTrade(input, SiFunction.OUTPATIENT_PRE_SETTLEMENT);
  394. log.info("【操作员:{}】门诊预结算:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
  395. Integer infcode = result.getInteger(RESULT_CODE);
  396. logDao.insert(new SiLog(input, result, p.getPatNo(), p.getTimes(), infcode, mzPreSetlmt.getPsnNo()));
  397. if (infcode == 0) {
  398. setlinfoDao.deletePreSettleInfo(p.getPatNo(), p.getTimes());
  399. JSONObject setlinfo = result.getJSONObject(OUTPUT).getJSONObject("setlinfo");
  400. FundDetail fundDetail = new FundDetail();
  401. fundDetail.setTotalCost(setlinfo.getString("medfee_sumamt"));
  402. fundDetail.setFundPay(setlinfo.getString("fund_pay_sumamt"));
  403. fundDetail.setCashPay(setlinfo.getString("psn_cash_pay"));
  404. fundDetail.setAcctPay(setlinfo.getString("acct_pay"));
  405. fundDetail.setSelfPay(setlinfo.getString("psn_part_amt"));
  406. fundDetail.setCardType(getCardType(mzPreSetlmt.getInsuplcAdmdvs()));
  407. SiSetlinfo setlEntity = JSONObject.parseObject(setlinfo.toJSONString(), SiSetlinfo.class);
  408. setlEntity.setPatNo(p.getPatNo());
  409. setlEntity.setTimes(p.getTimes());
  410. setlEntity.setLedgerSn(0);
  411. setlEntity.setStaffId(p.getStaffId());
  412. setlEntity.setSetlType(ClrType.OUTPATIENT.getCode());
  413. setlEntity.setInsuplcAdmdvs(mzPreSetlmt.getInsuplcAdmdvs());
  414. log.info("开始插入预结算临时信息:{}_{}", p.getPatNo(), p.getTimes());
  415. setlinfoDao.insertPreSettleInfo(setlEntity);
  416. log.info("插入预结算临时信息成功:{}_{}", p.getPatNo(), p.getTimes());
  417. return ResultVoUtil.success(fundDetail);
  418. }
  419. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  420. }
  421. private ResultVo<FundDetail> getFundDetailByPatientInfo(String patNo, int times, boolean realSettle) {
  422. SiSetlinfo sisetlinfo = mzDao.selectSettledInfo(patNo, times, 0);
  423. if (!realSettle && null == sisetlinfo) {
  424. sisetlinfo = setlinfoDao.selectPresettleInfo(patNo, times);
  425. }
  426. if (null != sisetlinfo) {
  427. return getFundDetailFromSetlinfo(sisetlinfo);
  428. }
  429. return null;
  430. }
  431. private ResultVo<FundDetail> getFundDetailFromSetlinfo(SiSetlinfo setlinfo) {
  432. FundDetail fundDetail = new FundDetail();
  433. fundDetail.setCardType(getCardType(setlinfo.getInsuplcAdmdvs()));
  434. fundDetail.setTotalCost(String.valueOf(setlinfo.getMedfeeSumamt()));
  435. fundDetail.setFundPay(String.valueOf(setlinfo.getFundPaySumamt()));
  436. fundDetail.setAcctPay(String.valueOf(setlinfo.getAcctPay()));
  437. fundDetail.setCashPay(String.valueOf(setlinfo.getPsnCashPay()));
  438. fundDetail.setSelfPay(String.valueOf(setlinfo.getPsnCashPay()));
  439. if (setlinfo.getRevoked() == 1) {
  440. fundDetail.reverseAll();
  441. }
  442. fundDetail.setSetlinfo(setlinfo);
  443. return ResultVoUtil.success(fundDetail);
  444. }
  445. private Integer getCardType(String insplc) {
  446. if (insplc.startsWith("4301")) {
  447. return 1;
  448. } else if (insplc.equals("439900")) {
  449. return 2;
  450. }
  451. return 3;
  452. }
  453. public ResultVo<FundDetail> outpatientSettlement(MzPatientInfo p) {
  454. ResultVo<FundDetail> setlfund = getFundDetailByPatientInfo(p.getPatNo(), p.getTimes(), true);
  455. if (setlfund != null) {
  456. return setlfund;
  457. }
  458. Setlmt setlmt = mzDao.selectSetlmt(p.getPatNo(), p.getTimes());
  459. if (null == setlmt) {
  460. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "没有此患者的医保就诊信息。");
  461. }
  462. p.setInsuplcAdmdvs(setlmt.getInsuplcAdmdvs());
  463. p.setPsnNo(setlmt.getPsnNo());
  464. p.setMdtrtId(setlmt.getMdtrtId());
  465. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.OUTPATIENT_SETTLEMENT,
  466. setlmt.getInsuplcAdmdvs(), p.getStaffId());
  467. ReadCardBizType readCardBizType = ReadCardBizType.get(p.getReadCardBizType());
  468. if (readCardBizType == ReadCardBizType.SETTLEMENT) {
  469. MdtrtCertType mdtrtCertType = MdtrtCertType.getByLabel(p.getMdtrtCertType());
  470. setlmt.setMdtrtCertType(mdtrtCertType.getCode());
  471. if (mdtrtCertType.getCode().equals(MdtrtCertType.SOCIAL_SECURITY_CARD.getCode())) {
  472. String[] out = p.getReadCardResult().split("\\|");
  473. setlmt.setMdtrtCertNo(out[2]);
  474. setlmt.setCardSn(out[3]);
  475. } else if (mdtrtCertType.getCode().equals(MdtrtCertType.ELECTRONIC_VOUCHER.getCode())) {
  476. String socialNo = mzDao.selectSocialNo(p.getPatNo());
  477. JSONObject qrinfo = JSONObject.parseObject(p.getReadCardResult());
  478. if (!Objects.equals(socialNo, qrinfo.getString("idNo").trim())) {
  479. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "电子医保凭证身份证与HIS身份证不一致,请确认是否人证相符。");
  480. }
  481. setlmt.setMdtrtCertNo(qrinfo.getString("ecToken"));
  482. }
  483. }
  484. setlmt.setMedfeeSumamt(setlmt.getMedfeeSumamt().setScale(2, RoundingMode.HALF_UP));
  485. setlmt.setPsnSetlway(PsnSetlWay.SETTLE_BY_ITEMS.getCode());
  486. setlmt.setChrgBchno(p.getPatNo() + "_" + p.getTimes());
  487. Integer acctUsedFlag = p.getRealAcctUsedFlag();
  488. if (null == acctUsedFlag) {
  489. Double acctPay = mzDao.selectAcctPayInPreSettle(setlmt.getMdtrtId());
  490. if (null != acctPay && acctPay > 0) {
  491. acctUsedFlag = 1;
  492. } else {
  493. acctUsedFlag = 0;
  494. }
  495. }
  496. setlmt.setAcctUsedFlag(String.valueOf(acctUsedFlag));
  497. setlmt.setInvono(SnowFlakeId.getInstance().nextId());
  498. setlmt.setExpContent(p.getExpContent());
  499. String ref = JSONObject.toJSONString(setlmt);
  500. input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
  501. JSONObject result = exec.executeTrade(input, SiFunction.OUTPATIENT_SETTLEMENT);
  502. log.info("【操作员:{}】门诊结算:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
  503. Integer infcode = result.getInteger(RESULT_CODE);
  504. logDao.insert(new SiLog(input, result, p.getPatNo(), p.getTimes(), infcode, setlmt.getPsnNo()));
  505. if (infcode == 0) {
  506. JSONObject setlinfo = result.getJSONObject(OUTPUT).getJSONObject("setlinfo");
  507. SiSetlinfo setlEntity = JSONObject.parseObject(setlinfo.toJSONString(), SiSetlinfo.class);
  508. List<SiSetldetail> fundList = new ArrayList<>();
  509. JSONArray setldetail = result.getJSONObject(OUTPUT).getJSONArray("setldetail");
  510. for (int i = 0; i < setldetail.size(); i++) {
  511. SiSetldetail setldetailEntity = JSONObject.parseObject(setldetail.getJSONObject(i).toJSONString(),
  512. SiSetldetail.class);
  513. fundList.add(setldetailEntity);
  514. }
  515. setlEntity.setSetldetail(fundList);
  516. return dealSetlEntity(p, setlEntity);
  517. }
  518. String errMsg = result.getString(ERROR_MESSAGE);
  519. if (errMsg.contains("服务提供者后端服务响应超时")) {
  520. return timeoutSetl(p);
  521. }
  522. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, errMsg);
  523. }
  524. public ResultVo<FundDetail> timeoutSetl(MzPatientInfo p) {
  525. if (StringUtil.isBlank(p.getMdtrtId())) {
  526. p.setMdtrtId(mzDao.getMdtrtId(p.getPatNo(), p.getTimes()));
  527. }
  528. if (StringUtil.isBlank(p.getMdtrtId())) {
  529. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "没有找到患者的医保就诊信息!");
  530. }
  531. ResultVo<FundDetail> setlfund = getFundDetailByPatientInfo(p.getPatNo(), p.getTimes(), true);
  532. if (setlfund != null) {
  533. return setlfund;
  534. }
  535. SiSetlinfo setlEntity = querySettlementInfo(p);
  536. if (null != setlEntity) {
  537. return dealSetlEntity(p, setlEntity);
  538. }
  539. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "超时结算失败。");
  540. }
  541. private ResultVo<FundDetail> dealSetlEntity(MzPatientInfo p, SiSetlinfo setlEntity) {
  542. saveSetlinfoToDatabase(setlEntity, p);
  543. if (!p.getStraitSettle() && BigDecimal.ZERO.compareTo(setlEntity.getPsnCashPay()) == 0) {
  544. ResultVo<String> mzChargeResponse = mzHisChargeProcess(p.getPatNo(), p.getTimes(),
  545. String.valueOf(setlEntity.getMedfeeSumamt()));
  546. if (mzChargeResponse.getCode() != ExceptionEnum.SUCCESS.getCode()) {
  547. revokeOutpatientSettlementForTask(setlEntity);
  548. return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, mzChargeResponse.getMessage());
  549. }
  550. }
  551. setlinfoDao.deletePreSettleInfo(p.getPatNo(), p.getTimes());
  552. return getFundDetailFromSetlinfo(setlEntity);
  553. }
  554. private SiSetlinfo querySettlementInfo(MzPatientInfo p) {
  555. String url = "http://172.16.32.167:8077/siQuery/querySettlementInfo";
  556. JSONObject params = new JSONObject();
  557. params.put("psnNo", p.getPsnNo());
  558. params.put("mdtrtId", p.getMdtrtId());
  559. ResultVo<LinkedHashMap> vo = new RestTemplate().postForObject(url, params, ResultVo.class);
  560. if (null == vo || vo.getCode() != ExceptionEnum.SUCCESS.getCode()) {
  561. return null;
  562. }
  563. LinkedHashMap resMap = vo.getData();
  564. SiSetlinfo entity = JSONObject.parseObject(JSON.toJSONString(resMap), SiSetlinfo.class);
  565. entity.setInsuplcAdmdvs(resMap.get("insuOptins").toString());
  566. if (null == entity.getPsnPartAmt()) {
  567. entity.setPsnPartAmt(new BigDecimal(resMap.get("psnPay").toString()));
  568. }
  569. if (null == entity.getPsnCashPay()) {
  570. entity.setPsnCashPay(new BigDecimal(resMap.get("cashPayamt").toString()));
  571. }
  572. return entity;
  573. }
  574. private void saveSetlinfoToDatabase(SiSetlinfo setlEntity, MzPatientInfo p) {
  575. setlEntity.setPatNo(p.getPatNo());
  576. setlEntity.setTimes(p.getTimes());
  577. setlEntity.setLedgerSn(0);
  578. setlEntity.setStaffId(p.getStaffId());
  579. setlEntity.setRevoked(YesOrNo.NO.getCode());
  580. setlEntity.setInsuplcAdmdvsName(mzDao.getInsuplcAdmdvsName(setlEntity.getMdtrtId()));
  581. setlEntity.setSetlType(ClrType.OUTPATIENT.getCode());
  582. if (StringUtil.isBlank(setlEntity.getInsuplcAdmdvs())) {
  583. setlEntity.setInsuplcAdmdvs(p.getInsuplcAdmdvs());
  584. }
  585. setlEntity.setBegntime(mzDao.selectBegntime(p.getPatNo(), p.getTimes()));
  586. setlEntity.setEndtime(setlEntity.getSetlTime());
  587. setlEntity.setMzSaved(p.getSaved());
  588. BigDecimal hospitalPart = BigDecimal.ZERO;
  589. List<SiSetldetail> fundList = setlEntity.getSetldetail();
  590. fundList = null == fundList ? new ArrayList<>() : fundList;
  591. for (int i = 0; i < fundList.size(); i++) {
  592. SiSetldetail detail = fundList.get(i);
  593. detail.setPatNo(p.getPatNo());
  594. detail.setTimes(p.getTimes());
  595. detail.setSetlId(setlEntity.getSetlId());
  596. detail.setSortNo(i + 1);
  597. detail.setLedgerSn(0);
  598. if (detail.getFundPayType().equals("999996") || detail.getSetlProcInfo().equals("999996")) {
  599. hospitalPart = hospitalPart.add(BigDecimal.valueOf(detail.getFundPayamt()));
  600. }
  601. setldetailDao.insert(detail);
  602. }
  603. setlEntity.setHospPartAmt(hospitalPart);
  604. setlinfoDao.insert(setlEntity);
  605. setlinfoDao.updateSiZyInfoSetlId(p.getPatNo(), p.getTimes(), 0, setlEntity.getSetlId(),
  606. setlEntity.getMedinsSetlId(), setlEntity.getMedinsSetlId());
  607. webHisService.saveCumInfo(webHisUrl, setlEntity);
  608. }
  609. public ResultVo<FundDetail> revokeOutpatientSettlement(MzPatientInfo p) {
  610. log.info("请求撤销门诊医保结算:{}", JSONObject.toJSON(p));
  611. if (null == p.getTimes()) {
  612. p.setTimes(mzDao.selectMaxTimes(p.getPatNo()));
  613. }
  614. SiSetlinfo setlinfo = mzDao.selectSettledInfo(p.getPatNo(), p.getTimes(), 0);
  615. if (null == setlinfo) {
  616. if (p.needRevokeRegistration()) {
  617. deletePresettleInfoAndRevokeYb(p);
  618. }
  619. setlinfo = mzDao.selectSettledInfo(p.getPatNo(), p.getTimes(), 1);
  620. if (null == setlinfo) {
  621. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "没有此患者的已结算数据。");
  622. }
  623. return getFundDetailFromSetlinfo(setlinfo);
  624. }
  625. String staffDept = mzDao.selectStaffDepartment(p.getStaffId());
  626. if (!Objects.equals(staffDept, "3020100") &&
  627. !Objects.equals(staffDept, "3060000") &&
  628. !Objects.equals(staffDept, "3100000")) {
  629. if (!Objects.equals(p.getStaffId(), setlinfo.getStaffId())) {
  630. return ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, "您没有权限撤销本条医保业务,请到收费窗口退处方,或者联系医保科处理。");
  631. }
  632. }
  633. if (BigDecimal.ZERO.compareTo(setlinfo.getPsnCashPay()) == 0
  634. && !Objects.equals(staffDept, "3060000")) {
  635. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "本次结算【个人现金支付为0】,请到收费窗口做退费操作。");
  636. }
  637. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_OUTPATIENT_SETTLEMENT,
  638. mzDao.selectAdmdvs(setlinfo.getPatNo(), setlinfo.getTimes(), setlinfo.getLedgerSn()), p.getStaffId());
  639. JSONObject data = new JSONObject();
  640. data.put("setl_id", setlinfo.getSetlId());
  641. data.put("mdtrt_id", setlinfo.getMdtrtId());
  642. data.put("psn_no", setlinfo.getPsnNo());
  643. input.getJSONObject("input").put("data", data);
  644. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_OUTPATIENT_SETTLEMENT);
  645. log.info("【操作员:{}】门诊结算撤销:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
  646. Integer infcode = result.getInteger(RESULT_CODE);
  647. logDao.insert(new SiLog(input, result, p.getPatNo(), p.getTimes(), infcode, setlinfo.getPsnNo()));
  648. if (p.needRevokeRegistration()) {
  649. deletePresettleInfoAndRevokeYb(p);
  650. }
  651. if (infcode == 0) {
  652. afterRevokeSetl(p.getPatNo(), p.getTimes(), input.getString("msgid"));
  653. return getFundDetailFromSetlinfo(setlinfo);
  654. }
  655. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  656. }
  657. public void revokeOutpatientSettlementForTask(SiSetlinfo setlinfo) {
  658. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_OUTPATIENT_SETTLEMENT,
  659. mzDao.selectAdmdvs(setlinfo.getPatNo(), setlinfo.getTimes(), setlinfo.getLedgerSn()), "99999");
  660. JSONObject data = new JSONObject();
  661. data.put("setl_id", setlinfo.getSetlId());
  662. data.put("mdtrt_id", setlinfo.getMdtrtId());
  663. data.put("psn_no", setlinfo.getPsnNo());
  664. input.getJSONObject("input").put("data", data);
  665. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_OUTPATIENT_SETTLEMENT);
  666. log.info("【操作员:99999】门诊结算撤销:\n参数:{},\n结果:{}", input, result);
  667. Integer infcode = result.getInteger(RESULT_CODE);
  668. logDao.insert(new SiLog(input, result, setlinfo.getPatNo(), setlinfo.getTimes(), infcode, setlinfo.getPsnNo()));
  669. if (null != infcode && infcode == 0) {
  670. afterRevokeSetl(setlinfo.getPatNo(), setlinfo.getTimes(), input.getString("msgid"));
  671. MzPatientInfo mzPatientInfo = new MzPatientInfo();
  672. mzPatientInfo.setPatNo(setlinfo.getPatNo());
  673. mzPatientInfo.setTimes(setlinfo.getTimes());
  674. mzPatientInfo.setStaffId("99999");
  675. revokeOutpatientFeeDetails(mzPatientInfo);
  676. revokeOutpatientRegistration(mzPatientInfo);
  677. }
  678. }
  679. public void afterRevokeSetl(String patNo, int times, String msgId) {
  680. MzgjUtil.finishProcessing(patNo);
  681. try {
  682. mzDao.deleteSetlInfo(patNo, times);
  683. mzDao.deleteSetlDetail(patNo, times);
  684. mzDao.updateRvkSetlMsgid(patNo, times, msgId);
  685. setlinfoDao.deletePreSettleInfo(patNo, times);
  686. } catch (Exception e) {
  687. String sql1 = String.format("update t_si_setlinfo set revoked=1,mz_saved=0 where pat_no='%s' and times=%d;\n", patNo, times);
  688. String sql2 = String.format("delete from t_si_setldetail where pat_no='%s' and times=%d;\n", patNo, times);
  689. String sql3 = String.format("update t_si_pat_info set rvk_setl_msgid='%s' where pat_no='%s' and times=%d;",
  690. msgId, patNo, times);
  691. String sql = sql1 + sql2 + sql3;
  692. mariadbService.insertNewRecord(sql);
  693. }
  694. }
  695. public ResultVo<FundDetail> directRegistration(DirectionRegParam param) {
  696. log.info("门诊共济:{}", param);
  697. if (MzgjUtil.inProcessing(param.getPatientId())) {
  698. log.info("【{}】此患者上一次的共济流程尚未结束,请稍后再试。", param.getPatientId());
  699. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者上一次的共济流程尚未结束,请稍后再试。");
  700. }
  701. ResultVo<FundDetail> setlfund = getFundDetailByPatientInfo(param.getPatientId(), param.getTimes(), false);
  702. if (setlfund != null) {
  703. return setlfund;
  704. }
  705. MzgjUtil.addProcess(param.getPatientId());
  706. ResultVo<FundDetail> response;
  707. try {
  708. response = executeMzgj(param);
  709. } catch (Exception e) {
  710. response = ResultVoUtil.fail(ExceptionEnum.INTERNAL_SERVER_ERROR, e.getMessage());
  711. } finally {
  712. MzgjUtil.finishProcessing(param.getPatientId());
  713. }
  714. return response;
  715. }
  716. private ResultVo<FundDetail> executeMzgj(DirectionRegParam param) {
  717. if (mzDao.selectExistCount(param.getPatientId(), param.getTimes()) == 0) {
  718. if (mzDao.updateMzTimes(param.getPatientId(), param.getTimes()) == 0) {
  719. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR,
  720. "[t_si_pat_info]不存在患者[" + param.getPatientId()
  721. + "]第[" + param.getTimes() + "]次的就诊信息,请重新查询。");
  722. }
  723. }
  724. mzDao.deleteTempPatinfo(param.getPatientId(), param.getTimes());
  725. MzPatientInfo mzptnt = new MzPatientInfo();
  726. mzptnt.setStaffId(param.getStaffId());
  727. mzptnt.setPatNo(param.getPatientId());
  728. mzptnt.setTimes(param.getTimes());
  729. mzptnt.setMedType(param.getMedType());
  730. mzptnt.setAcctUsedFlag(param.getAcctUsedFlag());
  731. mzptnt.setStraitSettle(param.getStraitSettle());
  732. mzptnt.setAbortBeforeUploadFees(param.abortBeforeUploadFees());
  733. mzptnt.setVisitDate(param.getVisitDate());
  734. mzptnt.setDeptCode(param.getVisitDeptCode());
  735. mzptnt.setDoctorCode(param.getDoctorCode());
  736. mzptnt.setIcdCodeNew(param.getIcdCodeNew());
  737. mzptnt.setIcdTextNew(param.getIcdTextNew());
  738. if (!param.abortBeforeUploadFees()) {
  739. // 生成医保费用
  740. String mzReceipts = generateSiMzFees(mzptnt);
  741. if (!mzReceipts.equals("SUCCESS")) {
  742. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, mzReceipts);
  743. }
  744. }
  745. // 填充医保基本信息
  746. SiPatInfo siPatInfo = mzDao.selectSiPatInfoForMz(mzptnt.getPatNo(), mzptnt.getTimes());
  747. mzptnt.setInsuplcAdmdvs(siPatInfo.getInsuplcAdmdvs());
  748. mzptnt.setInsutype(siPatInfo.getInsutype());
  749. mzptnt.setPsnType(siPatInfo.getPsnType());
  750. mzptnt.setName(siPatInfo.getPsnName());
  751. mzptnt.setSocialNo(siPatInfo.getCertno());
  752. // 解析读卡信息
  753. if (StringUtil.notBlank(param.getReadCardResult())) {
  754. MdtrtCertType mdtrtCertType = MdtrtCertType.get(param.getReadCardType());
  755. mzptnt.setMdtrtCertType(mdtrtCertType.getLabel());
  756. mzptnt.setReadCardBizType(ReadCardBizType.REGISTRATION.getCode());
  757. mzptnt.setReadCardResult(param.getReadCardResult());
  758. if (mdtrtCertType.getCode().equals(MdtrtCertType.ELECTRONIC_VOUCHER.getCode())) {
  759. JSONObject obj = JSONObject.parseObject(param.getReadCardResult());
  760. JSONObject qrdata = obj.getJSONObject("data");
  761. if (null != qrdata) {
  762. obj = qrdata;
  763. }
  764. String admdvs = obj.getString("insuOrg");
  765. if (StringUtil.isBlank(mzptnt.getInsuplcAdmdvs())) {
  766. mzptnt.setInsuplcAdmdvs(admdvs);
  767. }
  768. }
  769. }
  770. // 挂号登记
  771. ResultVo<String> regres = outpatientRegistration(mzptnt);
  772. if (regres.getCode() != ExceptionEnum.SUCCESS.getCode()) {
  773. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, regres.getMessage());
  774. }
  775. // 上传就诊信息
  776. siPatInfo = mzDao.selectSiPatInfoForMz(mzptnt.getPatNo(), mzptnt.getTimes());
  777. siPatInfo.setFromDirectReg(true);
  778. SpcChrDiseAcct spcChrDiseAcct = new SpcChrDiseAcct();
  779. spcChrDiseAcct.setStaffId(param.getStaffId());
  780. spcChrDiseAcct.setOpspDiseCode(param.getOpspDiseCode());
  781. spcChrDiseAcct.setOpspDiseName(param.getOpspDiseName());
  782. spcChrDiseAcct.setExpContent(param.getExpContent());
  783. ResultVo<String> upldMdtrtRes = uploadOutpatientInfo(mzptnt, spcChrDiseAcct, siPatInfo);
  784. if (upldMdtrtRes.getCode() != ExceptionEnum.SUCCESS.getCode()) {
  785. revokeOutpatientRegistration(mzptnt);
  786. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, upldMdtrtRes.getMessage());
  787. }
  788. if (param.abortBeforeUploadFees()) {
  789. return ResultVoUtil.success();
  790. }
  791. // 上传费用明细
  792. spcChrDiseAcct.setStaffId(param.getStaffId());
  793. spcChrDiseAcct.setPatNo(mzptnt.getPatNo());
  794. spcChrDiseAcct.setTimes(mzptnt.getTimes());
  795. spcChrDiseAcct.setFromDirectReg(true);
  796. ResultVo<SiPatInfo> upldFeeRes = uploadOutpatientFeeDetails(spcChrDiseAcct);
  797. if (upldFeeRes.getCode() != ExceptionEnum.SUCCESS.getCode()) {
  798. revokeOutpatientRegistration(mzptnt);
  799. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, upldFeeRes.getMessage());
  800. }
  801. // 如果是退费重收,直接结算
  802. if (param.getStraitSettle()) {
  803. return outpatientSettlement(mzptnt);
  804. }
  805. // 如果试算个人现金支付为0,直接结算
  806. mzptnt.setReadCardBizType(ReadCardBizType.SETTLEMENT.getCode());
  807. ResultVo<FundDetail> presetlResponse = outpatientPreSettlement(mzptnt);
  808. if (presetlResponse.getCode() == ExceptionEnum.SUCCESS.getCode()) {
  809. FundDetail fundDetail = presetlResponse.getData();
  810. BigDecimal cashpay = new BigDecimal(fundDetail.getCashPay());
  811. if (cashpay.compareTo(BigDecimal.ZERO) == 0) {
  812. return outpatientSettlement(mzptnt);
  813. }
  814. }
  815. return presetlResponse;
  816. }
  817. public String generateSiMzFees(MzPatientInfo mzptnt) {
  818. ResultVo<List<Map<String, Object>>> getMzRcptRes = webHisService.getMzReceipts(webHisUrl, mzptnt);
  819. if (null == getMzRcptRes) {
  820. return ExceptionEnum.NETWORK_ERROR.getMessage();
  821. }
  822. if (getMzRcptRes.getCode() != ExceptionEnum.SUCCESS.getCode()) {
  823. return getMzRcptRes.getMessage();
  824. }
  825. List<MzReceipt> mzReceipts = new ArrayList<>();
  826. for (Map<String, Object> item : getMzRcptRes.getData()) {
  827. int times = (int) item.get("times");
  828. if (times != mzptnt.getTimes()) {
  829. continue;
  830. }
  831. mzDao.deleteAllReceipts(mzptnt.getPatNo(), times);
  832. Map<Integer, List<MzReceipt>> orderReceiptsMap = FilterUtil.cast(item.get("mzReceipts"));
  833. for (Map.Entry<Integer, List<MzReceipt>> entry : orderReceiptsMap.entrySet()) {
  834. mzReceipts.addAll(entry.getValue());
  835. }
  836. }
  837. if (mzReceipts.isEmpty()) {
  838. return "没有可以上传到医保的费用。";
  839. }
  840. ResultVo<String> insertSiMzFeeRes = webHisService.insertSiMzFees(webHisUrl, mzReceipts);
  841. if (insertSiMzFeeRes.getCode() != ExceptionEnum.SUCCESS.getCode()) {
  842. return insertSiMzFeeRes.getMessage();
  843. }
  844. return "SUCCESS";
  845. }
  846. private String queryDiscount(String patNo, int times, List<String> mzReceipts) {
  847. JSONObject params = new JSONObject();
  848. params.put("patientId", patNo);
  849. params.put("times", times);
  850. params.put("chargeCodes", mzReceipts);
  851. JSONObject response = webHisService.checkDiscount(thmzUrl, params);
  852. int code = response.getIntValue("code");
  853. if (code == 0) {
  854. return "OK";
  855. }
  856. if (code == -2) {
  857. return "患者本次就诊有折扣优惠,无法进行医保统筹。";
  858. }
  859. return response.getString("message");
  860. }
  861. private ResultVo<String> mzHisChargeProcess(String patientId, int times, String totalFee) {
  862. JSONObject params = new JSONObject();
  863. params.put("patientId", patientId);
  864. params.put("times", times);
  865. params.put("totalFee", totalFee);
  866. int tryTimes = 1;
  867. JSONObject response = webHisService.mzChargeFee(thmzUrl, params);
  868. log.info("第{}次保存处方:\n参数:{}\n结果:{}", tryTimes, params, response);
  869. while (0 != response.getInteger("code") && tryTimes < 5
  870. && !response.getString("message").contains("已经收费")) {
  871. try {
  872. tryTimes += 1;
  873. response = webHisService.mzChargeFee(thmzUrl, params);
  874. log.info("第{}次保存处方:\n参数:{}\n结果:{}", tryTimes, params, response);
  875. TimeUnit.SECONDS.sleep(Math.min(tryTimes, 3));
  876. } catch (Exception e) {
  877. log.error("TIMEUNIT SLEEP ERROR", e);
  878. }
  879. }
  880. if (0 != response.getInteger("code") && !response.getString("message").contains("已经收费")) {
  881. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR, "自动保存处方失败,将为您撤销门诊医保业务。");
  882. }
  883. return ResultVoUtil.success();
  884. }
  885. public ResultVo<Integer> isPatientDuringSiSettle(String patientId) {
  886. int result = MzgjUtil.inProcessing(patientId) ? 1 : 0;
  887. return ResultVoUtil.success(result);
  888. }
  889. public void deletePresettleInfoAndRevokeYb(MzPatientInfo p) {
  890. int res = setlinfoDao.deletePreSettleInfo(p.getPatNo(), p.getTimes());
  891. if (res == 1) {
  892. revokeOutpatientFeeDetails(p);
  893. revokeOutpatientRegistration(p);
  894. }
  895. }
  896. private boolean existSettledButNotPay(String patNo, int times) {
  897. List<SiSetlinfo> setlinfos = scheduledDao.selectSettleInfosByPatient(patNo, times);
  898. for (SiSetlinfo item : setlinfos) {
  899. if (null != item.getPayCount() && item.getPayCount() > 0) {
  900. scheduledDao.updateMzSaved(item.getSetlId());
  901. } else {
  902. return true;
  903. }
  904. }
  905. return false;
  906. }
  907. public void test() {
  908. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_OUTPATIENT_SETTLEMENT, "430105", "01897");
  909. JSONObject data = new JSONObject();
  910. data.put("setl_id", "421253439");
  911. data.put("mdtrt_id", "758032270");
  912. data.put("psn_no", "43000020100034025147");
  913. input.getJSONObject("input").put("data", data);
  914. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_OUTPATIENT_SETTLEMENT);
  915. log.info("【操作员:01897】门诊结算撤销:\n参数:{},\n结果:{}",input, result);
  916. }
  917. }