DismissService.java 28 KB

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