|
@@ -2,13 +2,20 @@ package cn.newfeifan.mall.module.distri.service.ordercalc;
|
|
|
|
|
|
import cn.newfeifan.mall.framework.common.mq.message.order.DistriOrderMessage;
|
|
|
import cn.newfeifan.mall.framework.common.util.json.JsonUtils;
|
|
|
+import cn.newfeifan.mall.module.distri.controller.admin.duser.vo.DuserSaveReqVO;
|
|
|
+import cn.newfeifan.mall.module.distri.controller.admin.integral.vo.IntegralSaveReqVO;
|
|
|
import cn.newfeifan.mall.module.distri.controller.admin.ptprofit.vo.PtProfitSaveReqVO;
|
|
|
import cn.newfeifan.mall.module.distri.controller.admin.ptprofitlog.vo.PtProfitLogSaveReqVO;
|
|
|
+import cn.newfeifan.mall.module.distri.dal.dataobject.integral.IntegralDO;
|
|
|
import cn.newfeifan.mall.module.distri.dal.dataobject.orderpercentage.OrderPercentageDO;
|
|
|
import cn.newfeifan.mall.module.distri.dal.dataobject.ptprofit.PtProfitDO;
|
|
|
+import cn.newfeifan.mall.module.distri.dal.dataobject.sharepath.SharePathDO;
|
|
|
+import cn.newfeifan.mall.module.distri.service.duser.DuserService;
|
|
|
+import cn.newfeifan.mall.module.distri.service.integral.IntegralService;
|
|
|
import cn.newfeifan.mall.module.distri.service.orderpercentage.OrderPercentageService;
|
|
|
import cn.newfeifan.mall.module.distri.service.ptprofit.PtProfitService;
|
|
|
import cn.newfeifan.mall.module.distri.service.ptprofitlog.PtProfitLogService;
|
|
|
+import cn.newfeifan.mall.module.distri.service.sharepath.SharePathService;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
@@ -16,8 +23,10 @@ import javax.annotation.Resource;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
+import java.awt.print.PrinterGraphics;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import cn.newfeifan.mall.module.distri.controller.admin.ordercalc.vo.*;
|
|
|
import cn.newfeifan.mall.module.distri.dal.dataobject.ordercalc.OrderCalcDO;
|
|
@@ -49,6 +58,17 @@ public class OrderCalcServiceImpl implements OrderCalcService {
|
|
|
@Resource
|
|
|
private PtProfitLogService ptProfitLogService;
|
|
|
|
|
|
+ @Resource
|
|
|
+ private SharePathService sharePathService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private OrderPercentageService orderPercentageService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private DuserService duserService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private IntegralService integralService;
|
|
|
|
|
|
@Override
|
|
|
public Long createOrderCalc(OrderCalcSaveReqVO createReqVO) {
|
|
@@ -92,17 +112,28 @@ public class OrderCalcServiceImpl implements OrderCalcService {
|
|
|
return orderCalcMapper.selectPage(pageReqVO);
|
|
|
}
|
|
|
|
|
|
- @Resource
|
|
|
- private OrderPercentageService orderPercentageService;
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
public void calc(List<DistriOrderMessage> tradeOrderDO) {
|
|
|
|
|
|
+ // 获取到当前订单的所有人
|
|
|
+ List<Long> userId = tradeOrderDO.stream().map(DistriOrderMessage::getUserId).collect(Collectors.toList());
|
|
|
+ // 判断当前下单的人员是否有进入营销模块用户表, 如果没有的话, 需要初始化
|
|
|
+ duserService.hasUserAndCreat(userId);
|
|
|
+
|
|
|
OrderPercentageDO orderPercentageDO = orderPercentageService.queryStatus();
|
|
|
// 当前计算百分比模板
|
|
|
String percentTemplate = JsonUtils.toJsonString(orderPercentageDO);
|
|
|
|
|
|
+ calc(tradeOrderDO, orderPercentageDO, percentTemplate);
|
|
|
+
|
|
|
+ // todo 计算当天用户的碰撞
|
|
|
+
|
|
|
|
|
|
+ }
|
|
|
+
|
|
|
+ private void calc(List<DistriOrderMessage> tradeOrderDO, OrderPercentageDO orderPercentageDO, String percentTemplate) {
|
|
|
// 每个订单收益
|
|
|
List<OrderCalcSaveReqVO> saveList = new ArrayList<>();
|
|
|
// 平台收益
|
|
@@ -110,39 +141,39 @@ public class OrderCalcServiceImpl implements OrderCalcService {
|
|
|
// 平台收益日志
|
|
|
List<PtProfitLogSaveReqVO> ptProfitLogSaveReqVOS = new ArrayList<>();
|
|
|
|
|
|
- PtProfitDO ptProfitDO = ptProfitService.getPtProfit();
|
|
|
+ // 修改用户钱包
|
|
|
+ List<IntegralSaveReqVO> integralSaveReqVOS = new ArrayList<>();
|
|
|
+
|
|
|
|
|
|
+ // todo 修改用户身价 如何从订单中获取身价?
|
|
|
+ List<DuserSaveReqVO> duserSaveReqVOS = new ArrayList<>();
|
|
|
+
|
|
|
+ PtProfitDO ptProfitDO = ptProfitService.getPtProfit();
|
|
|
tradeOrderDO.forEach(k -> {
|
|
|
+ // 获取当前下单人关系
|
|
|
+ SharePathDO sharePath = getSharePath(k.getUserId());
|
|
|
+ // 获取下单人的钱包以及用户表
|
|
|
+ IntegralDO integralDO = integralService.selectByUser(sharePath.getDescendant());
|
|
|
+ IntegralDO integralDOAncestor = integralService.selectByUser(sharePath.getAncestor());
|
|
|
|
|
|
// 计算利润: (价格 - 成本价格) * 产品数量
|
|
|
Integer profit = (k.getPrice() - k.getCostPrice()) * k.getProductCount();
|
|
|
// 计算毛利: 利润 * 0.38
|
|
|
Integer grossProfit = profit * orderPercentageDO.getGrossProfitPerc();
|
|
|
- // 计算用户额度: 利润 * 0.38
|
|
|
- Integer userProfit = grossProfit * orderPercentageDO.getGrossProfitUserQuotaPerc();
|
|
|
+ // 计算推荐人额度: 利润 * 0.38
|
|
|
+ Integer ancestorQuota = grossProfit * orderPercentageDO.getGrossProfitUserQuotaPerc();
|
|
|
// 计算直推人额度: 利润 * 0.38
|
|
|
- Integer ancestorQuota = grossProfit * orderPercentageDO.getGrossProfitAncestorQuotaPerc();
|
|
|
+ Integer descendantQuota = grossProfit * orderPercentageDO.getGrossProfitAncestorQuotaPerc();
|
|
|
// 计算合赢奖: 利润 * 0.38
|
|
|
Integer bonusQuota = grossProfit * orderPercentageDO.getGrossProfitBonusQuotaPerc();
|
|
|
// 计算平台收益: 利润 * 0.38
|
|
|
Integer platformQuota = grossProfit * orderPercentageDO.getGrossProfitPlatformQuotaPerc();
|
|
|
|
|
|
-
|
|
|
- // 计算
|
|
|
- OrderCalcSaveReqVO orderCalcSaveReqVO = OrderCalcSaveReqVO.builder()
|
|
|
- .userId(k.getUserId())
|
|
|
- .orderNo(k.getNo())
|
|
|
- .cost(k.getCostPrice())
|
|
|
- .price(k.getPrice())
|
|
|
- .grossProfit(grossProfit)
|
|
|
- .grossProfitUserQuota(userProfit)
|
|
|
- .grossProfitAncestorQuota(ancestorQuota)
|
|
|
- .grossProfitBonusQuota(bonusQuota)
|
|
|
- .grossProfitPlatformQuota(platformQuota)
|
|
|
- .percentTemplate(percentTemplate)
|
|
|
- .build();
|
|
|
- saveList.add(orderCalcSaveReqVO);
|
|
|
-
|
|
|
+ // ========== 汇总计算 =================
|
|
|
+ addCaclDo(percentTemplate, saveList, k, grossProfit, ancestorQuota, descendantQuota, bonusQuota, platformQuota);
|
|
|
+ // ========== 修改用户钱包 =================
|
|
|
+ changeUserWallet(integralSaveReqVOS, integralDO, integralDOAncestor, ancestorQuota, descendantQuota);
|
|
|
+ // ========== 修改平台信息 =================
|
|
|
// 每个订单计算的过程
|
|
|
// 增加平台收益
|
|
|
// 平台服务费
|
|
@@ -157,36 +188,109 @@ public class OrderCalcServiceImpl implements OrderCalcService {
|
|
|
.ptGrossAdd(ptGrossAdd)
|
|
|
.ptTotalAdd(ptTotal).build();
|
|
|
ptProfitSaveReqVOS.add(ptProfitSaveReqVO);
|
|
|
-
|
|
|
- // 平台记录
|
|
|
- PtProfitLogSaveReqVO ptGrossAddLog = PtProfitLogSaveReqVO.builder()
|
|
|
- .orderId(k.getOrderId())
|
|
|
- .profitStatus(1)
|
|
|
- .amount(platformQuota)
|
|
|
- .afterAmount(ptProfitSaveReqVO.getPtGrossAdd())
|
|
|
- .percentTemplate(percentTemplate).build();
|
|
|
-
|
|
|
- PtProfitLogSaveReqVO ptAddLog = PtProfitLogSaveReqVO.builder()
|
|
|
- .orderId(k.getOrderId())
|
|
|
- .profitStatus(2)
|
|
|
- .amount(grossProfit * (PERCENT - orderPercentageDO.getGrossProfitBonusQuotaPerc()))
|
|
|
- .afterAmount(ptProfitSaveReqVO.getPtAdd())
|
|
|
- .percentTemplate(percentTemplate).build();
|
|
|
- ptProfitLogSaveReqVOS.add(ptGrossAddLog);
|
|
|
- ptProfitLogSaveReqVOS.add(ptAddLog);
|
|
|
-
|
|
|
-
|
|
|
+ // ========== 增加日志记录 =================
|
|
|
+ addLog(orderPercentageDO, percentTemplate, ptProfitLogSaveReqVOS, k, sharePath, integralDO, integralDOAncestor, grossProfit, ancestorQuota, descendantQuota, bonusQuota, platformQuota, ptProfitSaveReqVO);
|
|
|
});
|
|
|
|
|
|
// 将信息存储到数据库中
|
|
|
orderCalcMapper.insertBatch(BeanUtils.toBean(saveList, OrderCalcDO.class));
|
|
|
ptProfitService.saveBatch(ptProfitSaveReqVOS);
|
|
|
ptProfitLogService.saveBatch(ptProfitLogSaveReqVOS);
|
|
|
+ integralService.saveBatch(integralSaveReqVOS);
|
|
|
+ }
|
|
|
|
|
|
+ private static void addCaclDo(String percentTemplate, List<OrderCalcSaveReqVO> saveList, DistriOrderMessage k, Integer grossProfit, Integer ancestorQuota, Integer descendantQuota, Integer bonusQuota, Integer platformQuota) {
|
|
|
+ OrderCalcSaveReqVO orderCalcSaveReqVO = OrderCalcSaveReqVO.builder()
|
|
|
+ .userId(k.getUserId())
|
|
|
+ .orderNo(k.getNo())
|
|
|
+ .cost(k.getCostPrice())
|
|
|
+ .price(k.getPrice())
|
|
|
+ .grossProfit(grossProfit)
|
|
|
+ .grossProfitUserQuota(ancestorQuota)
|
|
|
+ .grossProfitAncestorQuota(descendantQuota)
|
|
|
+ .grossProfitBonusQuota(bonusQuota)
|
|
|
+ .grossProfitPlatformQuota(platformQuota)
|
|
|
+ .percentTemplate(percentTemplate)
|
|
|
+ .build();
|
|
|
+ saveList.add(orderCalcSaveReqVO);
|
|
|
+ }
|
|
|
|
|
|
- // todo 计算当天用户的碰撞
|
|
|
+ private static void changeUserWallet(List<IntegralSaveReqVO> integralSaveReqVOS, IntegralDO integralDO, IntegralDO integralDOAncestor, Integer ancestorQuota, Integer descendantQuota) {
|
|
|
+ // 修改用户钱包
|
|
|
+ integralDO.setCurrentQuota(integralDO.getCurrentQuota() + descendantQuota);
|
|
|
+ integralDOAncestor.setCurrentQuota(integralDO.getCurrentQuota() + ancestorQuota);
|
|
|
+
|
|
|
+ integralSaveReqVOS.add(BeanUtils.toBean(integralDO, IntegralSaveReqVO.class));
|
|
|
+ integralSaveReqVOS.add(BeanUtils.toBean(integralDOAncestor, IntegralSaveReqVO.class));
|
|
|
+ }
|
|
|
|
|
|
|
|
|
+ private static void addLog(OrderPercentageDO orderPercentageDO, String percentTemplate, List<PtProfitLogSaveReqVO> ptProfitLogSaveReqVOS, DistriOrderMessage k, SharePathDO sharePath, IntegralDO integralDO, IntegralDO integralDOAncestor, Integer grossProfit, Integer ancestorQuota, Integer descendantQuota, Integer bonusQuota, Integer platformQuota, PtProfitSaveReqVO ptProfitSaveReqVO) {
|
|
|
+
|
|
|
+ // 平台记录
|
|
|
+ PtProfitLogSaveReqVO ptGrossAddLog = PtProfitLogSaveReqVO.builder()
|
|
|
+ .orderId(k.getOrderId())
|
|
|
+ .profitStatus(1)
|
|
|
+ .orderNo(k.getNo())
|
|
|
+ .amount(platformQuota)
|
|
|
+ .afterAmount(ptProfitSaveReqVO.getPtGrossAdd())
|
|
|
+ .percentTemplate(percentTemplate).build();
|
|
|
+
|
|
|
+ PtProfitLogSaveReqVO ptAddLog = PtProfitLogSaveReqVO.builder()
|
|
|
+ .orderId(k.getOrderId())
|
|
|
+ .profitStatus(2)
|
|
|
+ .orderNo(k.getNo())
|
|
|
+ .amount(grossProfit * (PERCENT - orderPercentageDO.getGrossProfitBonusQuotaPerc()))
|
|
|
+ .afterAmount(ptProfitSaveReqVO.getPtAdd())
|
|
|
+ .percentTemplate(percentTemplate).build();
|
|
|
+
|
|
|
+
|
|
|
+ // 推荐人记录
|
|
|
+ PtProfitLogSaveReqVO tjrLog = PtProfitLogSaveReqVO.builder()
|
|
|
+ .orderId(k.getOrderId())
|
|
|
+ .profitStatus(2)
|
|
|
+ .orderNo(k.getNo())
|
|
|
+ .userId(sharePath.getAncestor())
|
|
|
+ .amount(ancestorQuota)
|
|
|
+ .afterAmount(integralDOAncestor.getCurrentQuota() + ancestorQuota)
|
|
|
+ .percentTemplate(percentTemplate).build();
|
|
|
+
|
|
|
+
|
|
|
+ // 直推人记录
|
|
|
+ PtProfitLogSaveReqVO ztrLog = PtProfitLogSaveReqVO.builder()
|
|
|
+ .orderId(k.getOrderId())
|
|
|
+ .profitStatus(2)
|
|
|
+ .orderNo(k.getNo())
|
|
|
+ .userId(sharePath.getDescendant())
|
|
|
+ .amount(descendantQuota)
|
|
|
+ .afterAmount(integralDO.getCurrentQuota() + descendantQuota)
|
|
|
+ .percentTemplate(percentTemplate).build();
|
|
|
+
|
|
|
+
|
|
|
+ // 合赢奖记录
|
|
|
+ PtProfitLogSaveReqVO hyjLog = PtProfitLogSaveReqVO.builder()
|
|
|
+ .orderId(k.getOrderId())
|
|
|
+ .profitStatus(2)
|
|
|
+ .orderNo(k.getNo())
|
|
|
+ .amount(bonusQuota)
|
|
|
+ .afterAmount(0)
|
|
|
+ .percentTemplate(percentTemplate).build();
|
|
|
+
|
|
|
+
|
|
|
+ ptProfitLogSaveReqVOS.add(ptGrossAddLog);
|
|
|
+ ptProfitLogSaveReqVOS.add(ptAddLog);
|
|
|
+ ptProfitLogSaveReqVOS.add(tjrLog);
|
|
|
+ ptProfitLogSaveReqVOS.add(ztrLog);
|
|
|
+ ptProfitLogSaveReqVOS.add(hyjLog);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
+ // 获取当前订单的下单人的直推关系
|
|
|
+ public SharePathDO getSharePath(Long userId) {
|
|
|
+
|
|
|
+ return sharePathService.getSharePathByDescendant(userId);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|