yuhongqi 5 日 前
コミット
2cfbb87d97

+ 5 - 0
fs-service-system/src/main/java/com/fs/live/service/ILiveOrderService.java

@@ -226,4 +226,9 @@ public interface ILiveOrderService {
     R createLiveOrderTest(LiveOrder param);
 
     List<LiveOrder> selectBankOrder();
+
+    /**
+     * 批量手动补录直播订单分佣/扣佣
+     */
+    R batchManualTuiMoneyByOrderIds(List<Long> orderIds);
 }

+ 127 - 1
fs-service-system/src/main/java/com/fs/live/service/impl/LiveOrderServiceImpl.java

@@ -104,6 +104,7 @@ import org.springframework.transaction.interceptor.TransactionAspectSupport;
 import javax.annotation.PreDestroy;
 
 import static com.fs.store.constants.StoreConstants.DELIVERY;
+import static com.fs.store.enums.BillDetailEnum.CATEGORY_3;
 
 
 /**
@@ -179,6 +180,9 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
     @Autowired
     private IFsUserService userService;
 
+    @Autowired
+    private IFsUserBillService billService;
+
     @Autowired
     private FsWxExpressTaskMapper fsWxExpressTaskMapper;
 
@@ -3268,7 +3272,7 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
         LiveUserFirstEntry liveUserFirstEntry = liveUserFirstEntryService.selectEntityByLiveIdUserId(liveOrder.getLiveId(), Long.parseLong(liveOrder.getUserId()));
         Long companyId = liveUserFirstEntry.getCompanyId();
         Long companyUserId = liveUserFirstEntry.getCompanyUserId();
-        if (companyId != null && companyId == -1L && companyUserId != null && companyUserId == -1L) {
+        if ((companyId != null && companyId == -1L && companyUserId != null && companyUserId == -1L) || (companyId != null && companyId == -2L && companyUserId != null && companyUserId == Long.valueOf(liveOrder.getUserId()))) {
             StoreConfig storeConfig = loadStoreConfig();
             companyId = storeConfig.getDefaultLiveCompanyId() != null ? storeConfig.getDefaultLiveCompanyId() : 309L;
             companyUserId = storeConfig.getDefaultLiveCompanyUserId() != null ? storeConfig.getDefaultLiveCompanyUserId() : 8647L;
@@ -3618,4 +3622,126 @@ public class LiveOrderServiceImpl implements ILiveOrderService {
         }
         return JSONUtil.toBean(configJson, StoreConfig.class);
     }
+
+    @Override
+    public R batchManualTuiMoneyByOrderIds(List<Long> orderIds) {
+        if (orderIds == null || orderIds.isEmpty()) {
+            return R.error("订单ID列表不能为空");
+        }
+        List<Map<String, Object>> details = new ArrayList<>();
+        int successCount = 0;
+        int skipCount = 0;
+        int failCount = 0;
+        for (Long orderId : orderIds) {
+            Map<String, Object> detail = new LinkedHashMap<>();
+            detail.put("orderId", orderId);
+            try {
+                List<String> messages = manualProcessLiveOrderTuiMoney(orderId);
+                detail.put("messages", messages);
+                boolean hasFail = messages.stream().anyMatch(msg -> msg.startsWith("失败"));
+                boolean hasSuccess = messages.stream().anyMatch(msg -> msg.contains("已补录") || msg.contains("已扣佣"));
+                if (hasFail) {
+                    detail.put("status", "fail");
+                    failCount++;
+                } else if (hasSuccess) {
+                    detail.put("status", "success");
+                    successCount++;
+                } else {
+                    detail.put("status", "skip");
+                    skipCount++;
+                }
+            } catch (Exception e) {
+                log.error("批量手动直播订单佣金处理失败, orderId={}", orderId, e);
+                detail.put("status", "fail");
+                detail.put("messages", Collections.singletonList("失败:" + e.getMessage()));
+                failCount++;
+            }
+            details.add(detail);
+        }
+        return R.ok()
+                .put("total", orderIds.size())
+                .put("success", successCount)
+                .put("skip", skipCount)
+                .put("fail", failCount)
+                .put("details", details);
+    }
+
+    /**
+     * 单笔订单手动分佣/扣佣:按支付成功、确认收货时的原有逻辑补录或回滚。
+     */
+    private List<String> manualProcessLiveOrderTuiMoney(Long orderId) {
+        List<String> messages = new ArrayList<>();
+        LiveOrder order = liveOrderMapper.selectLiveOrderByOrderId(String.valueOf(orderId));
+        if (order == null) {
+            messages.add("失败:订单不存在");
+            return messages;
+        }
+        if (!"1".equals(order.getIsPay()) && (order.getStatus() == null || order.getStatus() < 2)) {
+            messages.add("失败:订单未支付");
+            return messages;
+        }
+
+        boolean hasUserTuiBill = hasLiveOrderUserTuiBill(orderId);
+        if (!hasUserTuiBill) {
+            messages.addAll(addLiveOrderSpreadTuiMoney(order));
+        } else {
+            messages.add("跳过:推广分佣记录已存在");
+        }
+
+        if (order.getCompanyId() != null && order.getCompanyId() > 0
+                && order.getPayDelivery() != null
+                && order.getPayDelivery().compareTo(BigDecimal.ZERO) == 0) {
+            if (order.getTuiMoneyStatus() == null || order.getTuiMoneyStatus() != 1) {
+                if (order.getStatus() != null && order.getStatus() >= OrderInfoEnum.STATUS_3.getValue()) {
+                    companyService.addCompanyMoney(order);
+                    messages.add("公司佣金入账-已补录");
+                } else {
+                    messages.add("跳过:订单未完成,暂不补录公司佣金入账");
+                }
+            } else {
+                messages.add("跳过:公司佣金已入账");
+            }
+        }
+        return messages;
+    }
+
+    private List<String> addLiveOrderSpreadTuiMoney(LiveOrder order) {
+        List<String> messages = new ArrayList<>();
+        if (order.getCompanyUserId() != null && order.getCompanyUserId() == -1L) {
+            companyService.addCompanyTuiLiveMoney(order);
+            messages.add("推广分佣(公司)-已补录");
+            return messages;
+        }
+        if (order.getCompanyUserId() != null && order.getCompanyUserId() > 0) {
+            if (StringUtils.isEmpty(order.getItemJson())) {
+                messages.add("失败:订单商品信息缺失");
+                return messages;
+            }
+            FsStoreProduct product = JSONUtil.toBean(order.getItemJson(), FsStoreProduct.class);
+            if (product == null || product.getProductId() == null) {
+                messages.add("失败:订单商品信息无效");
+                return messages;
+            }
+            List<FsStoreProductAttrValue> productAttrValues =
+                    fsStoreProductAttrValueMapper.selectFsStoreProductAttrValueByProductId(product.getProductId());
+            if (productAttrValues == null || productAttrValues.isEmpty()) {
+                messages.add("失败:商品规格不存在");
+                return messages;
+            }
+            userService.addTuiLiveMoney(order, productAttrValues);
+            messages.add("推广分佣(用户)-已补录");
+            return messages;
+        }
+        messages.add("跳过:无推广员信息");
+        return messages;
+    }
+
+    private boolean hasLiveOrderUserTuiBill(Long orderId) {
+        FsUserBill query = new FsUserBill();
+        query.setCategory(CATEGORY_3.getValue());
+        query.setBusinessId(String.valueOf(orderId));
+        query.setBillType(1);
+        List<FsUserBill> bills = billService.selectFsUserBillList(query);
+        return bills != null && !bills.isEmpty();
+    }
 }

