|
@@ -0,0 +1,197 @@
|
|
|
+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.ptprofit.vo.PtProfitSaveReqVO;
|
|
|
+import cn.newfeifan.mall.module.distri.controller.admin.ptprofitlog.vo.PtProfitLogSaveReqVO;
|
|
|
+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.service.orderpercentage.OrderPercentageService;
|
|
|
+import cn.newfeifan.mall.module.distri.service.ptprofit.PtProfitService;
|
|
|
+import cn.newfeifan.mall.module.distri.service.ptprofitlog.PtProfitLogService;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+
|
|
|
+import org.springframework.validation.annotation.Validated;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+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;
|
|
|
+import cn.newfeifan.mall.framework.common.pojo.PageResult;
|
|
|
+import cn.newfeifan.mall.framework.common.pojo.PageParam;
|
|
|
+import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
|
|
|
+
|
|
|
+import cn.newfeifan.mall.module.distri.dal.mysql.ordercalc.OrderCalcMapper;
|
|
|
+
|
|
|
+import static cn.newfeifan.mall.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
|
+import static cn.newfeifan.mall.module.distri.constant.DistriConstants.PERCENT;
|
|
|
+import static cn.newfeifan.mall.module.distri.enums.ErrorCodeConstants.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 订单产生数据计算 Service 实现类
|
|
|
+ *
|
|
|
+ * @author 非繁人
|
|
|
+ */
|
|
|
+@Service
|
|
|
+@Validated
|
|
|
+public class OrderCalcServiceImpl implements OrderCalcService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private OrderCalcMapper orderCalcMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private PtProfitService ptProfitService;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private PtProfitLogService ptProfitLogService;
|
|
|
+
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Long createOrderCalc(OrderCalcSaveReqVO createReqVO) {
|
|
|
+ // 插入
|
|
|
+ OrderCalcDO orderCalc = BeanUtils.toBean(createReqVO, OrderCalcDO.class);
|
|
|
+ orderCalcMapper.insert(orderCalc);
|
|
|
+ // 返回
|
|
|
+ return orderCalc.getId();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void updateOrderCalc(OrderCalcSaveReqVO updateReqVO) {
|
|
|
+ // 校验存在
|
|
|
+ validateOrderCalcExists(updateReqVO.getId());
|
|
|
+ // 更新
|
|
|
+ OrderCalcDO updateObj = BeanUtils.toBean(updateReqVO, OrderCalcDO.class);
|
|
|
+ orderCalcMapper.updateById(updateObj);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteOrderCalc(Long id) {
|
|
|
+ // 校验存在
|
|
|
+ validateOrderCalcExists(id);
|
|
|
+ // 删除
|
|
|
+ orderCalcMapper.deleteById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void validateOrderCalcExists(Long id) {
|
|
|
+ if (orderCalcMapper.selectById(id) == null) {
|
|
|
+ throw exception(ORDER_CALC_NOT_EXISTS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public OrderCalcDO getOrderCalc(Long id) {
|
|
|
+ return orderCalcMapper.selectById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageResult<OrderCalcDO> getOrderCalcPage(OrderCalcPageReqVO pageReqVO) {
|
|
|
+ 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());
|
|
|
+
|
|
|
+
|
|
|
+ OrderPercentageDO orderPercentageDO = orderPercentageService.queryStatus();
|
|
|
+ // 当前计算百分比模板
|
|
|
+ String percentTemplate = JsonUtils.toJsonString(orderPercentageDO);
|
|
|
+
|
|
|
+
|
|
|
+ // 每个订单收益
|
|
|
+ List<OrderCalcSaveReqVO> saveList = new ArrayList<>();
|
|
|
+ // 平台收益
|
|
|
+ List<PtProfitSaveReqVO> ptProfitSaveReqVOS = new ArrayList<>();
|
|
|
+ // 平台收益日志
|
|
|
+ List<PtProfitLogSaveReqVO> ptProfitLogSaveReqVOS = new ArrayList<>();
|
|
|
+
|
|
|
+ PtProfitDO ptProfitDO = ptProfitService.getPtProfit();
|
|
|
+
|
|
|
+ tradeOrderDO.forEach(k -> {
|
|
|
+
|
|
|
+ // 计算利润: (价格 - 成本价格) * 产品数量
|
|
|
+ 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.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);
|
|
|
+
|
|
|
+ // 每个订单计算的过程
|
|
|
+ // 增加平台收益
|
|
|
+ // 平台服务费
|
|
|
+ Integer ptGrossAdd = ptProfitDO.getPtGrossAdd() + platformQuota;
|
|
|
+ // 平台收益
|
|
|
+ Integer ptAdd = ptProfitDO.getPtAdd() + grossProfit * (10000 - orderPercentageDO.getGrossProfitBonusQuotaPerc());
|
|
|
+ // 平台总收益
|
|
|
+ Integer ptTotal = ptProfitDO.getPtTotalAdd() + ptGrossAdd + ptAdd;
|
|
|
+
|
|
|
+ PtProfitSaveReqVO ptProfitSaveReqVO = PtProfitSaveReqVO.builder()
|
|
|
+ .ptAdd(ptAdd)
|
|
|
+ .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);
|
|
|
+
|
|
|
+
|
|
|
+ });
|
|
|
+
|
|
|
+ // 将信息存储到数据库中
|
|
|
+ orderCalcMapper.insertBatch(BeanUtils.toBean(saveList, OrderCalcDO.class));
|
|
|
+ ptProfitService.saveBatch(ptProfitSaveReqVOS);
|
|
|
+ ptProfitLogService.saveBatch(ptProfitLogSaveReqVOS);
|
|
|
+
|
|
|
+
|
|
|
+ // todo 计算当天用户的碰撞
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+}
|