|
@@ -1,11 +1,28 @@
|
|
|
package org.jeecg.modules.kpiImportList.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.kpiImportList.entity.KpiImportList;
|
|
|
import org.jeecg.modules.kpiImportList.mapper.KpiImportListMapper;
|
|
|
import org.jeecg.modules.kpiImportList.service.IKpiImportListService;
|
|
|
+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.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.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* @Description: 年指标导入
|
|
@@ -14,6 +31,61 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
* @Version: V1.0
|
|
|
*/
|
|
|
@Service
|
|
|
+@Slf4j
|
|
|
public class KpiImportListServiceImpl extends ServiceImpl<KpiImportListMapper, KpiImportList> implements IKpiImportListService {
|
|
|
-
|
|
|
+ /**
|
|
|
+ * 通过excel导入数据
|
|
|
+ * 导入年指标,平均计算月指标并导入
|
|
|
+ *
|
|
|
+ * @param request
|
|
|
+ * @param response
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response, Class<KpiImportList> 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);
|
|
|
+ params.setHeadRows(1);
|
|
|
+ params.setNeedSave(true);
|
|
|
+ try {
|
|
|
+ List<KpiImportList> list = ExcelImportUtil.importExcel(file.getInputStream(), clazz, params);
|
|
|
+ List<KpiImportList> listNew = new ArrayList<>(); //用来存放计算的月指标以及去除空值的数据
|
|
|
+ for (KpiImportList kpiImportList : list) {
|
|
|
+ if (kpiImportList.getDepartment() != null && kpiImportList.getKpi() != null && kpiImportList.getYear() != null) {
|
|
|
+ kpiImportList.setKpiMonth(kpiImportList.getKpi().divide(BigDecimal.valueOf(12), 2, RoundingMode.HALF_UP));
|
|
|
+ listNew.add(kpiImportList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //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("文件导入失败!");
|
|
|
+ }
|
|
|
}
|