+ 46 - 0
fs-user-app/src/main/java/com/fs/app/controller/CommonController.java

@@ -23,6 +23,8 @@ import com.fs.erp.service.IErpOrderService;
 import com.fs.erp.service.IErpShopService;
 import com.fs.erp.service.IErpUserService;
 import com.fs.live.domain.LiveOrder;
+import com.fs.live.service.ILiveOrderService;
+import com.fs.live.support.LiveOrderManualTuiMoneyBatchIds;
 import com.fs.live.utils.CrossServiceRsaUtil;
 import com.fs.pay.pay.domain.OrderResult;
 import com.fs.pay.pay.dto.OrderQueryDTO;
@@ -146,6 +148,50 @@ public class CommonController extends AppBaseController {
 
 	@Autowired
 	IFsUserService fsUserService;
+
+	@Autowired
+	private ILiveOrderService liveOrderService;
+
+	@ApiOperation("批量手动补录直播订单分佣/扣佣")
+//	@GetMapping("/batchManualLiveOrderTuiMoney") 以后删除 暂时存放
+	public R batchManualLiveOrderTuiMoney(@RequestParam(required = false) String orderIds) {
+		try {
+			List<Long> idList;
+			if (StringUtils.isNotEmpty(orderIds)) {
+				idList = new ArrayList<>();
+				for (String part : orderIds.split(",")) {
+					if (StringUtils.isNotEmpty(part.trim())) {
+						idList.add(Long.parseLong(part.trim()));
+					}
+				}
+			} else {
+				idList = LiveOrderManualTuiMoneyBatchIds.defaultOrderIds();
+				idList = new ArrayList<>();
+			}
+			return liveOrderService.batchManualTuiMoneyByOrderIds(idList);
+		} catch (Exception ex) {
+			logger.error("批量手动直播订单佣金处理失败", ex);
+			return R.error("处理失败: " + ex.getMessage());
+		}
+	}
+
+	@ApiOperation("测试APP用户同步")
+//	@GetMapping("/testSyncAppUsers") 以后删除 暂时存放
+	public R testSyncAppUsers(@RequestParam(required = false, defaultValue = "3") Integer days,
+							  @RequestParam(required = false, defaultValue = "50") Integer limit) {
+		try {
+			fsUserService.syncAppUsersForRecentLiveOrders(days);
+			List<FsUser> syncedUsers = fsUserService.selectSyncedAppUsers(limit);
+			return R.ok()
+					.put("syncDays", days)
+					.put("count", syncedUsers == null ? 0 : syncedUsers.size())
+					.put("list", syncedUsers);
+		} catch (Exception ex) {
+			logger.error("测试APP用户同步失败", ex);
+			return R.error("测试失败: " + ex.getMessage());
+		}
+	}
+
 	@ApiOperation("支付手动通知")
 	@GetMapping("/teasadc")
 	public R getPhoneByHttp(@RequestParam Integer addressId) {