DismissService.java 26 KB

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