IllegalChargesAnalysisService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. package thyyxxk.webserver.service.reports;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5. import thyyxxk.webserver.config.exception.ExceptionEnum;
  6. import thyyxxk.webserver.dao.his.reports.IllegalChargesAnalysisDao;
  7. import thyyxxk.webserver.entity.ResultVo;
  8. import thyyxxk.webserver.entity.datamodify.YzActOrder;
  9. import thyyxxk.webserver.entity.dictionary.PureCodeName;
  10. import thyyxxk.webserver.entity.reports.illegalchargesanalysis.IllegalChargeData;
  11. import thyyxxk.webserver.entity.reports.illegalchargesanalysis.IllegalChargeTemplate;
  12. import thyyxxk.webserver.entity.reports.illegalchargesanalysis.SearchChargeItem;
  13. import thyyxxk.webserver.utils.ListUtil;
  14. import thyyxxk.webserver.utils.ResultVoUtil;
  15. import thyyxxk.webserver.utils.StringUtil;
  16. import thyyxxk.webserver.utils.TokenUtil;
  17. import java.math.BigDecimal;
  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.List;
  21. import java.util.Map;
  22. /**
  23. * @description: 违规收费项目分析实现类
  24. * @author: DingJie
  25. * @create: 2021-04-15 16:47:34
  26. **/
  27. @Slf4j
  28. @Service
  29. public class IllegalChargesAnalysisService {
  30. private final IllegalChargesAnalysisDao dao;
  31. private static final String DASH = "_";
  32. private static final String ADMIN = "admin";
  33. private static final String ADMIN_2 = "admin_2";
  34. @Autowired
  35. public IllegalChargesAnalysisService(IllegalChargesAnalysisDao dao) {
  36. this.dao = dao;
  37. }
  38. public ResultVo<List<IllegalChargeTemplate>> fetchMyTemplates() {
  39. String code = TokenUtil.getTokenUserId() + "%";
  40. List<IllegalChargeTemplate> tempList = dao.selectMyTemplates(code);
  41. List<IllegalChargeTemplate> resultList = new ArrayList<>();
  42. Map<String, IllegalChargeTemplate> treeMap = new HashMap<>(tempList.size());
  43. for (IllegalChargeTemplate item : tempList) {
  44. item.analyzeValue();
  45. treeMap.put(item.getId(), item);
  46. if (null == item.getParent()) {
  47. resultList.add(item);
  48. }
  49. }
  50. for (IllegalChargeTemplate item : tempList) {
  51. IllegalChargeTemplate template = treeMap.get(item.getParent());
  52. if (null != template) {
  53. if (null == template.getChildren()) {
  54. template.setChildren(new ArrayList<>());
  55. }
  56. template.getChildren().add(item);
  57. }
  58. }
  59. return ResultVoUtil.success(resultList);
  60. }
  61. public ResultVo<String> insertNewTemplate(IllegalChargeTemplate template) {
  62. if (StringUtil.isBlank(template.getLabel())) {
  63. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "模板名称不能为空!");
  64. }
  65. if (template.getPersonal() == 0) {
  66. dao.insertNewTemplate(template);
  67. return ResultVoUtil.success("创建成功。");
  68. }
  69. StringBuilder id = new StringBuilder(template.getId());
  70. if (!id.toString().contains(DASH)) {
  71. template.setId(TokenUtil.getTokenUserId() + DASH + id);
  72. } else {
  73. if (id.toString().startsWith(ADMIN)) {
  74. id = new StringBuilder(id.toString().replace(ADMIN, TokenUtil.getTokenUserId()));
  75. template.setId(id.toString());
  76. }
  77. }
  78. if (template.getParent().startsWith(ADMIN) && !template.getParent().equals(ADMIN_2)) {
  79. template.setParent(template.getParent().replace("admin", TokenUtil.getTokenUserId()));
  80. }
  81. id = new StringBuilder(template.getId());
  82. String[] idArr = id.toString().split("-");
  83. String lastBit = idArr[idArr.length - 1];
  84. int position = Integer.parseInt(lastBit);
  85. Integer exist = dao.selectCurrentId(id.toString());
  86. while (exist != null) {
  87. position++;
  88. idArr[idArr.length - 1] = String.valueOf(position);
  89. id = new StringBuilder();
  90. for (int i = 0; i < idArr.length - 1; i++) {
  91. id.append(idArr[i]).append("-");
  92. }
  93. id.append(position);
  94. exist = dao.selectCurrentId(id.toString());
  95. }
  96. template.setId(id.toString());
  97. dao.insertNewTemplate(template);
  98. log.info("创建模板:{}", template);
  99. return ResultVoUtil.success("创建成功。");
  100. }
  101. public ResultVo<String> deleteTemplate(IllegalChargeTemplate template) {
  102. String id = template.getId() + "%";
  103. dao.deleteTemplate(id);
  104. return ResultVoUtil.success("删除成功。");
  105. }
  106. public ResultVo<List<PureCodeName>> searchChargeItem(SearchChargeItem param) {
  107. String pyCode = "%" + param.getContent().toUpperCase() + "%";
  108. switch (param.getMethod()) {
  109. case "alpha":
  110. return ResultVoUtil.success(dao.selectChargeItemByPyCode(pyCode));
  111. case "code":
  112. return ResultVoUtil.success(dao.selectChargeItemByCode(pyCode));
  113. case "name":
  114. return ResultVoUtil.success(dao.selectChargeItemByName(pyCode));
  115. default:
  116. return ResultVoUtil.success(new ArrayList<>());
  117. }
  118. }
  119. public ResultVo<String> saveTemplateChanges(IllegalChargeTemplate template) {
  120. if (template.getType() == 0) {
  121. dao.updateTemplate(template);
  122. return ResultVoUtil.success();
  123. }
  124. if (null == template.getMainCharges() || template.getMainCharges().isEmpty()) {
  125. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "收费主体不能为空!");
  126. }
  127. StringBuilder value = genTemplateValue(template.getMainCharges()).append("$");
  128. if (template.getAttribute() == 1) {
  129. if (null == template.getConflictCharges() || template.getConflictCharges().isEmpty()) {
  130. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "冲突收费不能为空!");
  131. }
  132. value.append(genTemplateValue(template.getConflictCharges()));
  133. } else {
  134. if (null == template.getMaxChargeNum()) {
  135. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "超量收费上限不能为空!");
  136. }
  137. value.append(template.getMaxChargeNum()).append("|").append(template.getMaxChargeUnit());
  138. }
  139. template.setValue(value.toString());
  140. dao.updateTemplate(template);
  141. return ResultVoUtil.success();
  142. }
  143. private StringBuilder genTemplateValue(List<PureCodeName> charges) {
  144. StringBuilder builder = new StringBuilder();
  145. for (int i = 0; i < charges.size(); i++) {
  146. PureCodeName charge = charges.get(i);
  147. builder.append(charge.getCode()).append("|").append(charge.getName());
  148. if (i < charges.size() - 1) {
  149. builder.append("#");
  150. }
  151. }
  152. return builder;
  153. }
  154. public ResultVo<List<IllegalChargeData>> analyzeTargetData(IllegalChargeTemplate template) {
  155. template.setStart(template.getStart() + " 00:00:00");
  156. template.setEnd(template.getEnd() + " 23:59:59");
  157. log.info("违规收费分析:{}", template);
  158. if (template.getAttribute() == 1) {
  159. return sameTimeCharge(template);
  160. }
  161. return overLimitCharge(template);
  162. }
  163. private ResultVo<List<IllegalChargeData>> sameTimeCharge(IllegalChargeTemplate template) {
  164. List<IllegalChargeData> resultList = new ArrayList<>();
  165. for (PureCodeName main : template.getMainCharges()) {
  166. List<IllegalChargeData> mainList = dao.selectChargeDataForSameTime(template.getStart(),
  167. template.getEnd(), main.getCode());
  168. if (ListUtil.notBlank(template.getMedtypes())) {
  169. mainList.removeIf(item -> !template.getMedtypes().contains(item.getMedtype()));
  170. }
  171. for (PureCodeName cflct : template.getConflictCharges()) {
  172. List<IllegalChargeData> cflctList = dao.selectChargeDataForSameTime(template.getStart(),
  173. template.getEnd(), cflct.getCode());
  174. if (ListUtil.notBlank(template.getMedtypes())) {
  175. cflctList.removeIf(item -> !template.getMedtypes().contains(item.getMedtype()));
  176. }
  177. Map<String, IllegalChargeData> map = new HashMap<>(mainList.size());
  178. for (IllegalChargeData item : mainList) {
  179. String key = item.getInpatientNo() + "_" + item.getChargeDay();
  180. map.put(key, item);
  181. }
  182. for (IllegalChargeData item : cflctList) {
  183. String key = item.getInpatientNo() + "_" + item.getChargeDay();
  184. if (map.containsKey(key)) {
  185. IllegalChargeData data = map.get(key);
  186. data.setConflictChargeCode(item.getChargeCode());
  187. data.setConflictChargeAmount(item.getChargeAmount());
  188. data.setConflictChargeDate(item.getChargeDate());
  189. data.setConflictChargeFee(item.getChargeFee());
  190. data.setConflictChargeName(item.getChargeName());
  191. data.setConflictDetailSn(item.getDetailSn());
  192. data.setConflictOrderNo(item.getOrderNo());
  193. resultList.add(data);
  194. }
  195. }
  196. }
  197. }
  198. return ResultVoUtil.success(resultList);
  199. }
  200. private ResultVo<List<IllegalChargeData>> overLimitCharge(IllegalChargeTemplate template) {
  201. List<IllegalChargeData> resultList = new ArrayList<>();
  202. for (PureCodeName main : template.getMainCharges()) {
  203. resultList.addAll(dao.selectChargeDataForOverLimit(template.getStart(), template.getEnd(),
  204. main.getCode(), template.getMaxChargeNum()));
  205. }
  206. if (resultList.isEmpty()) {
  207. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
  208. }
  209. return ResultVoUtil.success(resultList);
  210. }
  211. public ResultVo<List<YzActOrder>> selectOrderPair(BigDecimal orderNo, BigDecimal conflictOrderNo) {
  212. List<YzActOrder> list = dao.selectYzActOrder(orderNo, conflictOrderNo);
  213. if (list.isEmpty()) {
  214. list = dao.selectYzInActOrder(orderNo, conflictOrderNo);
  215. }
  216. if (list.isEmpty()) {
  217. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
  218. }
  219. if (list.size() == 1 || list.get(0).getActOrderNo().equals(orderNo)) {
  220. return ResultVoUtil.success(list);
  221. }
  222. List<YzActOrder> tmp = new ArrayList<>();
  223. tmp.add(list.get(1));
  224. tmp.add(list.get(0));
  225. return ResultVoUtil.success(tmp);
  226. }
  227. public ResultVo<List<IllegalChargeData>> queryHuanZheLiangCiRuYuanRiQi(IllegalChargeTemplate param) {
  228. List<IllegalChargeData> list = dao.queryHuanZheLiangCiRuYuanRiQi(param.getStart(), param.getEnd(), param.getMedtypes());
  229. if (list.isEmpty()) {
  230. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
  231. }
  232. List<IllegalChargeData> illegalChargeData = dao.queryZhenDuan(list);
  233. for (IllegalChargeData chargeData : list) {
  234. for (IllegalChargeData illegalChargeDatum : illegalChargeData) {
  235. if (chargeData.getInpatientNo().equals(illegalChargeDatum.getInpatientNo()) && chargeData.getAdmissTimes().equals(illegalChargeDatum.getAdmissTimes())) {
  236. chargeData.setDisDiag(illegalChargeDatum.getDisDiag());
  237. chargeData.setDisDiagComment(illegalChargeDatum.getDisDiagComment());
  238. break;
  239. }
  240. }
  241. }
  242. return ResultVoUtil.success(list);
  243. }
  244. public ResultVo<List<IllegalChargeData>> chaXunHuanZheZhuYuanJianGe(String startTime, String endTime, Integer tianShu) {
  245. return ResultVoUtil.success(dao.zhuYuanTianShu(startTime, endTime, tianShu));
  246. }
  247. }