|
@@ -3,17 +3,20 @@ package com.hwrj.cloud.admin.listener;
|
|
|
import com.alibaba.excel.EasyExcel;
|
|
|
import com.alibaba.excel.context.AnalysisContext;
|
|
|
import com.alibaba.excel.event.AnalysisEventListener;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
import com.hwrj.cloud.admin.dao.PmsProductExcelDao;
|
|
|
import com.hwrj.cloud.admin.dto.param.PmsProductExcel;
|
|
|
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.factory.annotation.Autowired;
|
|
|
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.net.URLEncoder;
|
|
|
+import java.text.DecimalFormat;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Date;
|
|
|
import java.util.List;
|
|
@@ -26,14 +29,19 @@ import java.util.regex.Pattern;
|
|
|
@Component
|
|
|
public class PmsProductExcelListener extends AnalysisEventListener<PmsProductExcel> {
|
|
|
|
|
|
+ private static final Logger LOGGER = LoggerFactory.getLogger(PmsProductExcelListener.class);
|
|
|
+
|
|
|
private PmsProductExcelDao productExcelDao;
|
|
|
|
|
|
public PmsProductExcelListener() {
|
|
|
|
|
|
}
|
|
|
|
|
|
- public PmsProductExcelListener(PmsProductExcelDao productExcelDao) {
|
|
|
+ private HttpServletResponse response;
|
|
|
+
|
|
|
+ public PmsProductExcelListener(PmsProductExcelDao productExcelDao, HttpServletResponse response) {
|
|
|
this.productExcelDao = productExcelDao;
|
|
|
+ this.response = response;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -44,76 +52,109 @@ public class PmsProductExcelListener extends AnalysisEventListener<PmsProductExc
|
|
|
|
|
|
List<PmsProductParam> list = new ArrayList();
|
|
|
|
|
|
- List<PmsProductParam> erroList = new ArrayList<>();
|
|
|
+ List<PmsProductExcel> erroList = new ArrayList<>();
|
|
|
|
|
|
|
|
|
@Override
|
|
|
public void invoke(PmsProductExcel pmsProductExcel, AnalysisContext analysisContext) {
|
|
|
- //获取对应的行数
|
|
|
- int num = analysisContext.readRowHolder().getRowIndex();
|
|
|
- PmsProductParam pmsProductParam=new PmsProductParam();
|
|
|
- BeanUtils.copyProperties(pmsProductExcel,pmsProductParam);
|
|
|
+ LOGGER.info("解析到一条数据:{}", JSONArray.toJSONString(pmsProductExcel));
|
|
|
|
|
|
+ //产品名称
|
|
|
String name = pmsProductExcel.getName();
|
|
|
- //标题
|
|
|
- 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);
|
|
|
- //web网页显示
|
|
|
- pmsProductParam.setDetailHtml(instructions);
|
|
|
- //手机网页显示
|
|
|
- pmsProductParam.setDetailMobileHtml(instructions);
|
|
|
+
|
|
|
+ //品牌
|
|
|
+ String brandName = pmsProductExcel.getBrandName();
|
|
|
+
|
|
|
+ //规格
|
|
|
+ String spec = pmsProductExcel.getSpec();
|
|
|
+
|
|
|
+ //价格
|
|
|
BigDecimal price = pmsProductExcel.getPrice();
|
|
|
- //市场价
|
|
|
- pmsProductParam.setOriginalPrice(price);
|
|
|
- //创建人 admin 1
|
|
|
- pmsProductParam.setCreateUser((long) 1);
|
|
|
- //创建时间
|
|
|
- pmsProductParam.setCreateTime(new Date());
|
|
|
- list.add(pmsProductParam);
|
|
|
- if (list.size() >= BATCH_COUNT) {
|
|
|
- saveData();
|
|
|
- list.clear();
|
|
|
+ // 四舍五入
|
|
|
+ price.setScale(2,BigDecimal.ROUND_HALF_UP);
|
|
|
+ // 不足两位小数补0
|
|
|
+ DecimalFormat decimalFormat = new DecimalFormat("0.00#");
|
|
|
+ String priceNew = decimalFormat.format(price);
|
|
|
+ //供应商
|
|
|
+ String umsCompanyInfo = pmsProductExcel.getUmsCompanyInfo();
|
|
|
+ String param = replaceBlank(name + brandName + priceNew + spec + umsCompanyInfo);
|
|
|
+
|
|
|
+ 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);
|
|
|
+ //web网页显示
|
|
|
+ pmsProductParam.setDetailHtml(instructions);
|
|
|
+ //手机网页显示
|
|
|
+ pmsProductParam.setDetailMobileHtml(instructions);
|
|
|
+ //市场价
|
|
|
+ pmsProductParam.setOriginalPrice(price);
|
|
|
+ //创建人 admin 1
|
|
|
+ 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
|
|
|
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
|
|
|
saveData();
|
|
|
- // log.info("所有数据解析完成!");
|
|
|
+ LOGGER.info("所有数据解析完成!");
|
|
|
+ if (erroList.size() > 0) {
|
|
|
+ String fileName = "导入失败数据"+System.currentTimeMillis();
|
|
|
+ // 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
|
|
|
+ response.setContentType("application/vnd.ms-excel");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
|
|
|
+ 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(){
|
|
|
- try {
|
|
|
- //调用saveData()方法
|
|
|
+ private void saveData() {
|
|
|
+ LOGGER.info("{}条数据,开始存储数据库!", list.size());
|
|
|
+ //调用saveData()方法
|
|
|
+ if(list.size()>0){
|
|
|
productExcelDao.saveData(list);
|
|
|
- } catch (Exception e) {
|
|
|
- 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);
|
|
|
+ LOGGER.info("存储数据库成功!");
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
public static String replaceBlank(String str) {
|
|
|
String dest = "";
|
|
|
- if (str!=null) {
|
|
|
+ if (str != null) {
|
|
|
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
|
|
|
Matcher m = p.matcher(str);
|
|
|
dest = m.replaceAll("");
|