SiZyService.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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.stereotype.Service;
  8. import thyyxxk.webserver.config.exception.ExceptionEnum;
  9. import thyyxxk.webserver.constants.sidicts.*;
  10. import thyyxxk.webserver.constants.YesOrNo;
  11. import thyyxxk.webserver.dao.his.medicalinsurance.*;
  12. import thyyxxk.webserver.entity.ResultVo;
  13. import thyyxxk.webserver.entity.medicalinsurance.log.SiLog;
  14. import thyyxxk.webserver.entity.medicalinsurance.inpatient.*;
  15. import thyyxxk.webserver.entity.medicalinsurance.query.SiPatInfo;
  16. import thyyxxk.webserver.entity.medicalinsurance.setlinfo.SiSetldetail;
  17. import thyyxxk.webserver.entity.medicalinsurance.setlinfo.SiSetlinfo;
  18. import thyyxxk.webserver.entity.yibao.dismiss.YbSettleFee;
  19. import thyyxxk.webserver.entity.yibao.patient.Overview;
  20. import thyyxxk.webserver.service.externalhttp.SiZySrvc;
  21. import thyyxxk.webserver.service.yibao.DismissService;
  22. import thyyxxk.webserver.utils.*;
  23. import java.util.Date;
  24. import java.util.List;
  25. /**
  26. * @description: 住院医保交易
  27. * @author: DingJie
  28. * @create: 2021-05-14 16:28:16
  29. * <p>
  30. * 联通云:
  31. * http://10.136.68.156:20005/isp-api/powercsb/1101
  32. * api_access_key=FqSRO1tkKe4PTsZS8mJkN3zs12U6Zkkwez6Qhy
  33. * api_secreKey=SKklCVOoGDag5ys4zkSE2qHYo8iU4FsG7DNyJQYI
  34. * <p>
  35. * 省政府二院:
  36. * http://10.90.0.11:8081/isp-api/powercsb/1101
  37. * api_access_key=sJAXxQd10aQLWZFAMH3PT9pHCGGqXPzz4Hv4Ei
  38. * api_secreKey=SK1BtAloxIaQUgv9m0gjqrQJ0NJikkPPUTlRGOzp
  39. **/
  40. @Slf4j
  41. @Service
  42. public class SiZyService {
  43. private static final String RESULT_CODE = "infcode";
  44. private static final String ERROR_MESSAGE = "err_msg";
  45. private static final String OUTPUT = "output";
  46. private final SiZyDao dao;
  47. private final SiQueryDao qrydao;
  48. private final SiSetlinfoDao setlinfodao;
  49. private final SiSetldetailDao setldetldao;
  50. private final SiChargeTempDao chrgtmpdao;
  51. private final SiLogDao logDao;
  52. private final ExecService exec;
  53. private final DismissService dismissService;
  54. private final SetlListUpldService setlListUpldService;
  55. private final SiZySrvc zySrvc;
  56. @Autowired
  57. public SiZyService(SiZyDao dao, SiQueryDao qrydao, SiSetlinfoDao setlinfoDao,
  58. SiSetldetailDao setldetldao, SiChargeTempDao chrgtmpdao, SiLogDao logDao, ExecService exec,
  59. DismissService dismissService, SetlListUpldService setlListUpldService, SiZySrvc zySrvc) {
  60. this.dao = dao;
  61. this.qrydao = qrydao;
  62. this.setlinfodao = setlinfoDao;
  63. this.setldetldao = setldetldao;
  64. this.chrgtmpdao = chrgtmpdao;
  65. this.logDao = logDao;
  66. this.exec = exec;
  67. this.dismissService = dismissService;
  68. this.setlListUpldService = setlListUpldService;
  69. this.zySrvc = zySrvc;
  70. }
  71. public ResultVo<String> admissRegister(ZyPatientInfo p) {
  72. if (p.getInsuplcAdmdvs().equals("430182")) {
  73. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "宁乡患者无法在我院办理住院业务。");
  74. }
  75. AdmMdtrtinfo admMdtrtinfo = dao.selectAdmMdtrtinfo(p.getInpatientNo(), p.getLedgerSn());
  76. if (null == admMdtrtinfo) {
  77. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的参保信息!");
  78. }
  79. if (StringUtil.isBlank(admMdtrtinfo.getChfpdrName())) {
  80. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "管床医生不能为空!");
  81. }
  82. List<AdmDiseinfo> admDiseinfos = dao.selectAdmDiags(p.getInpatientNo(), p.getAdmissTimes());
  83. if (null == admDiseinfos || admDiseinfos.isEmpty()) {
  84. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请填写医保入院诊断!");
  85. }
  86. ReadCardBizType readCardBizType = ReadCardBizType.get(p.getReadCardBizType());
  87. if (readCardBizType == ReadCardBizType.ADMISSION) {
  88. MdtrtCertType mdtrtCertType = MdtrtCertType.getByLabel(p.getMdtrtCertType());
  89. admMdtrtinfo.setMdtrtCertType(mdtrtCertType.getCode());
  90. if (mdtrtCertType == MdtrtCertType.SOCIAL_SECURITY_CARD) {
  91. String[] out = p.getReadCardResult().split("\\|");
  92. if (!p.getName().trim().equals(out[4].trim())) {
  93. revokeDischarge(p);
  94. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "社保卡姓名与HIS姓名不一致,请确认是否人证相符。");
  95. }
  96. admMdtrtinfo.setMdtrtCertNo(out[2]);
  97. admMdtrtinfo.setCardSn(out[3]);
  98. } else if (mdtrtCertType == MdtrtCertType.MEDICAL_INSURANCE_ELECTRONIC_VOUCHER) {
  99. JSONObject qrinfo = JSONObject.parseObject(p.getReadCardResult());
  100. if (!p.getName().trim().equals(qrinfo.getString("userName").trim())) {
  101. revokeDischarge(p);
  102. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "电子医保凭证姓名与HIS姓名不一致,请确认是否人证相符。");
  103. }
  104. if (!p.getSocialNo().trim().equals(qrinfo.getString("idNo").trim())) {
  105. revokeDischarge(p);
  106. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "电子医保凭证身份证与HIS身份证不一致,请确认是否人证相符。");
  107. }
  108. admMdtrtinfo.setMdtrtCertNo(qrinfo.getString("ecToken"));
  109. }
  110. }
  111. admMdtrtinfo.setInsutype(p.getInsutype());
  112. admMdtrtinfo.setAdmDiagDscr(admDiseinfos.get(0).getDiagName());
  113. admMdtrtinfo.setDscgMaindiagCode(admDiseinfos.get(0).getDiagCode());
  114. admMdtrtinfo.setDscgMaindiagName(admDiseinfos.get(0).getDiagName());
  115. admMdtrtinfo.setDiseCodg(admDiseinfos.get(0).getDiagCode());
  116. admMdtrtinfo.setDiseName(admDiseinfos.get(0).getDiagName());
  117. admMdtrtinfo.setMatnType(p.getMatnType());
  118. admMdtrtinfo.setLatechbFlag(p.getLatechbFlag());
  119. admMdtrtinfo.setPretFlag(p.getPretFlag());
  120. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.ADMISSION_REGISTRATION, p.getInsuplcAdmdvs());
  121. String ref = JSONObject.toJSONStringWithDateFormat(admMdtrtinfo, "yyyy-MM-dd HH:mm:ss");
  122. input.getJSONObject("input").put("mdtrtinfo", JSONObject.parseObject(ref));
  123. String diseRef = JSONArray.toJSONStringWithDateFormat(admDiseinfos, "yyyy-MM-dd HH:mm:ss");
  124. input.getJSONObject("input").put("diseinfo", JSONArray.parse(diseRef));
  125. JSONObject result = exec.executeTrade(input, SiFunction.ADMISSION_REGISTRATION);
  126. log.info("【操作员:{}】,医保入院登记:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result);
  127. if (null == result) {
  128. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  129. }
  130. Integer infcode = result.getInteger(RESULT_CODE);
  131. if (null == infcode) {
  132. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  133. }
  134. logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode));
  135. if (result.getIntValue(RESULT_CODE) == 0) {
  136. JSONObject output = result.getJSONObject(OUTPUT);
  137. String mdtrtId = output.getJSONObject("result").getString("mdtrt_id");
  138. p.setMdtrtId(mdtrtId);
  139. p.setAdmRegMsgid(input.getString("msgid"));
  140. dao.afterAdmissRegister(p);
  141. String restype = dao.selectResponceType(admMdtrtinfo.getMedType());
  142. dao.updateResponceType(restype, admMdtrtinfo.getMedType(), p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  143. return ResultVoUtil.success("入院登记成功,就诊号为:" + mdtrtId);
  144. }
  145. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  146. }
  147. public ResultVo<String> modifyAdmissionInfo(ZyPatientInfo p) {
  148. SiPatInfo siPatInfo = qrydao.selectSiPatInfoForZy(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  149. if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getMdtrtId())) {
  150. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "患者医保不在院!");
  151. }
  152. List<AdmDiseinfo> admDiseinfos = dao.selectAdmDiags(p.getInpatientNo(), p.getAdmissTimes());
  153. if (null == admDiseinfos || admDiseinfos.isEmpty()) {
  154. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "请填写医保入院诊断!");
  155. }
  156. admDiseinfos.forEach(item -> item.setMdtrtId(siPatInfo.getMdtrtId()));
  157. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.MODIFY_ADMISSION_INFO, siPatInfo.getInsuplcAdmdvs());
  158. JSONObject adminfo = new JSONObject();
  159. adminfo.put("mdtrt_id", siPatInfo.getMdtrtId());
  160. adminfo.put("psn_no", siPatInfo.getPsnNo());
  161. adminfo.put("begntime", DateUtil.formatDatetime(p.getYbRegisterDate()));
  162. adminfo.put("mdtrt_cert_type", MdtrtCertType.RESIDENT_IDENTITY_CARD.getCode());
  163. adminfo.put("med_type", siPatInfo.getMedType());
  164. adminfo.put("ipt_otp_no", p.getInpatientNo());
  165. adminfo.put("atddr_no", p.getReferPhysician());
  166. adminfo.put("chfpdr_name", p.getReferPhysicianName());
  167. adminfo.put("adm_diag_dscr", admDiseinfos.get(0).getDiagCode());
  168. adminfo.put("adm_dept_codg", p.getSmallDept());
  169. adminfo.put("adm_dept_name", p.getSmallDeptName());
  170. adminfo.put("adm_bed", p.getBedNo());
  171. adminfo.put("dscg_maindiag_code", admDiseinfos.get(0).getDiagCode());
  172. adminfo.put("dscg_maindiag_name", admDiseinfos.get(0).getDiagName());
  173. input.getJSONObject("input").put("adminfo", adminfo);
  174. String diseRef = JSONArray.toJSONStringWithDateFormat(admDiseinfos, "yyyy-MM-dd HH:mm:ss");
  175. input.getJSONObject("input").put("diseinfo", JSONArray.parse(diseRef));
  176. JSONObject result = exec.executeTrade(input, SiFunction.MODIFY_ADMISSION_INFO);
  177. log.info("【操作员:{}】,入院信息变更:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result);
  178. Integer infcode = result.getInteger(RESULT_CODE);
  179. logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode));
  180. return SiUtil.makeReturnWithoutOutput(result, "变更住院信息成功。");
  181. }
  182. public ResultVo<String> revokeAdmission(ZyPatientInfo p) {
  183. SiPatInfo siPatInfo = qrydao.selectSiPatInfoForZy(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  184. if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getMdtrtId())) {
  185. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保在院信息!");
  186. }
  187. revokeUploadFees(p);
  188. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_ADMISSION, siPatInfo.getInsuplcAdmdvs());
  189. JSONObject data = new JSONObject();
  190. data.put("mdtrt_id", siPatInfo.getMdtrtId());
  191. data.put("psn_no", siPatInfo.getPsnNo());
  192. input.getJSONObject("input").put("data", data);
  193. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_ADMISSION);
  194. log.info("【操作员:{}】,取消入院登记:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result);
  195. if (null == result) {
  196. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  197. }
  198. Integer infcode = result.getInteger(RESULT_CODE);
  199. logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode));
  200. if (null == infcode) {
  201. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  202. }
  203. if (infcode == 0) {
  204. qrydao.clearMdtrtIdForZy(siPatInfo.getPatNo(), siPatInfo.getTimes(), siPatInfo.getLedgerSn(), null);
  205. dao.updateResponceType("01", null, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  206. return ResultVoUtil.success("取消入院登记成功。");
  207. }
  208. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  209. }
  210. public ResultVo<String> uploadFeeDetail(Overview o) {
  211. o.setStaffId(TokenUtil.getTokenUserId());
  212. return zySrvc.uploadFeeDetail(o);
  213. }
  214. public ResultVo<String> hospitalizationPreSettlement(ZyPatientInfo p) {
  215. PreSetlmt preSetlmt = dao.selectPreSetlmt(p.getInpatientNo(), p.getAdmissTimes(),
  216. p.getLedgerSn(), DateUtil.getTodayEndTime());
  217. if (null == preSetlmt || StringUtil.isBlank(preSetlmt.getMdtrtId())) {
  218. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保在院信息!");
  219. }
  220. preSetlmt.setMdtrtCertType(MdtrtCertType.RESIDENT_IDENTITY_CARD.getCode());
  221. preSetlmt.setPsnSetlway(p.getDbg() ? PsnSetlWay.SETTLE_BY_QUOTA.getCode() : PsnSetlWay.SETTLE_BY_ITEMS.getCode());
  222. preSetlmt.setAcctUsedFlag(YesOrNo.NO.getCodeStr());
  223. preSetlmt.setMidSetlFlag(YesOrNo.NO.getCodeStr());
  224. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.HOSPITALIZATION_PRE_SETTLEMENT, preSetlmt.getInsuplcAdmdvs());
  225. String ref = JSONObject.toJSONString(preSetlmt);
  226. input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
  227. JSONObject result = exec.executeTrade(input, SiFunction.HOSPITALIZATION_PRE_SETTLEMENT);
  228. log.info("预结算:\n参数:{},\n结果:{}", input, result);
  229. if (null == result) {
  230. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  231. }
  232. Integer infcode = result.getInteger(RESULT_CODE);
  233. logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode));
  234. if (null == infcode) {
  235. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  236. }
  237. if (infcode == 0) {
  238. JSONObject setlinfo = result.getJSONObject(OUTPUT).getJSONObject("setlinfo");
  239. String fundPay = setlinfo.getString("fund_pay_sumamt");
  240. dao.updateFundPay(fundPay, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  241. String message = "患者【" + p.getName() + "】院内总费用与医保中心总费用一致,医保报销金额为:¥ " + fundPay + "。";
  242. return ResultVoUtil.success(message);
  243. }
  244. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  245. }
  246. public ResultVo<String> revokeUploadFees(ZyPatientInfo p) {
  247. SiPatInfo siPatInfo = qrydao.selectSiPatInfoForZy(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  248. if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getMdtrtId())) {
  249. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保在院信息!");
  250. }
  251. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_HOSPITALIZATION_FEE_DETAILS, siPatInfo.getInsuplcAdmdvs());
  252. JSONArray data = new JSONArray();
  253. if (null == p.getDetailSns() || p.getDetailSns().isEmpty()) {
  254. JSONObject item = new JSONObject();
  255. item.put("feedetl_sn", "0000");
  256. item.put("mdtrt_id", siPatInfo.getMdtrtId());
  257. item.put("psn_no", siPatInfo.getPsnNo());
  258. data.add(item);
  259. } else {
  260. p.getDetailSns().forEach(detailSn -> {
  261. JSONObject item = new JSONObject();
  262. item.put("feedetl_sn", detailSn);
  263. item.put("mdtrt_id", siPatInfo.getMdtrtId());
  264. item.put("psn_no", siPatInfo.getPsnNo());
  265. data.add(item);
  266. });
  267. }
  268. input.getJSONObject("input").put("data", data);
  269. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_HOSPITALIZATION_FEE_DETAILS);
  270. log.info("【操作员:{}】撤销已上传的费用:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), input, result);
  271. if (null == result) {
  272. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  273. }
  274. Integer infcode = result.getInteger(RESULT_CODE);
  275. logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode));
  276. if (null == infcode) {
  277. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  278. }
  279. if (infcode == 0) {
  280. QueryWrapper<SiChargeTemp> wrapper = new QueryWrapper<>();
  281. wrapper.eq("pat_no", p.getInpatientNo());
  282. wrapper.eq("times", p.getAdmissTimes());
  283. wrapper.eq("ledger_sn", p.getLedgerSn());
  284. if (null == p.getDetailSns() || p.getDetailSns().isEmpty()) {
  285. dao.revokeAllUploadFee(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  286. chrgtmpdao.delete(wrapper);
  287. } else {
  288. wrapper.in("detail_sn", p.getDetailSns());
  289. chrgtmpdao.delete(wrapper);
  290. dao.revokePartUploadFee(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), p.getDetailSns());
  291. }
  292. return ResultVoUtil.success("撤销费用上传成功。");
  293. }
  294. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  295. }
  296. public ResultVo<String> dischargeProcessing(ZyPatientInfo p) {
  297. if (null == p.getLedgerSn()) {
  298. p.setLedgerSn(dao.selectMaxLedgerSn(p.getInpatientNo(), p.getAdmissTimes()));
  299. }
  300. Dscginfo dscginfo;
  301. if (p.getMidSetl()) {
  302. dscginfo = dao.selectDscginfoForMidSetl(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  303. dscginfo.setEndtime(p.getZjdzDatetime());
  304. } else {
  305. dscginfo = dao.selectDscginfo(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  306. }
  307. if (null == dscginfo || StringUtil.isBlank(dscginfo.getMdtrtId())) {
  308. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保在院信息!");
  309. }
  310. List<SetlDiseinfo> setlDises = dao.selectSetlDises(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  311. if (null == setlDises || setlDises.isEmpty()) {
  312. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "患者医保出院诊断不能为空,请联系医生在病案首页填写。");
  313. }
  314. if (null == p.getStaffId()) {
  315. p.setStaffId(TokenUtil.getTokenUserId());
  316. }
  317. dscginfo.setDiseCodg(setlDises.get(0).getDiagCode());
  318. dscginfo.setDiseName(setlDises.get(0).getDiagName());
  319. dscginfo.setDscgWay(DscgWay.ORDER_TO_LEAVE_HOSPITAL.getCode());
  320. String ref = JSONObject.toJSONStringWithDateFormat(dscginfo, "yyyy-MM-dd HH:mm:ss");
  321. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.DISCHARGE_PROCESSING,
  322. dao.selectAdmdvs(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn()));
  323. input.getJSONObject("input").put("dscginfo", JSONObject.parseObject(ref));
  324. String diseRef = JSONArray.toJSONStringWithDateFormat(setlDises, "yyyy-MM-dd HH:mm:ss");
  325. input.getJSONObject("input").put("diseinfo", JSONArray.parse(diseRef));
  326. JSONObject result = exec.executeTrade(input, SiFunction.DISCHARGE_PROCESSING);
  327. log.info("【操作员:{}】,医保出院办理:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
  328. if (null == result) {
  329. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  330. }
  331. Integer infcode = result.getInteger(RESULT_CODE);
  332. logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode));
  333. if (null == infcode) {
  334. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  335. }
  336. if (infcode == 0) {
  337. p.setDismissDate(dscginfo.getEndtime());
  338. return inpatientSettlement(p);
  339. }
  340. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  341. }
  342. public ResultVo<String> revokeDischarge(ZyPatientInfo p) {
  343. SiPatInfo siPatInfo = qrydao.selectSiPatInfoForZy(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  344. if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getMdtrtId())) {
  345. transbackTables(p);
  346. return ResultVoUtil.success("取消出院办理成功。");
  347. }
  348. p.setMdtrtId(siPatInfo.getMdtrtId());
  349. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_DISCHARGE, siPatInfo.getInsuplcAdmdvs());
  350. JSONObject data = new JSONObject();
  351. data.put("mdtrt_id", siPatInfo.getMdtrtId());
  352. data.put("psn_no", siPatInfo.getPsnNo());
  353. input.getJSONObject("input").put("data", data);
  354. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_DISCHARGE);
  355. log.info("【操作员:{}】取消医保出院办理:\n参数:{},\n结果:{}", TokenUtil.getTokenUserId(), 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.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode));
  361. if (null == infcode) {
  362. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  363. }
  364. if (infcode == 0) {
  365. dao.deleteSetlInfo(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  366. dao.deleteSetlDetail(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  367. transbackTables(p);
  368. return ResultVoUtil.success("取消医保出院办理成功。");
  369. }
  370. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  371. }
  372. private ResultVo<String> inpatientSettlement(ZyPatientInfo p) {
  373. PreSetlmt preSetlmt;
  374. Date begntime = dismissService.getBegntime(p.getInpatientNo(), p.getAdmissTimes(), "zy_actpatient");
  375. if (p.getMidSetl()) {
  376. preSetlmt = dao.selectPreSetlmtForMidSetl(p.getInpatientNo(), p.getAdmissTimes(),
  377. p.getLedgerSn(), begntime, p.getZjdzDatetime());
  378. } else {
  379. preSetlmt = dao.selectPreSetlmt(p.getInpatientNo(), p.getAdmissTimes(),
  380. p.getLedgerSn(), "2999-12-31 23:59:59.999");
  381. }
  382. if (null == preSetlmt || StringUtil.isBlank(preSetlmt.getMdtrtId())) {
  383. revokeDischarge(p);
  384. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "此患者没有有效的医保在院信息!");
  385. }
  386. ReadCardBizType readCardBizType = ReadCardBizType.get(p.getReadCardBizType());
  387. if (readCardBizType == ReadCardBizType.SETTLEMENT) {
  388. MdtrtCertType mdtrtCertType = MdtrtCertType.getByLabel(p.getMdtrtCertType());
  389. preSetlmt.setMdtrtCertType(mdtrtCertType.getCode());
  390. if (mdtrtCertType == MdtrtCertType.SOCIAL_SECURITY_CARD) {
  391. String[] out = p.getReadCardResult().split("\\|");
  392. if (!p.getName().trim().equals(out[4].trim())) {
  393. revokeDischarge(p);
  394. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "社保卡姓名与HIS姓名不一致,请确认是否人证相符。");
  395. }
  396. preSetlmt.setMdtrtCertNo(out[2]);
  397. preSetlmt.setCardSn(out[3]);
  398. } else if (mdtrtCertType == MdtrtCertType.MEDICAL_INSURANCE_ELECTRONIC_VOUCHER) {
  399. JSONObject qrinfo = JSONObject.parseObject(p.getReadCardResult());
  400. if (!p.getName().trim().equals(qrinfo.getString("userName").trim())) {
  401. revokeDischarge(p);
  402. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "电子医保凭证姓名与HIS姓名不一致,请确认是否人证相符。");
  403. }
  404. if (!p.getSocialNo().trim().equals(qrinfo.getString("idNo").trim())) {
  405. revokeDischarge(p);
  406. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "电子医保凭证身份证与HIS身份证不一致,请确认是否人证相符。");
  407. }
  408. preSetlmt.setMdtrtCertNo(qrinfo.getString("ecToken"));
  409. }
  410. }
  411. preSetlmt.setPsnSetlway(p.getDbg() ? PsnSetlWay.SETTLE_BY_QUOTA.getCode() : PsnSetlWay.SETTLE_BY_ITEMS.getCode());
  412. preSetlmt.setAcctUsedFlag(YesOrNo.NO.getCodeStr());
  413. preSetlmt.setMidSetlFlag(YesOrNo.NO.getCodeStr());
  414. preSetlmt.setInvono(SnowFlakeId.instance().nextId());
  415. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.HOSPITALIZATION_SETTLEMENT, preSetlmt.getInsuplcAdmdvs());
  416. String ref = JSONObject.toJSONString(preSetlmt);
  417. input.getJSONObject("input").put("data", JSONObject.parseObject(ref));
  418. JSONObject result = exec.executeTrade(input, SiFunction.HOSPITALIZATION_SETTLEMENT);
  419. log.info("【操作员:{}】,医保出院结算:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
  420. if (null == result) {
  421. revokeDischarge(p);
  422. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  423. }
  424. Integer infcode = result.getInteger(RESULT_CODE);
  425. logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode));
  426. if (null == infcode) {
  427. revokeDischarge(p);
  428. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "医保中心报错:" + result.getString("message"));
  429. }
  430. if (infcode == 0) {
  431. setlListUpldService.yiBaoZhiFuFangShi(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  432. JSONObject setlinfo = result.getJSONObject(OUTPUT).getJSONObject("setlinfo");
  433. SiSetlinfo setlEntity = JSONObject.parseObject(setlinfo.toJSONString(), SiSetlinfo.class);
  434. setlEntity.setPatNo(p.getInpatientNo());
  435. setlEntity.setTimes(p.getAdmissTimes());
  436. setlEntity.setLedgerSn(p.getLedgerSn());
  437. setlEntity.setStaffId(TokenUtil.getTokenUserId());
  438. setlEntity.setRevoked(YesOrNo.NO.getCode());
  439. setlEntity.setSetlType(ClrType.INPATIENT.getCode());
  440. setlEntity.setInsuplcAdmdvs(preSetlmt.getInsuplcAdmdvs());
  441. setlEntity.setBegntime(begntime);
  442. setlEntity.setEndtime(p.getDismissDate());
  443. setlinfodao.insert(setlEntity);
  444. setlinfodao.updateSiZyInfoSetlId(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(),
  445. setlEntity.getSetlId(), setlEntity.getMedinsSetlId(), input.getString("msgid"));
  446. setlinfodao.updateApplySettled(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), YesOrNo.YES.getCode());
  447. JSONArray setldetail = result.getJSONObject(OUTPUT).getJSONArray("setldetail");
  448. for (int i = 0; i < setldetail.size(); i++) {
  449. SiSetldetail setldetailEntity = JSONObject.parseObject(setldetail.getJSONObject(i).toJSONString(), SiSetldetail.class);
  450. setldetailEntity.setPatNo(p.getInpatientNo());
  451. setldetailEntity.setTimes(p.getAdmissTimes());
  452. setldetailEntity.setLedgerSn(p.getLedgerSn());
  453. setldetldao.insert(setldetailEntity);
  454. }
  455. YbSettleFee settleFee = new YbSettleFee();
  456. if (p.getMidSetl()) {
  457. settleFee.setZjdzDatetime(p.getZjdzDatetime());
  458. }
  459. settleFee.setMidSetl(p.getMidSetl());
  460. settleFee.setStaffId(TokenUtil.getTokenUserId());
  461. settleFee.setInpatientNo(p.getInpatientNo());
  462. settleFee.setAdmissTimes(p.getAdmissTimes());
  463. settleFee.setBedNo(p.getBedNo());
  464. settleFee.setWardCode(p.getAdmissWard());
  465. settleFee.setDeptCode(p.getAdmissDept());
  466. settleFee.setTotalCost(setlinfo.getString("medfee_sumamt"));
  467. settleFee.setTczf(setlinfo.getString("fund_pay_sumamt"));
  468. settleFee.setHospitalPay(setlinfo.getString("hosp_part_amt"));
  469. settleFee.setGrzhzf(setlinfo.getString("acct_pay"));
  470. settleFee.setDbzf(setlinfo.getString("hifmi_pay"));
  471. settleFee.setXjzf(setlinfo.getString("psn_cash_pay"));
  472. settleFee.setTable(p.getTable());
  473. return dismissService.doDismiss(settleFee);
  474. }
  475. revokeDischarge(p);
  476. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, result.getString(ERROR_MESSAGE));
  477. }
  478. public ResultVo<String> revokeSettlement(ZyPatientInfo p) {
  479. SiPatInfo siPatInfo = qrydao.selectSiPatInfoForZy(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  480. if (null == p.getStaffId()) {
  481. p.setStaffId(TokenUtil.getTokenUserId());
  482. }
  483. if (null == p.getLedgerSn()) {
  484. p.setLedgerSn(dao.selectMaxLedgerSn(p.getInpatientNo(), p.getAdmissTimes()));
  485. }
  486. if (null == siPatInfo || StringUtil.isBlank(siPatInfo.getSetlId())) {
  487. return revokeDischarge(p);
  488. }
  489. p.setMdtrtId(siPatInfo.getMdtrtId());
  490. JSONObject input = exec.makeTradeHeaderWithInsureArea(SiFunction.REVOKE_HOSPITALIZATION_SETTLEMENT, siPatInfo.getInsuplcAdmdvs());
  491. JSONObject data = new JSONObject();
  492. data.put("mdtrt_id", siPatInfo.getMdtrtId());
  493. data.put("setl_id", siPatInfo.getSetlId());
  494. data.put("psn_no", siPatInfo.getPsnNo());
  495. input.getJSONObject("input").put("data", data);
  496. JSONObject result = exec.executeTrade(input, SiFunction.REVOKE_HOSPITALIZATION_SETTLEMENT);
  497. log.info("【操作员:{}】取消医保结算:\n参数:{},\n结果:{}", p.getStaffId(), input, result);
  498. if (null == result) {
  499. return ResultVoUtil.fail(ExceptionEnum.NETWORK_ERROR);
  500. }
  501. Integer infcode = result.getInteger(RESULT_CODE);
  502. logDao.insert(new SiLog(input, result, p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), infcode));
  503. if (null == infcode) {
  504. return revokeDischarge(p);
  505. }
  506. if (infcode == 0) {
  507. dao.updateRvkSetlMsgid(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), input.getString("msgid"));
  508. setlinfodao.updateApplySettled(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn(), YesOrNo.NO.getCode());
  509. transbackTables(p);
  510. }
  511. return revokeDischarge(p);
  512. }
  513. private void transbackTables(ZyPatientInfo p) {
  514. dao.deleteSiSetlFeeDetl(p.getMdtrtId());
  515. dao.deleteZyLedgerFileYb(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  516. dao.deleteZyReceipt(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  517. dao.updateSettleType(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  518. dao.updateChargeStatus(p.getInpatientNo(), p.getAdmissTimes(), p.getLedgerSn());
  519. }
  520. }