IllegalChargesAnalysisService.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 content = "%" + param.getContent().toUpperCase() + "%";
  108. String method;
  109. switch (param.getMethod()) {
  110. case "alpha":
  111. method = "py_code";
  112. break;
  113. case "code":
  114. method = "code";
  115. break;
  116. default:
  117. method = "name";
  118. break;
  119. }
  120. String include = param.getIncludeDeactivate() ? "like '%%'" : "!=1";
  121. return ResultVoUtil.success(dao.selectChargeItem(method, content, include));
  122. }
  123. public ResultVo<String> saveTemplateChanges(IllegalChargeTemplate template) {
  124. if (template.getType() == 0) {
  125. dao.updateTemplate(template);
  126. return ResultVoUtil.success();
  127. }
  128. if (null == template.getMainCharges() || template.getMainCharges().isEmpty()) {
  129. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "收费主体不能为空!");
  130. }
  131. StringBuilder value = genTemplateValue(template.getMainCharges()).append("$");
  132. if (template.getAttribute() == 1) {
  133. if (null == template.getConflictCharges() || template.getConflictCharges().isEmpty()) {
  134. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "冲突收费不能为空!");
  135. }
  136. value.append(genTemplateValue(template.getConflictCharges()));
  137. } else {
  138. if (null == template.getMaxChargeNum()) {
  139. return ResultVoUtil.fail(ExceptionEnum.LOGICAL_ERROR, "超量收费上限不能为空!");
  140. }
  141. value.append(template.getMaxChargeNum()).append("|").append(template.getMaxChargeUnit());
  142. }
  143. template.setValue(value.toString());
  144. dao.updateTemplate(template);
  145. return ResultVoUtil.success();
  146. }
  147. private StringBuilder genTemplateValue(List<PureCodeName> charges) {
  148. StringBuilder builder = new StringBuilder();
  149. for (int i = 0; i < charges.size(); i++) {
  150. PureCodeName charge = charges.get(i);
  151. builder.append(charge.getCode()).append("|").append(charge.getName());
  152. if (i < charges.size() - 1) {
  153. builder.append("#");
  154. }
  155. }
  156. return builder;
  157. }
  158. public ResultVo<List<IllegalChargeData>> analyzeTargetData(IllegalChargeTemplate template) {
  159. template.setStart(template.getStart() + " 00:00:00");
  160. template.setEnd(template.getEnd() + " 23:59:59");
  161. log.info("违规收费分析:{}", template);
  162. if (template.getAttribute() == 1) {
  163. return sameTimeCharge(template);
  164. }
  165. return overLimitCharge(template);
  166. }
  167. private ResultVo<List<IllegalChargeData>> sameTimeCharge(IllegalChargeTemplate template) {
  168. List<IllegalChargeData> resultList = new ArrayList<>();
  169. for (PureCodeName main : template.getMainCharges()) {
  170. List<IllegalChargeData> mainList = dao.selectChargeDataForSameTime(template.getStart(),
  171. template.getEnd(), main.getCode());
  172. if (ListUtil.notBlank(template.getMedtypes())) {
  173. mainList.removeIf(item -> !template.getMedtypes().contains(item.getMedtype()));
  174. }
  175. for (PureCodeName cflct : template.getConflictCharges()) {
  176. List<IllegalChargeData> cflctList = dao.selectChargeDataForSameTime(template.getStart(),
  177. template.getEnd(), cflct.getCode());
  178. if (ListUtil.notBlank(template.getMedtypes())) {
  179. cflctList.removeIf(item -> !template.getMedtypes().contains(item.getMedtype()));
  180. }
  181. Map<String, IllegalChargeData> map = new HashMap<>(mainList.size());
  182. for (IllegalChargeData item : mainList) {
  183. String key = item.getInpatientNo() + "_" + item.getChargeDay();
  184. map.put(key, item);
  185. }
  186. for (IllegalChargeData item : cflctList) {
  187. String key = item.getInpatientNo() + "_" + item.getChargeDay();
  188. if (map.containsKey(key)) {
  189. IllegalChargeData data = map.get(key);
  190. data.setConflictChargeCode(item.getChargeCode());
  191. data.setConflictChargeAmount(item.getChargeAmount());
  192. data.setConflictChargeDate(item.getChargeDate());
  193. data.setConflictChargeFee(item.getChargeFee());
  194. data.setConflictChargeName(item.getChargeName());
  195. data.setConflictDetailSn(item.getDetailSn());
  196. data.setConflictOrderNo(item.getOrderNo());
  197. resultList.add(data);
  198. }
  199. }
  200. }
  201. }
  202. return ResultVoUtil.success(resultList);
  203. }
  204. private ResultVo<List<IllegalChargeData>> overLimitCharge(IllegalChargeTemplate template) {
  205. List<IllegalChargeData> resultList = new ArrayList<>();
  206. for (PureCodeName main : template.getMainCharges()) {
  207. resultList.addAll(dao.selectChargeDataForOverLimit(template.getStart(), template.getEnd(),
  208. main.getCode(), template.getMaxChargeNum()));
  209. }
  210. if (resultList.isEmpty()) {
  211. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
  212. }
  213. return ResultVoUtil.success(resultList);
  214. }
  215. public ResultVo<List<YzActOrder>> selectOrderPair(BigDecimal orderNo, BigDecimal conflictOrderNo) {
  216. List<YzActOrder> list = dao.selectYzActOrder(orderNo, conflictOrderNo);
  217. if (list.isEmpty()) {
  218. list = dao.selectYzInActOrder(orderNo, conflictOrderNo);
  219. }
  220. if (list.isEmpty()) {
  221. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
  222. }
  223. if (list.size() == 1 || list.get(0).getActOrderNo().equals(orderNo)) {
  224. return ResultVoUtil.success(list);
  225. }
  226. List<YzActOrder> tmp = new ArrayList<>();
  227. tmp.add(list.get(1));
  228. tmp.add(list.get(0));
  229. return ResultVoUtil.success(tmp);
  230. }
  231. public ResultVo<List<IllegalChargeData>> queryHuanZheLiangCiRuYuanRiQi(IllegalChargeTemplate param) {
  232. List<IllegalChargeData> list = dao.queryHuanZheLiangCiRuYuanRiQi(param.getStart(), param.getEnd(), param.getMedtypes());
  233. if (list.isEmpty()) {
  234. return ResultVoUtil.fail(ExceptionEnum.NO_DATA_EXIST);
  235. }
  236. List<IllegalChargeData> illegalChargeData = dao.queryZhenDuan(list);
  237. for (IllegalChargeData chargeData : list) {
  238. for (IllegalChargeData illegalChargeDatum : illegalChargeData) {
  239. if (chargeData.getInpatientNo().equals(illegalChargeDatum.getInpatientNo()) && chargeData.getAdmissTimes().equals(illegalChargeDatum.getAdmissTimes())) {
  240. chargeData.setDisDiag(illegalChargeDatum.getDisDiag());
  241. chargeData.setDisDiagComment(illegalChargeDatum.getDisDiagComment());
  242. break;
  243. }
  244. }
  245. }
  246. return ResultVoUtil.success(list);
  247. }
  248. public ResultVo<List<IllegalChargeData>> chaXunHuanZheZhuYuanJianGe(String startTime, String endTime, Integer tianShu) {
  249. return ResultVoUtil.success(dao.zhuYuanTianShu(startTime, endTime, tianShu));
  250. }
  251. }