记得上下班打卡 | git大法好,push需谨慎

Commit bec2e19b authored by 张国柄's avatar 张国柄

~api:店铺商品管理:批量导入数据;

parent 545c4a3d
......@@ -3,6 +3,7 @@ package com.liquidnet.service.goblin.service.impl.manage;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.liquidnet.common.exception.LiquidnetServiceException;
import com.liquidnet.commons.lang.util.CollectionUtil;
......@@ -54,21 +55,21 @@ public class GoblinStoreMgtGoodsImportService {
*/
public static final String ALPHABET_NUMBER_32 = "^[a-zA-Z0-9]{0,32}$";
private void goodsInformationDataAnalysisProcessingValid(GoblinGoodsImportDto dto, List<String> skuBarCodeTmpList) {
private void goodsInformationDataAnalysisProcessingValid(GoblinGoodsImportDto dto, List<String> skuBarCodeTmpList, Integer rowNum) {
/* 商品编码校验|------------------------------------------------------------------------------ */
if (StringUtils.isNotEmpty(dto.getSpuCode()) && !Pattern.matches(ALPHABET_NUMBER_UNDER_50, dto.getSpuCode())) {
throw new LiquidnetServiceException("-1", "数据内容不规范【商品编码有误】");
throw new LiquidnetServiceException("-1", String.format("数据内容不规范【第%s行商品编码有误】", rowNum));
}
/* 商品名称校验|------------------------------------------------------------------------------ */
if (StringUtils.isEmpty(dto.getSpuName()) || dto.getSpuName().length() > 100) {
throw new LiquidnetServiceException("-1", "数据内容不规范【商品名称超出长度限制】");
throw new LiquidnetServiceException("-1", String.format("数据内容不规范【第%s行商品名称超出长度限制】", rowNum));
}
/* 商品图片校验|------------------------------------------------------------------------------ */
if (StringUtils.isNotEmpty(dto.getSpuImgs())) {
if (dto.getSpuImgs().startsWith("【图片链接】")) {
String[] spuImgsArr = dto.getSpuImgs().replace("【图片链接】", "").replace(";", ";").split(";");
if (spuImgsArr.length > 15) {
throw new LiquidnetServiceException("-1", "数据内容不规范【商品图片最多支持15张】");
throw new LiquidnetServiceException("-1", String.format("数据内容不规范【第%s行商品图片最多支持15张】", rowNum));
}
ArrayList<String> spuImgList = CollectionUtil.arrayListString();
for (String spuImg : spuImgsArr) {
......@@ -80,25 +81,25 @@ public class GoblinStoreMgtGoodsImportService {
dto.setSpuImgList(spuImgList);
}
} else {
throw new LiquidnetServiceException("-1", "数据内容不规范【商品图片格式有误】");
throw new LiquidnetServiceException("-1", String.format("数据内容不规范【第%s行商品图片格式有误】", rowNum));
}
}
/* 商品规格校验|------------------------------------------------------------------------------ */
if (StringUtils.isEmpty(dto.getSkuSpec())) {
throw new LiquidnetServiceException("-1", "数据内容不规范【商品规格信息缺失】");
throw new LiquidnetServiceException("-1", String.format("数据内容不规范【第%s行商品规格信息缺失】", rowNum));
}
String[] skuSpecArr = dto.getSkuSpec().replace(":", ":").replace(";", ";").split(";");
List<GoblinGoodsSpecDto> skuSpecDtos = ObjectUtil.getGoblinGoodsSpecDtoArrayList();
for (int i = 0, size = skuSpecArr.length; i < size; i++) {
String skuSpec = skuSpecArr[i];
if (StringUtils.isEmpty(skuSpec)) {
throw new LiquidnetServiceException("-1", "数据内容不规范【商品规格信息有误】");
throw new LiquidnetServiceException("-1", String.format("数据内容不规范【第%s行商品规格信息有误】", rowNum));
}
String[] specArr = skuSpec.split(":");
if (ArrayUtils.isEmpty(specArr) || specArr.length != 2
|| specArr[0].length() > 5 || specArr[1].length() > 40
) {
throw new LiquidnetServiceException("-1", "数据内容不规范【商品规格信息有误】");
throw new LiquidnetServiceException("-1", String.format("数据内容不规范【第%s行商品规格信息有误】", rowNum));
}
GoblinGoodsSpecDto skuSpecDto = GoblinGoodsSpecDto.getNew();
......@@ -109,15 +110,15 @@ public class GoblinStoreMgtGoodsImportService {
}
/* 规格编码校验|------------------------------------------------------------------------------ */
if (StringUtils.isNotEmpty(dto.getSkuCode()) && !Pattern.matches(ALPHABET_NUMBER_UNDER_50, dto.getSkuCode())) {
throw new LiquidnetServiceException("-1", "数据内容不规范【规格编码格式有误】");
throw new LiquidnetServiceException("-1", String.format("数据内容不规范【第%s行规格编码格式有误】", rowNum));
}
/* 价格校验|------------------------------------------------------------------------------ */
if (null == dto.getPrice() || dto.getPrice().compareTo(BigDecimal.valueOf(0.01)) < 0 || dto.getPrice().compareTo(BigDecimal.valueOf(9999999)) > 0) {
throw new LiquidnetServiceException("-1", "数据内容不规范【价格信息有误】");
throw new LiquidnetServiceException("-1", String.format("数据内容不规范【第%s行价格信息有误】", rowNum));
}
/* 库存校验|------------------------------------------------------------------------------ */
if (null == dto.getStock() || dto.getStock() < 0 || dto.getStock() > 9999999) {// 数据不规范停止解析并提示用户
throw new LiquidnetServiceException("-1", "数据内容不规范【库存信息有误】");
throw new LiquidnetServiceException("-1", String.format("数据内容不规范【第%s行库存信息有误】", rowNum));
}
/* 规格图片校验|------------------------------------------------------------------------------ */
if (StringUtils.isNotEmpty(dto.getSkuImg())) {
......@@ -126,21 +127,21 @@ public class GoblinStoreMgtGoodsImportService {
if (skuImgArr.length == 1) {
dto.setSkuImg(StringUtils.isNotBlank(skuImgArr[0]) ? skuImgArr[0] : null);
} else {
throw new LiquidnetServiceException("-1", "数据内容不规范【规格图片仅支持1张】");
throw new LiquidnetServiceException("-1", String.format("数据内容不规范【第%s行规格图片仅支持1张】", rowNum));
}
} else {
throw new LiquidnetServiceException("-1", "数据内容不规范【规格图片格式有误】");
throw new LiquidnetServiceException("-1", String.format("数据内容不规范【第%s行规格图片格式有误】", rowNum));
}
}
/* 规格条码校验|------------------------------------------------------------------------------ */
if (StringUtils.isNotEmpty(dto.getSkuBarCode())) {
if (Pattern.matches(ALPHABET_NUMBER_32, dto.getSkuBarCode())) {
if (skuBarCodeTmpList.contains(dto.getSkuBarCode())) {
throw new LiquidnetServiceException("-1", "数据内容不规范【表格内规格条码重复】");
throw new LiquidnetServiceException("-1", String.format("数据内容不规范【第%s行表格内规格条码重复】", rowNum));
}
skuBarCodeTmpList.add(dto.getSkuBarCode());
} else {
throw new LiquidnetServiceException("-1", "数据内容不规范【规格条码格式有误】");
throw new LiquidnetServiceException("-1", String.format("数据内容不规范【第%s行规格条码格式有误】", rowNum));
}
}
/* 校验|------------------------------------------------------------------------------ */
......@@ -156,6 +157,16 @@ public class GoblinStoreMgtGoodsImportService {
EasyExcel.read(file.getInputStream(), GoblinGoodsImportDto.class, new AnalysisEventListener<GoblinGoodsImportDto>() {
@Override
public void onException(Exception exception, AnalysisContext context) throws Exception {
if (exception instanceof ExcelDataConvertException) {
Integer rowIndex = ((ExcelDataConvertException) exception).getRowIndex();
Integer columnIndex = ((ExcelDataConvertException) exception).getColumnIndex();
throw new LiquidnetServiceException("-1", String.format("数据内容不规范【第%s行,第%s列数据格式有误】", rowIndex + 1, columnIndex + 1));
}
super.onException(exception, context);
}
@Override
public void invokeHead(Map<Integer, ReadCellData<?>> headMap, AnalysisContext context) {
Integer approximateTotalRowNumber = context.readSheetHolder().getApproximateTotalRowNumber();
......@@ -168,7 +179,9 @@ public class GoblinStoreMgtGoodsImportService {
@Override
public void invoke(GoblinGoodsImportDto dto, AnalysisContext analysisContext) {
goodsInformationDataAnalysisProcessingValid(dto, skuBarCodeTmpList);
Integer rowIndex = analysisContext.readRowHolder().getRowIndex();
goodsInformationDataAnalysisProcessingValid(dto, skuBarCodeTmpList, rowIndex + 1);
GoblinGoodsInfoVo lastGoodsInfoVo = CollectionUtils.isEmpty(goodsInfoVos) ? null : goodsInfoVos.get(goodsInfoVos.size() - 1);
GoblinGoodsInfoVo goodsInfoVo = goodsInformationDataAnalysisProcessingForSpu(dto, lastGoodsInfoVo, uid, storeId,
......@@ -181,12 +194,15 @@ public class GoblinStoreMgtGoodsImportService {
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {// 解析完成...
log.debug("dt3-1:{}", 1);
log.debug("dt3-1:{}", JsonUtils.toJson(goodsInfoVos));
log.debug("dt3-2:{}", JsonUtils.toJson(goodsSkuInfoVos));
if (goblinMongoUtils.countMgtGoodsSkuBySkuNoList(storeId, skuBarCodeTmpList) > 0) {
throw new LiquidnetServiceException("-1", "数据内容不规范【规格条码与已添加商品条码重复】");
List<String> existGoodsSkuNoList = goblinMongoUtils.existGoodsSkuNoBySkuNoList(storeId, skuBarCodeTmpList);
if (!CollectionUtils.isEmpty(existGoodsSkuNoList)) {
if (existGoodsSkuNoList.size() > 3) {
throw new LiquidnetServiceException("-1", String.format("规格条码与已添加商品条码重复【%s,...】", StringUtils.join(existGoodsSkuNoList.subList(0, 3), ",")));
}
throw new LiquidnetServiceException("-1", String.format("规格条码与已添加商品条码重复【%s,...】", StringUtils.join(existGoodsSkuNoList, ",")));
}
// goblinMongoUtils.insertMgtGoodsInfoVos(goodsInfoVos);
......
......@@ -840,10 +840,13 @@ public class GoblinMongoUtils {
return pagedResult.setList(list).setTotal(count, pageSize);
}
// 根据sku条码获取sku总数
public long countMgtGoodsSkuBySkuNoList(String storeId, List<String> skuNoList) {
return mongoTemplate.count(Query.query(Criteria.where("delFlg").is("0").and("storeId").is(storeId).and("skuNo").in(skuNoList)),
GoblinMixDetailsVo.class, GoblinMixDetailsVo.class.getSimpleName());
// 根据sku条码获取skuNo
public List<String> existGoodsSkuNoBySkuNoList(String storeId, List<String> skuNoList) {
Query query = Query.query(Criteria.where("delFlg").is("0").and("storeId").is(storeId).and("skuNo").in(skuNoList));
query.fields().include("skuNo");
List<GoblinGoodsSkuInfoVo> vos = mongoTemplate.find(query, GoblinGoodsSkuInfoVo.class, GoblinGoodsSkuInfoVo.class.getSimpleName());
return CollectionUtils.isEmpty(vos) ? null : vos.stream().map(GoblinGoodsSkuInfoVo::getSkuNo).collect(Collectors.toList());
// return mongoTemplate.count(query, GoblinGoodsSkuInfoVo.class, GoblinGoodsSkuInfoVo.class.getSimpleName());
}
// SKU信息
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment