|
@@ -0,0 +1,225 @@
|
|
|
|
+package com.diagbot.facade;
|
|
|
|
+
|
|
|
|
+import cn.afterturn.easypoi.exception.excel.ExcelImportException;
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
+import com.diagbot.entity.KlConcept;
|
|
|
|
+import com.diagbot.entity.KlConceptDetail;
|
|
|
|
+import com.diagbot.entity.KlConceptStatic;
|
|
|
|
+import com.diagbot.entity.StaticExcel;
|
|
|
|
+import com.diagbot.entity.wrapper.StaticExcelWrapper;
|
|
|
|
+import com.diagbot.enums.IsDeleteEnum;
|
|
|
|
+import com.diagbot.enums.StatusEnum;
|
|
|
|
+import com.diagbot.exception.CommonErrorCode;
|
|
|
|
+import com.diagbot.exception.CommonException;
|
|
|
|
+import com.diagbot.service.KlConceptDetailService;
|
|
|
|
+import com.diagbot.util.BeanUtil;
|
|
|
|
+import com.diagbot.util.ExcelUtils;
|
|
|
|
+import com.diagbot.util.ListUtil;
|
|
|
|
+import com.diagbot.util.UserUtils;
|
|
|
|
+import org.apache.commons.compress.utils.Lists;
|
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
|
+
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.List;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * @Description:
|
|
|
|
+ * @Author:zhaops
|
|
|
|
+ * @time: 2022/5/18 14:42
|
|
|
|
+ */
|
|
|
|
+@Component
|
|
|
|
+public class StaticImportFacade {
|
|
|
|
+ @Autowired
|
|
|
|
+ private KlConceptFacade klConceptFacade;
|
|
|
|
+ @Autowired
|
|
|
|
+ private KlConceptStaticFacade klConceptStaticFacade;
|
|
|
|
+ @Qualifier("klConceptDetailServiceImpl")
|
|
|
|
+ @Autowired
|
|
|
|
+ private KlConceptDetailService klConceptDetailService;
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 导入模板下载
|
|
|
|
+ *
|
|
|
|
+ * @param response
|
|
|
|
+ */
|
|
|
|
+ public void exportExcelModule(HttpServletResponse response) {
|
|
|
|
+ String fileName = "静态知识导入模板.xls";
|
|
|
|
+ ExcelUtils.exportExcel(Lists.newArrayList(), null, "sheet1", StaticExcel.class, fileName, response, 12.8f);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 导出文件标题
|
|
|
|
+ *
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public String getTitle() {
|
|
|
|
+ String title = "说明\n" +
|
|
|
|
+ "类型:100-疾病、101-药品通用名、106-手术和操作、107-实验室检查套餐、108-实验室检查子项目、109-辅助检查项目、110-辅助检查子项目、124-量表、123-护理、130-政策法规;\n" +
|
|
|
|
+ "内容类型:1-静态信息,2-注意事项,3-临床路径,4-治疗方案;";
|
|
|
|
+ return title;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 实体批量导入
|
|
|
|
+ *
|
|
|
|
+ * @param response
|
|
|
|
+ * @param file
|
|
|
|
+ */
|
|
|
|
+ public void importExcel(HttpServletResponse response, MultipartFile file) {
|
|
|
|
+ String userId = UserUtils.getCurrentPrincipleID();
|
|
|
|
+ List<StaticExcel> originList = readImportData(file);
|
|
|
|
+ if (ListUtil.isEmpty(originList)) {
|
|
|
|
+ throw new CommonException(CommonErrorCode.PARAM_IS_NULL, "模板内容为空,请填写后导入!");
|
|
|
|
+ } else {
|
|
|
|
+ List<StaticExcelWrapper> list = importDataProcess(originList, userId);
|
|
|
|
+ List<StaticExcelWrapper> insertList = list.stream().filter(i -> i.getCorrect().equals(1)).collect(Collectors.toList());
|
|
|
|
+ List<StaticExcelWrapper> errorList = list.stream().filter(i -> i.getCorrect().equals(0)).collect(Collectors.toList());
|
|
|
|
+ String fileName = "静态知识导入结果.xls";
|
|
|
|
+ Map<String, Map<Class<?>, List<?>>> map = new HashMap<>();
|
|
|
|
+ Map<Class<?>, List<?>> errorMap = new HashMap<Class<?>, List<?>>();
|
|
|
|
+ errorMap.put(StaticExcelWrapper.class, errorList);
|
|
|
|
+ map.put("导入失败数据", errorMap);
|
|
|
|
+ Map<Class<?>, List<?>> successMap = new HashMap<Class<?>, List<?>>();
|
|
|
|
+ successMap.put(StaticExcelWrapper.class, insertList);
|
|
|
|
+ map.put("导入成功数据", successMap);
|
|
|
|
+ ExcelUtils.exportExcel(map, null, fileName, response);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 读取导入数据
|
|
|
|
+ *
|
|
|
|
+ * @param file
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public List<StaticExcel> readImportData(MultipartFile file) {
|
|
|
|
+ List<StaticExcel> originList = Lists.newArrayList();
|
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
|
+ //是否Excel文件?
|
|
|
|
+ if (!(fileName.endsWith(".xls") || fileName.endsWith(".xlsx"))) {
|
|
|
|
+ throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "校验失败,请根据模板进行数据导入");
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ originList = ExcelUtils.importExcel(file, 0, 1, StaticExcel.class);
|
|
|
|
+ } catch (ExcelImportException e) {
|
|
|
|
+
|
|
|
|
+ } catch (CommonException e) {
|
|
|
|
+ throw new CommonException(CommonErrorCode.SERVER_IS_ERROR, "校验失败,请根据模板进行数据导入");
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ return originList;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 导入数据处理
|
|
|
|
+ *
|
|
|
|
+ * @param originList
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public List<StaticExcelWrapper> importDataProcess(List<StaticExcel> originList, String userId) {
|
|
|
|
+ List<StaticExcelWrapper> retList = Lists.newArrayList();
|
|
|
|
+ if (ListUtil.isEmpty(originList)) {
|
|
|
|
+ return retList;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<StaticExcelWrapper> list = Lists.newArrayList();
|
|
|
|
+ list = BeanUtil.listCopyTo(originList, StaticExcelWrapper.class);
|
|
|
|
+
|
|
|
|
+ //删除空行
|
|
|
|
+ list = list.stream()
|
|
|
|
+ .filter(StaticExcelWrapper::nonNull)
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
+
|
|
|
|
+ List<String> nameAndTypeList = list.stream()
|
|
|
|
+ .map(i -> i.getName() + "_" + i.getType())
|
|
|
|
+ .distinct()
|
|
|
|
+ .collect(Collectors.toList());
|
|
|
|
+ List<KlConcept> conceptList = klConceptFacade.list(new QueryWrapper<KlConcept>()
|
|
|
|
+ .eq("is_deleted", IsDeleteEnum.N.getKey())
|
|
|
|
+ .eq("status", StatusEnum.Enable.getKey())
|
|
|
|
+ .in("concat(lib_name,'_',lib_type)", nameAndTypeList));
|
|
|
|
+ Map<String, Long> nameAndTypeMap_exist = new HashMap<>();
|
|
|
|
+ List<Long> conceptIdList = Lists.newArrayList();
|
|
|
|
+ if (ListUtil.isNotEmpty(conceptList)) {
|
|
|
|
+ nameAndTypeMap_exist = conceptList.stream()
|
|
|
|
+ .collect(Collectors.toMap(k -> k.getLibName() + "_" + k.getLibType(), v -> v.getId()));
|
|
|
|
+ conceptIdList = conceptList.stream().map(KlConcept::getId).collect(Collectors.toList());
|
|
|
|
+ }
|
|
|
|
+ //校验标准词是否存在
|
|
|
|
+ for (StaticExcelWrapper item : list) {
|
|
|
|
+ String nameAndType = item.getName() + "_" + item.getType();
|
|
|
|
+ if (!nameAndTypeMap_exist.containsKey(nameAndType)) {
|
|
|
|
+ item.setCorrect(0);
|
|
|
|
+ item.setMessage("标准词不存在或已禁用");
|
|
|
|
+ } else {
|
|
|
|
+ item.setConceptId(nameAndTypeMap_exist.get(nameAndType));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //标准词校验失败
|
|
|
|
+ if (ListUtil.isEmpty(conceptList)) {
|
|
|
|
+ return list;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //校验静态知识是否已存在
|
|
|
|
+ List<KlConceptStatic> staticList_exist = klConceptStaticFacade.list(new QueryWrapper<KlConceptStatic>()
|
|
|
|
+ .eq("is_deleted", IsDeleteEnum.N.getKey())
|
|
|
|
+ .in("concept_id", conceptIdList));
|
|
|
|
+ List<Long> staticConceptIdList = Lists.newArrayList();
|
|
|
|
+ if (ListUtil.isNotEmpty(staticList_exist)) {
|
|
|
|
+ staticConceptIdList = staticList_exist.stream()
|
|
|
|
+ .map(KlConceptStatic::getConceptId)
|
|
|
|
+ .distinct().collect(Collectors.toList());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (StaticExcelWrapper item : list) {
|
|
|
|
+ if (item.getCorrect().equals(0)) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if (staticConceptIdList.contains(item.getConceptId())) {
|
|
|
|
+ item.setCorrect(0);
|
|
|
|
+ item.setMessage("该术语关联静态知识已维护或已禁用");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<StaticExcelWrapper> insertList = list.stream().filter(i -> i.getCorrect().equals(1)).collect(Collectors.toList());
|
|
|
|
+
|
|
|
|
+ //保存数据(新增)
|
|
|
|
+ Date now = new Date();
|
|
|
|
+ Map<Long, List<StaticExcelWrapper>> insertMap = insertList.stream().collect(Collectors.groupingBy(StaticExcelWrapper::getConceptId));
|
|
|
|
+ for (Map.Entry<Long, List<StaticExcelWrapper>> entry : insertMap.entrySet()) {
|
|
|
|
+ KlConceptStatic klConceptStatic = new KlConceptStatic();
|
|
|
|
+ if (ListUtil.isEmpty(entry.getValue())) {
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ BeanUtils.copyProperties(entry.getValue().get(0), klConceptStatic);
|
|
|
|
+ klConceptStatic.setGmtCreate(now);
|
|
|
|
+ klConceptStatic.setCreator(userId);
|
|
|
|
+ klConceptStatic.setGmtModified(now);
|
|
|
|
+ klConceptStatic.setModifier(userId);
|
|
|
|
+ klConceptStaticFacade.saveOrUpdate(klConceptStatic);
|
|
|
|
+
|
|
|
|
+ List<KlConceptDetail> detailList = Lists.newArrayList();
|
|
|
|
+ detailList = BeanUtil.listCopyTo(entry.getValue(), KlConceptDetail.class);
|
|
|
|
+ for (KlConceptDetail klConceptDetail : detailList) {
|
|
|
|
+ klConceptDetail.setConceptId(klConceptStatic.getConceptId());
|
|
|
|
+ klConceptDetail.setGmtCreate(now);
|
|
|
|
+ klConceptDetail.setCreator(userId);
|
|
|
|
+ klConceptDetail.setGmtModified(now);
|
|
|
|
+ klConceptDetail.setModifier(userId);
|
|
|
|
+ }
|
|
|
|
+ klConceptDetailService.saveBatch(detailList);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return list;
|
|
|
|
+ }
|
|
|
|
+}
|