DismissService.java 28 KB

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