Эх сурвалжийг харах

add:规则知识:基础信息-导入时,添加去重和数据库已有数据的更新逻辑
优化数据库字段驼峰与java getter/setter冲突的问题

Scott 2 жил өмнө
parent
commit
8da743bffd

+ 2 - 1
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/medical/controller/MedicalInsuranceDrugsController.java

@@ -172,7 +172,8 @@ public class MedicalInsuranceDrugsController extends JeecgController<MedicalInsu
 //    @RequiresPermissions("medical:medical_insurance_drugs:importExcel")
     @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
     public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
-        return super.importExcel(request, response, MedicalInsuranceDrugs.class);
+		return medicalInsuranceDrugsService.importExcel(request, response);
+//        return super.importExcel(request, response, MedicalInsuranceDrugs.class);
     }
 
 }

+ 7 - 7
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/medical/entity/MedicalInsuranceDrugs.java

@@ -4,10 +4,8 @@ import java.io.Serializable;
 import java.io.UnsupportedEncodingException;
 import java.util.Date;
 import java.math.BigDecimal;
-import com.baomidou.mybatisplus.annotation.IdType;
-import com.baomidou.mybatisplus.annotation.TableId;
-import com.baomidou.mybatisplus.annotation.TableName;
-import com.baomidou.mybatisplus.annotation.TableLogic;
+
+import com.baomidou.mybatisplus.annotation.*;
 import lombok.Data;
 import com.fasterxml.jackson.annotation.JsonFormat;
 import org.springframework.format.annotation.DateTimeFormat;
