|
@@ -1,11 +1,27 @@
|
|
|
package org.jeecg.modules.costModelWxPrice.service.impl;
|
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.jeecg.common.api.vo.Result;
|
|
|
import org.jeecg.modules.costModelWxPrice.entity.CostModelWxPrice;
|
|
|
import org.jeecg.modules.costModelWxPrice.mapper.CostModelWxPriceMapper;
|
|
|
import org.jeecg.modules.costModelWxPrice.service.ICostModelWxPriceService;
|
|
|
+import org.jeecgframework.poi.excel.ExcelImportUtil;
|
|
|
+import org.jeecgframework.poi.excel.entity.ImportParams;
|
|
|
+import org.jetbrains.annotations.NotNull;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+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.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.*;
|
|
|
|
|
|
/**
|
|
|
* @Description: 外协价格库
|
|
@@ -14,6 +30,127 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
* @Version: V1.0
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class CostModelWxPriceServiceImpl extends ServiceImpl<CostModelWxPriceMapper, CostModelWxPrice> implements ICostModelWxPriceService {
|
|
|
|
|
|
+ @Autowired
|
|
|
+ @SuppressWarnings("all")
|
|
|
+ public CostModelWxPriceMapper wxpMapper;
|
|
|
+
|
|
|
+ /**查看表中所有数据-用来判断导入的数据是否有重复数据*/
|
|
|
+ public List<CostModelWxPrice> getWXPAllDetailList(){
|
|
|
+ return wxpMapper.getWXPAllDetailList();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过excel导入数据
|
|
|
+ *如果数据重复,删除重复的数据,然后在写入数据库
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response, Class<CostModelWxPrice> clazz) {
|
|
|
+ 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(0);/*表格标题所占行数(默认是0)*/
|
|
|
+ params.setHeadRows(1);/*表头所占据的行数行数,默认1,代表标题占据一行*/
|
|
|
+ params.setNeedSave(true);
|
|
|
+ try {
|
|
|
+ //1.从excel中获取到数据放入到list中
|
|
|
+ List<CostModelWxPrice> list = ExcelImportUtil.importExcel(file.getInputStream(), clazz, params);
|
|
|
+ System.out.println("从excel表中获取的数据");
|
|
|
+ list.forEach(System.out::println);
|
|
|
+ System.out.println(list.getClass());
|
|
|
+ System.out.println(list.getClass().getName());
|
|
|
+
|
|
|
+ //2.新建一个list来给list中的数据去重
|
|
|
+ List<CostModelWxPrice> listNew = new ArrayList<>();
|
|
|
+ for(CostModelWxPrice wxp:list){
|
|
|
+// System.out.println(wxp);
|
|
|
+// System.out.println(wxp.getWxProject());
|
|
|
+// System.out.println(wxp.getWxCompany());
|
|
|
+// System.out.println(wxp.getDanjia().getClass());
|
|
|
+// String wxpj = wxp.getWxProject(); //外协项目
|
|
|
+// String wxcom = wxp.getWxCompany(); //外协单位
|
|
|
+// String dj = wxp.getDanjia(); //单价
|
|
|
+// String unit = wxp.getUnit(); //计量单位
|
|
|
+ if (!listNew.contains(wxp)) {
|
|
|
+ listNew.add(wxp);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.out.println("列表去重后的数据");
|
|
|
+ listNew.forEach(System.out::println);
|
|
|
+ System.out.println(listNew.getClass());
|
|
|
+ System.out.println(listNew.getClass().getName());
|
|
|
+
|
|
|
+ //3.从数据库中读取全部表中数据
|
|
|
+ List<CostModelWxPrice> list1 = getWXPAllDetailList();
|
|
|
+ System.out.println("从数据库中取出的数据");
|
|
|
+ list1.forEach(System.out::println);
|
|
|
+ System.out.println(list1.getClass());
|
|
|
+ System.out.println(list1.getClass().getName());
|
|
|
+
|
|
|
+ Iterator<CostModelWxPrice> iterator = listNew.iterator();
|
|
|
+ //4.判断去重后的listNew中是否有数据已经在数据库中记录过了,如果已经保存过直接删掉
|
|
|
+ while(iterator.hasNext()){
|
|
|
+ CostModelWxPrice wxpNew = iterator.next();
|
|
|
+ System.out.println("进入第一层循环");
|
|
|
+ System.out.println(wxpNew);
|
|
|
+ if((wxpNew.getWxProject()!=null)&(wxpNew.getDanjia()!=null)&(wxpNew.getUnit()!=null)&(wxpNew.getWxCompany()!=null)){
|
|
|
+ for(CostModelWxPrice wxp1:list1){
|
|
|
+ System.out.println("进入第二层循环判断数据是否已经存在");
|
|
|
+// System.out.println(wxp1);
|
|
|
+ if((wxpNew.getWxProject().equals(wxp1.getWxProject()))&(wxpNew.getDanjia().equals(wxp1.getDanjia()))&(wxpNew.getUnit().equals(wxp1.getUnit()))&(wxpNew.getWxCompany().equals(wxp1.getWxCompany()))){
|
|
|
+ System.out.println("进入判断");
|
|
|
+ iterator.remove();
|
|
|
+ System.out.println("当前元素重复,进行删除");
|
|
|
+ //listNew.forEach(System.out::println); //foreach中不能remove,不然会报错java.util.ConcurrentModificationException
|
|
|
+ System.out.println();
|
|
|
+ break;//因为wxpNew元素已经被删除了,所有此段循环可以跳过了
|
|
|
+ }
|
|
|
+ System.out.println("第二层循环还在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else{//如果外协价格表中的四个字段中有值为null,则删除该条记录
|
|
|
+ iterator.remove();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ System.out.println("最终需要存入的数据");
|
|
|
+ listNew.forEach(System.out::println);
|
|
|
+ System.out.println(listNew.getClass());
|
|
|
+ System.out.println(listNew.getClass().getName());
|
|
|
+
|
|
|
+ //update-begin-author:taoyan date:20190528 for:批量插入数据
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
+ this.saveBatch(listNew);
|
|
|
+ //400条 saveBatch消耗时间1592毫秒 循环插入消耗时间1947毫秒
|
|
|
+ //1200条 saveBatch消耗时间3687毫秒 循环插入消耗时间5212毫秒
|
|
|
+ log.info("消耗时间" + (System.currentTimeMillis() - start) + "毫秒");
|
|
|
+ //update-end-author:taoyan date:20190528 for:批量插入数据
|
|
|
+ return Result.ok("文件导入成功!数据行数:" + listNew.size());
|
|
|
+ } 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("文件导入失败!");
|
|
|
+ }
|
|
|
}
|