MedicineManagementService.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package thyyxxk.webserver.service.zhuyuanyiji;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Service;
  6. import thyyxxk.webserver.config.exception.ExceptionEnum;
  7. import thyyxxk.webserver.dao.his.zhuyuanyiji.MedicineManagementDao;
  8. import thyyxxk.webserver.entity.ResultVo;
  9. import thyyxxk.webserver.entity.medicaltechnology.AsidePage;
  10. import thyyxxk.webserver.entity.medicaltechnology.FetchMedicines;
  11. import thyyxxk.webserver.entity.medicaltechnology.MedicinePage;
  12. import thyyxxk.webserver.entity.medicaltechnology.PoisonousAnesthetics;
  13. import thyyxxk.webserver.service.PublicServer;
  14. import thyyxxk.webserver.service.hutoolcache.UserCache;
  15. import thyyxxk.webserver.utils.ListUtil;
  16. import thyyxxk.webserver.utils.ResultVoUtil;
  17. import thyyxxk.webserver.utils.TokenUtil;
  18. import java.math.BigDecimal;
  19. import java.util.*;
  20. @Slf4j
  21. @Service
  22. public class MedicineManagementService {
  23. private final MedicineManagementDao dao;
  24. private final PublicServer publicService;
  25. private final UserCache userCache;
  26. @Autowired
  27. public MedicineManagementService(MedicineManagementDao dao, PublicServer publicService, UserCache userCache) {
  28. this.dao = dao;
  29. this.publicService = publicService;
  30. this.userCache = userCache;
  31. }
  32. public ResultVo<List<MedicinePage>> fetchMedicinePages(FetchMedicines params) {
  33. // transformDateToDatetime(params);
  34. List<MedicinePage> list = dao.selectMedicinePages(buildFetchPageNoQw(params));
  35. if (list.isEmpty()) {
  36. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
  37. }
  38. return ResultVoUtil.success(list);
  39. }
  40. public ResultVo<List<MedicinePage>> fetchUsedMedicines(FetchMedicines params) {
  41. transformDateToDatetime(params);
  42. buildChildDepartments(params);
  43. List<MedicinePage> list = dao.selectUsedMedicines(params);
  44. if (list.isEmpty()) {
  45. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
  46. }
  47. return ResultVoUtil.success(list);
  48. }
  49. public ResultVo<List<MedicinePage>> submitMedicinePages(FetchMedicines params) {
  50. if (ListUtil.isBlank(params.getChargeList())) {
  51. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST, "请勾选需要提交的药品。");
  52. }
  53. // transformDateToDatetime(params);
  54. Map<String, String> deptChargeCodes = new HashMap<>();
  55. for (MedicinePage page : params.getChargeList()) {
  56. String execUnit = page.getExecUnit();
  57. if (deptChargeCodes.containsKey(execUnit)) {
  58. String tempVal = deptChargeCodes.get(page.getExecUnit()) + ",'" + page.getChargeCode() + "'";
  59. deptChargeCodes.replace(execUnit, tempVal);
  60. } else {
  61. deptChargeCodes.put(execUnit, "'" + page.getChargeCode() + "'");
  62. }
  63. }
  64. for (Map.Entry<String, String> entry : deptChargeCodes.entrySet()) {
  65. params.setDept(entry.getKey());
  66. params.setChargeCodes(entry.getValue());
  67. int pageNo = publicService.getTheDrugListNo();
  68. dao.deleteExistTargetPageNo(pageNo);
  69. params.setPageNo(pageNo);
  70. dao.insertNewPageNo(params);
  71. dao.submitMedicinePages(params);
  72. log.info("【操作员:{}】提交药品单:{}", TokenUtil.getInstance().getTokenUserId(), params);
  73. }
  74. return ResultVoUtil.success(dao.selectMedicinePages(buildFetchPageNoQw(params)));
  75. }
  76. private QueryWrapper<?> buildFetchPageNoQw(FetchMedicines params) {
  77. QueryWrapper<?> qw = new QueryWrapper<>();
  78. if(params.getChargeCodes().equals("2")){
  79. qw.gt("b.drug_flag ","0");
  80. }
  81. if(params.getChargeCodes().equals("3")){
  82. qw.apply("b.drug_flag = '' or b.drug_flag is null and b.infusion_flag is null or b.infusion_flag = 0");
  83. }
  84. if(params.getChargeCodes().equals("4")){
  85. qw.gt("b.infusion_flag ","0");
  86. }
  87. qw.ge("charge_date", params.getStart())
  88. .le("charge_date", params.getEnd())
  89. .eq("isnull(a.page_no,0)", "0")
  90. .eq("isnull(confirm_flag,'0')", "0")
  91. .eq("a.group_no", "73")
  92. .apply(" a.charge_code=b.code and isnull(a.serial,'01')=b.serial")
  93. .in("exec_unit", publicService.getChildDeptByUserCode(false))
  94. .groupBy("b.code,b.serial having sum(a.amount)!=0");
  95. return qw;
  96. }
  97. public ResultVo<Map<String, Object>> fetchPoisonousAnesthetics(FetchMedicines params) {
  98. transformDateToDatetime(params);
  99. List<PoisonousAnesthetics> list = dao.selectPoisonousAnesthetics(params);
  100. if (list.isEmpty()) {
  101. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
  102. }
  103. Map<String, List<PoisonousAnesthetics>> medicines = new TreeMap<>();
  104. for (PoisonousAnesthetics item : list) {
  105. String key = item.getChargeDate() + "^" + item.getChargeCode();
  106. if (medicines.containsKey(key)) {
  107. medicines.get(key).add(item);
  108. } else {
  109. List<PoisonousAnesthetics> temp = new ArrayList<>();
  110. temp.add(item);
  111. medicines.put(key, temp);
  112. }
  113. }
  114. Map<String, BigDecimal> amounts = new HashMap<>();
  115. for (Map.Entry<String, List<PoisonousAnesthetics>> entry : medicines.entrySet()) {
  116. BigDecimal sum = BigDecimal.ZERO;
  117. for (PoisonousAnesthetics item : entry.getValue()) {
  118. sum = sum.add(item.getChargeAmount());
  119. }
  120. amounts.put(entry.getKey(), sum);
  121. }
  122. Map<String, Object> map = new HashMap<>();
  123. map.put("medicines", medicines);
  124. map.put("amounts", amounts);
  125. return ResultVoUtil.success(map);
  126. }
  127. public ResultVo<List<AsidePage>> fetchSubmittedPageNos(FetchMedicines params) {
  128. transformDateToDatetime(params);
  129. QueryWrapper<?> qw = new QueryWrapper<>();
  130. qw.ge("charge_date", params.getStart())
  131. .le("charge_date", params.getEnd())
  132. .gt("page_no", 0).eq("group_no", "73")
  133. .in("exec_unit", publicService.getChildDeptByUserCode(false))
  134. .groupBy("page_no,exec_unit");
  135. if (null != params.getPageNo()) {
  136. qw.eq("page_no", params.getPageNo());
  137. }
  138. List<AsidePage> list = dao.selectSubmittedPageNos(qw);
  139. if (list.isEmpty()) {
  140. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
  141. }
  142. list.forEach(item -> item.setConfirmId(userCache.getEmployeeName(item.getConfirmId())));
  143. return ResultVoUtil.success(list);
  144. }
  145. public ResultVo<List<MedicinePage>> fetchMedicinePageDetail(AsidePage params) {
  146. QueryWrapper<?> qw = new QueryWrapper<>();
  147. qw.eq("a.page_no", params.getPageNo())
  148. .eq("a.group_no", "73")
  149. .eq("a.exec_unit", params.getExecUnit())
  150. .apply("a.charge_code=b.code and isnull(a.serial,'01')=b.serial")
  151. .groupBy("b.code,b.serial having sum(a.amount)!=0");
  152. return ResultVoUtil.success(dao.fetchMedicinePageDetail(qw));
  153. }
  154. private void transformDateToDatetime(FetchMedicines params) {
  155. params.setStart(params.getStart() + " 00:00:00.000");
  156. params.setEnd(params.getEnd() + " 23:59:59.999");
  157. }
  158. private void buildChildDepartments(FetchMedicines params) {
  159. String dept = params.getDept();
  160. List<String> children = dao.selectChildDepartments(dept);
  161. StringBuilder builder = new StringBuilder();
  162. builder.append("'").append(dept).append("'");
  163. for (String child : children) {
  164. builder.append(",'").append(child).append("'");
  165. }
  166. params.setDept(builder.toString());
  167. }
  168. }