|
@@ -1,47 +1,201 @@
|
|
|
package cn.rankin.task.task.order;
|
|
|
|
|
|
import cn.rankin.common.utils.api.model.APIResult;
|
|
|
+import cn.rankin.common.utils.enums.BaseStatusEnum;
|
|
|
+import cn.rankin.common.utils.enums.ProductTypeEnum;
|
|
|
+import cn.rankin.data.api.product.entity.Goods;
|
|
|
+import cn.rankin.data.api.product.entity.Package;
|
|
|
+import cn.rankin.data.api.product.vo.PackageProductVo;
|
|
|
import cn.rankin.data.api.trade.entity.Order;
|
|
|
import cn.rankin.data.api.trade.entity.OrderDetail;
|
|
|
-import cn.rankin.task.service.TradeService;
|
|
|
+import cn.rankin.data.api.trade.entity.OrderGoods;
|
|
|
+import cn.rankin.task.service.OrderService;
|
|
|
+import cn.rankin.task.service.ProductService;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
@Slf4j
|
|
|
@Component
|
|
|
public class OrderSplitTask {
|
|
|
|
|
|
- public final static long ONE_MINUTE = 10 * 1000;
|
|
|
+ public final static long ONE_MINUTE = 60 * 1000;
|
|
|
|
|
|
public final static Integer SIZE = 100;
|
|
|
|
|
|
@Autowired
|
|
|
- private TradeService tradeService;
|
|
|
+ private ProductService productService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private OrderService orderService;
|
|
|
|
|
|
@Scheduled(fixedDelay = ONE_MINUTE)
|
|
|
public void run() {
|
|
|
- List<Order> orderList = loadOrder(SIZE);
|
|
|
+ // 查询订单
|
|
|
+ List<Order> orderList = orderService.findSplitList(SIZE);
|
|
|
+ if (CollectionUtils.isEmpty(orderList)) {
|
|
|
+ log.info("不存在需要处理的订单");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 逐单处理, 如果不拆直接拿到detail即可
|
|
|
+ for (Order order : orderList) {
|
|
|
+ String orderId = order.getId();
|
|
|
+ List<OrderDetail> orderDetailList = splitOrder(order);
|
|
|
+ if (orderDetailList == null) {
|
|
|
+ log.error("split order process is null, orderId={}", orderId);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ log.info("start split order, orderId={}", orderId);
|
|
|
+ Boolean result = orderService.split(orderId, orderDetailList);
|
|
|
+ if (result) {
|
|
|
+ log.info("split order success, orderId={}", orderId);
|
|
|
+ }else {
|
|
|
+ log.error("split order error, orderId={]", orderId);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 开始早错数据库了啊,更改状态,保存分拆的订单
|
|
|
+
|
|
|
+ // 将一个订单分拆处理成多个
|
|
|
+ public List<OrderDetail> splitOrder(Order order) {
|
|
|
+ // 将一个订单的虚拟商品拆成一个单,其他类型商品(周边)按厂商分拆到多个单
|
|
|
+ OrderDetail virtualOrder = null;
|
|
|
+ Map<String, OrderDetail> orderDetailCpIdMap = new HashMap<>();
|
|
|
+
|
|
|
+ String orderId = order.getId();
|
|
|
+ List<OrderGoods> goodsList = order.getGoods();
|
|
|
+
|
|
|
+ List<String> goodsIdList = new ArrayList<>();
|
|
|
+ goodsList.forEach(orderGoods -> goodsIdList.add(orderGoods.getGoodsId()));
|
|
|
+
|
|
|
+ //
|
|
|
+ Map<String, Goods> goodsMap = this.loadGoods(goodsIdList);
|
|
|
+ if (goodsMap == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
|
|
|
+ for (OrderGoods orderGoods : goodsList) {
|
|
|
+ ProductTypeEnum type = orderGoods.getType();
|
|
|
+ String goodsId = orderGoods.getGoodsId();
|
|
|
+ Goods goods = goodsMap.get(goodsId);
|
|
|
+ if (goods == null) {
|
|
|
+ log.error("商品不存在, goodsId={}, orderId={}", goodsId, orderId);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ OrderDetail orderDetail = null;
|
|
|
+ if (type.equals(ProductTypeEnum.SUPPORT)) {
|
|
|
+ // 周边,按cpId拆分
|
|
|
+ String cpId = goods.getCpId();
|
|
|
+ orderDetail = orderDetailCpIdMap.get(cpId);
|
|
|
+ if (orderDetail == null) {
|
|
|
+ orderDetail = initOrderDetail(order);
|
|
|
+ }
|
|
|
+ } else if (type.equals(ProductTypeEnum.COURSE)) {
|
|
|
+ // 课程,虚拟商品,放到virtualOrder中
|
|
|
+ if (virtualOrder == null) {
|
|
|
+ virtualOrder = initOrderDetail(order);
|
|
|
+ }
|
|
|
+ orderDetail = virtualOrder;
|
|
|
+ } else {
|
|
|
+ // 课程包,如果包含虚拟商品,则处理到虚拟商品, 如果是周边则不处理,因为实物已经在订单产生的时候生成了单独订单
|
|
|
+ String productId = goods.getPid();
|
|
|
+ Package pkg = loadPackage(productId);
|
|
|
+ if (pkg == null) {
|
|
|
+ log.error("load order package not exists, packageId={}, orderId={}", productId, orderId);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ List<PackageProductVo> packageProductVoList = pkg.getProducts();
|
|
|
+ for (PackageProductVo packageProductVo : packageProductVoList) {
|
|
|
+ ProductTypeEnum packageProductType = packageProductVo.getType();
|
|
|
+ if (packageProductType.equals(ProductTypeEnum.COURSE)) {
|
|
|
+ if (virtualOrder == null) {
|
|
|
+ virtualOrder = initOrderDetail(order);
|
|
|
+ }
|
|
|
+ orderDetail = virtualOrder;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果可以分拆产生一个订单,或者此商品可以归到一个分拆的订单中, 则处理
|
|
|
+ if (orderDetail != null) {
|
|
|
+ List<OrderGoods> orderGoodsList = orderDetail.getGoods();
|
|
|
+ orderGoodsList.add(orderGoods);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ List<OrderDetail> orderDetailList = new ArrayList<>();
|
|
|
+ for (String cpId : orderDetailCpIdMap.keySet()) {
|
|
|
+ OrderDetail orderDetail = orderDetailCpIdMap.get(cpId);
|
|
|
+ if (orderDetail != null) {
|
|
|
+ orderDetail.setCpId(cpId);
|
|
|
+ List<OrderGoods> orderGoodsList = orderDetail.getGoods();
|
|
|
+ if (!CollectionUtils.isEmpty(orderGoodsList)) {
|
|
|
+ for (OrderGoods orderGoods : orderGoodsList) {
|
|
|
+ Goods goods = goodsMap.get(orderGoods.getGoodsId());
|
|
|
+ orderDetail.setOriginPrice(orderDetail.getOriginPrice().add(goods.getMerchantPrice()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ orderDetailList.add(orderDetail);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if (virtualOrder != null) {
|
|
|
+ orderDetailList.add(virtualOrder);
|
|
|
+ }
|
|
|
+
|
|
|
+ return orderDetailList;
|
|
|
}
|
|
|
|
|
|
- // 取订单
|
|
|
- public List<Order> loadOrder(Integer limit) {
|
|
|
- APIResult<List<Order>> apiResult = tradeService.findPayList(limit);
|
|
|
+ public Package loadPackage(String pkgId) {
|
|
|
+ APIResult<Package> apiResult = productService.getPackage(pkgId);
|
|
|
if (!apiResult.getSuccess()) {
|
|
|
- log.error("load order error: %s", apiResult.getMessage());
|
|
|
+ log.error("load package error: {}", apiResult.getMessage());
|
|
|
return null;
|
|
|
}
|
|
|
return apiResult.getData();
|
|
|
}
|
|
|
|
|
|
- // 分拆
|
|
|
- public List<OrderDetail> splitOrder(List<Order> orderList) {
|
|
|
- return null;
|
|
|
+ public Map<String, Goods> loadGoods(List<String> goodsIdList) {
|
|
|
+ APIResult<List<Goods>> apiResult = productService.findGoodsByIds(goodsIdList);
|
|
|
+ if (!apiResult.getSuccess()) {
|
|
|
+ log.error("load goods error: {}", apiResult.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, Goods> map = new HashMap<>();
|
|
|
+ List<Goods> goodsList = apiResult.getData();
|
|
|
+ for (Goods goods : goodsList) {
|
|
|
+ String goodsId = goods.getId();
|
|
|
+ map.put(goodsId, goods);
|
|
|
+ }
|
|
|
+ return map;
|
|
|
}
|
|
|
|
|
|
- // 保存
|
|
|
+ public OrderDetail initOrderDetail(Order order) {
|
|
|
+ OrderDetail orderDetail = new OrderDetail();
|
|
|
+ orderDetail.setOrderId(order.getId());
|
|
|
+ orderDetail.setUid(order.getUid());
|
|
|
+ orderDetail.setName(order.getName());
|
|
|
+ orderDetail.setMobile(order.getMobile());
|
|
|
+ orderDetail.setAddress(order.getAddress());
|
|
|
+ orderDetail.setStatus(BaseStatusEnum.NORMAL);
|
|
|
+ orderDetail.setGoods(new ArrayList<>());
|
|
|
+ orderDetail.setOriginPrice(BigDecimal.ZERO);
|
|
|
+ orderDetail.setAdjustPrice(BigDecimal.ZERO);
|
|
|
+ orderDetail.setFinalPrice(BigDecimal.ZERO);
|
|
|
+ return orderDetail;
|
|
|
+ }
|
|
|
}
|