|
|
@@ -2,29 +2,41 @@ package org.jeecg.modules.medical.service.impl;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.jeecg.common.api.vo.Result;
|
|
|
+import org.jeecg.common.exception.JeecgBootException;
|
|
|
import org.jeecg.common.system.util.JwtUtil;
|
|
|
+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.entity.AdviceDetailsVO;
|
|
|
-import org.jeecg.modules.medical.entity.MaterialItems;
|
|
|
-import org.jeecg.modules.medical.entity.MedicalInsuranceDrugs;
|
|
|
-import org.jeecg.modules.medical.entity.TreatmentItems;
|
|
|
+import org.jeecg.modules.medical.common.bo.ExportRuleFieldDTO;
|
|
|
+import org.jeecg.modules.medical.common.bo.ExportRuleTitleDTO;
|
|
|
+import org.jeecg.modules.medical.entity.*;
|
|
|
import org.jeecg.modules.medical.mapper.MaterialItemsMapper;
|
|
|
import org.jeecg.modules.medical.service.IMaterialItemsService;
|
|
|
+import org.jeecg.modules.utils.DefaultExcelImportServer;
|
|
|
+import org.jeecg.modules.utils.DefaultExcelImportUtil;
|
|
|
+import org.jeecgframework.poi.excel.annotation.Excel;
|
|
|
+import org.jeecgframework.poi.excel.entity.ImportParams;
|
|
|
+import org.jeecgframework.poi.util.PoiPublicUtil;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.ObjectUtils;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+import org.springframework.web.multipart.MultipartHttpServletRequest;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.lang.reflect.Field;
|
|
|
import java.math.BigDecimal;
|
|
|
-import java.util.Date;
|
|
|
-import java.util.List;
|
|
|
-import java.util.Optional;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -33,6 +45,7 @@ import java.util.stream.Collectors;
|
|
|
* @Date: 2023-04-23
|
|
|
* @Version: V1.0
|
|
|
*/
|
|
|
+@Slf4j
|
|
|
@Service
|
|
|
public class MaterialItemsServiceImpl extends ServiceImpl<MaterialItemsMapper, MaterialItems> implements IMaterialItemsService {
|
|
|
|
|
|
@@ -41,6 +54,12 @@ public class MaterialItemsServiceImpl extends ServiceImpl<MaterialItemsMapper, M
|
|
|
|
|
|
@Override
|
|
|
public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
|
|
|
+
|
|
|
+ Integer rmDuplicateNumber = 0; // excel文件重复数量
|
|
|
+ Integer totalNumber = 0;//导入文件总数
|
|
|
+ Integer addNumber = 0; //新增数量
|
|
|
+ Integer updateNumber = 0;//更新文件数量
|
|
|
+
|
|
|
String username = "";
|
|
|
try {
|
|
|
username = JwtUtil.getUsername(request.getHeader("X-Access-Token"));
|
|
|
@@ -48,46 +67,138 @@ public class MaterialItemsServiceImpl extends ServiceImpl<MaterialItemsMapper, M
|
|
|
log.error("找不到用户登录的tokne");
|
|
|
}
|
|
|
final String finalUsername = username;
|
|
|
- return null;
|
|
|
- /*return excelImportUtilService.importExcel(request, MaterialItems.class,
|
|
|
- // 根据什么字段去重
|
|
|
- MaterialItems::getItemId,
|
|
|
- // 根据导入数据,查询哪些在数据库中已经有了
|
|
|
- list -> {
|
|
|
- List<String> collect = list.stream().map(MaterialItems::getItemId).collect(Collectors.toList());
|
|
|
- return listByItemIdIn(collect);
|
|
|
- },
|
|
|
- // 根据导入数据,和数据库中的数据,取出交集 用于更新
|
|
|
- (list, dbExistsList) ->{
|
|
|
- List<MaterialItems> collect = list.stream().filter(
|
|
|
- a -> dbExistsList.stream().map(MaterialItems::getItemId)
|
|
|
- .anyMatch(
|
|
|
- medicineCode -> ObjectUtils.nullSafeEquals(a.getItemId(), medicineCode)
|
|
|
- )
|
|
|
- ).collect(Collectors.toList());
|
|
|
- //取出交集之后,要将相同数据的数据库id给新数据
|
|
|
- if (!CollectionUtils.isEmpty(collect)) {
|
|
|
- collect = collect.stream().peek(item -> {
|
|
|
- Optional<MaterialItems> first = dbExistsList.stream().filter(o ->
|
|
|
- ObjectUtils.nullSafeEquals(o.getItemId(), item.getItemId())).findFirst();
|
|
|
- if (first.isPresent()) {
|
|
|
- MaterialItems materialItem = first.get();
|
|
|
- item.setId(materialItem.getId());
|
|
|
- item.setUpdateTime(new Date());
|
|
|
- item.setUpdateBy(finalUsername);
|
|
|
- }
|
|
|
- }).collect(Collectors.toList());
|
|
|
+
|
|
|
+ MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
|
|
|
+ Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
|
|
|
+ for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
|
|
|
+ // 获取上传文件对象
|
|
|
+ MultipartFile file = entity.getValue();
|
|
|
+ ImportParams params = new ImportParams();
|
|
|
+ params.setTitleRows(2);
|
|
|
+ params.setHeadRows(1);
|
|
|
+ params.setNeedSave(true);
|
|
|
+
|
|
|
+ try {
|
|
|
+ List<MaterialItems> list = DefaultExcelImportUtil
|
|
|
+ .importExcel(file.getInputStream(), MaterialItems.class, params);
|
|
|
+ if (CollectionUtils.isEmpty(list)) {
|
|
|
+ log.error("导入文件没有数据,请检查");
|
|
|
+ return Result.ok("上传的文件没有数据,请检查");
|
|
|
+ }
|
|
|
+ totalNumber = list.size();
|
|
|
+ // 检查导入必填数据
|
|
|
+ for (MaterialItems item : list) {
|
|
|
+ // 医院项目编码
|
|
|
+ String itemIdHosp = item.getItemIdHosp();
|
|
|
+ if (!StringUtils.hasText(itemIdHosp)) {
|
|
|
+ log.error("数据导入时,找不到医院项目编码,请检查");
|
|
|
+ throw new JeecgBootException("医院项目编码为空,医院项目编码必填,请检查!");
|
|
|
}
|
|
|
- return collect;
|
|
|
- } ,
|
|
|
- // 根据导入数据,和数据库中的数据,取出差集 用于新增
|
|
|
- (list, dbExistsList) -> list.stream().filter(
|
|
|
- excelData -> dbExistsList.stream().map(MaterialItems::getItemId).noneMatch(medicineCode ->
|
|
|
- ObjectUtils.nullSafeEquals(excelData.getItemId(), medicineCode))
|
|
|
- ).collect(Collectors.toList()),
|
|
|
- this::updateBatchById,
|
|
|
- this::saveBatch
|
|
|
- );*/
|
|
|
+ }
|
|
|
+ // 倒序去重
|
|
|
+ List<MaterialItems> collect = list.stream().map(item -> item).collect(Collectors.toList());
|
|
|
+ Collections.reverse(collect);
|
|
|
+ list = collect.stream().collect(
|
|
|
+ Collectors.collectingAndThen(
|
|
|
+ Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(
|
|
|
+ this::getDuplicateRm
|
|
|
+ ))), ArrayList::new
|
|
|
+ )
|
|
|
+ );
|
|
|
+ // 随后把顺序倒回来
|
|
|
+ Collections.reverse(list);
|
|
|
+ // 文件总条数 - 去重之后剩余数量 = excel 文件的重复数据
|
|
|
+ if (!CollectionUtils.isEmpty(list)) {
|
|
|
+ rmDuplicateNumber = totalNumber - list.size();
|
|
|
+ }
|
|
|
+
|
|
|
+ // 根据条件,查询数据库去重。
|
|
|
+ List<MaterialItems> dbExistsList = this.loadDbExistsList(list);
|
|
|
+ // 更新列表
|
|
|
+ List<MaterialItems> updateList = new ArrayList<>();
|
|
|
+ // 新增列表
|
|
|
+ List<MaterialItems> 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)) {
|
|
|
+ updateNumber = updateList.size();
|
|
|
+ this.updateBatchById(updateList, 500);
|
|
|
+ }
|
|
|
+ // 取出差集
|
|
|
+ instalList = excelImportUtilService.getInsertDatas(list, dbExistsList, this::getDuplicateRm);
|
|
|
+ }
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ if (!CollectionUtils.isEmpty(instalList)) {
|
|
|
+ addNumber = instalList.size();
|
|
|
+ this.saveBatch(instalList, 500);
|
|
|
+ }
|
|
|
+ log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
|
|
|
+ //update-end-author:taoyan date:20190528 for:批量插入数据
|
|
|
+ return Result.ok("文件导入成功,请点击刷新!文件总行数:" + totalNumber + "; 文件自身重复:" + rmDuplicateNumber + "; 新增行数: " + addNumber + "; 更新行数: " + updateNumber);
|
|
|
+ } catch (Exception e) {
|
|
|
+ //update-begin-author:taoyan date:20211124 for: 导入数据重复增加提示
|
|
|
+ String msg = e.getMessage();
|
|
|
+ log.error(msg, e);
|
|
|
+ if (msg != null && msg.indexOf("Duplicate entry") >= 0) {
|
|
|
+ return Result.error("文件导入失败:有重复数据!");
|
|
|
+ } else {
|
|
|
+ return Result.error("文件导入失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ //update-end-author:taoyan date:20211124 for: 导入数据重复增加提示
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ file.getInputStream().close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Result.error("文件导入失败!");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据导入数据,检查数据库是否存在
|
|
|
+ *
|
|
|
+ * @param list
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public List<MaterialItems> loadDbExistsList(List<MaterialItems> list) {
|
|
|
+ // 固定这两个字段,projectCode为必须,correlationProjectCode为可选
|
|
|
+ if (CollectionUtils.isEmpty(list)) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ QueryWrapper<MaterialItems> queryWrapper = new QueryWrapper<>();
|
|
|
+ List<String> itemIdHostKeys = new ArrayList<>();
|
|
|
+ for (MaterialItems item : list) {
|
|
|
+ String itemIdHosp = item.getItemIdHosp();
|
|
|
+ if (!StringUtils.hasText(itemIdHosp)) {
|
|
|
+ log.error("数据导入时,没有找到{}请检查导入文件内容", "医保项目编码");
|
|
|
+ throw new JeecgBootException("没有找到项目编码,请检查导入内容");
|
|
|
+ }
|
|
|
+ itemIdHostKeys.add(itemIdHosp);
|
|
|
+ }
|
|
|
+ String itemIdHostSQLField = oConvertUtils.camelToUnderline("itemIdHosp");
|
|
|
+ queryWrapper.in(itemIdHostSQLField, itemIdHostKeys);
|
|
|
+ return this.list(queryWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构建去重因子
|
|
|
+ * @param medicalInsRuleProject
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getDuplicateRm(MaterialItems materialItems) {
|
|
|
+ return ifnull(materialItems.getItemIdHosp());
|
|
|
}
|
|
|
|
|
|
@Override
|