@@ -74,13 +72,15 @@ public class MedicalInsuranceDrugs implements Serializable {
     @ApiModelProperty(value = "用量")
     private java.lang.String drugUseTotal;
 	/**自付比例;甲类为100%,乙类根据当地实际情况(80%,70%),丙类(0%)*/
-	@Excel(name = "自付比例", width = 15)
+	@TableField(value = "p_type_pct")
+    @Excel(name = "自付比例", width = 15)
     @ApiModelProperty(value = "自付比例;甲类为100%,乙类根据当地实际情况(80%,70%),丙类(0%)")
-    private java.lang.String pTypePct;
+    private java.lang.String ptypePct;
 	/**支付类别;指甲、乙、丙类*/
+    @TableField(value = "p_category")
 	@Excel(name = "支付类别", width = 15)
     @ApiModelProperty(value = "支付类别;指甲、乙、丙类")
-    private java.lang.String pCategory;
+    private java.lang.String pcategory;
 	/**备注;药品使用范围、限制性范围备注*/
 	@Excel(name = "备注", width = 15)
     @ApiModelProperty(value = "备注;药品使用范围、限制性范围备注")

+ 19 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/medical/service/IMedicalInsuranceDrugsService.java

@@ -1,8 +1,13 @@
 package org.jeecg.modules.medical.service;
 
+import org.jeecg.common.api.vo.Result;
 import org.jeecg.modules.medical.entity.MedicalInsuranceDrugs;
 import com.baomidou.mybatisplus.extension.service.IService;
 
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.util.List;
+
 /**
  * @Description: medical_insurance_drugs
  * @Author: jeecg-boot
@@ -11,4 +16,18 @@ import com.baomidou.mybatisplus.extension.service.IService;
  */
 public interface IMedicalInsuranceDrugsService extends IService<MedicalInsuranceDrugs> {
 
+    /**
+     * 按照模板导入
+     * @param request
+     * @param response
+     */
+    Result<?> importExcel(HttpServletRequest request, HttpServletResponse response);
+
+
+    /**
+     * 根据医保药品编码获取数据库已经存在的数据
+     * @param medicineCodes
+     * @return
+     */
+    List<MedicalInsuranceDrugs> listByMedicineCodeIn(List<String> medicineCodes);
 }

+ 111 - 0
jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/medical/service/impl/MedicalInsuranceDrugsServiceImpl.java

@@ -1,11 +1,30 @@
 package org.jeecg.modules.medical.service.impl;
 
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.math3.geometry.partitioning.BSPTree;
+import org.jeecg.common.api.vo.Result;
 import org.jeecg.modules.medical.entity.MedicalInsuranceDrugs;
 import org.jeecg.modules.medical.mapper.MedicalInsuranceDrugsMapper;
 import org.jeecg.modules.medical.service.IMedicalInsuranceDrugsService;
+import org.jeecgframework.poi.excel.ExcelImportUtil;
+import org.jeecgframework.poi.excel.entity.ImportParams;
 import org.springframework.stereotype.Service;
 
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import org.springframework.transaction.annotation.Transactional;
+import org.springframework.util.CollectionUtils;
+import org.springframework.util.ObjectUtils;
+import org.springframework.web.multipart.MultipartFile;
+import org.springframework.web.multipart.MultipartHttpServletRequest;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.transaction.TransactionScoped;
+import java.io.IOException;
+import java.util.*;
+import java.util.stream.Collectors;
 
 /**
  * @Description: medical_insurance_drugs
@@ -13,7 +32,99 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  * @Date:   2023-04-23
  * @Version: V1.0
  */
+@Slf4j
+@Transactional
 @Service
 public class MedicalInsuranceDrugsServiceImpl extends ServiceImpl<MedicalInsuranceDrugsMapper, MedicalInsuranceDrugs> implements IMedicalInsuranceDrugsService {
 
+    @Transactional(readOnly = false)
+    @Override
+    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
+        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<MedicalInsuranceDrugs> list = ExcelImportUtil.importExcel(file.getInputStream(), MedicalInsuranceDrugs.class, params);
+                if (CollectionUtils.isEmpty(list)) {
+                    return Result.error("文件导入失败:没有找到要导入的数据!");
+                }
+                if (list.size() > 10000) {
+                    return Result.error("文件导入失败:不可一次性导入超过10000条数据!");
+                }
+                // 去重
+                list = list.stream().collect(
+                        Collectors.collectingAndThen(
+                                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(MedicalInsuranceDrugs::getMedicineCode))), ArrayList::new
+                        )
+                );
+
+                //检查是否有更新的数据,根据医保药品编码
+                List<String> collect = list.stream().map(MedicalInsuranceDrugs::getMedicineCode).collect(Collectors.toList());
+                List<MedicalInsuranceDrugs> dbExistsList = this.listByMedicineCodeIn(collect);
+                // 更新列表
+                List<MedicalInsuranceDrugs> updateList = new ArrayList<>();
+                // 新增列表
+                List<MedicalInsuranceDrugs> instalList = list;
+                if (!CollectionUtils.isEmpty(dbExistsList)) { // 不为空时, 需要将存在的做更新操作
+                    // 取出交集
+                    updateList = list.stream().filter(
+                            a -> dbExistsList.stream().map(MedicalInsuranceDrugs::getMedicineCode)
+                                    .anyMatch(
+                                            medicineCode -> ObjectUtils.nullSafeEquals(a.getMedicineCode(), medicineCode)
+                                    )
+                    ).collect(Collectors.toList());
+                    // 如果有则需要更新
+                    if (!CollectionUtils.isEmpty(updateList)) {
+                        this.updateBatchById(updateList, 500);
+                    }
+                    // 取出差集
+                    instalList = list.stream().filter(
+                            excelData -> dbExistsList.stream().map(MedicalInsuranceDrugs::getMedicineCode).noneMatch(medicineCode ->
+                                    ObjectUtils.nullSafeEquals(excelData.getMedicineCode(), medicineCode))
+                    ).collect(Collectors.toList());
+                }
+                //update-begin-author:taoyan date:20190528 for:批量插入数据
+                long start = System.currentTimeMillis();
+                if (!CollectionUtils.isEmpty(instalList)) {
+                    this.saveBatch(instalList, 500);
+                }
+
+                //400条 saveBatch消耗时间1592毫秒  循环插入消耗时间1947毫秒
+                //1200条  saveBatch消耗时间3687毫秒 循环插入消耗时间5212毫秒
+                log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
+                //update-end-author:taoyan date:20190528 for:批量插入数据
+                return Result.ok("文件导入成功!数据行数:" + list.size());
+            } catch (Exception e) {
+                //update-begin-author:taoyan date:20211124 for: 导入数据重复增加提示
+                String msg = e.getMessage();
+                log.error(msg, e);
+                if(msg!=null && msg.contains("Duplicate entry")){
+                    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("文件导入失败!");
+    }
+
+    @Override
+    public List<MedicalInsuranceDrugs> listByMedicineCodeIn(List<String> medicineCodes) {
+        LambdaQueryWrapper<MedicalInsuranceDrugs> in = Wrappers.<MedicalInsuranceDrugs>lambdaQuery()
+                .in(MedicalInsuranceDrugs::getMedicineCode, medicineCodes);
+        return list(in);
+    }
 }