|
@@ -13,6 +13,7 @@ import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import java.math.BigDecimal;
|
|
@@ -59,7 +60,26 @@ public class PmsProductExcelListener extends AnalysisEventListener<PmsProductExc
|
|
|
@Override
|
|
|
public void invoke(PmsProductExcel pmsProductExcel, AnalysisContext analysisContext) {
|
|
|
LOGGER.info("解析到一条数据:{}", JSONArray.toJSONString(pmsProductExcel));
|
|
|
+ String productCategoryName = pmsProductExcel.getProductCategoryName();
|
|
|
+ if (StringUtils.isEmpty(productCategoryName)) {
|
|
|
+ pmsProductExcel.setErrMsg("没有商品类型无法导入!");
|
|
|
+ erroList.add(pmsProductExcel);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String param = getParm(pmsProductExcel);
|
|
|
+ //判断数据是否重复
|
|
|
+ Long aLong = productExcelDao.checkOne(param);
|
|
|
+ if (aLong == 0) {
|
|
|
+ addData(pmsProductExcel, analysisContext);
|
|
|
+ return;
|
|
|
+ } else if (aLong > 0) {
|
|
|
+ pmsProductExcel.setErrMsg("数据库已存在!");
|
|
|
+ erroList.add(pmsProductExcel);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ private String getParm(PmsProductExcel pmsProductExcel) {
|
|
|
//产品名称
|
|
|
String name = pmsProductExcel.getName();
|
|
|
|
|
@@ -72,57 +92,56 @@ public class PmsProductExcelListener extends AnalysisEventListener<PmsProductExc
|
|
|
//价格
|
|
|
BigDecimal price = pmsProductExcel.getPrice();
|
|
|
// 四舍五入
|
|
|
- price.setScale(2,BigDecimal.ROUND_HALF_UP);
|
|
|
+ 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);
|
|
|
+ return 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) CurrUserUtil.getUserId());
|
|
|
- //创建时间
|
|
|
- pmsProductParam.setCreateTime(new Date());
|
|
|
- list.add(pmsProductParam);
|
|
|
- if (list.size() >= BATCH_COUNT) {
|
|
|
- saveData();
|
|
|
- list.clear();
|
|
|
- }
|
|
|
- } else {
|
|
|
- pmsProductExcel.setErrMsg("数据库已存在!");
|
|
|
- erroList.add(pmsProductExcel);
|
|
|
+ private void addData(PmsProductExcel pmsProductExcel, AnalysisContext analysisContext) {
|
|
|
+ //获取对应的行数
|
|
|
+ int num = analysisContext.readRowHolder().getRowIndex();
|
|
|
+ PmsProductParam pmsProductParam = new PmsProductParam();
|
|
|
+ BeanUtils.copyProperties(pmsProductExcel, pmsProductParam);
|
|
|
+ //名称
|
|
|
+ String name = pmsProductExcel.getName();
|
|
|
+ //价格
|
|
|
+ BigDecimal price = pmsProductExcel.getPrice();
|
|
|
+ //标题
|
|
|
+ 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) CurrUserUtil.getUserId());
|
|
|
+ //创建时间
|
|
|
+ pmsProductParam.setCreateTime(new Date());
|
|
|
+ list.add(pmsProductParam);
|
|
|
+ if (list.size() >= BATCH_COUNT) {
|
|
|
+ saveData();
|
|
|
+ list.clear();
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
@SneakyThrows
|
|
@@ -131,7 +150,7 @@ public class PmsProductExcelListener extends AnalysisEventListener<PmsProductExc
|
|
|
saveData();
|
|
|
LOGGER.info("所有数据解析完成!");
|
|
|
if (erroList.size() > 0) {
|
|
|
- String fileName = "导入失败数据"+System.currentTimeMillis();
|
|
|
+ String fileName = "导入失败数据" + System.currentTimeMillis();
|
|
|
// 这里注意 有同学反应使用swagger 会导致各种问题,请直接用浏览器或者用postman
|
|
|
response.setContentType("application/vnd.ms-excel");
|
|
|
response.setCharacterEncoding("utf-8");
|
|
@@ -146,7 +165,7 @@ public class PmsProductExcelListener extends AnalysisEventListener<PmsProductExc
|
|
|
private void saveData() {
|
|
|
LOGGER.info("{}条数据,开始存储数据库!", list.size());
|
|
|
//调用saveData()方法
|
|
|
- if(list.size()>0){
|
|
|
+ if (list.size() > 0) {
|
|
|
productExcelDao.saveData(list);
|
|
|
LOGGER.info("存储数据库成功!");
|
|
|
}
|