SiMzFeeService.java 50 KB

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