DrugConfigFacade.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  1. package com.diagbot.facade;
  2. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  3. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  4. import com.baomidou.mybatisplus.core.metadata.IPage;
  5. import com.diagbot.client.CdssCoreClient;
  6. import com.diagbot.dto.DictionaryInfoDTO;
  7. import com.diagbot.dto.HosRelationNumDTO;
  8. import com.diagbot.dto.RespDTO;
  9. import com.diagbot.entity.DrugConfig;
  10. import com.diagbot.enums.ConceptTypeEnum;
  11. import com.diagbot.enums.IsDeleteEnum;
  12. import com.diagbot.exception.CommonErrorCode;
  13. import com.diagbot.exception.CommonException;
  14. import com.diagbot.service.DrugConfigService;
  15. import com.diagbot.util.DateUtil;
  16. import com.diagbot.util.EntityUtil;
  17. import com.diagbot.util.ExcelUtils;
  18. import com.diagbot.util.ListUtil;
  19. import com.diagbot.util.RespDTOUtil;
  20. import com.diagbot.util.StringUtil;
  21. import com.diagbot.util.UserUtils;
  22. import com.diagbot.vo.ConceptVO;
  23. import com.diagbot.vo.DrugConfigListVO;
  24. import com.diagbot.vo.DrugConfigPageVO;
  25. import com.diagbot.vo.HosRelationNumPageVO;
  26. import com.diagbot.vo.HospitalIdVO;
  27. import com.diagbot.vo.IdListVO;
  28. import com.diagbot.vo.IdVO;
  29. import com.google.common.collect.Lists;
  30. import org.apache.commons.lang3.StringUtils;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.stereotype.Component;
  33. import org.springframework.web.multipart.MultipartFile;
  34. import javax.servlet.http.HttpServletResponse;
  35. import java.util.ArrayList;
  36. import java.util.Date;
  37. import java.util.HashMap;
  38. import java.util.List;
  39. import java.util.Map;
  40. import java.util.stream.Collectors;
  41. /**
  42. * @Description:
  43. * @Author:zhaops
  44. * @time: 2020/7/29 15:04
  45. */
  46. @Component
  47. public class DrugConfigFacade {
  48. @Autowired
  49. private DrugConfigService drugConfigService;
  50. @Autowired
  51. private CdssCoreClient cdssCoreClient;
  52. @Autowired
  53. private DictionaryFacade dictionaryFacade;
  54. /**
  55. * 判断是否已存在
  56. *
  57. * @param drugConfig
  58. * @return
  59. */
  60. public Boolean isExistRecord(DrugConfig drugConfig) {
  61. QueryWrapper<DrugConfig> queryWrapper = new QueryWrapper<>();
  62. queryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey())
  63. .eq("hospital_id", drugConfig.getHospitalId())
  64. .eq("his_name", drugConfig.getHisName())
  65. .eq("unique_name", drugConfig.getUniqueName());
  66. if (StringUtil.isBlank(drugConfig.getForm())) {
  67. queryWrapper.and(i -> i.isNull("form")
  68. .or()
  69. .eq("form", ""));
  70. } else {
  71. queryWrapper.eq("form", drugConfig.getForm());
  72. }
  73. DrugConfig oldRecord = drugConfigService.getOne(queryWrapper, false);
  74. if (drugConfig.getId() == null
  75. && oldRecord != null) {
  76. throw new CommonException(CommonErrorCode.IS_EXISTS, "该条关联已存在,无法保存");
  77. }
  78. if (drugConfig.getId() != null
  79. && oldRecord != null
  80. && !drugConfig.getId().equals(oldRecord.getId())) {
  81. throw new CommonException(CommonErrorCode.IS_EXISTS, "该条关联已存在,无法保存");
  82. }
  83. return false;
  84. }
  85. /**
  86. * 保存记录-单条
  87. *
  88. * @param drugConfig
  89. * @return
  90. */
  91. public Boolean saveOrUpdateRecord(DrugConfig drugConfig) {
  92. String userId = UserUtils.getCurrentPrincipleID();
  93. Date now = DateUtil.now();
  94. drugConfig.setModifier(userId);
  95. drugConfig.setGmtModified(now);
  96. QueryWrapper<DrugConfig> queryWrapper = new QueryWrapper<>();
  97. queryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey())
  98. .eq("hospital_id", drugConfig.getHospitalId())
  99. .eq("his_name", drugConfig.getHisName())
  100. .eq("unique_name", drugConfig.getUniqueName());
  101. if (StringUtil.isBlank(drugConfig.getForm())) {
  102. queryWrapper.and(i -> i.isNull("form")
  103. .or()
  104. .eq("form", ""));
  105. } else {
  106. queryWrapper.eq("form", drugConfig.getForm());
  107. }
  108. DrugConfig oldRecord = drugConfigService.getOne(queryWrapper, false);
  109. if (drugConfig.getId() == null
  110. && oldRecord != null) {
  111. throw new CommonException(CommonErrorCode.IS_EXISTS, "该条关联已存在,无法保存");
  112. }
  113. if (drugConfig.getId() != null
  114. && oldRecord != null
  115. && !drugConfig.getId().equals(oldRecord.getId())) {
  116. throw new CommonException(CommonErrorCode.IS_EXISTS, "该条关联已存在,无法保存");
  117. }
  118. //新增数据
  119. if (drugConfig.getId() == null) {
  120. drugConfig.setCreator(userId);
  121. drugConfig.setGmtCreate(now);
  122. }
  123. if (drugConfig.getIsDeleted() == null) {
  124. drugConfig.setIsDeleted(IsDeleteEnum.N.getKey());
  125. }
  126. drugConfigService.saveOrUpdate(drugConfig);
  127. return true;
  128. }
  129. /**
  130. * 保存记录-批量
  131. *
  132. * @param drugConfigListVO
  133. * @return
  134. */
  135. public Boolean saveOrUpdateRecords(DrugConfigListVO drugConfigListVO) {
  136. if (ListUtil.isEmpty(drugConfigListVO.getDrugConfigList())) {
  137. return false;
  138. }
  139. return saveOrUpdateRecords(drugConfigListVO.getDrugConfigList());
  140. }
  141. /**
  142. * 批量保存
  143. *
  144. * @param drugConfigList
  145. * @return
  146. */
  147. public Boolean saveOrUpdateRecords(List<DrugConfig> drugConfigList) {
  148. if (ListUtil.isEmpty(drugConfigList)) {
  149. return false;
  150. }
  151. String userId = UserUtils.getCurrentPrincipleID();
  152. Date now = DateUtil.now();
  153. //数据不完整的不保存
  154. //过滤外部名称或公表名为空的数据
  155. drugConfigList = drugConfigList
  156. .stream()
  157. .filter(i -> i.getHospitalId() != null)
  158. .filter(i -> StringUtil.isNotBlank(i.getHisName()))
  159. .filter(i -> StringUtil.isNotBlank(i.getUniqueName()))
  160. .collect(Collectors.toList());
  161. if (ListUtil.isEmpty(drugConfigList)) {
  162. return false;
  163. }
  164. Long hospitalId = drugConfigList.get(0).getHospitalId();
  165. List<String> hisNames = drugConfigList
  166. .stream()
  167. .map(i -> i.getHisName())
  168. .collect(Collectors.toList());
  169. List<String> uniqueNames = drugConfigList
  170. .stream()
  171. .map(i -> i.getUniqueName())
  172. .collect(Collectors.toList());
  173. // 验证数据是否已存在,已存在的先删除
  174. // 没id的删除重新插入,有id的更新
  175. List<Long> deleteIds = Lists.newLinkedList();
  176. Map<String, Map<String, Map<String, List<Long>>>> configMap
  177. = getConfigMap(hospitalId, hisNames, uniqueNames);
  178. drugConfigList.forEach(drugConfig -> {
  179. drugConfig.setHospitalId(Long.valueOf(hospitalId));
  180. drugConfig.setModifier(userId);
  181. drugConfig.setGmtModified(now);
  182. String form = StringUtil.isBlank(drugConfig.getForm()) ? "" : drugConfig.getForm();
  183. if (drugConfig.getId() == null) {
  184. if (configMap.get(drugConfig.getHisName()) != null
  185. && configMap.get(drugConfig.getHisName()).get(form) != null
  186. && ListUtil.isNotEmpty(configMap.get(drugConfig.getHisName()).get(form).get(drugConfig.getUniqueName()))) {
  187. deleteIds.addAll(configMap.get(drugConfig.getHisName()).get(form).get(drugConfig.getUniqueName()));
  188. }
  189. drugConfig.setCreator(userId);
  190. drugConfig.setGmtCreate(now);
  191. }
  192. if (drugConfig.getIsDeleted() == null) {
  193. drugConfig.setIsDeleted(IsDeleteEnum.N.getKey());
  194. }
  195. });
  196. //删除已存在映射关系
  197. IdListVO idListVO = new IdListVO();
  198. idListVO.setIds(deleteIds);
  199. deleteRecords(idListVO);
  200. drugConfigService.saveOrUpdateBatch(drugConfigList);
  201. return true;
  202. }
  203. /**
  204. * 删除记录-单条
  205. *
  206. * @param idVO
  207. * @return
  208. */
  209. public Boolean deleteRecord(IdVO idVO) {
  210. UpdateWrapper<DrugConfig> updateWrapper = new UpdateWrapper<>();
  211. updateWrapper.eq("id", idVO.getId())
  212. .set("is_deleted", IsDeleteEnum.Y.getKey());
  213. drugConfigService.removeById(idVO.getId());
  214. return true;
  215. }
  216. /**
  217. * 删除记录-批量
  218. *
  219. * @param idListVO
  220. * @return
  221. */
  222. public Boolean deleteRecords(IdListVO idListVO) {
  223. if (ListUtil.isEmpty(idListVO.getIds())) {
  224. return false;
  225. }
  226. UpdateWrapper<DrugConfig> updateWrapper = new UpdateWrapper<>();
  227. updateWrapper.in("id", idListVO.getIds())
  228. .set("is_deleted", IsDeleteEnum.Y.getKey());
  229. drugConfigService.removeByIds(idListVO.getIds());
  230. return true;
  231. }
  232. /**
  233. * 分页查询
  234. *
  235. * @param drugConfigPageVO
  236. * @return
  237. */
  238. public IPage<DrugConfig> getPage(DrugConfigPageVO drugConfigPageVO) {
  239. return drugConfigService.getPage(drugConfigPageVO);
  240. }
  241. /**
  242. * 数据导入
  243. *
  244. * @param file
  245. * @param hospitalIdVO
  246. */
  247. public void importExcel(MultipartFile file, HospitalIdVO hospitalIdVO) {
  248. List<DrugConfig> drugConfigList = ExcelUtils.importExcel(file, 1, 1, DrugConfig.class);
  249. if (ListUtil.isNotEmpty(drugConfigList)) {
  250. drugConfigList.forEach(drugConfig -> {
  251. drugConfig.setHospitalId(hospitalIdVO.getHospitalId());
  252. });
  253. importExcelRecords(drugConfigList, hospitalIdVO);
  254. } else {
  255. throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "校验失败,导入数据不能为空");
  256. }
  257. }
  258. /**
  259. * 数据导入
  260. *
  261. * @param drugConfigList
  262. * @return
  263. */
  264. public Boolean importExcelRecords(List<DrugConfig> drugConfigList,HospitalIdVO hospitalIdVO) {
  265. Long hospitalId = hospitalIdVO.getHospitalId();
  266. String userId = UserUtils.getCurrentPrincipleID();
  267. Date now = DateUtil.now();
  268. //1、数据完整性校验
  269. //2、去除前后空格
  270. //过滤空数据,保留重复数据,方便计行
  271. drugConfigList = drugConfigList.stream()
  272. .filter(i -> StringUtil.isNotBlank(i.getHisName())
  273. || StringUtil.isNotBlank(i.getForm())
  274. || StringUtil.isNotBlank(i.getUniqueCode())
  275. || StringUtil.isNotBlank(i.getUniqueName()))
  276. .collect(Collectors.toList());
  277. if (ListUtil.isEmpty(drugConfigList)) {
  278. throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "校验失败,导入数据不能为空");
  279. }
  280. List<String> emptyNumList = Lists.newLinkedList();
  281. //药品剂型
  282. List<DictionaryInfoDTO> dicTypeMappingList = dictionaryFacade.getListByGroupType(9);
  283. List<String> formList = dicTypeMappingList.stream()
  284. .filter(i -> StringUtil.isNotBlank(i.getName()))
  285. .map(i -> i.getName())
  286. .distinct()
  287. .collect(Collectors.toList());
  288. List<String> formErrNumList = Lists.newLinkedList();
  289. for (int i = 0; i < drugConfigList.size(); i++) {
  290. if (StringUtil.isBlank(drugConfigList.get(i).getHisName())
  291. || StringUtil.isBlank(drugConfigList.get(i).getUniqueName())) {
  292. emptyNumList.add(String.valueOf(i + 3));
  293. }
  294. if (StringUtil.isNotBlank(drugConfigList.get(i).getHisName())) {
  295. drugConfigList.get(i).setHisName(drugConfigList.get(i).getHisName().trim());
  296. }
  297. if (StringUtil.isNotBlank(drugConfigList.get(i).getUniqueName())) {
  298. drugConfigList.get(i).setUniqueName(drugConfigList.get(i).getUniqueName().trim());
  299. }
  300. if (StringUtil.isNotBlank(drugConfigList.get(i).getForm())) {
  301. if (!formList.contains(drugConfigList.get(i).getForm())) {
  302. formErrNumList.add(String.valueOf(i + 3));
  303. } else {
  304. drugConfigList.get(i).setForm(drugConfigList.get(i).getForm().trim());
  305. }
  306. }
  307. if (StringUtil.isNotBlank(drugConfigList.get(i).getUniqueCode())) {
  308. drugConfigList.get(i).setUniqueCode(drugConfigList.get(i).getUniqueCode().trim());
  309. } else {
  310. drugConfigList.get(i).setUniqueCode(null);
  311. }
  312. }
  313. if (ListUtil.isNotEmpty(emptyNumList)) {
  314. throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "以下行数(不计入空行)存在不完整数据:"
  315. + emptyNumList.stream().collect(Collectors.joining("、"))
  316. + "。导入取消,请修改后再试。\n");
  317. }
  318. if (ListUtil.isNotEmpty(formErrNumList)) {
  319. throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "以下行数(不计入空行)药品剂型与数据库药品剂型不匹配:"
  320. + formErrNumList.stream().collect(Collectors.joining("、"))
  321. + "。导入取消,请修改后再试。\n");
  322. }
  323. List<String> hisNames = drugConfigList
  324. .stream()
  325. .map(i -> i.getHisName())
  326. .collect(Collectors.toList());
  327. List<String> uniqueNames = drugConfigList
  328. .stream()
  329. .map(i -> i.getUniqueName())
  330. .collect(Collectors.toList());
  331. // 验证数据是否已存在,已存在的先删除
  332. // 没id的删除重新插入,有id的更新
  333. List<Long> deleteIds = Lists.newLinkedList();
  334. Map<String, Map<String, Map<String, List<Long>>>> configMap
  335. = getConfigMap(Long.valueOf(hospitalId), hisNames, uniqueNames);
  336. drugConfigList.forEach(drugConfig -> {
  337. drugConfig.setHospitalId(Long.valueOf(hospitalId));
  338. drugConfig.setModifier(userId);
  339. drugConfig.setGmtModified(now);
  340. String form = StringUtil.isBlank(drugConfig.getForm()) ? "" : drugConfig.getForm();
  341. if (drugConfig.getId() == null) {
  342. if (configMap.get(drugConfig.getHisName()) != null
  343. && configMap.get(drugConfig.getHisName()).get(form) != null
  344. && ListUtil.isNotEmpty(configMap.get(drugConfig.getHisName()).get(form).get(drugConfig.getUniqueName()))) {
  345. deleteIds.addAll(configMap.get(drugConfig.getHisName()).get(form).get(drugConfig.getUniqueName()));
  346. }
  347. drugConfig.setCreator(userId);
  348. drugConfig.setGmtCreate(now);
  349. }
  350. if (drugConfig.getIsDeleted() == null) {
  351. drugConfig.setIsDeleted(IsDeleteEnum.N.getKey());
  352. }
  353. });
  354. //标准术语校验
  355. List<String> errorNumList = Lists.newLinkedList();
  356. ConceptVO conceptVO = new ConceptVO();
  357. conceptVO.setNames(uniqueNames);
  358. conceptVO.setType(ConceptTypeEnum.Drug.getKey());
  359. RespDTO<List<String>> respDTO = cdssCoreClient.getConceptNames(conceptVO);
  360. RespDTOUtil.respNGDealCover(respDTO, "标准术语校验失败");
  361. List<String> names = respDTO.data;
  362. for (int i = 0; i < drugConfigList.size(); i++) {
  363. if (!names.contains(drugConfigList.get(i).getUniqueName())) {
  364. errorNumList.add(String.valueOf(i + 2));
  365. }
  366. }
  367. if (ListUtil.isNotEmpty(errorNumList)) {
  368. throw new CommonException(CommonErrorCode.PARAM_IS_NULL,
  369. "以下行数(不计空行)标准术语在数据库中不存在:"
  370. + errorNumList.stream().collect(Collectors.joining("、"))
  371. + "。导入取消,请修改后再试。");
  372. }
  373. //重复数据过滤
  374. drugConfigList = drugConfigList
  375. .stream()
  376. .distinct()
  377. .collect(Collectors.toList());
  378. //删除已存在映射关系
  379. IdListVO idListVO = new IdListVO();
  380. idListVO.setIds(deleteIds);
  381. deleteRecords(idListVO);
  382. drugConfigService.saveOrUpdateBatch(drugConfigList);
  383. return true;
  384. }
  385. /**
  386. * 获取映射关系-公表名
  387. *
  388. * @param hospitalId
  389. * @param hisNames
  390. * @param uniqueNames
  391. * @return Map<hisName,Map<form,Map<uniqueName,id>>>
  392. */
  393. public Map<String, Map<String,Map<String, List<Long>>>> getConfigMap(Long hospitalId, List<String> hisNames, List<String> uniqueNames) {
  394. Map<String, Map<String, Map<String, List<Long>>>> retMap = new HashMap<>();
  395. QueryWrapper<DrugConfig> queryWrapper = new QueryWrapper<>();
  396. queryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey())
  397. .eq("hospital_id", hospitalId);
  398. if (ListUtil.isNotEmpty(hisNames)) {
  399. queryWrapper.in("his_name", hisNames);
  400. }
  401. if (ListUtil.isNotEmpty(uniqueNames)) {
  402. queryWrapper.in("unique_name", uniqueNames);
  403. }
  404. List<DrugConfig> records = drugConfigService.list(queryWrapper);
  405. if (ListUtil.isEmpty(records)) {
  406. return retMap;
  407. }
  408. Map<String, List<DrugConfig>> hisNameMap = EntityUtil.makeEntityListMap(records, "hisName");
  409. for (Map.Entry<String, List<DrugConfig>> entry : hisNameMap.entrySet()) {
  410. if (ListUtil.isNotEmpty(entry.getValue())) {
  411. Map<String, Map<String, List<Long>>> formMap = new HashMap<>();
  412. entry.getValue().forEach(i -> {
  413. if (StringUtil.isBlank(i.getForm())) {
  414. i.setForm("");
  415. }
  416. });
  417. Map<String, List<DrugConfig>> subMap
  418. = EntityUtil.makeEntityListMap(entry.getValue(), "form");
  419. for (Map.Entry<String, List<DrugConfig>> subEntry : subMap.entrySet()) {
  420. if (ListUtil.isNotEmpty(subEntry.getValue())) {
  421. Map<String, List<DrugConfig>> thirdMap = EntityUtil.makeEntityListMap(subEntry.getValue(), "uniqueName");
  422. Map<String, List<Long>> idMap = new HashMap<>();
  423. for (Map.Entry<String, List<DrugConfig>> thirdEntry : thirdMap.entrySet()) {
  424. idMap.put(thirdEntry.getKey(), thirdEntry.getValue().stream().map(i -> i.getId()).distinct().collect(Collectors.toList()));
  425. }
  426. formMap.put(subEntry.getKey(), idMap);
  427. }
  428. }
  429. retMap.put(entry.getKey(), formMap);
  430. }
  431. }
  432. return retMap;
  433. }
  434. /**
  435. * 获取映射关系-公表名
  436. *
  437. * @param hospitalId
  438. * @param hisNames
  439. * @param uniqueNames
  440. * @return Map<uniqueName,Map<hisName,Map<form,List<id>>>>
  441. */
  442. public Map<String, Map<String,Map<String, List<Long>>>> getUniqueConfigMap(Long hospitalId, List<String> hisNames, List<String> uniqueNames) {
  443. Map<String, Map<String, Map<String, List<Long>>>> retMap = new HashMap<>();
  444. QueryWrapper<DrugConfig> queryWrapper = new QueryWrapper<>();
  445. queryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey())
  446. .eq("hospital_id", hospitalId);
  447. if (ListUtil.isNotEmpty(hisNames)) {
  448. queryWrapper.in("his_name", hisNames);
  449. }
  450. if (ListUtil.isNotEmpty(uniqueNames)) {
  451. queryWrapper.in("unique_name", uniqueNames);
  452. }
  453. List<DrugConfig> records = drugConfigService.list(queryWrapper);
  454. if (ListUtil.isEmpty(records)) {
  455. return retMap;
  456. }
  457. records.forEach(record -> {
  458. if (StringUtil.isBlank(record.getForm())) {
  459. record.setForm("");
  460. }
  461. });
  462. Map<String, List<DrugConfig>> uniqueNameMap = EntityUtil.makeEntityListMap(records, "uniqueName");
  463. for (Map.Entry<String, List<DrugConfig>> entry : uniqueNameMap.entrySet()) {
  464. if (ListUtil.isNotEmpty(entry.getValue())) {
  465. Map<String, Map<String, List<Long>>> subMap = new HashMap<>();
  466. Map<String, List<DrugConfig>> hisNameMap
  467. = EntityUtil.makeEntityListMap(entry.getValue(), "hisName");
  468. for (Map.Entry<String, List<DrugConfig>> hisEntry : hisNameMap.entrySet()) {
  469. if (ListUtil.isNotEmpty(hisEntry.getValue())) {
  470. Map<String, List<DrugConfig>> thirdMap = EntityUtil.makeEntityListMap(hisEntry.getValue(), "form");
  471. Map<String, List<Long>> idMap = new HashMap<>();
  472. for (Map.Entry<String, List<DrugConfig>> thirdEntry : thirdMap.entrySet()) {
  473. idMap.put(thirdEntry.getKey(), thirdEntry.getValue().stream().map(i -> i.getId()).distinct().collect(Collectors.toList()));
  474. }
  475. subMap.put(hisEntry.getKey(), idMap);
  476. }
  477. }
  478. retMap.put(entry.getKey(), subMap);
  479. }
  480. }
  481. return retMap;
  482. }
  483. /**
  484. * 获取映射关系-公表名
  485. *
  486. * @param hospitalId
  487. * @param hisNames
  488. * @param uniqueNames
  489. * @return Map<hisName,Map<uniqueName,id>>
  490. */
  491. public Map<String, Map<String,Long>> getConfigMapWithoutForm(Long hospitalId, List<String> hisNames, List<String> uniqueNames) {
  492. Map<String, Map<String, Long>> retMap = new HashMap<>();
  493. QueryWrapper<DrugConfig> queryWrapper = new QueryWrapper<>();
  494. queryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey())
  495. .eq("hospital_id", hospitalId);
  496. if (ListUtil.isNotEmpty(hisNames)) {
  497. queryWrapper.in("his_name", hisNames);
  498. }
  499. if (ListUtil.isNotEmpty(uniqueNames)) {
  500. queryWrapper.in("unique_name", uniqueNames);
  501. }
  502. List<DrugConfig> records = drugConfigService.list(queryWrapper);
  503. if (ListUtil.isEmpty(records)) {
  504. return retMap;
  505. }
  506. Map<String, List<DrugConfig>> hisNameMap
  507. = EntityUtil.makeEntityListMap(records, "hisName");
  508. for (Map.Entry<String, List<DrugConfig>> entry : hisNameMap.entrySet()) {
  509. if (ListUtil.isNotEmpty(entry.getValue())) {
  510. retMap.put(entry.getKey(),
  511. EntityUtil.makeMapWithKeyValue(entry.getValue(), "uniqueName", "id"));
  512. }
  513. }
  514. return retMap;
  515. }
  516. /**
  517. * 获取映射关系
  518. * Map<uniqueName,Map<hisName,id>>
  519. *
  520. * @param hospitalId
  521. * @param hisNames
  522. * @param uniqueNames
  523. * @return
  524. */
  525. public Map<String,Map<String,Long>> getUniqueNameConfigMapWithoutForm(Long hospitalId, List<String> hisNames, List<String> uniqueNames) {
  526. Map<String, Map<String, Long>> retMap = new HashMap<>();
  527. QueryWrapper<DrugConfig> queryWrapper = new QueryWrapper<>();
  528. queryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey())
  529. .eq("hospital_id", hospitalId);
  530. if (ListUtil.isNotEmpty(hisNames)) {
  531. queryWrapper.in("his_name", hisNames);
  532. }
  533. if (ListUtil.isNotEmpty(uniqueNames)) {
  534. queryWrapper.in("unique_name", uniqueNames);
  535. }
  536. List<DrugConfig> records = drugConfigService.list(queryWrapper);
  537. if (ListUtil.isEmpty(records)) {
  538. return retMap;
  539. }
  540. records.forEach(i -> {
  541. if (StringUtil.isBlank(i.getForm())) {
  542. i.setForm("");
  543. }
  544. });
  545. Map<String, List<DrugConfig>> uniqueNameMap
  546. = EntityUtil.makeEntityListMap(records, "uniqueName");
  547. for (Map.Entry<String, List<DrugConfig>> entry : uniqueNameMap.entrySet()) {
  548. if (ListUtil.isNotEmpty(entry.getValue())) {
  549. retMap.put(entry.getKey(),
  550. EntityUtil.makeMapWithKeyValue(entry.getValue(), "hisName", "id"));
  551. }
  552. }
  553. return retMap;
  554. }
  555. /**
  556. * 数据导出
  557. *
  558. * @param response
  559. * @param hospitalIdVO
  560. */
  561. public void exportExcel(HttpServletResponse response, HospitalIdVO hospitalIdVO) {
  562. QueryWrapper<DrugConfig> queryWrapper = new QueryWrapper<>();
  563. queryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey())
  564. .eq("hospital_id", hospitalIdVO.getHospitalId())
  565. .orderByDesc("gmt_modified");
  566. List<DrugConfig> records = drugConfigService.list(queryWrapper);
  567. String fileName = "药品映射.xls";
  568. ExcelUtils.exportExcel(records, getFrom(), "sheet1", DrugConfig.class, fileName, response, 12.8f);
  569. }
  570. /**
  571. * 各医院映射关系数列表
  572. *
  573. * @param hosRelationNumPageVO
  574. * @return
  575. */
  576. public IPage<HosRelationNumDTO> getRelationNumPage(HosRelationNumPageVO hosRelationNumPageVO) {
  577. return drugConfigService.getRelationNumPage(hosRelationNumPageVO);
  578. }
  579. /**
  580. * 数据导入模板导出
  581. *
  582. * @param response
  583. */
  584. public void exportExcelModule(HttpServletResponse response) {
  585. String fileName = "药品映射模板.xls";
  586. ExcelUtils.exportExcel(new ArrayList<>(), getFrom(), "sheet1", DrugConfig.class, fileName, response, 12.8f);
  587. }
  588. /**
  589. * 剂型说明
  590. *
  591. * @return
  592. */
  593. private String getFrom() {
  594. String from = "药品模板——药品剂型填写说明[不填";
  595. //药品剂型
  596. List<DictionaryInfoDTO> dicTypeMappingList = dictionaryFacade.getListByGroupType(9);
  597. List<String> formList = dicTypeMappingList.stream()
  598. .filter(i -> StringUtil.isNotBlank(i.getName()))
  599. .map(i -> i.getName())
  600. .distinct()
  601. .collect(Collectors.toList());
  602. if (ListUtil.isNotEmpty(formList)) {
  603. for (String s : formList) {
  604. if (StringUtil.isNotBlank(s)) {
  605. from += "、" + s;
  606. }
  607. }
  608. }
  609. from += "]";
  610. return from;
  611. }
  612. /**
  613. * 导入数据预匹配
  614. * @param file
  615. * @param response
  616. */
  617. public void precDataMatch(MultipartFile file,HttpServletResponse response) {
  618. List<DrugConfig> originList = ExcelUtils.importExcel(file, 1, 1, DrugConfig.class);
  619. List<DrugConfig> retList = dataProcess(originList);
  620. String fileName = "药品关联数据(预匹配).xls";
  621. ExcelUtils.exportExcel(retList, getFrom(), "sheet1", DrugConfig.class, fileName, response, 12.8f);
  622. }
  623. /**
  624. * 导入数据验证
  625. *
  626. * @param file
  627. * @return
  628. */
  629. public Boolean dataVerify(MultipartFile file) {
  630. List<DrugConfig> originList = ExcelUtils.importExcel(file, 1, 1, DrugConfig.class);
  631. List<DrugConfig> retList = dataProcess(originList);
  632. return true;
  633. }
  634. /**
  635. * 数据处理
  636. *
  637. * @param originList
  638. * @return
  639. */
  640. public List<DrugConfig> dataProcess(List<DrugConfig> originList) {
  641. List<DrugConfig> retList = Lists.newLinkedList();
  642. List<String> hisNameList = originList.stream().map(i -> i.getHisName()).distinct().collect(Collectors.toList());
  643. Map<String, List<DrugConfig>> allMap = getAll(hisNameList);
  644. //药品剂型
  645. List<DictionaryInfoDTO> dicTypeMappingList = dictionaryFacade.getListByGroupType(9);
  646. List<String> formList = dicTypeMappingList.stream()
  647. .filter(i -> StringUtil.isNotBlank(i.getName()))
  648. .map(i -> i.getName())
  649. .distinct()
  650. .collect(Collectors.toList());
  651. //去除空格
  652. originList.forEach(item -> {
  653. item.setHisName(item.getHisName().trim());
  654. });
  655. //获取标准术语
  656. List<String> precUniqueName = Lists.newArrayList();
  657. if (allMap != null) {
  658. for (Map.Entry<String, List<DrugConfig>> entry : allMap.entrySet()) {
  659. if (ListUtil.isNotEmpty(entry.getValue())) {
  660. precUniqueName.addAll(entry.getValue().stream().map(i -> i.getUniqueName()).collect(Collectors.toList()));
  661. }
  662. }
  663. }
  664. precUniqueName = precUniqueName.stream().distinct().collect(Collectors.toList());
  665. ConceptVO conceptVO = new ConceptVO();
  666. conceptVO.setNames(precUniqueName);
  667. conceptVO.setType(ConceptTypeEnum.Drug.getKey());
  668. RespDTO<List<String>> respDTO = cdssCoreClient.getConceptNames(conceptVO);
  669. RespDTOUtil.respNGDealCover(respDTO, "标准术语校验失败");
  670. List<String> uniqueNames = respDTO.data;
  671. if (ListUtil.isNotEmpty(originList)) {
  672. for (DrugConfig originItem : originList) {
  673. if (allMap.containsKey(originItem.getHisName())) {
  674. List<DrugConfig> items = allMap.get(originItem.getHisName());
  675. boolean flag = false;
  676. for (DrugConfig item : items) {
  677. if (uniqueNames.contains(item.getUniqueName())) {
  678. if (!formList.contains(item.getForm())) {
  679. item.setForm("");
  680. }
  681. retList.add(item);
  682. flag = true;
  683. }
  684. }
  685. if (!flag) {
  686. if (!formList.contains(originItem.getForm())) {
  687. originItem.setForm("");
  688. }
  689. retList.add(originItem);
  690. }
  691. } else {
  692. retList.add(originItem);
  693. }
  694. }
  695. }
  696. retList = retList.stream()
  697. .distinct()
  698. .collect(Collectors.toList());
  699. return retList;
  700. }
  701. /**
  702. * 获取所有医院映射数据
  703. * @return
  704. */
  705. public Map<String,List<DrugConfig>> getAll(List<String> hisNameList) {
  706. Map<String, List<DrugConfig>> retMap = new HashMap<>();
  707. QueryWrapper<DrugConfig> queryWrapper = new QueryWrapper<>();
  708. if (ListUtil.isNotEmpty(hisNameList)) {
  709. queryWrapper.in("his_name", hisNameList);
  710. }
  711. List<DrugConfig> records = drugConfigService.list(queryWrapper);
  712. if (ListUtil.isEmpty(records)) {
  713. return retMap;
  714. }
  715. records.forEach(record -> {
  716. record.setHospitalId(null);
  717. record.setId(null);
  718. record.setUniqueCode(StringUtils.isBlank(record.getUniqueCode()) ? "" : record.getUniqueCode());
  719. record.setForm(StringUtils.isBlank(record.getForm()) ? null : record.getForm());
  720. });
  721. records = records
  722. .stream()
  723. .filter(record -> record.getIsDeleted().equals(IsDeleteEnum.N.getKey()))
  724. .distinct()
  725. .collect(Collectors.toList());
  726. if (ListUtil.isEmpty(records)) {
  727. return retMap;
  728. }
  729. retMap = EntityUtil.makeEntityListMap(records, "hisName");
  730. return retMap;
  731. }
  732. /**
  733. * 查找指定医院映射关系
  734. *
  735. * @param hospitalId
  736. * @return
  737. */
  738. public List<DrugConfig> getListByHospitalId(Long hospitalId) {
  739. QueryWrapper<DrugConfig> queryWrapper = new QueryWrapper<>();
  740. queryWrapper.eq("is_deleted", IsDeleteEnum.N.getKey())
  741. .eq("hospital_id", hospitalId);
  742. return drugConfigService.list(queryWrapper);
  743. }
  744. }