1
This commit is contained in:
@@ -5,7 +5,11 @@ import com.alibaba.fastjson2.JSONObject;
|
||||
import com.ruoyi.common.core.domain.entity.SysUser;
|
||||
import com.ruoyi.common.utils.SecurityUtils;
|
||||
import com.ruoyi.common.utils.StringUtils;
|
||||
import com.ruoyi.erp.domain.ERPShop;
|
||||
import com.ruoyi.erp.domain.PublishShop;
|
||||
import com.ruoyi.erp.request.ERPAccount;
|
||||
import com.ruoyi.erp.request.ProductCreateRequest;
|
||||
import com.ruoyi.erp.request.ProductPublishRequest;
|
||||
import com.ruoyi.jarvis.domain.BatchPublishItem;
|
||||
import com.ruoyi.jarvis.domain.BatchPublishTask;
|
||||
import com.ruoyi.jarvis.domain.request.BatchPublishRequest;
|
||||
@@ -14,6 +18,7 @@ import com.ruoyi.jarvis.mapper.BatchPublishItemMapper;
|
||||
import com.ruoyi.jarvis.mapper.BatchPublishTaskMapper;
|
||||
import com.ruoyi.jarvis.service.IBatchPublishService;
|
||||
import com.ruoyi.jarvis.service.IJDOrderService;
|
||||
import com.ruoyi.jarvis.service.IOuterIdGeneratorService;
|
||||
import com.ruoyi.jarvis.util.LineReportParser;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -46,6 +51,9 @@ public class BatchPublishServiceImpl implements IBatchPublishService
|
||||
@Autowired
|
||||
private IJDOrderService jdOrderService;
|
||||
|
||||
@Autowired
|
||||
private IOuterIdGeneratorService outerIdGeneratorService;
|
||||
|
||||
/**
|
||||
* 解析线报消息,提取商品列表
|
||||
*/
|
||||
@@ -320,19 +328,104 @@ public class BatchPublishServiceImpl implements IBatchPublishService
|
||||
log.info("开始发品: 商品={}, 账号={}", item.getProductName(), item.getAccountRemark());
|
||||
|
||||
try {
|
||||
// TODO: 调用实际的发品接口
|
||||
// 这里需要调用 ProductController 的 createByPromotion 接口
|
||||
// 暂时模拟成功
|
||||
|
||||
// 模拟发品成功
|
||||
item.setStatus(2); // 发布成功
|
||||
item.setProductId(System.currentTimeMillis()); // 模拟商品ID
|
||||
item.setProductStatus(1); // 模拟商品状态
|
||||
item.setOuterId("OUTER_" + item.getSkuid()); // 模拟商家编码
|
||||
item.setErrorMessage(null);
|
||||
itemMapper.updateBatchPublishItem(item);
|
||||
// 获取ERP账号
|
||||
ERPAccount account = getAccountByAppid(item.getTargetAccount());
|
||||
if (account == null) {
|
||||
throw new RuntimeException("未找到ERP账号: " + item.getTargetAccount());
|
||||
}
|
||||
|
||||
// 获取商品详情(从原始数据中获取)
|
||||
Map<String, Object> productDetail = getProductDetail(item.getSkuid());
|
||||
if (productDetail == null) {
|
||||
throw new RuntimeException("无法获取商品详情: " + item.getSkuid());
|
||||
}
|
||||
|
||||
// 获取通用参数
|
||||
BatchPublishRequest.CommonParams commonParams = request.getCommonParams();
|
||||
|
||||
// 1. 组装 ERPShop
|
||||
ERPShop erpShop = new ERPShop();
|
||||
erpShop.setChannelCatid(commonParams.getChannelCatId());
|
||||
erpShop.setItemBizType(commonParams.getItemBizType());
|
||||
erpShop.setSpBizType(commonParams.getSpBizType());
|
||||
erpShop.setPrice(item.getPublishPrice()); // 价格(分)
|
||||
erpShop.setExpressFee(Math.round(commonParams.getExpressFee() * 100)); // 邮费转分
|
||||
erpShop.setStock(commonParams.getStock());
|
||||
|
||||
// 自动生成商家编码
|
||||
String outerId = outerIdGeneratorService.generateOuterId(item.getSkuid());
|
||||
if (StringUtils.isEmpty(outerId)) {
|
||||
outerId = "BATCH_" + item.getSkuid() + "_" + System.currentTimeMillis();
|
||||
}
|
||||
erpShop.setOuterid(outerId);
|
||||
erpShop.setStuffStatus(commonParams.getStuffStatus());
|
||||
|
||||
// 2. 发布店铺(必填)
|
||||
PublishShop shop = new PublishShop();
|
||||
shop.setUserName(commonParams.getUserName());
|
||||
shop.setProvince(commonParams.getProvince());
|
||||
shop.setCity(commonParams.getCity());
|
||||
shop.setDistrict(commonParams.getDistrict());
|
||||
shop.setTitle(item.getProductName()); // 使用商品名称
|
||||
|
||||
// 生成商品描述(简单版)
|
||||
String content = "【正品保障】" + item.getProductName() + "\n\n" +
|
||||
"SKUID: " + item.getSkuid() + "\n" +
|
||||
"店铺信息: " + productDetail.getOrDefault("shopName", "京东商城");
|
||||
shop.setContent(content);
|
||||
|
||||
// 商品图片
|
||||
List<String> images = extractImages(productDetail);
|
||||
shop.setImages(images);
|
||||
|
||||
shop.setWhiteImages(commonParams.getWhiteImages());
|
||||
shop.setServiceSupport(commonParams.getServiceSupport());
|
||||
|
||||
List<PublishShop> publishShops = new ArrayList<>();
|
||||
publishShops.add(shop);
|
||||
erpShop.setPublishShop(publishShops);
|
||||
|
||||
// 3. 属性(如果有)
|
||||
if (StringUtils.isNotEmpty(commonParams.getChannelPv())) {
|
||||
try {
|
||||
Object channelPv = JSON.parse(commonParams.getChannelPv());
|
||||
// TODO: 解析并设置属性
|
||||
} catch (Exception e) {
|
||||
log.warn("解析商品属性失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 调用ERP发品接口
|
||||
ProductCreateRequest createRequest = new ProductCreateRequest(account);
|
||||
JSONObject body = JSONObject.parseObject(JSON.toJSONString(erpShop));
|
||||
createRequest.setRequestBody(body);
|
||||
String resp = createRequest.getResponseBody();
|
||||
|
||||
// 5. 解析响应
|
||||
JSONObject responseData = JSONObject.parseObject(resp);
|
||||
if (responseData != null && responseData.getInteger("code") == 0) {
|
||||
// 发品成功
|
||||
JSONObject data = responseData.getJSONObject("data");
|
||||
if (data != null) {
|
||||
Long productId = data.getLong("product_id");
|
||||
Integer productStatus = data.getInteger("product_status");
|
||||
|
||||
item.setStatus(2); // 发布成功
|
||||
item.setProductId(productId);
|
||||
item.setProductStatus(productStatus);
|
||||
item.setOuterId(outerId);
|
||||
item.setErrorMessage(null);
|
||||
itemMapper.updateBatchPublishItem(item);
|
||||
|
||||
log.info("发品成功: 商品ID={}, 商家编码={}", productId, outerId);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 发品失败
|
||||
String errorMsg = responseData != null ? responseData.getString("msg") : "发品失败";
|
||||
throw new RuntimeException(errorMsg);
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("发品失败", e);
|
||||
item.setStatus(3); // 发布失败
|
||||
@@ -342,6 +435,80 @@ public class BatchPublishServiceImpl implements IBatchPublishService
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据appid获取ERP账号
|
||||
*/
|
||||
private ERPAccount getAccountByAppid(String appid) {
|
||||
for (ERPAccount account : ERPAccount.values()) {
|
||||
if (account.getApiKey().equals(appid)) {
|
||||
return account;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取商品详情(从缓存或重新查询)
|
||||
*/
|
||||
private Map<String, Object> getProductDetail(String skuid) {
|
||||
try {
|
||||
// 调用JD接口查询商品详情
|
||||
Map<String, String> requestBody = new HashMap<>();
|
||||
requestBody.put("promotionContent", skuid);
|
||||
|
||||
String result = jdOrderService.generatePromotionContent(requestBody);
|
||||
if (StringUtils.isEmpty(result)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Object resultObj = JSON.parse(result);
|
||||
if (resultObj instanceof List) {
|
||||
List<?> list = (List<?>) resultObj;
|
||||
if (!list.isEmpty() && list.get(0) instanceof Map) {
|
||||
return (Map<String, Object>) list.get(0);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
log.error("查询商品详情失败: {}", skuid, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 提取商品图片
|
||||
*/
|
||||
private List<String> extractImages(Map<String, Object> productDetail) {
|
||||
List<String> images = new ArrayList<>();
|
||||
|
||||
// 从商品详情中提取图片
|
||||
Object imagesObj = productDetail.get("images");
|
||||
if (imagesObj instanceof List) {
|
||||
List<?> imageList = (List<?>) imagesObj;
|
||||
for (Object img : imageList) {
|
||||
if (img != null) {
|
||||
images.add(img.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有图片,尝试获取主图
|
||||
if (images.isEmpty()) {
|
||||
Object mainImage = productDetail.get("mainImage");
|
||||
if (mainImage != null) {
|
||||
images.add(mainImage.toString());
|
||||
}
|
||||
}
|
||||
|
||||
// 如果还是没有,添加一个占位图
|
||||
if (images.isEmpty()) {
|
||||
images.add("https://placeholder.com/300x300");
|
||||
}
|
||||
|
||||
return images;
|
||||
}
|
||||
|
||||
/**
|
||||
* 延迟上架商品
|
||||
*/
|
||||
@@ -380,17 +547,53 @@ public class BatchPublishServiceImpl implements IBatchPublishService
|
||||
item.setStatus(4);
|
||||
itemMapper.updateBatchPublishItem(item);
|
||||
|
||||
// TODO: 调用实际的上架接口
|
||||
// 这里需要调用 ProductController 的 publish 接口
|
||||
// 暂时模拟成功
|
||||
// 检查商品ID是否存在
|
||||
if (item.getProductId() == null) {
|
||||
throw new RuntimeException("商品ID不存在,无法上架");
|
||||
}
|
||||
|
||||
// 模拟上架成功
|
||||
item.setStatus(5); // 已上架
|
||||
item.setPublishTime(new Date());
|
||||
item.setErrorMessage(null);
|
||||
itemMapper.updateBatchPublishItem(item);
|
||||
// 获取ERP账号
|
||||
ERPAccount account = getAccountByAppid(item.getTargetAccount());
|
||||
if (account == null) {
|
||||
throw new RuntimeException("未找到ERP账号: " + item.getTargetAccount());
|
||||
}
|
||||
|
||||
// 获取任务信息(获取会员名)
|
||||
BatchPublishTask task = taskMapper.selectBatchPublishTaskById(item.getTaskId());
|
||||
if (task == null) {
|
||||
throw new RuntimeException("任务不存在");
|
||||
}
|
||||
|
||||
// 解析通用参数
|
||||
BatchPublishRequest.CommonParams commonParams = JSON.parseObject(
|
||||
task.getCommonParams(),
|
||||
BatchPublishRequest.CommonParams.class
|
||||
);
|
||||
|
||||
// 调用ERP上架接口
|
||||
ProductPublishRequest publishRequest = new ProductPublishRequest(account);
|
||||
publishRequest.setProductId(item.getProductId());
|
||||
publishRequest.setUserName(commonParams.getUserName());
|
||||
publishRequest.setSpecifyPublishTime(null); // 立即上架
|
||||
|
||||
String resp = publishRequest.getResponseBody();
|
||||
|
||||
// 解析响应
|
||||
JSONObject responseData = JSONObject.parseObject(resp);
|
||||
if (responseData != null && responseData.getInteger("code") == 0) {
|
||||
// 上架成功
|
||||
item.setStatus(5); // 已上架
|
||||
item.setPublishTime(new Date());
|
||||
item.setErrorMessage(null);
|
||||
itemMapper.updateBatchPublishItem(item);
|
||||
|
||||
log.info("上架成功: itemId={}, productId={}", itemId, item.getProductId());
|
||||
} else {
|
||||
// 上架失败
|
||||
String errorMsg = responseData != null ? responseData.getString("msg") : "上架失败";
|
||||
throw new RuntimeException(errorMsg);
|
||||
}
|
||||
|
||||
log.info("上架成功: itemId={}", itemId);
|
||||
} catch (Exception e) {
|
||||
log.error("上架失败", e);
|
||||
item.setStatus(6); // 上架失败
|
||||
|
||||
Reference in New Issue
Block a user