SiZyService.java 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. package thyyxxk.webserver.service.medicalinsurance;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.stereotype.Service;
  9. import thyyxxk.webserver.config.exception.ExceptionEnum;
  10. import thyyxxk.webserver.constants.sidicts.*;
  11. import thyyxxk.webserver.constants.YesOrNo;
  12. import thyyxxk.webserver.dao.his.inpatient.PatientDao;
  13. import thyyxxk.webserver.dao.his.medicalinsurance.*;
  14. import thyyxxk.webserver.entity.ResultVo;
  15. import thyyxxk.webserver.entity.inpatient.patient.NotUploadedFee;
  16. import thyyxxk.webserver.entity.medicalinsurance.log.SiLog;
  17. import thyyxxk.webserver.entity.medicalinsurance.inpatient.*;
  18. import thyyxxk.webserver.entity.medicalinsurance.manage.InsSetlDetlChkRslt;
  19. import thyyxxk.webserver.entity.medicalinsurance.manage.InstStlLdgChk;
  20. import thyyxxk.webserver.entity.medicalinsurance.query.SiPatInfo;
  21. import thyyxxk.webserver.entity.medicalinsurance.setlinfo.SiSetldetail;
  22. import thyyxxk.webserver.entity.medicalinsurance.setlinfo.SiSetlinfo;
  23. import thyyxxk.webserver.entity.inpatient.dismiss.MedinsSettleFee;
  24. import thyyxxk.webserver.entity.inpatient.patient.Overview;
  25. import thyyxxk.webserver.service.externalhttp.SiZySrvc;
  26. import thyyxxk.webserver.service.inpatient.DismissService;
  27. import thyyxxk.webserver.utils.*;
  28. import java.math.BigDecimal;
  29. import java.util.*;
  30. /**
  31. * @description: 住院医保交易
  32. * @author: DingJie
  33. * @create: 2021-05-14 16:28:16
  34. **/
  35. @Slf4j
  36. @Service
  37. public class SiZyService {
  38. private static final String RESULT_CODE = "infcode";
  39. private static final String ERROR_MESSAGE = "err_msg";
  40. private static final String OUTPUT = "output";
  41. private final SiZyDao dao;
  42. private final PatientDao patientDao;
  43. private final SiQueryService queryService;
  44. private final SiQueryDao queryDao;
  45. private final SiSetlinfoDao setlinfodao;
  46. private final SiSetldetailDao setldetldao;
  47. private final SiChargeTempDao chrgtmpdao;
  48. private final SiLogDao logDao;
  49. private final ExecService exec;
  50. private final DismissService dismissService;
  51. private final SiZySrvc zySrvc;
  52. private final SiManageService manageService;
  53. @Value("${si-zy-fee-url}")
  54. private String siZyFeeUrl;
  55. @Autowired
  56. public SiZyService(SiZyDao dao, PatientDao patientDao, SiQueryService queryService, SiQueryDao queryDao, SiSetlinfoDao setlinfoDao,
  57. SiSetldetailDao setldetldao, SiChargeTempDao chrgtmpdao, SiLogDao logDao, ExecService exec,
  58. DismissService dismissService, SiZySrvc zySrvc, SiManageService manageService) {
  59. this.dao = dao;
  60. this.patientDao = patientDao;
  61. this.queryService = queryService;
  62. this.queryDao = queryDao;
  63. this.setlinfodao = setlinfoDao;
  64. this.setldetldao = setldetldao;
  65. this.chrgtmpdao = chrgtmpdao;
  66. this.logDao = logDao;
  67. this.exec = exec;
  68. this.dismissService = dismissService;
  69. this.zySrvc = zySrvc;
  70. this.manageService = manageService;
  71. }
  72. public ResultVo<String> admissRegister(ZyPatientInfo p) {
  73. AdmMdtrtinfo admMdtrtinfo = dao.selectAdmMdtrtinfo(p.getInpatientNo(), p.getLedgerSn());
  74. if (null == admMdtrtinfo) {
  75. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的参保信息!");
  76. }
  77. Date setlEndTime = dao.selectSetlEndTime(p.getInpatientNo(), p.getAdmissTimes());
  78. if (null != setlEndTime && setlEndTime.after(admMdtrtinfo.getBegntime())) {
  79. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者有 " +
  80. DateUtil.formatDatetime(setlEndTime, "yyyy-MM-dd") + " 的医保结算条目,不可重复登记。");
  81. }
  82. if (StringUtil.isBlank(admMdtrtinfo.getChfpdrName())) {
  83. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "管床医生不能为空!");
  84. }
  85. List<AdmDiseinfo> admDiseinfos = dao.selectAdmDiags(p.getInpatientNo(), p.getAdmissTimes());
  86. if (null == admDiseinfos || admDiseinfos.isEmpty()) {
  87. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请填写医保入院诊断!");
  88. }
  89. admDiseinfos.forEach(item -> {
  90. item.setDiseDorNo(admMdtrtinfo.getAtddrNo());
  91. item.setDiseDorName(admMdtrtinfo.getChfpdrName());
  92. });
  93. ReadCardBizType readCardBizType = ReadCardBizType.get(p.getReadCardBizType());
  94. if (readCardBizType != ReadCardBizType.ADMISSION && !p.getInsuplcAdmdvs().startsWith("43")) {
  95. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "省外异地的患者请读社保卡登记!");
  96. }
  97. if (readCardBizType == ReadCardBizType.ADMISSION) {
  98. MdtrtCertType mdtrtCertType = MdtrtCertType.getByLabel(p.getMdtrtCertType());
  99. admMdtrtinfo.setMdtrtCertType(mdtrtCertType.getCode());
  100. if (mdtrtCertType.getCode().equals(MdtrtCertType.SOCIAL_SECURITY_CARD.getCode())) {
  101. String[] out = p.getReadCardResult().split("\\|");
  102. if (!p.getName().trim().equals(out[4].trim())) {
  103. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "社保卡姓名与HIS姓名不一致,请确认是否人证相符。");
  104. }
  105. admMdtrtinfo.setMdtrtCertNo(out[2]);
  106. admMdtrtinfo.setCardSn(out[3]);
  107. } else if (mdtrtCertType.getCode().equals(MdtrtCertType.MEDICAL_INSURANCE_ELECTRONIC_VOUCHER.getCode())) {
  108. JSONObject qrinfo = JSONObject.parseObject(p.getReadCardResult());
  109. if (!p.getName().trim().equals(qrinfo.getString("userName").trim())) {
  110. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "电子医保凭证姓名与HIS姓名不一致,请确认是否人证相符。");
  111. }
  112. if (!p.getSocialNo().trim().equals(qrinfo.getString("idNo").trim())) {
  113. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "电子医保凭证身份证与HIS身份证不一致,请确认是否人证相符。");
  114. }
  115. admMdtrtinfo.setMdtrtCertNo(qrinfo.getString("ecToken"));
  116. }
  117. }
  118. if (StringUtil.isBlank(admMdtrtinfo.getDiseCodg())) {
  119. admMdtrtinfo.setDiseCodg(admDiseinfos.get(0).getDiagCode());
  120. admMdtrtinfo.setDiseName(admDiseinfos.get(0).getDiagName());
  121. }
  122. admMdtrtinfo.setInsutype(p.getInsutype());
  123. admMdtrtinfo.setAdmDiagDscr(admDiseinfos.get(0).getDiagName());
  124. admMdtrtinfo.setDscgMaindiagCode(admDiseinfos.get(0).getDiagCode());
  125. admMdtrtinfo.setDscgMaindiagName(admDiseinfos.get(0).getDiagName());
  126. admMdtrtinfo.setMatnType(p.getMatnType());
  127. admMdtrtinfo.setLatechbFlag(p.getLatechbFlag());
  128. admMdtrtinfo.setPretFlag(p.getPretFlag());
  129. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.ADMISSION_REGISTRATION, p.getInsuplcAdmdvs());
  130. String ref = JSONObject.toJSONStringWithDateFormat(admMdtrtinfo, "yyyy-MM-dd HH:mm:ss");
  131. input.getJSONObject("input").put("mdtrtinfo", JSONObject.parseObject(ref));
  132. String diseRef = JSONArray.toJSONStringWithDateFormat(admDiseinfos, "yyyy-MM-dd HH:mm:ss");
  133. input.getJSONObject("input").put("diseinfo", JSONArray.parse(diseRef));
  134. JSONObject result = exec.executeTrade(input, SiFunction.ADMISSION_REGISTRATION);
  135. log.info("【操作员:{}】,医保入院登记:\n参数:{},\n结果:{}", TokenUtil.getInstance().getTokenUserId(), input, result);
  136. if (null == result) {
  137. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  138. }
  139. Integer infcode = result.getInteger(RESULT_CODE);
  140. if (null == infcode) {
  141. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  142. }
  143. logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode, admMdtrtinfo.getPsnNo()));
  144. if (result.getIntValue(RESULT_CODE) == 0) {
  145. JSONObject output = result.getJSONObject(OUTPUT);
  146. String mdtrtId = output.getJSONObject("result").getString("mdtrt_id");
  147. p.setMdtrtId(mdtrtId);
  148. p.setAdmRegMsgid(input.getString("msgid"));
  149. dao.afterAdmissRegister(p);
  150. String restype = dao.selectResponceType(admMdtrtinfo.getMedType());
  151. dao.updateResponceType(restype, admMdtrtinfo.getMedType(), p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  152. return ResultVoUtil.success("入院登记成功,就诊号为:" + mdtrtId);
  153. }
  154. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  155. }
  156. public ResultVo<String> modifyAdmissionInfo(ZyPatientInfo p) {
  157. SiPatInfo siPatInfo = queryDao.selectSiPatInfoForZy(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  158. if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getMdtrtId())) {
  159. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "患者医保不在院!");
  160. }
  161. List<AdmDiseinfo> admDiseinfos = dao.selectAdmDiags(p.getInpatientNo(), p.getAdmissTimes());
  162. if (null == admDiseinfos || admDiseinfos.isEmpty()) {
  163. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请填写医保入院诊断!");
  164. }
  165. admDiseinfos.forEach(item -> item.setMdtrtId(siPatInfo.getMdtrtId()));
  166. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.MODIFY_ADMISSION_INFO, siPatInfo.getInsuplcAdmdvs());
  167. JSONObject adminfo = new JSONObject();
  168. adminfo.put("mdtrt_id", siPatInfo.getMdtrtId());
  169. adminfo.put("psn_no", siPatInfo.getPsnNo());
  170. adminfo.put("begntime", DateUtil.formatDatetime(p.getYbRegisterDate()));
  171. adminfo.put("mdtrt_cert_type", MdtrtCertType.RESIDENT_IDENTITY_CARD.getCode());
  172. adminfo.put("med_type", siPatInfo.getMedType());
  173. adminfo.put("ipt_otp_no", p.getInpatientNo());
  174. adminfo.put("atddr_no", p.getReferPhysician());
  175. adminfo.put("chfpdr_name", p.getReferPhysicianName());
  176. adminfo.put("adm_diag_dscr", admDiseinfos.get(0).getDiagCode());
  177. adminfo.put("adm_dept_codg", p.getSmallDept());
  178. adminfo.put("adm_dept_name", p.getSmallDeptName());
  179. adminfo.put("adm_bed", p.getBedNo());
  180. adminfo.put("dscg_maindiag_code", admDiseinfos.get(0).getDiagCode());
  181. adminfo.put("dscg_maindiag_name", admDiseinfos.get(0).getDiagName());
  182. input.getJSONObject("input").put("adminfo", adminfo);
  183. String diseRef = JSONArray.toJSONStringWithDateFormat(admDiseinfos, "yyyy-MM-dd HH:mm:ss");
  184. input.getJSONObject("input").put("diseinfo", JSONArray.parse(diseRef));
  185. JSONObject result = exec.executeTrade(input, SiFunction.MODIFY_ADMISSION_INFO);
  186. log.info("【操作员:{}】,入院信息变更:\n参数:{},\n结果:{}", TokenUtil.getInstance().getTokenUserId(), input, result);
  187. Integer infcode = result.getInteger(RESULT_CODE);
  188. logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode, siPatInfo.getPsnNo()));
  189. return SiUtil.makeReturnWithoutOutput(result, "变更住院信息成功。");
  190. }
  191. public ResultVo<String> revokeAdmission(ZyPatientInfo p) {
  192. SiPatInfo siPatInfo = queryDao.selectSiPatInfoForZy(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  193. if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getMdtrtId())) {
  194. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保在院信息!");
  195. }
  196. revokeUploadFees(p);
  197. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_ADMISSION, siPatInfo.getInsuplcAdmdvs());
  198. JSONObject data = new JSONObject();
  199. data.put("mdtrt_id", siPatInfo.getMdtrtId());
  200. data.put("psn_no", siPatInfo.getPsnNo());
  201. input.getJSONObject("input").put("data", data);
  202. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_ADMISSION);
  203. log.info("【操作员:{}】,取消入院登记:\n参数:{},\n结果:{}", TokenUtil.getInstance().getTokenUserId(), input, result);
  204. if (null == result) {
  205. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  206. }
  207. Integer infcode = result.getInteger(RESULT_CODE);
  208. logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode, siPatInfo.getPsnNo()));
  209. if (null == infcode) {
  210. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  211. }
  212. if (infcode == 0) {
  213. queryDao.clearMdtrtIdForZy(siPatInfo.getPatNo(), siPatInfo.getTimes(), siPatInfo.getLedgerSn(), null);
  214. dao.updateResponceType("01", null, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  215. return ResultVoUtil.success("取消入院登记成功。");
  216. }
  217. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  218. }
  219. public ResultVo<String> uploadFeeDetail(Overview o) {
  220. o.setStaffId(TokenUtil.getInstance().getTokenUserId());
  221. return zySrvc.uploadFeeDetail(siZyFeeUrl, o);
  222. }
  223. public ResultVo<String> multipleUpload(List<Overview> overviews) {
  224. for (Overview o : overviews) {
  225. if (o.getStatus() == 0) {
  226. String message = String.format("【%s床,%s,%s】未进行医保登记,无法进行费用上传。",
  227. o.getBedNo(), o.getInpatientNo(), o.getName());
  228. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, message);
  229. }
  230. if (invalidMultipleUploadPatient(o.getMedType())) {
  231. String message = String.format("【%s床,%s,%s】是工伤患者,不支持批量上传。",
  232. o.getBedNo(), o.getInpatientNo(), o.getName());
  233. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, message);
  234. }
  235. o.setStaffId(TokenUtil.getInstance().getTokenUserId());
  236. }
  237. return zySrvc.uploadMultiplePatientFees(siZyFeeUrl, overviews);
  238. }
  239. private boolean invalidMultipleUploadPatient(String medtype) {
  240. MedType medType = MedType.get(medtype);
  241. return null == medType || medType == MedType.INJURY_HOSPITALIZATION
  242. || medType == MedType.SELF_PAY || medType == MedType.GCP_EXPERIMENT;
  243. }
  244. public ResultVo<String> hospitalizationPreSettlement(ZyPatientInfo p) {
  245. PreSetlmt preSetlmt = dao.selectPreSetlmt(p.getInpatientNo(), p.getAdmissTimes(),
  246. p.getLedgerSn(), DateUtil.getTodayEndTime());
  247. if (null == preSetlmt || StringUtil.isBlank(preSetlmt.getMdtrtId())) {
  248. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保在院信息!");
  249. }
  250. preSetlmt.setMdtrtCertType(MdtrtCertType.RESIDENT_IDENTITY_CARD.getCode());
  251. preSetlmt.setPsnSetlway(p.getDbg() ? PsnSetlWay.SETTLE_BY_QUOTA.getCode() : PsnSetlWay.SETTLE_BY_ITEMS.getCode());
  252. preSetlmt.setAcctUsedFlag(YesOrNo.NO.getCodeStr());
  253. preSetlmt.setMidSetlFlag(YesOrNo.NO.getCodeStr());
  254. preSetlmt.setMdtrtareaAdmvs(SiUtil.getInstitutionArea(preSetlmt.getInsuplcAdmdvs()));
  255. preSetlmt.setDscgTime(dao.selectActOrderDisDate(p.getInpatientNo(), p.getAdmissTimes()));
  256. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.HOSPITALIZATION_PRE_SETTLEMENT, preSetlmt.getInsuplcAdmdvs());
  257. String ref = JSONObject.toJSONStringWithDateFormat(preSetlmt, "yyyy-MM-dd");
  258. input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
  259. JSONObject result = exec.executeTrade(input, SiFunction.HOSPITALIZATION_PRE_SETTLEMENT);
  260. log.info("预结算:\n参数:{},\n结果:{}", input, result);
  261. if (null == result) {
  262. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  263. }
  264. Integer infcode = result.getInteger(RESULT_CODE);
  265. logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode, preSetlmt.getPsnNo()));
  266. if (null == infcode) {
  267. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  268. }
  269. if (infcode == 0) {
  270. JSONObject setlinfo = result.getJSONObject(OUTPUT).getJSONObject("setlinfo");
  271. String fundPay = setlinfo.getString("fund_pay_sumamt");
  272. dao.updateFundPay(fundPay, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  273. patientDao.recountDeposit(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  274. patientDao.updateBalance(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  275. String balance = patientDao.selectLedgerBalance(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  276. if (StringUtil.isBlank(balance)) {
  277. balance = "0";
  278. }
  279. patientDao.updateZyActPatientBalance(p.getInpatientNo(), balance);
  280. String message = "患者【" + p.getName() + "】院内总费用与医保中心总费用一致,医保报销金额为:¥ " + fundPay + "。";
  281. return ResultVoUtil.success(message);
  282. }
  283. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  284. }
  285. public ResultVo<String> revokeUploadFees(ZyPatientInfo p) {
  286. SiPatInfo siPatInfo = queryDao.selectSiPatInfoForZy(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  287. if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getMdtrtId())) {
  288. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保在院信息!");
  289. }
  290. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_HOSPITALIZATION_FEE_DETAILS, siPatInfo.getInsuplcAdmdvs());
  291. JSONArray data = new JSONArray();
  292. if (null == p.getDetailSns() || p.getDetailSns().isEmpty()) {
  293. JSONObject item = new JSONObject();
  294. item.put("feedetl_sn", "0000");
  295. item.put("mdtrt_id", siPatInfo.getMdtrtId());
  296. item.put("psn_no", siPatInfo.getPsnNo());
  297. data.add(item);
  298. } else {
  299. p.getDetailSns().forEach(detailSn -> {
  300. JSONObject item = new JSONObject();
  301. item.put("feedetl_sn", detailSn);
  302. item.put("mdtrt_id", siPatInfo.getMdtrtId());
  303. item.put("psn_no", siPatInfo.getPsnNo());
  304. data.add(item);
  305. });
  306. }
  307. input.getJSONObject("input").put("data", data);
  308. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_HOSPITALIZATION_FEE_DETAILS);
  309. log.info("【操作员:{}】撤销已上传的费用:\n参数:{},\n结果:{}", TokenUtil.getInstance().getTokenUserId(), input, result);
  310. if (null == result) {
  311. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  312. }
  313. Integer infcode = result.getInteger(RESULT_CODE);
  314. logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode, siPatInfo.getPsnNo()));
  315. if (null == infcode) {
  316. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  317. }
  318. if (infcode == 0) {
  319. QueryWrapper<SiChargeTemp> wrapper = new QueryWrapper<>();
  320. wrapper.eq("pat_no", p.getInpatientNo());
  321. wrapper.eq("times", p.getAdmissTimes());
  322. wrapper.eq("ledger_sn", p.getLedgerSn());
  323. if (null == p.getDetailSns() || p.getDetailSns().isEmpty()) {
  324. dao.revokeAllUploadFee(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  325. dao.resetFundpayAmt(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  326. chrgtmpdao.delete(wrapper);
  327. } else {
  328. wrapper.in("feedetl_sn", p.getDetailSns());
  329. chrgtmpdao.delete(wrapper);
  330. dao.revokePartUploadFee(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), p.getDetailSns());
  331. }
  332. return ResultVoUtil.success("撤销费用上传成功。");
  333. }
  334. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  335. }
  336. public ResultVo<String> pairNegativeFee(NotUploadedFee fee) {
  337. if (null == fee.getOriDetailSn()) {
  338. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请先处理违规退费。");
  339. }
  340. SiPatInfo siPatInfo = queryDao.selectSiPatInfoForZy(fee.getPatNo(), fee.getTimes(), fee.getLedgerSn());
  341. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_HOSPITALIZATION_FEE_DETAILS,
  342. siPatInfo.getInsuplcAdmdvs());
  343. JSONArray data = new JSONArray();
  344. JSONObject item = new JSONObject();
  345. item.put("feedetl_sn", fee.getOriDetailSn());
  346. item.put("mdtrt_id", siPatInfo.getMdtrtId());
  347. item.put("psn_no", siPatInfo.getPsnNo());
  348. data.add(item);
  349. input.getJSONObject("input").put("data", data);
  350. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_HOSPITALIZATION_FEE_DETAILS);
  351. log.info("【操作员:{}】退费处理:\n参数:{},\n结果:{}", TokenUtil.getInstance().getTokenUserId(), input, result);
  352. if (null == result) {
  353. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  354. }
  355. Integer infcode = result.getInteger(RESULT_CODE);
  356. logDao.insert(new SiLog(input, result, fee.getPatNo(), fee.getTimes(), fee.getLedgerSn(), infcode, siPatInfo.getPsnNo()));
  357. if (null == infcode) {
  358. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  359. }
  360. if (infcode == 0) {
  361. QueryWrapper<SiChargeTemp> wrapper = new QueryWrapper<>();
  362. wrapper.eq("pat_no", fee.getPatNo());
  363. wrapper.eq("times", fee.getTimes());
  364. wrapper.eq("ledger_sn", fee.getLedgerSn());
  365. wrapper.eq("feedetl_sn", fee.getOriDetailSn());
  366. dao.updateTransFlag(fee.getPatNo(), fee.getTimes(), fee.getDetailSn(), fee.getOriDetailSn());
  367. chrgtmpdao.delete(wrapper);
  368. }
  369. return ResultVoUtil.success("退费处理成功。");
  370. }
  371. public ResultVo<String> revokeSingleCharge(String patNo, int times, int ledgerSn, int detailSn) {
  372. SiPatInfo siPatInfo = queryDao.selectSiPatInfoForZy(patNo, times, ledgerSn);
  373. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_HOSPITALIZATION_FEE_DETAILS,
  374. siPatInfo.getInsuplcAdmdvs());
  375. JSONArray data = new JSONArray();
  376. JSONObject item = new JSONObject();
  377. item.put("feedetl_sn", detailSn);
  378. item.put("mdtrt_id", siPatInfo.getMdtrtId());
  379. item.put("psn_no", siPatInfo.getPsnNo());
  380. data.add(item);
  381. input.getJSONObject("input").put("data", data);
  382. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_HOSPITALIZATION_FEE_DETAILS);
  383. log.info("【操作员:{}】退费处理:\n参数:{},\n结果:{}", TokenUtil.getInstance().getTokenUserId(), input, result);
  384. if (null == result) {
  385. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  386. }
  387. Integer infcode = result.getInteger(RESULT_CODE);
  388. logDao.insert(new SiLog(input, result, patNo, times, ledgerSn, infcode, siPatInfo.getPsnNo()));
  389. if (null == infcode) {
  390. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  391. }
  392. if (infcode == 0) {
  393. QueryWrapper<SiChargeTemp> wrapper = new QueryWrapper<>();
  394. wrapper.eq("pat_no", patNo);
  395. wrapper.eq("times", times);
  396. wrapper.eq("ledger_sn", ledgerSn);
  397. wrapper.eq("feedetl_sn", detailSn);
  398. chrgtmpdao.delete(wrapper);
  399. }
  400. return ResultVoUtil.success("处理成功。");
  401. }
  402. public ResultVo<String> dischargeProcessing(ZyPatientInfo p) {
  403. if (null == p.getLedgerSn()) {
  404. p.setLedgerSn(dao.selectMaxLedgerSn(p.getInpatientNo(), p.getAdmissTimes()));
  405. }
  406. Dscginfo dscginfo;
  407. if (p.getMidSetl()) {
  408. dscginfo = dao.selectDscginfoForMidSetl(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  409. dscginfo.setEndtime(p.getZjdzDatetime());
  410. } else {
  411. dscginfo = dao.selectDscginfo(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  412. }
  413. if (null == dscginfo || StringUtil.isBlank(dscginfo.getMdtrtId())) {
  414. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保在院信息!");
  415. }
  416. if (StringUtil.isBlank(dscginfo.getDscgDeptCodg())) {
  417. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "出院科室【" +
  418. dscginfo.getDscgDeptName() + "】编码不符合医保字典规范,请联系医保科匹配。");
  419. }
  420. List<SetlDiseinfo> setlDises = dao.selectSetlDises(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  421. if (null == setlDises || setlDises.isEmpty()) {
  422. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "患者医保出院诊断不能为空,请联系医生在病案首页填写。");
  423. }
  424. if (null == p.getStaffId()) {
  425. p.setStaffId(TokenUtil.getInstance().getTokenUserId());
  426. }
  427. if (StringUtil.isBlank(p.getAcctUsedFlag())) {
  428. p.setAcctUsedFlag(YesOrNo.NO.getCodeStr());
  429. }
  430. // if (p.getAge() >= 7 && p.getAge() < 70) {
  431. // ReadCardBizType readCardBizType = ReadCardBizType.get(p.getReadCardBizType());
  432. // if (readCardBizType != ReadCardBizType.SETTLEMENT) {
  433. // return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请先读卡再做出院结算。");
  434. // }
  435. // }
  436. dscginfo.setDiseCodg(setlDises.get(0).getDiagCode());
  437. dscginfo.setDiseName(setlDises.get(0).getDiagName());
  438. dscginfo.setDscgWay(DscgWay.ORDER_TO_LEAVE_HOSPITAL.getCode());
  439. String ref = JSONObject.toJSONStringWithDateFormat(dscginfo, "yyyy-MM-dd HH:mm:ss");
  440. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.DISCHARGE_PROCESSING,
  441. dao.selectAdmdvs(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn()));
  442. input.getJSONObject("input").put("dscginfo", JSONObject.parseObject(ref));
  443. String diseRef = JSONArray.toJSONStringWithDateFormat(setlDises, "yyyy-MM-dd HH:mm:ss");
  444. input.getJSONObject("input").put("diseinfo", JSONArray.parse(diseRef));
  445. JSONObject result = exec.executeTrade(input, SiFunction.DISCHARGE_PROCESSING);
  446. log.info("【操作员:{}】,医保出院办理:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
  447. if (null == result) {
  448. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  449. }
  450. Integer infcode = result.getInteger(RESULT_CODE);
  451. logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode, dscginfo.getPsnNo()));
  452. if (null == infcode) {
  453. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  454. }
  455. if (infcode == 0) {
  456. if (p.getStopHere()) {
  457. return ResultVoUtil.success("出院办理成功。");
  458. }
  459. p.setDismissDate(dscginfo.getEndtime());
  460. return inpatientSettlement(p);
  461. }
  462. String errMsg = result.getString(ERROR_MESSAGE);
  463. if (null != errMsg && errMsg.contains("该人员已经是出院状态")) {
  464. p.setDismissDate(dscginfo.getEndtime());
  465. return inpatientSettlement(p);
  466. }
  467. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, errMsg);
  468. }
  469. public ResultVo<String> revokeDischarge(ZyPatientInfo p) {
  470. SiPatInfo siPatInfo = queryDao.selectSiPatInfoForZy(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  471. if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getMdtrtId())) {
  472. transbackTables(p);
  473. return ResultVoUtil.success("取消出院办理成功。");
  474. }
  475. p.setMdtrtId(siPatInfo.getMdtrtId());
  476. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_DISCHARGE, siPatInfo.getInsuplcAdmdvs());
  477. JSONObject data = new JSONObject();
  478. data.put("mdtrt_id", siPatInfo.getMdtrtId());
  479. data.put("psn_no", siPatInfo.getPsnNo());
  480. input.getJSONObject("input").put("data", data);
  481. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_DISCHARGE);
  482. log.info("【操作员:{}】取消医保出院办理:\n参数:{},\n结果:{}", TokenUtil.getInstance().getTokenUserId(), input, result);
  483. if (null == result) {
  484. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  485. }
  486. Integer infcode = result.getInteger(RESULT_CODE);
  487. logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode, siPatInfo.getPsnNo()));
  488. if (null == infcode) {
  489. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  490. }
  491. if (infcode == 0) {
  492. dao.deleteSetlInfo(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  493. dao.deleteSetlDetail(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  494. transbackTables(p);
  495. return ResultVoUtil.success("取消医保出院办理成功。");
  496. }
  497. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  498. }
  499. private ResultVo<String> inpatientSettlement(ZyPatientInfo p) {
  500. PreSetlmt preSetlmt;
  501. Date begntime = p.getYbRegisterDate();
  502. if (null == begntime) {
  503. begntime = dismissService.getBegntime(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), "zy_actpatient");
  504. }
  505. if (p.getMidSetl()) {
  506. preSetlmt = dao.selectPreSetlmtForMidSetl(p.getInpatientNo(), p.getAdmissTimes(),
  507. p.getLedgerSn(), begntime, p.getZjdzDatetime());
  508. } else {
  509. preSetlmt = dao.selectPreSetlmt(p.getInpatientNo(), p.getAdmissTimes(),
  510. p.getLedgerSn(), "2999-12-31 23:59:59.999");
  511. }
  512. if (null == preSetlmt || StringUtil.isBlank(preSetlmt.getMdtrtId())) {
  513. revokeDischarge(p);
  514. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保在院信息!");
  515. }
  516. ReadCardBizType readCardBizType = ReadCardBizType.get(p.getReadCardBizType());
  517. if (readCardBizType == ReadCardBizType.SETTLEMENT) {
  518. MdtrtCertType mdtrtCertType = MdtrtCertType.getByLabel(p.getMdtrtCertType());
  519. preSetlmt.setMdtrtCertType(mdtrtCertType.getCode());
  520. if (mdtrtCertType.getCode().equals(MdtrtCertType.SOCIAL_SECURITY_CARD.getCode())) {
  521. String[] out = p.getReadCardResult().split("\\|");
  522. if (!p.getName().trim().equals(out[4].trim())) {
  523. revokeDischarge(p);
  524. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "社保卡姓名与HIS姓名不一致,请确认是否人证相符。");
  525. }
  526. preSetlmt.setMdtrtCertNo(out[2]);
  527. preSetlmt.setCardSn(out[3]);
  528. } else if (mdtrtCertType.getCode().equals(MdtrtCertType.MEDICAL_INSURANCE_ELECTRONIC_VOUCHER.getCode())) {
  529. JSONObject qrinfo = JSONObject.parseObject(p.getReadCardResult());
  530. if (!p.getName().trim().equals(qrinfo.getString("userName").trim())) {
  531. revokeDischarge(p);
  532. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "电子医保凭证姓名与HIS姓名不一致,请确认是否人证相符。");
  533. }
  534. if (!p.getSocialNo().trim().equals(qrinfo.getString("idNo").trim())) {
  535. revokeDischarge(p);
  536. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "电子医保凭证身份证与HIS身份证不一致,请确认是否人证相符。");
  537. }
  538. preSetlmt.setMdtrtCertNo(qrinfo.getString("ecToken"));
  539. }
  540. }
  541. preSetlmt.setPsnSetlway(p.getDbg() ? PsnSetlWay.SETTLE_BY_QUOTA.getCode() : PsnSetlWay.SETTLE_BY_ITEMS.getCode());
  542. preSetlmt.setAcctUsedFlag(p.getAcctUsedFlag());
  543. preSetlmt.setMidSetlFlag(YesOrNo.NO.getCodeStr());
  544. preSetlmt.setInvono(SnowFlakeId.instance().nextId());
  545. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.HOSPITALIZATION_SETTLEMENT, preSetlmt.getInsuplcAdmdvs());
  546. String ref = JSONObject.toJSONString(preSetlmt);
  547. input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
  548. JSONObject result = exec.executeTrade(input, SiFunction.HOSPITALIZATION_SETTLEMENT);
  549. log.info("【操作员:{}】,医保出院结算:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
  550. if (null == result) {
  551. revokeDischarge(p);
  552. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  553. }
  554. Integer infcode = result.getInteger(RESULT_CODE);
  555. logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode, preSetlmt.getPsnNo()));
  556. if (null == infcode) {
  557. revokeDischarge(p);
  558. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  559. }
  560. if (infcode == 0) {
  561. JSONObject setlinfo = result.getJSONObject(OUTPUT).getJSONObject("setlinfo");
  562. SiSetlinfo setlinfoEntity = JSONObject.parseObject(setlinfo.toJSONString(), SiSetlinfo.class);
  563. setlinfoEntity.setPatNo(p.getInpatientNo());
  564. setlinfoEntity.setTimes(p.getAdmissTimes());
  565. setlinfoEntity.setLedgerSn(p.getLedgerSn());
  566. setlinfoEntity.setStaffId(TokenUtil.getInstance().getTokenUserId());
  567. setlinfoEntity.setRevoked(YesOrNo.NO.getCode());
  568. setlinfoEntity.setSetlType(ClrType.INPATIENT.getCode());
  569. setlinfoEntity.setInsuplcAdmdvs(preSetlmt.getInsuplcAdmdvs());
  570. setlinfoEntity.setBegntime(begntime);
  571. setlinfoEntity.setEndtime(p.getDismissDate());
  572. setlinfoEntity.setHiPaymtd(CommonUtil.calcHiPaymtd(setlinfoEntity.getInsuplcAdmdvs(),setlinfoEntity.getMedType(),setlinfoEntity.getInsutype(),setlinfoEntity.getSetlTime()));
  573. setlinfoEntity.setMedinsType(MedInsTypeUtil.getMedInsType(setlinfoEntity));
  574. JSONArray setldetail = result.getJSONObject(OUTPUT).getJSONArray("setldetail");
  575. BigDecimal hospitalPart = BigDecimal.ZERO;
  576. for (int i = 0; i < setldetail.size(); i++) {
  577. SiSetldetail setldetailEntity = JSONObject.parseObject(setldetail.getJSONObject(i).toJSONString(), SiSetldetail.class);
  578. setldetailEntity.setPatNo(p.getInpatientNo());
  579. setldetailEntity.setTimes(p.getAdmissTimes());
  580. setldetailEntity.setLedgerSn(p.getLedgerSn());
  581. if (setldetailEntity.getFundPayType().equals("999996") || setldetailEntity.getSetlProcInfo().equals("999996")) {
  582. hospitalPart = hospitalPart.add(setldetailEntity.getFundPayamt());
  583. }
  584. setldetldao.insert(setldetailEntity);
  585. }
  586. setlinfoEntity.setHospPartAmt(hospitalPart.doubleValue());
  587. setlinfodao.insert(setlinfoEntity);
  588. setlinfodao.updateSiZyInfoSetlId(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(),
  589. setlinfoEntity.getSetlId(), setlinfoEntity.getMedinsSetlId(), input.getString("msgid"));
  590. setlinfodao.updateApplySettled(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), YesOrNo.YES.getCode());
  591. // 跨省异地数据不参与对账,对总账时无需统计进去。
  592. if (setlinfoEntity.getFundPaySumamt() > 0 && setlinfoEntity.getInsuplcAdmdvs().startsWith("43")) {
  593. InstStlLdgChk ldgChk = new InstStlLdgChk();
  594. ldgChk.setInsutype(setlinfoEntity.getInsutype());
  595. ldgChk.setClrType(setlinfoEntity.getClrType());
  596. ldgChk.setSetlOptins(setlinfoEntity.getClrOptins());
  597. ldgChk.setStmtBegndate(setlinfoEntity.getSetlTime());
  598. ldgChk.setStmtEnddate(setlinfoEntity.getSetlTime());
  599. ResultVo<String> totalLedgerCheck = manageService.institutionSettlementLedgerCheck(ldgChk);
  600. if (totalLedgerCheck.getCode() != ExceptionEnum.SUCCESS.getCode()) {
  601. InstStlLdgChk detlChk = new InstStlLdgChk();
  602. detlChk.setSetlOptins(ldgChk.getSetlOptins());
  603. detlChk.setStmtBegndate(ldgChk.getStmtBegndate());
  604. detlChk.setStmtEnddate(ldgChk.getStmtEnddate());
  605. detlChk.setClrType(ldgChk.getClrType());
  606. detlChk.setRefdSetlFlag(YesOrNo.NO.getCodeStr());
  607. ResultVo<List<InsSetlDetlChkRslt>> detailLedgerCheck = manageService.institutionSettlementDetailCheck(detlChk);
  608. if (detailLedgerCheck.getCode() != ExceptionEnum.SUCCESS.getCode()) {
  609. revokeSettlement(p);
  610. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, detailLedgerCheck.getMessage());
  611. }
  612. for (InsSetlDetlChkRslt insSetlDetlChkRslt : detailLedgerCheck.getData()) {
  613. if (Objects.equals(insSetlDetlChkRslt.getSetlId(), setlinfoEntity.getSetlId())) {
  614. if (!Objects.equals(insSetlDetlChkRslt.getStmtRslt(), "0")) {
  615. revokeSettlement(p);
  616. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, insSetlDetlChkRslt.getMemo());
  617. }
  618. break;
  619. }
  620. }
  621. }
  622. }
  623. queryService.saveCumInfo(setlinfoEntity);
  624. MedinsSettleFee settleFee = new MedinsSettleFee();
  625. if (p.getMidSetl()) {
  626. settleFee.setZjdzDatetime(p.getZjdzDatetime());
  627. }
  628. settleFee.setMidSetl(p.getMidSetl());
  629. settleFee.setStaffId(TokenUtil.getInstance().getTokenUserId());
  630. settleFee.setInpatientNo(p.getInpatientNo());
  631. settleFee.setAdmissTimes(p.getAdmissTimes());
  632. settleFee.setBedNo(p.getBedNo());
  633. settleFee.setWardCode(p.getAdmissWard());
  634. settleFee.setDeptCode(p.getAdmissDept());
  635. settleFee.setTotalCost(setlinfo.getString("medfee_sumamt"));
  636. settleFee.setTczf(setlinfo.getString("fund_pay_sumamt"));
  637. settleFee.setHospitalPay(setlinfo.getString("hosp_part_amt"));
  638. settleFee.setGrzhzf(setlinfo.getString("acct_pay"));
  639. settleFee.setDbzf(setlinfo.getString("hifmi_pay"));
  640. settleFee.setXjzf(setlinfo.getString("psn_cash_pay"));
  641. settleFee.setTable(p.getTable());
  642. return dismissService.executeDischarging(settleFee);
  643. }
  644. revokeDischarge(p);
  645. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  646. }
  647. public ResultVo<String> revokeSettlement(ZyPatientInfo p) {
  648. SiPatInfo siPatInfo = queryDao.selectSiPatInfoForZy(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  649. if (null == p.getStaffId()) {
  650. p.setStaffId(TokenUtil.getInstance().getTokenUserId());
  651. }
  652. if (null == p.getLedgerSn()) {
  653. p.setLedgerSn(dao.selectMaxLedgerSn(p.getInpatientNo(), p.getAdmissTimes()));
  654. }
  655. if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getSetlId())) {
  656. return revokeDischarge(p);
  657. }
  658. p.setMdtrtId(siPatInfo.getMdtrtId());
  659. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_HOSPITALIZATION_SETTLEMENT, siPatInfo.getInsuplcAdmdvs());
  660. JSONObject data = new JSONObject();
  661. data.put("mdtrt_id", siPatInfo.getMdtrtId());
  662. data.put("setl_id", siPatInfo.getSetlId());
  663. data.put("psn_no", siPatInfo.getPsnNo());
  664. input.getJSONObject("input").put("data", data);
  665. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_HOSPITALIZATION_SETTLEMENT);
  666. log.info("【操作员:{}】取消医保结算:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
  667. if (null == result) {
  668. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  669. }
  670. Integer infcode = result.getInteger(RESULT_CODE);
  671. logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode, siPatInfo.getPsnNo()));
  672. if (null == infcode) {
  673. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  674. }
  675. String errMsg = null;
  676. if (infcode != 0) {
  677. errMsg = result.getString("err_msg");
  678. }
  679. ResultVo<String> rvkDischrgRslt = revokeDischarge(p);
  680. if (rvkDischrgRslt.getCode() == ExceptionEnum.SUCCESS.getCode()) {
  681. dao.updateRvkSetlMsgid(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), input.getString("msgid"));
  682. setlinfodao.updateApplySettled(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), YesOrNo.NO.getCode());
  683. transbackTables(p);
  684. return rvkDischrgRslt;
  685. }
  686. return null == errMsg ? rvkDischrgRslt : ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, errMsg);
  687. }
  688. private void transbackTables(ZyPatientInfo p) {
  689. dao.deleteSiSetlFeeDetl(p.getMdtrtId());
  690. dao.deleteZyLedgerFileYb(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  691. dao.deleteZyReceipt(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  692. dao.updateSettleType(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  693. dao.updateChargeStatus(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  694. }
  695. }