|
|
@@ -1,29 +1,36 @@
|
|
|
package org.jeecg.modules.medical.service.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson.util.BiFunction;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import net.sf.saxon.s9api.TeeDestination;
|
|
|
import org.apache.shiro.SecurityUtils;
|
|
|
import org.jeecg.common.api.vo.Result;
|
|
|
import org.jeecg.common.exception.JeecgBootException;
|
|
|
import org.jeecg.common.system.query.QueryGenerator;
|
|
|
-import org.jeecg.common.system.vo.DictDto;
|
|
|
+import org.jeecg.common.system.util.JwtUtil;
|
|
|
import org.jeecg.common.system.vo.LoginUser;
|
|
|
import org.jeecg.common.system.vo.MedicalDto;
|
|
|
import org.jeecg.common.util.oConvertUtils;
|
|
|
+import org.jeecg.modules.medical.common.ExcelImportUtilService;
|
|
|
+import org.jeecg.modules.medical.common.bo.ExportRuleFieldDTO;
|
|
|
import org.jeecg.modules.medical.common.bo.ExportRuleTitleDTO;
|
|
|
+import org.jeecg.modules.medical.entity.MaterialItems;
|
|
|
import org.jeecg.modules.medical.entity.MedicalInsRuleProject;
|
|
|
import org.jeecg.modules.medical.mapper.MedicalInsRuleProjectMapper;
|
|
|
import org.jeecg.modules.medical.service.IMedicalInsRuleProjectService;
|
|
|
+import org.jeecg.modules.system.service.ISysDataSourceService;
|
|
|
import org.jeecg.modules.system.service.ISysDictService;
|
|
|
+import org.jeecg.modules.utils.DefaultExcelImportServer;
|
|
|
import org.jeecg.modules.utils.DefaultExcelImportUtil;
|
|
|
-import org.jeecgframework.poi.excel.ExcelImportUtil;
|
|
|
import org.jeecgframework.poi.excel.annotation.Excel;
|
|
|
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
|
|
|
import org.jeecgframework.poi.excel.entity.ExportParams;
|
|
|
import org.jeecgframework.poi.excel.entity.ImportParams;
|
|
|
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
|
|
|
import org.jeecgframework.poi.util.PoiPublicUtil;
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -40,10 +47,10 @@ import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
-import java.lang.reflect.Array;
|
|
|
import java.lang.reflect.Field;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.util.*;
|
|
|
+import java.util.function.Function;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -57,10 +64,14 @@ import java.util.stream.Collectors;
|
|
|
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
|
|
public class MedicalInsRuleProjectServiceImpl extends ServiceImpl<MedicalInsRuleProjectMapper, MedicalInsRuleProject> implements IMedicalInsRuleProjectService {
|
|
|
|
|
|
+ public static final String PROJECTCODE = "projectCode";
|
|
|
+ public static final String CORRELATIONPROJECTCODE = "correlationProjectCode";
|
|
|
+
|
|
|
@Value("${jeecg.path.upload}")
|
|
|
private String upLoadPath;
|
|
|
|
|
|
private final ISysDictService sysDictService;
|
|
|
+ private final ExcelImportUtilService<MedicalInsRuleProject, String> excelImportUtilService;
|
|
|
|
|
|
@Override
|
|
|
public ModelAndView exportTemplateXls(HttpServletRequest request, MedicalInsRuleProject object, Class<MedicalInsRuleProject> clazz, String title) throws UnsupportedEncodingException {
|
|
|
@@ -148,7 +159,25 @@ public class MedicalInsRuleProjectServiceImpl extends ServiceImpl<MedicalInsRule
|
|
|
|
|
|
@Transactional(readOnly = false)
|
|
|
@Override
|
|
|
- public Result<?> importTemplateExcel(HttpServletRequest request, HttpServletResponse response, Class<MedicalInsRuleProject> medicalInsRuleProjectClass) {
|
|
|
+ public Result<?> importTemplateExcel(HttpServletRequest request, HttpServletResponse response,
|
|
|
+ Class<MedicalInsRuleProject> medicalInsRuleProjectClass, Map<String, Integer> args1) {
|
|
|
+
|
|
|
+ String username = "";
|
|
|
+ try {
|
|
|
+ username = JwtUtil.getUsername(request.getHeader("X-Access-Token"));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("找不到用户登录的tokne");
|
|
|
+ }
|
|
|
+ final String finalUsername = username;
|
|
|
+ if (CollectionUtils.isEmpty(args1) || args1.containsKey("id")) {
|
|
|
+ log.error("导入时,必须携带项目规则id,参数为id");
|
|
|
+ throw new JeecgBootException("项目规则id不可为空");
|
|
|
+ }
|
|
|
+ Integer id = args1.get("id");
|
|
|
+ if (null == id) {
|
|
|
+ log.error("导入时,必须携带项目规则id,参数为id, id为null");
|
|
|
+ throw new JeecgBootException("项目规则id不可为空");
|
|
|
+ }
|
|
|
|
|
|
|
|
|
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
|
@@ -163,24 +192,32 @@ public class MedicalInsRuleProjectServiceImpl extends ServiceImpl<MedicalInsRule
|
|
|
|
|
|
|
|
|
//需要ID
|
|
|
- ExportRuleTitleDTO exportRuleTitle = baseMapper.selectTitleNameById(2);
|
|
|
+ ExportRuleTitleDTO exportRuleTitle = baseMapper.selectTitleNameById(id);
|
|
|
if (null == exportRuleTitle) {
|
|
|
log.error("根据规则id={},获取不到规则表头配置");
|
|
|
throw new JeecgBootException("找不到规则表头");
|
|
|
}
|
|
|
+ // 查询字段名称
|
|
|
+ List<String> fieldList = new ArrayList<>();
|
|
|
List<String> reserveList = new ArrayList<>();
|
|
|
if (StringUtils.hasText(exportRuleTitle.getSelectedRoles())) {
|
|
|
String[] split = exportRuleTitle.getSelectedRoles().split(",");
|
|
|
if (split.length > 0) {
|
|
|
// 获取标头
|
|
|
- List<MedicalDto> dictDtos = sysDictService.queryDictByKeys(new ArrayList<>(Arrays.asList(split)));
|
|
|
+ fieldList = new ArrayList<>(Arrays.asList(split));
|
|
|
+ List<MedicalDto> dictDtos = sysDictService.queryDictByKeys(fieldList);
|
|
|
if (!CollectionUtils.isEmpty(dictDtos)) {
|
|
|
reserveList = dictDtos.stream().map(MedicalDto::getText).collect(Collectors.toList());
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ // 获取本规则的字段名
|
|
|
+ List<ExportRuleFieldDTO> ruleFieldDTOS = baseMapper.selectRuleFieldListByFieldList(fieldList);
|
|
|
+ if (CollectionUtils.isEmpty(ruleFieldDTOS)) {
|
|
|
+ log.error("找不到规则={}对应的字段映射,字段={}", id);
|
|
|
+ throw new JeecgBootException("获取不到规则对应的字段映射");
|
|
|
+ }
|
|
|
|
|
|
- LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
|
|
|
|
|
|
|
|
|
Field[] fields = PoiPublicUtil.getClassFields(medicalInsRuleProjectClass);
|
|
|
@@ -207,11 +244,50 @@ public class MedicalInsRuleProjectServiceImpl extends ServiceImpl<MedicalInsRule
|
|
|
// 设置忽略列表
|
|
|
params.setIgnoreHeaderList(ignoreField);
|
|
|
try {
|
|
|
+ List<String> excelFieldLists = new DefaultExcelImportServer().getExcelFieldLists(file.getInputStream(), 0);
|
|
|
+ if (CollectionUtils.isEmpty(excelFieldLists)) {
|
|
|
+ log.error("规则导入时,上传的文档没有找不到标题头");
|
|
|
+ throw new JeecgBootException("规则导入时,找不到Excel的标题头,请确认模板是否正确");
|
|
|
+ }
|
|
|
+ //检查有没有比预期的多
|
|
|
+ //检查有没有比预期的少
|
|
|
+
|
|
|
+
|
|
|
// List<MedicalInsRuleProject> list = ExcelImportUtil.importExcel(file.getInputStream(), MedicalInsRuleProject.class, params);
|
|
|
- List<MedicalInsRuleProject> list = DefaultExcelImportUtil.importExcel(file.getInputStream(), MedicalInsRuleProject.class, params);
|
|
|
+ List<MedicalInsRuleProject> list = DefaultExcelImportUtil
|
|
|
+ .importExcel(file.getInputStream(), MedicalInsRuleProject.class, params);
|
|
|
//update-begin-author:taoyan date:20190528 for:批量插入数据
|
|
|
+
|
|
|
+ // 根据条件,查询数据库去重。
|
|
|
+ List<MedicalInsRuleProject> dbExistsList = this.loadDbExistsList(list, exportRuleTitle);
|
|
|
+ // 更新列表
|
|
|
+ List<MedicalInsRuleProject> updateList = new ArrayList<>();
|
|
|
+ // 新增列表
|
|
|
+ List<MedicalInsRuleProject> instalList = list;
|
|
|
+ if (!CollectionUtils.isEmpty(dbExistsList)) { // 不为空时, 需要将存在的做更新操作
|
|
|
+ // 取出交集
|
|
|
+ updateList = excelImportUtilService.getUpdateDatas(list, dbExistsList,
|
|
|
+ this::getDuplicateRm,
|
|
|
+ (nd, od) -> {
|
|
|
+ nd.setId(od.getId());
|
|
|
+ nd.setUpdateBy(finalUsername); //更新人
|
|
|
+ nd.setUpdateTime(new Date());
|
|
|
+ return nd;
|
|
|
+ });//
|
|
|
+ // 如果有则需要更新
|
|
|
+ if (!CollectionUtils.isEmpty(updateList)) {
|
|
|
+ this.updateBatchById(updateList, 500);
|
|
|
+ }
|
|
|
+ // 取出差集
|
|
|
+ instalList = excelImportUtilService.getInsertDatas(list, dbExistsList, this::getDuplicateRm);
|
|
|
+ //extractDiffSet.apply(list, dbExistsList);
|
|
|
+ }
|
|
|
long start = System.currentTimeMillis();
|
|
|
- this.saveBatch(list);
|
|
|
+ if (!CollectionUtils.isEmpty(instalList)) {
|
|
|
+// this.saveBatch(instalList, 500);
|
|
|
+// insertService.apply(instalList, 500);
|
|
|
+ this.saveBatch(instalList, 500);
|
|
|
+ }
|
|
|
//400条 saveBatch消耗时间1592毫秒 循环插入消耗时间1947毫秒
|
|
|
//1200条 saveBatch消耗时间3687毫秒 循环插入消耗时间5212毫秒
|
|
|
log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
|
|
|
@@ -237,4 +313,60 @@ public class MedicalInsRuleProjectServiceImpl extends ServiceImpl<MedicalInsRule
|
|
|
}
|
|
|
return Result.error("文件导入失败!");
|
|
|
}
|
|
|
+
|
|
|
+ private String getDuplicateRm(MedicalInsRuleProject medicalInsRuleProject) {
|
|
|
+ return medicalInsRuleProject.getProjectCode().concat(ifnull(medicalInsRuleProject.getCorrelationProjectCode()));
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private String ifnull(String str) {
|
|
|
+ if (StringUtils.hasText(str)) {
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据导入数据,检查数据库是否存在
|
|
|
+ *
|
|
|
+ * @param list
|
|
|
+ * @param exportRuleTitle
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<MedicalInsRuleProject> loadDbExistsList(List<MedicalInsRuleProject> list, ExportRuleTitleDTO exportRuleTitle) {
|
|
|
+ // 固定这两个字段,projectCode为必须,correlationProjectCode为可选
|
|
|
+ if (exportRuleTitle.getSelectedRoles().indexOf(PROJECTCODE) <= -1) {
|
|
|
+ log.warn("导入时,没有找到字段={}, 本次导入不做导入数据与数据库去重", PROJECTCODE);
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ if (CollectionUtils.isEmpty(list)) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ QueryWrapper<MedicalInsRuleProject> queryWrapper = new QueryWrapper<MedicalInsRuleProject>();
|
|
|
+ for (MedicalInsRuleProject item : list) {
|
|
|
+ String projectCode = item.getProjectCode();
|
|
|
+ if (!StringUtils.hasText(projectCode)) {
|
|
|
+ log.error("根据模板导入时,没有找到projectCode请检查导入文件内容");
|
|
|
+ throw new JeecgBootException("没有找到项目编码,请检查导入内容");
|
|
|
+ }
|
|
|
+ String projectCodeField = oConvertUtils.camelToUnderline("projectCode");
|
|
|
+ queryWrapper.in(projectCodeField, projectCodeField);
|
|
|
+ if (exportRuleTitle.getSelectedRoles().indexOf(CORRELATIONPROJECTCODE) > -1) {
|
|
|
+ String correlationProjectCodeField = oConvertUtils.camelToUnderline(CORRELATIONPROJECTCODE);
|
|
|
+ String correlationProjectCode = item.getCorrelationProjectCode();
|
|
|
+ if (StringUtils.hasText(correlationProjectCode)) {
|
|
|
+ queryWrapper.in(correlationProjectCodeField, correlationProjectCode);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return this.list(queryWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+// String role = "a,b,c";
|
|
|
+// System.out.println(role.indexOf("a") > -1);
|
|
|
+ String s = oConvertUtils.camelToUnderline("projectCode");
|
|
|
+ System.out.println(s);
|
|
|
+ }
|
|
|
}
|