|
@@ -3,17 +3,20 @@ package com.hwrj.cloud.admin.listener;
|
|
import com.alibaba.excel.EasyExcel;
|
|
import com.alibaba.excel.EasyExcel;
|
|
import com.alibaba.excel.context.AnalysisContext;
|
|
import com.alibaba.excel.context.AnalysisContext;
|
|
import com.alibaba.excel.event.AnalysisEventListener;
|
|
import com.alibaba.excel.event.AnalysisEventListener;
|
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
import com.hwrj.cloud.admin.dao.PmsProductExcelDao;
|
|
import com.hwrj.cloud.admin.dao.PmsProductExcelDao;
|
|
import com.hwrj.cloud.admin.dto.param.PmsProductExcel;
|
|
import com.hwrj.cloud.admin.dto.param.PmsProductExcel;
|
|
import com.hwrj.cloud.admin.dto.param.PmsProductParam;
|
|
import com.hwrj.cloud.admin.dto.param.PmsProductParam;
|
|
-import com.hwrj.cloud.security.util.SpringUtil;
|
|
+import lombok.SneakyThrows;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.beans.BeanUtils;
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
-import sun.plugin2.util.SystemUtil;
|
|
|
|
|
|
|
|
-import java.io.File;
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
+import java.text.DecimalFormat;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -26,14 +29,19 @@ import java.util.regex.Pattern;
|
|
@Component
|
|
@Component
|
|
public class PmsProductExcelListener extends AnalysisEventListener<PmsProductExcel> {
|
|
public class PmsProductExcelListener extends AnalysisEventListener<PmsProductExcel> {
|
|
|
|
|
|
|
|
+ private static final Logger LOGGER = LoggerFactory.getLogger(PmsProductExcelListener.class);
|
|
|
|
+
|
|
private PmsProductExcelDao productExcelDao;
|
|
private PmsProductExcelDao productExcelDao;
|
|
|
|
|
|
public PmsProductExcelListener() {
|
|
public PmsProductExcelListener() {
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- public PmsProductExcelListener(PmsProductExcelDao productExcelDao) {
|
|
+ private HttpServletResponse response;
|
|
|
|
+
|
|
|
|
+ public PmsProductExcelListener(PmsProductExcelDao productExcelDao, HttpServletResponse response) {
|
|
this.productExcelDao = productExcelDao;
|
|
this.productExcelDao = productExcelDao;
|
|
|
|
+ this.response = response;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
@@ -44,76 +52,109 @@ public class PmsProductExcelListener extends AnalysisEventListener<PmsProductExc
|
|
|
|
|
|
List<PmsProductParam> list = new ArrayList();
|
|
List<PmsProductParam> list = new ArrayList();
|
|
|
|
|
|
- List<PmsProductParam> erroList = new ArrayList<>();
|
|
+ List<PmsProductExcel> erroList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
public void invoke(PmsProductExcel pmsProductExcel, AnalysisContext analysisContext) {
|
|
public void invoke(PmsProductExcel pmsProductExcel, AnalysisContext analysisContext) {
|
|
-
|
|
+ LOGGER.info("解析到一条数据:{}", JSONArray.toJSONString(pmsProductExcel));
|
|
- int num = analysisContext.readRowHolder().getRowIndex();
|
|
|
|
- PmsProductParam pmsProductParam=new PmsProductParam();
|
|
|
|
- BeanUtils.copyProperties(pmsProductExcel,pmsProductParam);
|
|
|
|
|
|
|
|
|
|
+
|
|
String name = pmsProductExcel.getName();
|
|
String name = pmsProductExcel.getName();
|
|
-
|
|
+
|
|
- pmsProductParam.setDetailTitle(name);
|
|
+
|
|
-
|
|
+ String brandName = pmsProductExcel.getBrandName();
|
|
- pmsProductParam.setNote(name);
|
|
+
|
|
-
|
|
+
|
|
- pmsProductParam.setKeywords(name);
|
|
+ String spec = pmsProductExcel.getSpec();
|
|
-
|
|
+
|
|
- String description = replaceBlank(pmsProductExcel.getDescription());
|
|
+
|
|
-
|
|
|
|
-
|
|
|
|
- pmsProductParam.setDescription(description);
|
|
|
|
-
|
|
|
|
- pmsProductParam.setSubTitle(description);
|
|
|
|
-
|
|
|
|
- String instructions = replaceBlank(pmsProductExcel.getInstructions());
|
|
|
|
-
|
|
|
|
- pmsProductParam.setDetailDesc(instructions);
|
|
|
|
-
|
|
|
|
- pmsProductParam.setDetailHtml(instructions);
|
|
|
|
-
|
|
|
|
- pmsProductParam.setDetailMobileHtml(instructions);
|
|
|
|
BigDecimal price = pmsProductExcel.getPrice();
|
|
BigDecimal price = pmsProductExcel.getPrice();
|
|
-
|
|
+
|
|
- pmsProductParam.setOriginalPrice(price);
|
|
+ price.setScale(2,BigDecimal.ROUND_HALF_UP);
|
|
-
|
|
+
|
|
- pmsProductParam.setCreateUser((long) 1);
|
|
+ DecimalFormat decimalFormat = new DecimalFormat("0.00#");
|
|
-
|
|
+ String priceNew = decimalFormat.format(price);
|
|
- pmsProductParam.setCreateTime(new Date());
|
|
+
|
|
- list.add(pmsProductParam);
|
|
+ String umsCompanyInfo = pmsProductExcel.getUmsCompanyInfo();
|
|
- if (list.size() >= BATCH_COUNT) {
|
|
+ String param = replaceBlank(name + brandName + priceNew + spec + umsCompanyInfo);
|
|
- saveData();
|
|
+
|
|
- list.clear();
|
|
+ Long aLong = productExcelDao.checkOne(param);
|
|
|
|
+ if (aLong == 0) {
|
|
|
|
+
|
|
|
|
+ int num = analysisContext.readRowHolder().getRowIndex();
|
|
|
|
+ PmsProductParam pmsProductParam = new PmsProductParam();
|
|
|
|
+ BeanUtils.copyProperties(pmsProductExcel, pmsProductParam);
|
|
|
|
+
|
|
|
|
+ pmsProductParam.setDetailTitle(name);
|
|
|
|
+
|
|
|
|
+ pmsProductParam.setNote(name);
|
|
|
|
+
|
|
|
|
+ pmsProductParam.setKeywords(name);
|
|
|
|
+
|
|
|
|
+ String description = replaceBlank(pmsProductExcel.getDescription());
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ pmsProductParam.setDescription(description);
|
|
|
|
+
|
|
|
|
+ pmsProductParam.setSubTitle(description);
|
|
|
|
+
|
|
|
|
+ String instructions = replaceBlank(pmsProductExcel.getInstructions());
|
|
|
|
+
|
|
|
|
+ pmsProductParam.setDetailDesc(instructions);
|
|
|
|
+
|
|
|
|
+ pmsProductParam.setDetailHtml(instructions);
|
|
|
|
+
|
|
|
|
+ pmsProductParam.setDetailMobileHtml(instructions);
|
|
|
|
+
|
|
|
|
+ pmsProductParam.setOriginalPrice(price);
|
|
|
|
+
|
|
|
|
+ pmsProductParam.setCreateUser((long) 1);
|
|
|
|
+
|
|
|
|
+ pmsProductParam.setCreateTime(new Date());
|
|
|
|
+ list.add(pmsProductParam);
|
|
|
|
+ if (list.size() >= BATCH_COUNT) {
|
|
|
|
+ saveData();
|
|
|
|
+ list.clear();
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ pmsProductExcel.setErrMsg("数据库已存在!");
|
|
|
|
+ erroList.add(pmsProductExcel);
|
|
}
|
|
}
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ @SneakyThrows
|
|
@Override
|
|
@Override
|
|
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
|
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
|
saveData();
|
|
saveData();
|
|
-
|
|
+ LOGGER.info("所有数据解析完成!");
|
|
|
|
+ if (erroList.size() > 0) {
|
|
|
|
+ String fileName = "导入失败数据"+System.currentTimeMillis();
|
|
|
|
+
|
|
|
|
+ response.setContentType("application/vnd.ms-excel");
|
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
|
+
|
|
|
|
+ String fileNameNew = URLEncoder.encode(fileName, "UTF-8");
|
|
|
|
+ response.setHeader("Content-disposition", "attachment;filename=" + fileNameNew + ".xlsx");
|
|
|
|
+ EasyExcel.write(response.getOutputStream(), PmsProductExcel.class).sheet("Sheet").doWrite(erroList);
|
|
|
|
+ }
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
- private void saveData(){
|
|
+ private void saveData() {
|
|
- try {
|
|
+ LOGGER.info("{}条数据,开始存储数据库!", list.size());
|
|
-
|
|
+
|
|
|
|
+ if(list.size()>0){
|
|
productExcelDao.saveData(list);
|
|
productExcelDao.saveData(list);
|
|
- } catch (Exception e) {
|
|
+ LOGGER.info("存储数据库成功!");
|
|
- e.getMessage();
|
|
|
|
- erroList.addAll(list);
|
|
|
|
- String name="导入失败数据";
|
|
|
|
- String templateFileName = "D:\\file"+ File.separator + name+System.currentTimeMillis()+".xlsx";
|
|
|
|
- EasyExcel.write(templateFileName, PmsProductParam.class).sheet(name).doWrite(erroList);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
public static String replaceBlank(String str) {
|
|
public static String replaceBlank(String str) {
|
|
String dest = "";
|
|
String dest = "";
|
|
- if (str!=null) {
|
|
+ if (str != null) {
|
|
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
|
|
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
|
|
Matcher m = p.matcher(str);
|
|
Matcher m = p.matcher(str);
|
|
dest = m.replaceAll("");
|
|
dest = m.replaceAll("");
|