|
|
@@ -1,11 +1,32 @@
|
|
|
package org.jeecg.modules.medical.service.impl;
|
|
|
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+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.util.oConvertUtils;
|
|
|
+import org.jeecg.modules.medical.common.ExcelImportUtilService;
|
|
|
+import org.jeecg.modules.medical.entity.MaterialItems;
|
|
|
import org.jeecg.modules.medical.entity.Medicine;
|
|
|
import org.jeecg.modules.medical.mapper.MedicineMapper;
|
|
|
import org.jeecg.modules.medical.service.IMedicineService;
|
|
|
+import org.jeecg.modules.utils.DefaultExcelImportUtil;
|
|
|
+import org.jeecgframework.poi.excel.entity.ImportParams;
|
|
|
+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.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.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @Description: medicine
|
|
|
@@ -14,6 +35,148 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
* @Version: V1.0
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class MedicineServiceImpl extends ServiceImpl<MedicineMapper, Medicine> implements IMedicineService {
|
|
|
+ @Autowired
|
|
|
+ private ExcelImportUtilService<Medicine, String> excelImportUtilService;
|
|
|
+ @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"));
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("找不到用户登录的tokne");
|
|
|
+ }
|
|
|
+ final String finalUsername = username;
|
|
|
+
|
|
|
+ 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<Medicine> list = DefaultExcelImportUtil
|
|
|
+ .importExcel(file.getInputStream(), Medicine.class, params);
|
|
|
+ if (CollectionUtils.isEmpty(list)) {
|
|
|
+ log.error("导入文件没有数据,请检查");
|
|
|
+ return Result.ok("上传的文件没有数据,请检查");
|
|
|
+ }
|
|
|
+ totalNumber = list.size();
|
|
|
+ // 检查导入必填数据
|
|
|
+ for (Medicine item : list) {
|
|
|
+ // 医院项目编码
|
|
|
+ String itemIdHosp = item.getMedicineCode();
|
|
|
+ if (!StringUtils.hasText(itemIdHosp)) {
|
|
|
+ log.error("数据导入时,找不到医院项目编码,请检查");
|
|
|
+ throw new JeecgBootException("医院项目编码为空,医院项目编码必填,请检查!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 倒序去重
|
|
|
+ List<Medicine> 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<Medicine> dbExistsList = this.loadDbExistsList(list);
|
|
|
+ // 更新列表
|
|
|
+ List<Medicine> updateList = new ArrayList<>();
|
|
|
+ // 新增列表
|
|
|
+ List<Medicine> 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
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private String getDuplicateRm(Medicine medicine) {
|
|
|
+ return ifnull(medicine.getMedicineCode());
|
|
|
+ }
|
|
|
|
|
|
+ @Override
|
|
|
+ public List<Medicine> loadDbExistsList(List<Medicine> list) {
|
|
|
+ if (CollectionUtils.isEmpty(list)) {
|
|
|
+ return new ArrayList<>();
|
|
|
+ }
|
|
|
+ QueryWrapper<Medicine> queryWrapper = new QueryWrapper<>();
|
|
|
+ List<String> itemIdHostKeys = new ArrayList<>();
|
|
|
+ for (Medicine item : list) {
|
|
|
+ String medicineCode = item.getMedicineCode();
|
|
|
+ if (!StringUtils.hasText(medicineCode)) {
|
|
|
+ log.error("数据导入时,没有找到{}请检查导入文件内容", "医保项目编码");
|
|
|
+ throw new JeecgBootException("没有找到项目编码,请检查导入内容");
|
|
|
+ }
|
|
|
+ itemIdHostKeys.add(medicineCode);
|
|
|
+ }
|
|
|
+ String itemIdHostSQLField = oConvertUtils.camelToUnderline("medicineCode");
|
|
|
+ queryWrapper.in(itemIdHostSQLField, itemIdHostKeys);
|
|
|
+ return this.list(queryWrapper);
|
|
|
+ }
|
|
|
}
|