|
|
@ -1,10 +1,17 @@ |
|
|
|
package com.qs.serve.modules.tzc.service.impl; |
|
|
|
|
|
|
|
import cn.hutool.core.collection.CollUtil; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
|
|
|
import com.qs.serve.modules.erp.entity.so.ErpDispatchSumPmsQuery; |
|
|
|
import com.qs.serve.modules.erp.entity.vo.ErpDispatchSumPmsVo; |
|
|
|
import com.qs.serve.modules.erp.mapper.ErpDispatchDataMapper; |
|
|
|
import com.qs.serve.modules.tbs.entity.TbsActivity; |
|
|
|
import com.qs.serve.modules.tbs.entity.TbsActivityGoods; |
|
|
|
import com.qs.serve.modules.tbs.entity.TbsCostApply; |
|
|
|
import com.qs.serve.modules.tbs.mapper.TbsActivityMapper; |
|
|
|
import com.qs.serve.modules.tbs.service.*; |
|
|
|
import com.qs.serve.modules.tzc.consts.TzcArgTypes; |
|
|
|
import com.qs.serve.modules.tzc.entity.TzcRebateGoods; |
|
|
|
import com.qs.serve.modules.tzc.entity.TzcRebatePeriod; |
|
|
|
import com.qs.serve.modules.tzc.entity.dto.TzcRebateCalculateDTO; |
|
|
|
import com.qs.serve.modules.tzc.entity.dto.TzcRebateLevelAmountDTO; |
|
|
@ -16,7 +23,10 @@ import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import java.math.BigDecimal; |
|
|
|
import java.time.LocalDate; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
/** |
|
|
|
* @author YenHex |
|
|
@ -47,27 +57,157 @@ public class TzcRebateApplication4VerificationServiceImpl implements TzcRebateAp |
|
|
|
private final TzcRebateGoodsService tzcRebateGoodsService; |
|
|
|
private final TzcRebateLevelsConfigService tzcRebateLevelsConfigService; |
|
|
|
private final TzcRebateLevelsService tzcRebateLevelsService; |
|
|
|
private final ErpDispatchDataMapper erpDispatchDataMapper; |
|
|
|
|
|
|
|
@Override |
|
|
|
public Object getRebateBatchVerificationList(Long costApplyId) { |
|
|
|
// 过滤不需要补充的返利期间
|
|
|
|
// 加载相关活动
|
|
|
|
// 已核销金额
|
|
|
|
TbsCostApply costApply = tbsCostApplyService.getById(costApplyId); |
|
|
|
List<TzcRebatePeriod> tzcRebatePeriods = tzcRebatePeriodService.list( |
|
|
|
new LambdaQueryWrapper<TzcRebatePeriod>() |
|
|
|
.eq(TzcRebatePeriod::getRebateId, costApply.getRebateId()) |
|
|
|
// 过滤不需要补充的返利期间
|
|
|
|
.eq(TzcRebatePeriod::getCompensationFlag,1) |
|
|
|
); |
|
|
|
List<TzcRebateGoods> goodsList = tzcRebateGoodsService.list( |
|
|
|
new LambdaQueryWrapper<TzcRebateGoods>() |
|
|
|
.eq(TzcRebateGoods::getRebateId, costApply.getRebateId()) |
|
|
|
); |
|
|
|
Map<String, List<TzcRebateGoods>> goodsMap = goodsList.stream() |
|
|
|
.collect(Collectors.groupingBy(TzcRebateGoods::getTargetType)); |
|
|
|
// 区间目标合计金额
|
|
|
|
BigDecimal totalPeriodAmt= BigDecimal.ZERO; |
|
|
|
// 同期进货合计金额
|
|
|
|
BigDecimal totalSamePeriodAmt= BigDecimal.ZERO; |
|
|
|
// 实际发货金额
|
|
|
|
BigDecimal totalDispatchAmt = BigDecimal.ZERO; |
|
|
|
for (TzcRebatePeriod period : tzcRebatePeriods) { |
|
|
|
totalPeriodAmt = totalPeriodAmt.add(period.getPeriodAmount()); |
|
|
|
totalSamePeriodAmt = totalSamePeriodAmt.add(period.getSamePeriodAmount()); |
|
|
|
BigDecimal dispatchAmt = this.getPeriodDispatch( |
|
|
|
costApply.getSupplierCode(), |
|
|
|
period.getPeriodStartDate(), |
|
|
|
period.getPeriodEndDate(), |
|
|
|
goodsMap); |
|
|
|
totalDispatchAmt = totalDispatchAmt.add(dispatchAmt); |
|
|
|
} |
|
|
|
// 计算满足的最高坎级
|
|
|
|
TzcRebateCalculateDTO calculateDTO = tzcRebateApplication4CostService.getHitRebateMaxLevel( |
|
|
|
totalDispatchAmt, |
|
|
|
costApply.getRebateId(), |
|
|
|
totalSamePeriodAmt, |
|
|
|
totalPeriodAmt |
|
|
|
); |
|
|
|
List<TzcRebateLevelAmountDTO> levelAmountList = tzcRebateApplication4CostService.calculateRebate( |
|
|
|
calculateDTO,totalDispatchAmt, totalSamePeriodAmt, TzcArgTypes.LevelOverallReturn); |
|
|
|
|
|
|
|
if (CollUtil.isNotEmpty(levelAmountList)){ |
|
|
|
// 合计每个坎级总目标返利金额
|
|
|
|
BigDecimal maxTotalReturn = BigDecimal.ZERO; |
|
|
|
for (TzcRebateLevelAmountDTO amountDTO : levelAmountList) { |
|
|
|
maxTotalReturn = maxTotalReturn.add(amountDTO.getLevelReturnAmt()); |
|
|
|
} |
|
|
|
|
|
|
|
// 统计返利每个期间返利金额
|
|
|
|
BigDecimal totalVerification = BigDecimal.ZERO; |
|
|
|
for (TzcRebatePeriod period : tzcRebatePeriods) { |
|
|
|
TbsActivity activity = tbsActivityService.getOne(new LambdaQueryWrapper<TbsActivity>() |
|
|
|
.eq(TbsActivity::getRebateId, costApply.getRebateId()) |
|
|
|
.eq(TbsActivity::getRebatePeriodId, period.getId()) |
|
|
|
,false |
|
|
|
); |
|
|
|
if (activity==null){ |
|
|
|
log.warn("返利期间未关联活动,返利ID:"+costApply.getRebateId()+"期间ID:"+period.getId()); |
|
|
|
continue; |
|
|
|
} |
|
|
|
// 已核销金额
|
|
|
|
BigDecimal verificationAmt = vtbVerificationMapper.totalActivityVerification(activity.getId()+""); |
|
|
|
// 计算每个区间可补金额
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 计算返利每个期间补充金额
|
|
|
|
// 结合核销金额及最高补偿金额,计算返利金额
|
|
|
|
// 返回活动对应的核销金额及返利计算流程
|
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 获取期间发货金额 |
|
|
|
* @param supplierCode |
|
|
|
* @param startDate |
|
|
|
* @param endDate |
|
|
|
* @param goodsMap |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public BigDecimal getPeriodDispatch(String supplierCode, |
|
|
|
LocalDate startDate, |
|
|
|
LocalDate endDate, |
|
|
|
Map<String, List<TzcRebateGoods>> goodsMap){ |
|
|
|
ErpDispatchSumPmsQuery query = new ErpDispatchSumPmsQuery(); |
|
|
|
query.setSupplierCode(supplierCode); |
|
|
|
query.setStartDate(startDate); |
|
|
|
query.setEndDate(endDate); |
|
|
|
|
|
|
|
boolean hasGoods = false; |
|
|
|
List<TzcRebateGoods> brandGoodsList = goodsMap.get("brand"); |
|
|
|
if(brandGoodsList!=null){ |
|
|
|
List<Long> ids = brandGoodsList.stream() |
|
|
|
.map(TzcRebateGoods::getTargetId).collect(Collectors.toList()); |
|
|
|
query.setBrandIds(ids); |
|
|
|
hasGoods = true; |
|
|
|
} |
|
|
|
List<TzcRebateGoods> seriesGoodsList = goodsMap.get("series"); |
|
|
|
if(seriesGoodsList!=null){ |
|
|
|
List<Long> ids = seriesGoodsList.stream() |
|
|
|
.map(TzcRebateGoods::getTargetId).collect(Collectors.toList()); |
|
|
|
query.setSeriesIds(ids); |
|
|
|
hasGoods = true; |
|
|
|
} |
|
|
|
List<TzcRebateGoods> categoryGoodsList = goodsMap.get("category"); |
|
|
|
if(categoryGoodsList!=null){ |
|
|
|
List<Long> ids = categoryGoodsList.stream() |
|
|
|
.map(TzcRebateGoods::getTargetId).collect(Collectors.toList()); |
|
|
|
query.setCategoryIds(ids); |
|
|
|
hasGoods = true; |
|
|
|
} |
|
|
|
List<TzcRebateGoods> spuGoodsList = goodsMap.get("spu"); |
|
|
|
if(spuGoodsList!=null){ |
|
|
|
List<Long> ids = spuGoodsList.stream() |
|
|
|
.map(TzcRebateGoods::getTargetId).collect(Collectors.toList()); |
|
|
|
query.setSpuIds(ids); |
|
|
|
hasGoods = true; |
|
|
|
} |
|
|
|
List<TzcRebateGoods> skuGoodsList = goodsMap.get("sku"); |
|
|
|
if(skuGoodsList!=null){ |
|
|
|
List<Long> ids = skuGoodsList.stream() |
|
|
|
.map(TzcRebateGoods::getTargetId).collect(Collectors.toList()); |
|
|
|
query.setSkuIds(ids); |
|
|
|
hasGoods = true; |
|
|
|
} |
|
|
|
BigDecimal amt = BigDecimal.ZERO; |
|
|
|
if (hasGoods) { |
|
|
|
amt = erpDispatchDataMapper.queryCusAmtByPms(query); |
|
|
|
if (amt==null){ |
|
|
|
amt = BigDecimal.ZERO; |
|
|
|
} |
|
|
|
} |
|
|
|
return amt; |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public TzcRebateCalculateDTO getActivityRebateAmount(Long activityId, BigDecimal dispatchAmt) { |
|
|
|
TbsActivity activity = tbsActivityMapper.selectById(activityId); |
|
|
|
TzcRebatePeriod rebatePeriod = tzcRebatePeriodService.getById(activity.getRebatePeriodId()); |
|
|
|
BigDecimal sameDispatchAmt = rebatePeriod.getSamePeriodAmount(); |
|
|
|
//BigDecimal sameDispatchAmt = BigDecimal.ZERO;
|
|
|
|
// 获取最高命中的坎级
|
|
|
|
TzcRebateCalculateDTO calculateDTO = tzcRebateApplication4CostService.getHitRebateMaxLevel( |
|
|
|
dispatchAmt, sameDispatchAmt, activity.getRebatePeriodId()); |
|
|
|
dispatchAmt, |
|
|
|
activity.getRebateId(), |
|
|
|
sameDispatchAmt, |
|
|
|
rebatePeriod.getPeriodAmount() |
|
|
|
); |
|
|
|
calculateDTO.setActivityId(activityId); |
|
|
|
List<TzcRebateLevelAmountDTO> levelAmountList = tzcRebateApplication4CostService.calculateRebate( |
|
|
|
calculateDTO,dispatchAmt, sameDispatchAmt, TzcArgTypes.LevelReturn); |
|
|
|