|
|
@@ -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);
|
|
|
+ }
|
|
|
}
|