DismissService.java 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. package thyyxxk.webserver.service.inpatient;
  2. import cn.hutool.core.util.StrUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import lombok.RequiredArgsConstructor;
  6. import lombok.extern.slf4j.Slf4j;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.http.client.SimpleClientHttpRequestFactory;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.transaction.annotation.Transactional;
  12. import org.springframework.web.client.RestTemplate;
  13. import thyyxxk.webserver.config.exception.BizException;
  14. import thyyxxk.webserver.config.exception.ExceptionEnum;
  15. import thyyxxk.webserver.constants.Capacity;
  16. import thyyxxk.webserver.constants.sidicts.MedType;
  17. import thyyxxk.webserver.constants.sidicts.PsnCertType;
  18. import thyyxxk.webserver.dao.his.inpatient.DismissDao;
  19. import thyyxxk.webserver.entity.ResultVo;
  20. import thyyxxk.webserver.entity.inpatient.dismiss.*;
  21. import thyyxxk.webserver.entity.inpatient.patient.Overview;
  22. import thyyxxk.webserver.entity.inpatient.patient.Patient;
  23. import thyyxxk.webserver.entity.medicalinsurance.injury.InjuryCareQualification;
  24. import thyyxxk.webserver.http.drg.DrgWebApi;
  25. import thyyxxk.webserver.service.externalhttp.SiInjuryFeeUpld;
  26. import thyyxxk.webserver.service.externalhttp.SiZySrvc;
  27. import thyyxxk.webserver.utils.*;
  28. import java.math.BigDecimal;
  29. import java.util.*;
  30. /**
  31. * @author dj
  32. */
  33. @Slf4j
  34. @Service
  35. @RequiredArgsConstructor
  36. public class DismissService {
  37. private final DismissDao dao;
  38. private final SiZySrvc zySrvc;
  39. private final SiInjuryFeeUpld injuryFeeUpld;
  40. private final RestTemplate template;
  41. private final DrgWebApi drgWebApi;
  42. @Value("${si-zy-fee-url}")
  43. private String siZyFeeUrl;
  44. @Value("${si-injury-fee-url}")
  45. private String siInjuryFeeUrl;
  46. @Value("${rmHkUserApi}")
  47. private String rmHkUserApi;
  48. public ResultVo<JSONArray> powersiPreDischarge(String visitId) {
  49. return drgWebApi.powersiPreDischarge(visitId);
  50. }
  51. public ResultVo<Object> dismiss(Patient param) {
  52. ResultVo<List<ActOrderGroup>> actOrders = getActOrders(param.getInpatientNo(), param.getAdmissTimes());
  53. if (!Objects.equals(actOrders.getCode(), ExceptionEnum.SUCCESS.getCode())) {
  54. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, actOrders.getMessage());
  55. }
  56. if (!actOrders.getData().isEmpty()) {
  57. return ResultVoUtil.fail(ExceptionEnum.ABNORMAL_YZ_ACT_ORDER, actOrders.getData());
  58. }
  59. ResultVo<List<IllegalFee>> calculate = calculateForDismiss(param);
  60. if (Objects.equals(calculate.getCode(), ExceptionEnum.EXIST_NEGATIVE_FEES.getCode())) {
  61. return ResultVoUtil.fail(ExceptionEnum.EXIST_NEGATIVE_FEES, calculate.getData());
  62. }
  63. if (!Objects.equals(calculate.getCode(), ExceptionEnum.SUCCESS.getCode())) {
  64. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, calculate.getMessage());
  65. }
  66. return ResultVoUtil.success();
  67. }
  68. public ResultVo<List<ActOrderGroup>> getActOrders(String patNo, Integer times) {
  69. final Integer ledgerSn = dao.getLedgerSn(patNo, times);
  70. if (ledgerSn < 1) {
  71. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "获取帐页数失败。");
  72. }
  73. dao.acceptFeeStepOne(patNo, times);
  74. if (dao.acceptFeeStepTwo(patNo, times) < 0) {
  75. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "接收费用失败。");
  76. }
  77. if (dao.getOrderList(patNo, times) < 0) {
  78. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "查询医嘱失败。");
  79. }
  80. List<ActOrderDetail> allOrders = dao.getActOrderDetail();
  81. if (allOrders == null) {
  82. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "查询医嘱失败。");
  83. }
  84. return analyzeActOrders(allOrders);
  85. }
  86. private ResultVo<List<ActOrderGroup>> analyzeActOrders(List<ActOrderDetail> allOrders) {
  87. HashMap<String, ActOrderGroup> temp = new HashMap<>(Capacity.DEFAULT);
  88. for (ActOrderDetail item : allOrders) {
  89. String actOrderNo = item.getActOrderNo();
  90. String fee = item.getChargeFee();
  91. if (!temp.containsKey(actOrderNo)) {
  92. ActOrderGroup aModel = new ActOrderGroup(actOrderNo, fee, item.getChargeStatus(), item.getCxFlag());
  93. if (aModel.getList() == null) {
  94. aModel.setList(new ArrayList<>());
  95. }
  96. aModel.getList().add(item);
  97. temp.put(actOrderNo, aModel);
  98. } else {
  99. temp.get(actOrderNo).setFee(DecimalUtil.add(fee, temp.get(actOrderNo).getFee()));
  100. temp.get(actOrderNo).getList().add(item);
  101. }
  102. }
  103. List<ActOrderGroup> list = new ArrayList<>();
  104. for (HashMap.Entry<String, ActOrderGroup> modelEntry : temp.entrySet()) {
  105. list.add(modelEntry.getValue());
  106. }
  107. return ResultVoUtil.success(list);
  108. }
  109. public ResultVo<List<IllegalFee>> calculateForDismiss(Patient param) {
  110. final String patNo = param.getInpatientNo();
  111. final Integer times = param.getAdmissTimes();
  112. if (!param.getMidSetl()) {
  113. Integer disActOrderCount = dao.countDisActOrders(patNo, times);
  114. if (null == disActOrderCount || disActOrderCount == 0) {
  115. disActOrderCount = dao.countDisActOrders2(patNo, times);
  116. if (null == disActOrderCount) {
  117. disActOrderCount = 0;
  118. }
  119. }
  120. if (disActOrderCount < 1) {
  121. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者没有出院医嘱,请检查。");
  122. }
  123. if (disActOrderCount > 1) {
  124. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者的出院医嘱不止一条,请检查。");
  125. }
  126. }
  127. if (StringUtil.isBlank(param.getDutyNurse())) {
  128. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "患者责任护士不能为空,请前往入院登记页面填写并保存。");
  129. }
  130. if (StringUtil.isBlank(param.getCountry())) {
  131. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "患者国籍不能为空,请前往入院登记页面填写并保存。");
  132. }
  133. if (StringUtil.isBlank(param.getNation())) {
  134. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "患者民族不能为空,请前往入院登记页面填写并保存。");
  135. }
  136. if (StringUtil.isBlank(param.getContactName())) {
  137. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "患者联系人不能为空,请前往入院登记页面填写并保存。");
  138. }
  139. if (StringUtil.isBlank(param.getContactRelation())) {
  140. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "患者联系人关系不能为空,请前往入院登记页面填写并保存。");
  141. }
  142. if (StringUtil.isBlank(param.getContactAddrName())) {
  143. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "患者联系人地址不能为空,请前往入院登记页面填写并保存。");
  144. }
  145. if (StringUtil.isBlank(param.getContactPhone())) {
  146. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "患者联系人电话不能为空,请前往入院登记页面填写并保存。");
  147. }
  148. if (null == param.getAdmissDate()) {
  149. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "没有找到入院时间,请重新获取病人信息。");
  150. }
  151. if (StringUtil.isBlank(param.getPsnCertType())) {
  152. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "患者证件类型不能为空,请前往入院登记页面补充并保存。");
  153. }
  154. if (param.getPsnCertType().equals(PsnCertType.RESIDENT_IDENTITY_CARD.getCode()) &&
  155. !IdCardUtil.isValidatedIdCard(param.getSocialNo())) {
  156. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "身份证不合法!");
  157. }
  158. Integer age = param.getAge();
  159. if (param.getPsnCertType().equals(PsnCertType.RESIDENT_IDENTITY_CARD.getCode())) {
  160. age = IdCardUtil.getAgeByIdCard(param.getSocialNo());
  161. }
  162. if (null != age && (age < 16 || age > 60)) {
  163. if (param.getName().equals(param.getContactName()) ||
  164. param.getContactRelation().equals("0")) {
  165. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "16岁以下或60岁以上的患者,联系人不能填写本人。");
  166. }
  167. }
  168. Date endtime = getEndtime(param.getMidSetl(), patNo, times, param.getZjdzDatetime());
  169. Date tmpendtime = param.getMidSetl() ? endtime : DateUtil.parse("2999-12-31 23:59:59");
  170. final int ledgerSn = dao.getLedgerSn(patNo, times);
  171. if (dao.hasUnsettledStepOne(patNo, times, ledgerSn) < 0) {
  172. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "获取记账信息失败。");
  173. }
  174. if (dao.hasUnsettledStepTwo(patNo, times) > 0) {
  175. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者有未结算的记账金额,请联系收费窗口。");
  176. }
  177. if (dao.hasNotAccounted(patNo, times) > 0) {
  178. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者有未上账金额。");
  179. }
  180. List<String> unreceivedDrug = dao.hasUnreceivedDrugList(patNo, times);
  181. if (ListUtil.notBlank(unreceivedDrug)) {
  182. throw new BizException(ExceptionEnum.LOGICAL_HTML_ERROR, dischargeErrorMessage(unreceivedDrug));
  183. }
  184. if (dao.hasUnreceivedFees(patNo, times, tmpendtime) > 0) {
  185. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者有未接收的费用。");
  186. }
  187. if (dao.hasUnSubmitDrugList(patNo, times) > 0) {
  188. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者有未提交的药单。");
  189. }
  190. if (dao.hasUnDeliverHerbal(patNo, times) > 0) {
  191. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者有未发药的中草药/颗粒剂。");
  192. }
  193. List<IllegalFee> unhandledList = dao.hasUntreatedDrugOrder(patNo, times);
  194. if (!unhandledList.isEmpty()) {
  195. return ResultVoUtil.fail(ExceptionEnum.EXIST_UNHANDLED_DRUG_ORDER, unhandledList);
  196. }
  197. List<IllegalFee> refundDrugs = dao.selectPageNoTys(patNo, times);
  198. if (!refundDrugs.isEmpty()) {
  199. for (IllegalFee drug : refundDrugs) {
  200. if (drug.getPageNoTy().equals("-1")) {
  201. drug.setPageNoTy("未提交");
  202. drug.setRemark(StrUtil.format("有【{}】,退药单未生成,请去入院登记的【生成退药单】中生成退药单。",
  203. DateUtil.formatDatetime(drug.getYpChargeDate(), "yyyy-MM-dd HH:mm")));
  204. } else {
  205. drug.setRemark("请联系药房处理,如果需要退费请药房点击同意,不需要退费请点击拒绝。");
  206. }
  207. }
  208. return ResultVoUtil.fail(ExceptionEnum.EXIST_UNHANDLED_REFUND_DRUG, refundDrugs);
  209. }
  210. if (dao.hasUnconfirmedMedicalTech(patNo, times, tmpendtime) > 0) {
  211. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者有未确认的医技。");
  212. }
  213. List<IllegalFee> feeNegativeList = dao.feeOrderNegative(patNo, times);
  214. if (!feeNegativeList.isEmpty()) {
  215. return ResultVoUtil.fail(ExceptionEnum.EXIST_NEGATIVE_FEES, feeNegativeList);
  216. }
  217. if (dao.hasSettled(patNo, times, ledgerSn) != 0) {
  218. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者已存在结算信息。");
  219. }
  220. if (dao.hasUncheckedFee(patNo, times, ledgerSn) > 0) {
  221. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者有未结账的费用。");
  222. }
  223. Date begntime = getBegntime(patNo, times, ledgerSn, "zy_actpatient");
  224. dismissFeeAnalyse(patNo, times, ledgerSn, begntime, tmpendtime);
  225. BigDecimal feeOffset = dao.getFeeOffset(patNo, times, ledgerSn);
  226. if (feeOffset.compareTo(BigDecimal.ZERO) != 0) {
  227. BigDecimal beforeAdmFee = dao.selectOverTimeLimitFee(patNo, times, ledgerSn, "<=", begntime);
  228. if (beforeAdmFee.compareTo(BigDecimal.ZERO) != 0) {
  229. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者明细费用与账页费用不一致,有【" + beforeAdmFee + "元】小于入院时间的费用。");
  230. }
  231. if (!param.getMidSetl()) {
  232. BigDecimal afterDisFee = dao.selectOverTimeLimitFee(patNo, times, ledgerSn, ">", tmpendtime);
  233. if (afterDisFee.compareTo(BigDecimal.ZERO) != 0) {
  234. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者明细费用与账页费用不一致,有【" + afterDisFee + "元】大于出院时间的费用。");
  235. }
  236. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者明细费用与账页费用不一致,请确认所有医嘱、药单等都已接收。");
  237. }
  238. }
  239. if (dao.selectReceiptCount(patNo, times, ledgerSn) > 0) {
  240. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者有未冲销的发票,请联系财务进行冲销。");
  241. }
  242. dao.deleteTemporaryTable(patNo, times);
  243. BriefMdtrtInfo medinfo = dao.selectMdtrtId(patNo, times, ledgerSn);
  244. if (null == medinfo) {
  245. medinfo = new BriefMdtrtInfo();
  246. }
  247. if (StringUtil.notBlank(medinfo.getMdtrtId())) {
  248. if (dao.hasDismissDiag(patNo, times) == 0) {
  249. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "此患者没有医保出院诊断。");
  250. }
  251. if (null != param.getDeathFlag() && param.getDeathFlag()) {
  252. return ResultVoUtil.success();
  253. }
  254. Integer deathOrderCount = dao.selectDeathOrderCount(patNo, times);
  255. if (null != deathOrderCount && deathOrderCount > 0) {
  256. return ResultVoUtil.success();
  257. }
  258. Overview o = new Overview();
  259. o.setInpatientNo(patNo);
  260. o.setAdmissTimes(times);
  261. o.setLedgerSn(ledgerSn);
  262. o.setStaffId(param.getStaffId());
  263. o.setSid(param.getSid());
  264. o.setMidSetl(param.getMidSetl());
  265. if (o.getMidSetl()) {
  266. o.setBegntime(begntime);
  267. o.setEndtime(tmpendtime);
  268. }
  269. ResultVo<String> feeCheck = zySrvc.uploadFeeDetail(siZyFeeUrl, o);
  270. if (!Objects.equals(feeCheck.getCode(), ExceptionEnum.SUCCESS.getCode())) {
  271. throw new BizException(ExceptionEnum.LOGICAL_ERROR, feeCheck.getMessage());
  272. }
  273. }
  274. return ResultVoUtil.success();
  275. }
  276. private String dischargeErrorMessage(List<String> messages) {
  277. StringBuilder str = new StringBuilder("此患者有未接收的药单" + "<br />");
  278. for (String message : messages) {
  279. str.append(message).append("<br>");
  280. }
  281. return str.toString();
  282. }
  283. @Transactional(rollbackFor = Exception.class)
  284. public String executeDischarging(MedinsSettleFee settleFee) throws BizException {
  285. updateHicNo(settleFee.getInpatientNo());
  286. settleFee.setLedgerSn(dao.getLedgerSn(settleFee.getInpatientNo(), settleFee.getAdmissTimes()));
  287. BriefMdtrtInfo medinfo = dao.selectMdtrtId(settleFee.getInpatientNo(), settleFee.getAdmissTimes(), settleFee.getLedgerSn());
  288. if (null == medinfo) {
  289. medinfo = new BriefMdtrtInfo();
  290. }
  291. // 医保病人进行医保结算
  292. if (StringUtil.notBlank(medinfo.getMdtrtId())) {
  293. dao.deleteZyLedgerFileYb(settleFee.getInpatientNo(), settleFee.getAdmissTimes(), settleFee.getLedgerSn());
  294. // 如果医院支付为正,则 现金支付 = 现金支付 + 医院支付
  295. if (!settleFee.getHospitalPay().startsWith("-")) {
  296. settleFee.setXjzf(DecimalUtil.add(settleFee.getXjzf(), settleFee.getHospitalPay()));
  297. }
  298. if (dao.insertZyLedgerFileYb(settleFee) < 1) {
  299. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "写医保结算表失败。");
  300. }
  301. }
  302. if (writeReceiptTable(settleFee) < 1) {
  303. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "写发票表失败。");
  304. }
  305. if (updateCostStatusToSettled(settleFee, StringUtil.notBlank(medinfo.getMdtrtId())) < 1) {
  306. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "更新费用状态失败。");
  307. }
  308. int code = settleFee.getMidSetl() ? hisMiddleSettle(settleFee) : setHisStatusOut(settleFee);
  309. if (code < 1) {
  310. throw new BizException(ExceptionEnum.LOGICAL_ERROR, "更新住院状态失败。" + code);
  311. }
  312. new DestructionUser(settleFee.getInpatientNo(), settleFee.getAdmissTimes()).start();
  313. return "OK";
  314. }
  315. class DestructionUser extends Thread {
  316. private final String patNo;
  317. private final Integer times;
  318. public DestructionUser(String patNo, Integer times) {
  319. this.patNo = patNo;
  320. this.times = times;
  321. }
  322. @Override
  323. public void run() {
  324. try {
  325. JSONObject obj = new JSONObject();
  326. obj.put("patNo", patNo);
  327. obj.put("times", times);
  328. String res = template.postForObject(rmHkUserApi, obj, String.class);
  329. log.info("删除门禁。{}", res);
  330. } catch (Exception e) {
  331. log.error("删除患者门禁出错", e);
  332. }
  333. }
  334. }
  335. private int writeReceiptTable(MedinsSettleFee indata) {
  336. if (dao.beforeWriteReceiptTable(indata.getInpatientNo(), indata.getAdmissTimes(),
  337. indata.getLedgerSn()) < 1) {
  338. return -1;
  339. }
  340. Date admissDate = getBegntime(indata.getInpatientNo(), indata.getAdmissTimes(), indata.getLedgerSn(), indata.getTable());
  341. Date dismissDate = getEndtime(indata.getMidSetl(), indata.getInpatientNo(), indata.getAdmissTimes(),
  342. indata.getZjdzDatetime());
  343. Date tempendtime = indata.getMidSetl() ? dismissDate : DateUtil.parse("2999-12-31 23:59:59");
  344. ReceiptEntity receiptEntity = dismissFeeAnalyse(indata.getInpatientNo(), indata.getAdmissTimes(),
  345. indata.getLedgerSn(), admissDate, tempendtime);
  346. if (indata.getLedgerSn() > 1) {
  347. admissDate = DateUtil.addOneSecond(admissDate);
  348. }
  349. if (dao.writeReceiptTable(indata.getInpatientNo(), indata.getAdmissTimes(), indata.getLedgerSn(), 1,
  350. admissDate, dismissDate, indata.getWardCode(), indata.getDeptCode(), dismissDate, "01",
  351. indata.getStaffId(), receiptEntity.getAdult()) < 1) {
  352. return -1;
  353. }
  354. if (null != receiptEntity.getInfant()) {
  355. return dao.writeReceiptTable(indata.getInpatientNo(), indata.getAdmissTimes(), indata.getLedgerSn(), 4,
  356. admissDate, dismissDate, indata.getWardCode(), indata.getDeptCode(), dismissDate, "04",
  357. indata.getStaffId(), receiptEntity.getInfant());
  358. }
  359. return 1;
  360. }
  361. private ReceiptEntity dismissFeeAnalyse(String patNo, int times, int ledgerSn, Date begntime, Date endtime) {
  362. Map<String, String> ledgerMap = initReceiptMap();
  363. Map<String, String> infantMap = initReceiptMap();
  364. Map<String, String> adultMap = initReceiptMap();
  365. List<ReceiptFee> ledgerFees = dao.selectLedgerFees(patNo, times, ledgerSn, begntime, endtime);
  366. if (null == ledgerFees) {
  367. ledgerFees = new ArrayList<>();
  368. }
  369. for (ReceiptFee fee : ledgerFees) {
  370. ledgerMap.replace(fee.getBillCode(), fee.getLedgerFee());
  371. ledgerMap.replace("total", DecimalUtil.add(ledgerMap.get("total"), fee.getLedgerFee()));
  372. infantMap.replace(fee.getBillCode(), fee.getInfantFee());
  373. infantMap.replace("total", DecimalUtil.add(infantMap.get("total"), fee.getInfantFee()));
  374. adultMap.replace(fee.getBillCode(), fee.getAdultFee());
  375. adultMap.replace("total", DecimalUtil.add(adultMap.get("total"), fee.getAdultFee()));
  376. }
  377. String restype = dao.selectResponceType(patNo);
  378. String deposit = dao.selectDepositSumamt(patNo, times, ledgerSn);
  379. if (StringUtil.isBlank(deposit)) {
  380. deposit = "0";
  381. }
  382. String balance = DecimalUtil.minus(deposit, ledgerMap.get("total"));
  383. dao.updateZyLedgerFileCharges(patNo, times, ledgerSn, deposit, balance, restype, ledgerMap);
  384. ReceiptEntity entity = new ReceiptEntity();
  385. if (DecimalUtil.compare(infantMap.get("total"), "0") == 1) {
  386. String patNo1 = patNo + "$1";
  387. String patNo6 = patNo + "$6";
  388. dao.updateInfantfee(patNo1, patNo6, times, infantMap);
  389. entity.setInfant(infantMap);
  390. }
  391. String totalCost = dao.selectTotalCharge(patNo, times);
  392. dao.updateZyActpatientCharges(patNo, totalCost, balance, ledgerMap);
  393. entity.setAdult(adultMap);
  394. return entity;
  395. }
  396. private Map<String, String> initReceiptMap() {
  397. Map<String, String> map = new HashMap<>();
  398. map.put("total", "0.00");
  399. List<String> billCodes = dao.selectBillCodes();
  400. billCodes.forEach(itm -> map.put(itm, "0.00"));
  401. return map;
  402. }
  403. public Date getBegntime(String patNo, int times, int ledgerSn, String table) {
  404. return ledgerSn > 1 ? dao.selectLastLedgerAccountDate(patNo, times, ledgerSn) :
  405. dao.selectAdmissDate(patNo, times, table);
  406. }
  407. private Date getEndtime(Boolean midsetl, String patNo, Integer times, Date zjdzDate) {
  408. if (midsetl) {
  409. return zjdzDate;
  410. }
  411. Date disdate = dao.selectActOrderDisDate(patNo, times);
  412. if (null == disdate) {
  413. disdate = dao.selectActOrderDisDate2(patNo, times);
  414. }
  415. return disdate;
  416. }
  417. private int updateCostStatusToSettled(MedinsSettleFee settle, boolean medins) {
  418. dao.updateZyDetailCharge(settle.getInpatientNo(), settle.getAdmissTimes(), settle.getLedgerSn());
  419. if (!settle.getMidSetl()) {
  420. settle.setZjdzDatetime(new Date());
  421. }
  422. if (medins) {
  423. int hasInfant = dao.hasInfant(settle.getInpatientNo(), settle.getAdmissTimes());
  424. if (hasInfant == 0) {
  425. return dao.updateCostStatusWithoutInfant(settle);
  426. }
  427. return dao.updateCostStatusWithInfant(settle);
  428. }
  429. return dao.updateZifeiCostStatus(settle);
  430. }
  431. private int setHisStatusOut(MedinsSettleFee settle) {
  432. final Date disDate;
  433. String patNo = settle.getInpatientNo();
  434. int times = settle.getAdmissTimes();
  435. if ("zy_actpatient".equals(settle.getTable())) {
  436. disDate = dao.selectActOrderDisDate(patNo, times);
  437. } else {
  438. disDate = dao.selectActOrderDisDate2(patNo, times);
  439. }
  440. if (dao.updateZyActpatient(patNo, times,
  441. disDate, settle.getStaffId(), settle.getTable()) < 1) {
  442. return -1;
  443. }
  444. if ("zy_actpatient".equals(settle.getTable())) {
  445. dao.deleteZyInactpatient(patNo, times);
  446. if (dao.insertZyInactpatient(patNo, times) < 1) {
  447. return -2;
  448. }
  449. }
  450. final String patNo1 = patNo + "$1";
  451. final String patNo6 = patNo + "$6";
  452. dao.updateZyActpatientAgain(patNo1, patNo6, times, disDate, settle.getTable());
  453. if ("zy_actpatient".equals(settle.getTable())) {
  454. dao.insertZyInactpatientAgain(patNo1, patNo6, times);
  455. }
  456. if (dao.updateZyAdt(patNo, times, settle.getWardCode(), settle.getDeptCode(),
  457. settle.getBedNo(), disDate) < 1) {
  458. return -3;
  459. }
  460. dao.updateZyBedMi(patNo, times);
  461. dao.deleteZyActpatient(patNo);
  462. dao.deleteZyActpatientAgain(patNo1, patNo6, times);
  463. dao.deleteZjdzSettleDeposit(patNo, times);
  464. return insertNewZyWorkLog(settle);
  465. }
  466. private int insertNewZyWorkLog(MedinsSettleFee param) {
  467. final String userName = dao.getOperateName(param.getStaffId());
  468. String logType = param.getMidSetl() ? "zjdz" : "cyjs";
  469. return dao.insertNewZyWorkLog(param.getInpatientNo(), param.getAdmissTimes(), param.getLedgerSn(),
  470. param.getWardCode(), logType, param.getDeptCode(), param.getStaffId(), userName);
  471. }
  472. private int hisMiddleSettle(MedinsSettleFee param) {
  473. String patNo = param.getInpatientNo();
  474. int times = param.getAdmissTimes();
  475. Date onMoreSecond = DateUtil.addOneSecond(param.getZjdzDatetime());
  476. dao.clearMedinsInfo(patNo, times, onMoreSecond);
  477. int currentLedgerSn = dao.getLedgerSn(patNo, times);
  478. param.setLedgerSn(currentLedgerSn);
  479. int newLedgerSn = currentLedgerSn + 1;
  480. dao.updateTimesBilledByIncreaseOne(patNo, times, param.getTable());
  481. dao.insertNewLedgerFile(patNo, times, newLedgerSn);
  482. dao.updateFeesLedgerSn(patNo, times, newLedgerSn, currentLedgerSn, param.getZjdzDatetime());
  483. dao.updateZyLedgerFileTotalCharge(patNo, times, currentLedgerSn);
  484. dao.updateZyLedgerFileTotalCharge(patNo, times, newLedgerSn);
  485. DepositFile depositFile = dao.selectLatestDepositFile(patNo, times);
  486. if (null == depositFile) {
  487. return insertNewZyWorkLog(param);
  488. }
  489. String lastSettleAmount = dao.selectLastLedgerSettle(patNo, times, currentLedgerSn);
  490. if (StringUtil.isBlank(lastSettleAmount)) {
  491. return insertNewZyWorkLog(param);
  492. }
  493. BigDecimal settleDecimal = new BigDecimal(lastSettleAmount);
  494. if (settleDecimal.compareTo(BigDecimal.ZERO) < 1) {
  495. return insertNewZyWorkLog(param);
  496. }
  497. depositFile.setLedgerSn(newLedgerSn);
  498. depositFile.initZjdzSettleDeposit(lastSettleAmount);
  499. dao.insertNewDeposit(depositFile);
  500. return insertNewZyWorkLog(param);
  501. }
  502. private void updateHicNo(String zyh) {
  503. String hicNoNew = dao.getHicNoNew(zyh);
  504. if (null != hicNoNew) {
  505. if (hicNoNew.length() > 16) {
  506. hicNoNew = hicNoNew.substring(0, 16);
  507. }
  508. dao.updateCpy(zyh, hicNoNew);
  509. if (hicNoNew.length() > 10) {
  510. hicNoNew = hicNoNew.substring(0, 10);
  511. }
  512. dao.updateHic(zyh, hicNoNew);
  513. }
  514. }
  515. }