...
|
...
|
@@ -4,10 +4,12 @@ import java.math.BigDecimal; |
|
|
import java.text.ParseException;
|
|
|
import java.text.SimpleDateFormat;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.yoho.core.dal.datasource.annotation.Database;
|
|
|
import com.yoho.core.rest.client.ServiceCaller;
|
|
|
import com.yoho.service.model.order.request.OrderRequest;
|
|
|
import com.yoho.ufo.dal.*;
|
|
|
import com.yoho.ufo.event.model.StorageNumEvent;
|
|
|
import com.yoho.ufo.model.brand.BrandSeries;
|
...
|
...
|
@@ -15,6 +17,9 @@ import com.yoho.ufo.model.commoditybasicrole.color.ProductColor; |
|
|
import com.yoho.ufo.util.ImagesConstant;
|
|
|
import com.yoho.ufo.util.ImagesHelper;
|
|
|
import com.yohobuy.ufo.model.enums.InboxBusinessTypeEnum;
|
|
|
import com.yohobuy.ufo.model.order.req.BatchImportPrdReq;
|
|
|
import com.yohobuy.ufo.model.order.req.SellerReq;
|
|
|
import com.yohobuy.ufo.model.order.vo.AddressInfo;
|
|
|
import com.yohobuy.ufo.model.request.product.ProductRequestBatchBo;
|
|
|
import org.apache.commons.collections.CollectionUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
...
|
...
|
@@ -27,18 +32,23 @@ import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.context.ApplicationContextAware;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.yoho.ufo.dal.model.Goods;
|
|
|
import com.yoho.ufo.dal.model.GoodsImages;
|
|
|
import com.yoho.ufo.dal.model.Product;
|
|
|
import com.yoho.ufo.dal.model.ProductImportTran;
|
|
|
import com.yoho.ufo.dal.model.ProductImportTranItem;
|
|
|
import com.yoho.ufo.dal.model.Storage;
|
|
|
import com.yoho.ufo.dal.model.StoragePrice;
|
|
|
import com.yoho.ufo.exception.CommonException;
|
|
|
import com.yoho.ufo.exception.PlatformException;
|
|
|
import com.yoho.ufo.model.brand.Brand;
|
|
|
import com.yoho.ufo.model.commoditybasicrole.category.ProductSort;
|
|
|
import com.yoho.ufo.model.commoditybasicrole.size.Size;
|
|
|
import com.yoho.ufo.service.IProductService;
|
|
|
import com.yoho.ufo.service.UserProxyService;
|
|
|
import com.yoho.ufo.util.CollectionUtil;
|
|
|
import com.yohobuy.ufo.model.common.ApiResponse;
|
|
|
import com.yohobuy.ufo.model.common.PageResponseBO;
|
...
|
...
|
@@ -69,6 +79,10 @@ public class ProductServiceImpl implements IProductService, ApplicationContextAw |
|
|
private StoragePriceMapper storagePriceMapper;
|
|
|
@Autowired
|
|
|
private UfoSizeMapper sizeMapper;
|
|
|
@Autowired
|
|
|
private ProductImportTranMapper productImportTranMapper;
|
|
|
@Autowired
|
|
|
private ProductImportTranItemMapper productImportTranItemMapper;
|
|
|
/*@Autowired
|
|
|
private InboxService inboxService;*/
|
|
|
|
...
|
...
|
@@ -80,6 +94,13 @@ public class ProductServiceImpl implements IProductService, ApplicationContextAw |
|
|
private UfoBrandSeriesMapper brandSeriesMapper;
|
|
|
@Autowired
|
|
|
private ProductIntroService productIntroService;
|
|
|
|
|
|
@Autowired
|
|
|
private UserProxyService userProxyService;
|
|
|
|
|
|
@Autowired
|
|
|
private BatchService batchService;
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<Void> addOrUpdate(ProductRequestBo bo, boolean isCheckUrl) {
|
|
|
checkProductInfo(bo, isCheckUrl);
|
...
|
...
|
@@ -712,4 +733,220 @@ public static void main(String[] args) { |
|
|
|
|
|
return new ApiResponse<>(200, "操作成功!");
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public ApiResponse<ProductImportTranItem> getBatchImportResult(Integer tranId) throws PlatformException {
|
|
|
ProductImportTran exist = productImportTranMapper.selectByPrimaryKey(tranId);
|
|
|
if (exist == null) {
|
|
|
throw new PlatformException("任务不存在!", 400);
|
|
|
}
|
|
|
ProductImportTranItem item = productImportTranItemMapper.selectTranInfoByTranId(tranId);
|
|
|
if (item == null || item.getItemCount() == null || item.getItemCount() == 0) {
|
|
|
item = new ProductImportTranItem();
|
|
|
item.setStatus(0);
|
|
|
} else if (item.getStatus() != null && item.getStatus() > 0) {
|
|
|
ProductImportTran update = new ProductImportTran();
|
|
|
update.setId(tranId);
|
|
|
update.setStatus(item.getMaxStatus());
|
|
|
productImportTranMapper.updateByPrimaryKeySelective(update);
|
|
|
}
|
|
|
return new ApiResponse<>(200, "查询成功", item);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void batchImportFromXls(MultipartFile file, Integer editUid) throws Exception {
|
|
|
LOGGER.info("用户{}开始导入商品数据!", editUid);
|
|
|
// 查看是否有正在执行
|
|
|
int now = (int) (System.currentTimeMillis() / 1000);
|
|
|
ProductImportTran exist;
|
|
|
if ((exist = productImportTranMapper.selectUserProcessing(editUid)) != null
|
|
|
&& (now - exist.getCreateTime()) < 300) {
|
|
|
LOGGER.warn("用户{}正在导入中,结束", editUid);
|
|
|
throw new PlatformException("有导入操作正在进行中!", 400);
|
|
|
}
|
|
|
|
|
|
List<ProductImportTranItem> items = batchService.parseData(file, "batchImportStoragePrice", ProductImportTranItem.class);
|
|
|
if (CollectionUtils.isEmpty(items)) {
|
|
|
LOGGER.info("用户{}导入的excel为空,结束", editUid);
|
|
|
throw new PlatformException("导入的excel数据为空!", 400);
|
|
|
}
|
|
|
LOGGER.info("Excel解析成功一共{}行", items.size());
|
|
|
|
|
|
Integer count = 0, uid = null;
|
|
|
Set<String> uniqSet = new HashSet<>();
|
|
|
for (int i = 0; i < items.size(); i++) {
|
|
|
ProductImportTranItem item = items.get(i);
|
|
|
if (StringUtils.isAnyBlank(item.getProductCode(), item.getSizeName())) {
|
|
|
LOGGER.info("第{}行数据:商品编码或尺码为空!", i + 1);
|
|
|
throw new PlatformException("第" + (i + 1) + "行数据:商品编码或尺码为空!", 400);
|
|
|
}
|
|
|
if (item.getUid() == null || item.getUid() < 1) {
|
|
|
LOGGER.info("第{}行数据:uid不合法!", i + 1);
|
|
|
throw new PlatformException("第" + (i + 1) + "行数据:uid:" + item.getUid() + "不合法!", 400);
|
|
|
}
|
|
|
if (uid == null) {
|
|
|
uid = item.getUid();
|
|
|
} else if (item.getUid() != uid) {
|
|
|
LOGGER.info("excel中存在多个uid!");
|
|
|
throw new PlatformException("excel中存在多个uid!", 400);
|
|
|
}
|
|
|
if (item.getNum() == null || item.getNum() < 1) {
|
|
|
LOGGER.info("第{}行数据:数量不合法!", i + 1);
|
|
|
throw new PlatformException("第" + (i + 1) + "行数据:数量" + item.getNum() + "不合法!", 400);
|
|
|
}
|
|
|
count += item.getNum();
|
|
|
if (count > 200) {
|
|
|
LOGGER.info("总数量大于200!");
|
|
|
throw new PlatformException("总数量不能大于200!", 400);
|
|
|
}
|
|
|
BigDecimal price = item.getPrice();
|
|
|
if (price == null || price.compareTo(BigDecimal.ZERO) <= 0
|
|
|
|| price.divideAndRemainder(BigDecimal.TEN)[1].compareTo(new BigDecimal("9")) != 0) {
|
|
|
LOGGER.info("第{}行数据:金额不合法!", i + 1);
|
|
|
throw new PlatformException("第" + (i + 1) + "行数据:金额" + price + "不合法!", 400);
|
|
|
}
|
|
|
|
|
|
String key = item.getProductCode() + " *SPLIT* " + item.getSizeName();
|
|
|
if (uniqSet.contains(key)) {
|
|
|
LOGGER.info("第{}行数据:货号{}+尺码{}有重复行!", i + 1, item.getProductCode(), item.getSizeName());
|
|
|
throw new PlatformException(
|
|
|
"第" + (i + 1) + "行数据:货号+尺码有重复行:" + item.getProductCode() + "," + item.getSizeName(), 400);
|
|
|
}
|
|
|
uniqSet.add(key);
|
|
|
}
|
|
|
|
|
|
List<String> productCodeList = CollectionUtil.distinct(items, ProductImportTranItem::getProductCode);
|
|
|
List<Product> productList = productMapper.selectProductListByProductCodes(productCodeList);
|
|
|
Map<String, Product> codeProductMap = CollectionUtil.extractMap(productList, Product::getProductCode);
|
|
|
for (int i = 0; i < items.size(); i++) {
|
|
|
ProductImportTranItem item = items.get(i);
|
|
|
Product p = codeProductMap.get(item.getProductCode());
|
|
|
if (p == null) {
|
|
|
LOGGER.info("第{}行数据:商品编码{}不存在!", i + 1, item.getProductCode());
|
|
|
throw new PlatformException("第" + (i + 1) + "行数据:商品编码" + item.getProductCode() + "不存在!", 400);
|
|
|
}
|
|
|
if (item.getPrice().compareTo(p.getMaxPrice()) > 0 || item.getPrice().compareTo(p.getMinPrice()) < 0) {
|
|
|
LOGGER.info("第{}行数据:价格{}超出规定范围!", i + 1, item.getPrice());
|
|
|
throw new PlatformException("第" + (i + 1) + "行数据:价格" + item.getPrice() + "超出规定范围!", 400);
|
|
|
}
|
|
|
item.setProductId(p.getId());
|
|
|
}
|
|
|
|
|
|
List<String> sizeNameList = CollectionUtil.distinct(items, ProductImportTranItem::getSizeName);
|
|
|
List<Size> sizeList = sizeMapper.selectBySizeNameList(sizeNameList);
|
|
|
Map<String, Size> sizeNameMap = CollectionUtil.extractMap(sizeList, Size::getSizeName);
|
|
|
for (int i = 0; i < items.size(); i++) {
|
|
|
ProductImportTranItem item = items.get(i);
|
|
|
Size s = sizeNameMap.get(item.getSizeName());
|
|
|
if (s == null) {
|
|
|
LOGGER.info("第{}行数据:尺码{}不存在!", i + 1, item.getSizeName());
|
|
|
throw new PlatformException("第" + (i + 1) + "行数据:尺码" + item.getSizeName() + "不存在!", 400);
|
|
|
}
|
|
|
item.setSizeId(s.getId());
|
|
|
}
|
|
|
|
|
|
StringBuilder sql = new StringBuilder();
|
|
|
Map<Integer, Set<Integer>> productIdSizeMap = new HashMap<>();
|
|
|
for (int i = 0; i < items.size(); i++) {
|
|
|
ProductImportTranItem item = items.get(i);
|
|
|
Size s = sizeNameMap.get(item.getSizeName());
|
|
|
Set<Integer> sizeIds = productIdSizeMap.get(item.getProductId());
|
|
|
if (sizeIds == null) {
|
|
|
sizeIds = new HashSet<>();
|
|
|
productIdSizeMap.put(item.getProductId(), sizeIds);
|
|
|
}
|
|
|
sizeIds.add(s.getId());
|
|
|
}
|
|
|
productIdSizeMap.forEach((k, v) -> {
|
|
|
sql.append("or (product_id=" + k + " and size_id in(" + StringUtils.join(v, ",") + ")) ");
|
|
|
});
|
|
|
List<Storage> storageList = storageMapper.selectByCondition(sql.substring(3));
|
|
|
|
|
|
Map<String, Storage> storageMap = CollectionUtil.extractMap(storageList,
|
|
|
s -> s.getProductId() + "_" + s.getSizeId());
|
|
|
for (int i = 0; i < items.size(); i++) {
|
|
|
ProductImportTranItem item = items.get(i);
|
|
|
Storage s = storageMap.get(item.getProductId() + "_" + item.getSizeId());
|
|
|
if (s == null) {
|
|
|
LOGGER.info("第{}行数据:商品{}无该尺码{}!", i + 1, item.getProductCode(), item.getSizeName());
|
|
|
throw new PlatformException(
|
|
|
"第" + (i + 1) + "行数据:该商品" + item.getProductCode() + "下的这个尺码" + item.getSizeName() + "不存在!",
|
|
|
400);
|
|
|
}
|
|
|
item.setStorageId(s.getId());
|
|
|
}
|
|
|
|
|
|
// 检查用户身份
|
|
|
try {
|
|
|
LOGGER.info("get user account info");
|
|
|
SellerReq req = new SellerReq();
|
|
|
req.setUid(uid);
|
|
|
Boolean userResp = serviceCaller.call("ufo-gateway.isNormalSuper", req, Boolean.class, 10);
|
|
|
LOGGER.info("get user account info result is {}", userResp);
|
|
|
|
|
|
if (userResp == null || !userResp) {
|
|
|
throw new PlatformException("uid:" + uid + "账号不是超级卖家或者余额不足!", 400);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
LOGGER.error("get user account info error", e);
|
|
|
throw new PlatformException("uid:" + uid + "账号信息获取出错!", 400);
|
|
|
}
|
|
|
|
|
|
// 获取地址信息
|
|
|
AddressInfo address = null;
|
|
|
try {
|
|
|
LOGGER.info("get user address info");
|
|
|
address = userProxyService.getAddressInfoNotHidden(uid);
|
|
|
if (address == null) {
|
|
|
throw new PlatformException("uid:" + uid + "获取地址信息出错!", 400);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
LOGGER.error("get user address info error", e);
|
|
|
throw new PlatformException("uid:" + uid + "获取地址信息出错!", 400);
|
|
|
}
|
|
|
|
|
|
AddressInfo addressHidden = null;
|
|
|
try {
|
|
|
LOGGER.info("get user hidden address info");
|
|
|
addressHidden = userProxyService.getHiddenAddressInfo(uid);
|
|
|
if (addressHidden == null) {
|
|
|
throw new PlatformException("uid:" + uid + "获取加密地址信息出错!", 400);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
LOGGER.error("get user hidden address info error", e);
|
|
|
throw new PlatformException("uid:" + uid + "获取加密地址信息出错!", 400);
|
|
|
}
|
|
|
|
|
|
LOGGER.info("增加事务记录");
|
|
|
ProductImportTran tran = new ProductImportTran();
|
|
|
tran.setStatus(0);
|
|
|
tran.setEditUid(editUid);
|
|
|
tran.setUid(uid);
|
|
|
tran.setCreateTime(now);
|
|
|
productImportTranMapper.insert(tran);
|
|
|
LOGGER.info("增加事务记录成功rec={}", tran);
|
|
|
|
|
|
int batchNum = 40;
|
|
|
List<List<ProductImportTranItem>> batchLists = CollectionUtil.split(items, batchNum);
|
|
|
LOGGER.info("一共{}行,每批次{}行,一共{}批", items.size(), batchNum, batchLists.size());
|
|
|
for (int i = 0; i < batchLists.size(); i++) {
|
|
|
LOGGER.info("一共{}行,每批次{}行,一共{}批,处理批次{}...", items.size(), batchNum, batchLists.size(), i + 1);
|
|
|
List<ProductImportTranItem> batch = batchLists.get(i);
|
|
|
BatchImportPrdReq<ProductImportTranItem> batchReq = new BatchImportPrdReq<>();
|
|
|
batchReq.setHiddenBackAddress(addressHidden);
|
|
|
batchReq.setNoHiddenBackAddress(address);
|
|
|
batchReq.setProductList(batch);
|
|
|
batchReq.setTaskId(tran.getId());
|
|
|
|
|
|
try {
|
|
|
LOGGER.info("batch import send taskId={}, batchId={}", tran.getId(), (i + 1));
|
|
|
serviceCaller.call("ufo-gateway.batchImportPrds", batchReq, Void.class, 10);
|
|
|
LOGGER.info("batch import send success taskId={}, batchId={}", tran.getId(), (i + 1));
|
|
|
} catch (Exception e) {
|
|
|
LOGGER.info("batch import send failed taskId={}, batchId={}", tran.getId(), (i + 1));
|
|
|
}
|
|
|
}
|
|
|
LOGGER.info("batch import over taskId={}", tran.getId());
|
|
|
}
|
|
|
|
|
|
} |
...
|
...
|
|