|
@@ -0,0 +1,192 @@
|
|
|
+package cn.rankin.task.task.order;
|
|
|
+
|
|
|
+import cn.rankin.common.utils.api.model.APIResult;
|
|
|
+import cn.rankin.common.utils.constant.Constant;
|
|
|
+import cn.rankin.common.utils.enums.OrderStatusEnum;
|
|
|
+import cn.rankin.common.utils.enums.ProductTypeEnum;
|
|
|
+import cn.rankin.data.api.product.vo.GoodsVo;
|
|
|
+import cn.rankin.data.api.product.vo.PackageVo;
|
|
|
+import cn.rankin.data.api.product.vo.ProductVo;
|
|
|
+import cn.rankin.data.api.trade.entity.OrderGoods;
|
|
|
+import cn.rankin.data.api.trade.entity.ProductSoldSnapshot;
|
|
|
+import cn.rankin.data.api.trade.entity.UserOrder;
|
|
|
+import cn.rankin.data.api.user.vo.CampusVo;
|
|
|
+import cn.rankin.data.api.user.vo.TerminalUserVo;
|
|
|
+import cn.rankin.task.service.ProductService;
|
|
|
+import cn.rankin.task.service.TradeService;
|
|
|
+import cn.rankin.task.service.UserService;
|
|
|
+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.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class OrderCompleteTask {
|
|
|
+
|
|
|
+ public final static long ONE_MINUTE = 10 * 1000;
|
|
|
+
|
|
|
+ public final static Integer SIZE = 100;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private TradeService tradeService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ProductService productService;
|
|
|
+
|
|
|
+ @Scheduled(fixedDelay = ONE_MINUTE)
|
|
|
+ public void run() {
|
|
|
+ // 查询订单
|
|
|
+ APIResult<List<UserOrder>> orderListResult = tradeService.findPayList(SIZE);
|
|
|
+ if (!orderListResult.getSuccess()) {
|
|
|
+ log.error("get order list error: {}", orderListResult.getMessage());
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<UserOrder> userOrderList = orderListResult.getData();
|
|
|
+ if (CollectionUtils.isEmpty(userOrderList)) {
|
|
|
+ log.info("order list empty, return");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //取预加载信息所需信息
|
|
|
+ List<String> userIdList = new ArrayList<>();
|
|
|
+ for (UserOrder userOrder : userOrderList) {
|
|
|
+ String userId = userOrder.getUid();
|
|
|
+ if (!userIdList.contains(userId)) {
|
|
|
+ userIdList.add(userId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 加载用户信息
|
|
|
+ List<TerminalUserVo> terminalUserVoList = loadUser(userIdList);
|
|
|
+ if (CollectionUtils.isEmpty(terminalUserVoList)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<String> campusIdList = new ArrayList<>();
|
|
|
+ Map<String, TerminalUserVo> terminalUserVoMap = new HashMap<>();
|
|
|
+ for (TerminalUserVo terminalUserVo : terminalUserVoList) {
|
|
|
+ String userId = terminalUserVo.getId();
|
|
|
+ String campusId = terminalUserVo.getCampusId();
|
|
|
+ if (!campusIdList.contains(campusId)) {
|
|
|
+ campusIdList.add(campusId);
|
|
|
+ }
|
|
|
+ terminalUserVoMap.put(userId, terminalUserVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 加载校区信息
|
|
|
+ Map<String, CampusVo> campusVoMap = loadCampus(campusIdList);
|
|
|
+ if (CollectionUtils.isEmpty(campusVoMap)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 逐单处理
|
|
|
+ for (UserOrder userOrder : userOrderList) {
|
|
|
+ String orderId = userOrder.getId();
|
|
|
+ APIResult<Boolean> orderChangeResult = tradeService.complete(orderId, OrderStatusEnum.COMPLETE);
|
|
|
+ if (!orderChangeResult.getSuccess()) {
|
|
|
+ log.error("order complete status error, orderId={}", orderId);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<ProductSoldSnapshot> toSnaptshots(UserOrder userOrder, TerminalUserVo terminalUserVo, CampusVo campusVo) {
|
|
|
+ List<ProductSoldSnapshot> snapshotList = new ArrayList<>();
|
|
|
+ String orderId = userOrder.getId();
|
|
|
+ String userId = userOrder.getUid();
|
|
|
+ String userName = terminalUserVo.getName();
|
|
|
+ String userCode = terminalUserVo.getCode();
|
|
|
+ String campusId = campusVo.getId();
|
|
|
+ String campusCode = campusVo.getCode();
|
|
|
+ String campusName = campusVo.getName();
|
|
|
+
|
|
|
+ List<String> goodsIdList = new ArrayList<>();
|
|
|
+ List<String> productIdList = new ArrayList<>();
|
|
|
+ List<OrderGoods> orderGoodsList = userOrder.getGoods();
|
|
|
+
|
|
|
+ for (OrderGoods orderGoods : orderGoodsList) {
|
|
|
+ ProductTypeEnum type = orderGoods.getType();
|
|
|
+ String goodsId = orderGoods.getGoodsId();
|
|
|
+ String productId = orderGoods.getPid();
|
|
|
+ if (!goodsIdList.contains(goodsId)) {
|
|
|
+ goodsIdList.add(goodsId);
|
|
|
+ }
|
|
|
+ if(!productIdList.contains(productId)) {
|
|
|
+ productIdList.add(productId);
|
|
|
+ }
|
|
|
+ if (type.equals(ProductTypeEnum.PACKAGE)) {
|
|
|
+ APIResult<PackageVo> apiResult = productService.findProductById(productId);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public List<TerminalUserVo> loadUser(List<String> userIdList) {
|
|
|
+ APIResult<List<TerminalUserVo>> apiResult = userService.findUserByIds(userIdList);
|
|
|
+ if (!apiResult.getSuccess()) {
|
|
|
+ log.error("load user error, {}", apiResult.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<TerminalUserVo> terminalUserVoList = apiResult.getData();
|
|
|
+ return terminalUserVoList;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String, CampusVo> loadCampus(List<String> campusIdList) {
|
|
|
+ APIResult<Map<String, CampusVo>> apiResult = userService.findCampusByIds(campusIdList);
|
|
|
+ if (!apiResult.getSuccess()) {
|
|
|
+ log.error("load campus error, {}", apiResult.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return apiResult.getData();
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String, ProductVo> loadProduct(List<String> productIdList) {
|
|
|
+ APIResult<List<ProductVo>> apiResult = productService.findProductByIds(productIdList);
|
|
|
+ if (!apiResult.getSuccess()) {
|
|
|
+ log.error("load product error, {}", apiResult.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<ProductVo> productVoList = apiResult.getData();
|
|
|
+ Map<String, ProductVo> productVoMap = new HashMap<>();
|
|
|
+ for (ProductVo productVo : productVoList) {
|
|
|
+ String pid = productVo.getPid();
|
|
|
+ productVoMap.put(pid, productVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ return productVoMap;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String, GoodsVo> loadGoods(List<String> goodsIdList) {
|
|
|
+ APIResult<List<GoodsVo>> apiResult = productService.findGoodsByIds(goodsIdList);
|
|
|
+ if (!apiResult.getSuccess()) {
|
|
|
+ log.error("load goods error, {}", apiResult.getMessage());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ List<GoodsVo> goodsVoList = apiResult.getData();
|
|
|
+ Map<String, GoodsVo> goodsVoMap = new HashMap<>();
|
|
|
+ for (GoodsVo goodsVo : goodsVoList) {
|
|
|
+ String goodsId = goodsVo.getId();
|
|
|
+ goodsVoMap.put(goodsId, goodsVo);
|
|
|
+ }
|
|
|
+
|
|
|
+ return goodsVoMap;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|