XiangMuLuRuService.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. package thyyxxk.webserver.service.yibao;
  2. import com.alibaba.fastjson.JSON;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.apache.commons.collections4.ListUtils;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.transaction.annotation.Transactional;
  8. import thyyxxk.webserver.config.exception.ExceptionEnum;
  9. import thyyxxk.webserver.dao.his.yibao.TransferInOfExpensesDao;
  10. import thyyxxk.webserver.dao.his.yibao.XiangMuLuRuDao;
  11. import thyyxxk.webserver.entity.ResultVo;
  12. import thyyxxk.webserver.entity.datamodify.FeiYongLeiXin;
  13. import thyyxxk.webserver.entity.datamodify.GetDropdownBox;
  14. import thyyxxk.webserver.entity.datamodify.ZyDetailCharge;
  15. import thyyxxk.webserver.utils.*;
  16. import java.math.BigDecimal;
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. /**
  22. * <p>
  23. * 描述: 项目录入
  24. * </p>
  25. *
  26. * @author xc
  27. * @date 2021-08-02 10:37
  28. */
  29. @Service
  30. @Slf4j
  31. public class XiangMuLuRuService {
  32. private final XiangMuLuRuDao dao;
  33. private final TransferInOfExpensesDao transferInOfExpensesDao;
  34. @Autowired
  35. public XiangMuLuRuService(XiangMuLuRuDao dao, TransferInOfExpensesDao transferInOfExpensesDao) {
  36. this.dao = dao;
  37. this.transferInOfExpensesDao = transferInOfExpensesDao;
  38. }
  39. /**
  40. * 获取患者费用
  41. *
  42. * @param param 查询条件
  43. * @return 返回患者费用
  44. */
  45. public ResultVo<List<ZyDetailCharge>> getHuanZheFeiYong(ZyDetailCharge param) {
  46. log.info("查询患者费用==》住院号{},住院次数:{},项目名称:{},录入日期:{},费用类型:{}", param.getInpatientNo(), param.getAdmissTimes(),
  47. param.getChargeCode(), param.getStartTime(), param.getOrderNo());
  48. int infantFlag = 0;
  49. // 如果是婴儿的话那么就需要截取 字符串了 在通过婴儿标识来判断
  50. if (param.getInpatientNo().contains("$")) {
  51. param.setInpatientNo(param.getInpatientNo().split("\\$")[0]);
  52. infantFlag = 1;
  53. }
  54. List<ZyDetailCharge> list = dao.getHuanZheFeiYong(param.getInpatientNo(), param.getAdmissTimes(), param.getChargeCode(), infantFlag,
  55. param.getStartTime(), param.getOrderNo());
  56. Map<Integer, Integer> map = new HashMap<>();
  57. for (int i = 0; i < list.size(); i++) {
  58. // 判断一下这一条数据 是否为 退费数据 再把原数据和 本身添加一个退费的 flag
  59. if (map.containsKey(list.get(i).getOriDetailSn())) {
  60. // 把当前的数据 添加一个有 退费标志的
  61. list.get(i).setTuiFeiFlag(1);
  62. // 在吧 对应退费的原数据 也添加一个
  63. list.get(map.get(list.get(i).getOriDetailSn())).setTuiFeiFlag(1);
  64. } else {
  65. // 保存下标
  66. map.put(list.get(i).getDetailSn(), i);
  67. }
  68. }
  69. return ResultVoUtil.success(list);
  70. }
  71. /**
  72. * 搜索项目信息
  73. *
  74. * @param pyCode 拼音码
  75. * @return 返回项目信息
  76. */
  77. public ResultVo<List<GetDropdownBox>> getChargeCode(String pyCode) {
  78. return ResultVoUtil.success(dao.getChargeCode(StringUtil.isContainChinese(pyCode)));
  79. }
  80. /**
  81. * @param param 需要生成退费 的数据
  82. * @return 返回
  83. */
  84. @Transactional(rollbackFor = Exception.class)
  85. public ResultVo<String> xiangMuTuiFei(ZyDetailCharge param) {
  86. if (param.getList().size() > 100) {
  87. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "项目退费一次性大于100条数据");
  88. }
  89. if (StringUtil.isBlank(param.getInpatientNo()) || param.getAdmissTimes() == null || param.getLedgerSn() == null || param.getList().size() == 0) {
  90. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "患者信息不全");
  91. }
  92. // 判断数据是否为 退费数据
  93. List<ZyDetailCharge> chaKanSFtuiFei = dao.chaKanSFYiJingTuiFeiLe(param.getInpatientNo(), param.getAdmissTimes(), param.getLedgerSn(), param.getList());
  94. for (ZyDetailCharge charge : chaKanSFtuiFei) {
  95. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, String.format("流水号为:【%s】已退费,请勿重复退费", charge.getOriDetailSn()));
  96. }
  97. // 判断是否为婴儿
  98. if (param.getInpatientNo().contains("$")) {
  99. param.setInpatientNo(param.getInpatientNo().split("\\$")[0]);
  100. }
  101. // 获取到用户角色
  102. List<Integer> yongHuJueSe = dao.huoQuJueSe(TokenUtil.getTokenUserId());
  103. //获取到这个人是否属于这个科室
  104. List<String> zhiXinKeShi = dao.chaXunZhiZXinKeShi(param.getDeptCode());
  105. // 获取原来的数据
  106. List<ZyDetailCharge> yuanTuiFeiList = dao.huoQuJuTiFeiYong(param.getInpatientNo(), param.getAdmissTimes(), param.getLedgerSn(), param.getList());
  107. // 获取患者的总费用
  108. BigDecimal sum = new BigDecimal(0);
  109. // 退费的list
  110. // 获取最大值
  111. Integer maxDetailSn = transferInOfExpensesDao.getMaxDetailSn(param.getInpatientNo(), param.getAdmissTimes());
  112. List<ZyDetailCharge> tuiFeiList = new ArrayList<>();
  113. for (ZyDetailCharge pojo : yuanTuiFeiList) {
  114. // 判断患者的费用是否存在负数
  115. if (pojo.getChargeFee().signum() == -1 || pojo.getChargeAmount().signum() == -1) {
  116. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, String.format("患者费用存在负数,流水号:【%s】,该数据为退费数据", pojo.getDetailSn()));
  117. }
  118. // 管理员角色可以无视
  119. if (!yongHuJueSe.contains(1)) {
  120. if (!zhiXinKeShi.contains(pojo.getExecUnit())) {
  121. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, String.format("流水号为【%s】,请对应的执行科室进行退费。", pojo.getDetailSn()));
  122. }
  123. }
  124. // 判断如果这个药品的医嘱号 大于0 那么就不能退
  125. if (pojo.getChargeCode().equals("BILL01") || pojo.getChargeCode().equals("BILL02")) {
  126. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, String.format("流水号为【%s】,中草药费或西药费,无法退费。", pojo.getDetailSn()));
  127. }
  128. sum = sum.add(pojo.getChargeFee().multiply(pojo.getChargeAmount()));
  129. pojo.setChargeFee(pojo.getChargeFee().negate());
  130. pojo.setChargeAmount(pojo.getChargeAmount().negate());
  131. pojo.setOpIdCode(TokenUtil.getTokenUserId());
  132. pojo.setOriDetailSn(pojo.getDetailSn());
  133. pojo.setDetailSn(maxDetailSn += 1);
  134. pojo.setOldGenTime(DateUtil.formatDatetime(pojo.getGenTime(), DateUtil.DEFAULT_PATTERN));
  135. // 向退费 list 里面添加
  136. tuiFeiList.add(pojo);
  137. }
  138. // 总费用计算
  139. FeiYongLeiXin fy = JiSuanFeiYong.jiSuan(yuanTuiFeiList, false);
  140. // 计算出这一次 总退的费用
  141. fy.setTotalCharge(sum.negate());
  142. // 执行退费的操作 20 条的退费
  143. if (tuiFeiList.isEmpty()) {
  144. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "查询不到患者原数据,请联系管理员。");
  145. } else {
  146. List<List<ZyDetailCharge>> fenGe = ListUtils.partition(tuiFeiList, 20);
  147. fenGe.forEach(dao::xiangMuTuiFei);
  148. dao.huanZheZongFeiYong(fy, param.getInpatientNo(), param.getAdmissTimes(), "zy_actpatient");
  149. log.info("操作项目退费 ==》 操作人:{},数据:{}", TokenUtil.getTokenUserId(), JSON.toJSONString(tuiFeiList));
  150. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "项目退费操作成功。");
  151. }
  152. }
  153. /**
  154. * 获取模板
  155. *
  156. * @param deptCode 根据科室搜索
  157. * @return 返回模板
  158. */
  159. public ResultVo<List<ZyDetailCharge>> getMuBan(String deptCode) {
  160. return ResultVoUtil.success(dao.getMuBan(deptCode));
  161. }
  162. /**
  163. * 获取模板的具体信息
  164. *
  165. * @param patternName 模板名称
  166. * @param opIdCode 存模板的人
  167. * @return 返回具体的模板
  168. */
  169. public ResultVo<List<ZyDetailCharge>> getMuBanXinXi(String patternName, String opIdCode) {
  170. return ResultVoUtil.success(dao.getMuBanXinXi(patternName, opIdCode));
  171. }
  172. /**
  173. * 通过拼音码 来搜索项目
  174. *
  175. * @param pyCode 拼音码
  176. * @return 返回项目
  177. */
  178. public ResultVo<List<ZyDetailCharge>> queryXiangMu(String pyCode) {
  179. return ResultVoUtil.success(dao.queryXiangMu(pyCode.toUpperCase() + "%"));
  180. }
  181. /**
  182. * 这个项目录入
  183. *
  184. * @param param 参数
  185. * @return 返回提示语句
  186. */
  187. @Transactional(rollbackFor = Exception.class)
  188. public ResultVo<String> xiangMuFeiYongShangChuan(ZyDetailCharge param) {
  189. if (param.getList().size() > 50) {
  190. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "一次性项目录入大于50条");
  191. }
  192. if (StringUtil.isBlank(param.getInpatientNo()) || param.getAdmissTimes() == null || param.getLedgerSn() == null || param.getList().size() == 0
  193. || StringUtil.isBlank(param.getWard()) || StringUtil.isBlank(param.getDept()) || StringUtil.isBlank(param.getZySerialNo())) {
  194. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "患者信息不全。");
  195. }
  196. // 婴儿 要特殊处理
  197. int infantFlag = 0;
  198. // 如果带有这个 $ 说明是婴儿
  199. if (param.getInpatientNo().contains("$")) {
  200. infantFlag = 1;
  201. param.setInpatientNo(param.getInpatientNo().split("\\$")[0]);
  202. }
  203. // 获取最大流水号
  204. Integer maxDetailSn = transferInOfExpensesDao.getMaxDetailSn(param.getInpatientNo(), param.getAdmissTimes());
  205. // 获取项目的 总费用
  206. BigDecimal sum = new BigDecimal(0);
  207. // 录入人的id
  208. param.setOpIdCode(TokenUtil.getTokenUserId());
  209. for (ZyDetailCharge zyDetailCharge : param.getList()) {
  210. zyDetailCharge.setDetailSn(maxDetailSn += 1);
  211. // 判断患者的费用是否存在负数
  212. if (zyDetailCharge.getChargeAmount().signum() == -1 || zyDetailCharge.getAmount().signum() == -1) {
  213. return ResultVoUtil.fail(ExceptionEnum.EXIST_NEGATIVE_FEES);
  214. }
  215. sum = sum.add(zyDetailCharge.getChargeAmount().multiply(zyDetailCharge.getAmount()));
  216. // 这里不会保存
  217. zyDetailCharge.setChargeAmount(zyDetailCharge.getChargeAmount().multiply(zyDetailCharge.getAmount()));
  218. if (StringUtil.isBlank(zyDetailCharge.getChargeCode())) {
  219. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "项目编码空值。");
  220. }
  221. if (StringUtil.isBlank(zyDetailCharge.getDeptCode())) {
  222. return ResultVoUtil.fail(ExceptionEnum.NULL_POINTER, "执行科室为空。");
  223. }
  224. }
  225. FeiYongLeiXin fy = JiSuanFeiYong.jiSuan(param.getList(), true);
  226. fy.setTotalCharge(sum);
  227. // 在此处 插入费用
  228. dao.chaRuFeiYong(param, param.getList(), infantFlag);
  229. dao.huanZheZongFeiYong(fy, param.getInpatientNo(), param.getAdmissTimes(), "zy_actpatient");
  230. log.info("项目录入费用上传 ==》 操作人:{},数据:{}", param.getOpIdCode(), JSON.toJSONString(param));
  231. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "费用上传成功。");
  232. }
  233. /**
  234. * 上传以及修改项目录入模板
  235. *
  236. * @param zyDetailCharge 模板的一些数据
  237. * @return 返回成功提示
  238. */
  239. public ResultVo<String> shangChuanMuBan(ZyDetailCharge zyDetailCharge) {
  240. if (dao.chaKanMuBanMingChengSFcunZi(zyDetailCharge.getName(), TokenUtil.getTokenUserId()) > 0) {
  241. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "模板名称重复");
  242. }
  243. zyDetailCharge.setOpIdCode(TokenUtil.getTokenUserId());
  244. // 在创建新的模板之前 需要先删除 原来的
  245. dao.delMuBan(zyDetailCharge.getName(), zyDetailCharge.getOpIdCode());
  246. dao.baoCunMuBan(zyDetailCharge);
  247. log.info("保存项目录入模板 ==》 操作人 :{},数据:{}", zyDetailCharge.getOpIdCode(), JSON.toJSONString(zyDetailCharge));
  248. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "保存模板成功。");
  249. }
  250. /**
  251. * 医嘱退费匹配
  252. *
  253. * @param inpatientNo 住院号
  254. * @param admissTimes 住院次数
  255. * @return 返回是否匹配成功
  256. */
  257. public ResultVo<String> yiZhuTuiFeiPiPei(String inpatientNo, Integer admissTimes) {
  258. // 获取正的医嘱费用
  259. List<ZyDetailCharge> getYiZhuFeiYongZhenShu = dao.getYiZhuFeiYong(inpatientNo, admissTimes, ">");
  260. // 获取负的医嘱费用
  261. List<ZyDetailCharge> getYiZhuFeiYongFuShu = dao.getYiZhuFeiYong(inpatientNo, admissTimes, "<");
  262. // 需要保存需要匹配的一些流水数据
  263. List<ZyDetailCharge> piPei = new ArrayList<>();
  264. // 保存正数的数据用 map 来匹配
  265. Map<String, ZyDetailCharge> zhenShuMap = new HashMap<>();
  266. for (ZyDetailCharge zhenShu : getYiZhuFeiYongZhenShu) {
  267. String key = zhenShu.getOrderNo() + DateUtil.formatDatetime(zhenShu.getChargeDate()) + zhenShu.getChargeCodeMx() + zhenShu.getChargeAmount();
  268. zhenShuMap.put(key, zhenShu);
  269. }
  270. // 用负数拼接的 key 去查找
  271. for (ZyDetailCharge fuShu : getYiZhuFeiYongFuShu) {
  272. String key = fuShu.getOrderNo() + DateUtil.formatDatetime(fuShu.getChargeDate()) + fuShu.getChargeCodeMx() + fuShu.getChargeAmount().negate();
  273. if (zhenShuMap.containsKey(key)) {
  274. fuShu.setOriDetailSn(zhenShuMap.get(key).getDetailSn());
  275. piPei.add(fuShu);
  276. }
  277. }
  278. // 开始匹配
  279. if (piPei.size() > 0) {
  280. // 100 条的更新 ListUtils.partition 用来拆分 list size 是拆分的条数
  281. List<List<ZyDetailCharge>> fenDuan = ListUtils.partition(piPei, 100);
  282. for (List<ZyDetailCharge> list : fenDuan) {
  283. dao.yiZhuTuiFeiPiPei(inpatientNo, admissTimes, list);
  284. }
  285. log.info("医嘱退费匹配 ==》 操作人:{},住院号:{},住院次数:{}", TokenUtil.getTokenUserId(), inpatientNo, admissTimes);
  286. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, String.format("医嘱退费匹配,共有【%d】条,匹配成功【%d】条", getYiZhuFeiYongFuShu.size(), piPei.size()));
  287. } else {
  288. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "该患者没有可以匹配的医嘱退费");
  289. }
  290. }
  291. public ResultVo<String> shanChuMuBan(String patterName, String opIdCode) {
  292. log.info("删除项目模板 ==》操作人:{},模板名称:{}", TokenUtil.getTokenUserId(), patterName);
  293. dao.delMuBan(patterName, opIdCode);
  294. return ResultVoUtil.success(ExceptionEnum.SUCCESS_AND_NOTIFICATION, "删除成功");
  295. }
  296. }