@ -4,8 +4,8 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qs.serve.framework.base.model.ResultFlag ;
import com.qs.serve.modules.tbs.common.TbsActivityState ;
import com.qs.serve.modules.tbs.common.TbsCostApplyState ;
import com.qs.serve.modules.tbs.entity.TbsActivity ;
import com.qs.serve.modules.tbs.entity.TbsCostApply ;
import com.qs.serve.modules.tbs.entity.* ;
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.* ;
@ -17,8 +17,12 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service ;
import java.math.BigDecimal ;
import java.math.RoundingMode ;
import java.time.LocalDateTime ;
import java.util.ArrayList ;
import java.util.List ;
import java.util.Map ;
import java.util.stream.Collectors ;
/ * *
* @author YenHex
@ -38,6 +42,7 @@ public class TzcRebateCheckApplicationServiceImpl implements TzcRebateCheckAppli
private final TzcRebateLevelsConfigService tzcRebateLevelsConfigService ;
private final TzcRebateLevelsService tzcRebateLevelsService ;
private final TbsActivityMapper tbsActivityMapper ;
private final TbsCostApplyService tbsCostApplyService ;
private final TbsActivityService tbsActivityService ;
private final TbsActivityCenterService tbsActivityCenterService ;
@ -46,10 +51,19 @@ public class TzcRebateCheckApplicationServiceImpl implements TzcRebateCheckAppli
private final TbsActivityCenterGoodsService tbsActivityCenterGoodsService ;
@Override
public void commitRebate ( Long id ) {
TzcRebate rebate = tzcRebateService . getById ( id ) ;
public void commitRebate ( Long rebateId ) {
// 100
final BigDecimal n100 = new BigDecimal ( "100" ) ;
Integer preCheckDays = tbsActivityMapper . getActivityPreCheckDays ( ) ;
if ( preCheckDays = = null ) {
// 默认延迟半个月
preCheckDays = 15 ;
}
TzcRebate rebate = tzcRebateService . getById ( rebateId ) ;
QueryWrapper qw = new QueryWrapper < > ( ) ;
qw . eq ( "rebate_id" , id ) ;
qw . eq ( "rebate_id" , rebateI d) ;
List < TzcRebateLevelsArgument > argumentList = tzcRebateLevelsArgumentService . list ( qw ) ;
List < TzcRebateSubject > subjectList = tzcRebateSubjectService . list ( qw ) ;
List < TzcRebatePeriod > periodList = tzcRebatePeriodService . list ( qw ) ;
@ -63,10 +77,12 @@ public class TzcRebateCheckApplicationServiceImpl implements TzcRebateCheckAppli
// 总返利金额
BigDecimal totalAllAmount = maxLevelsAmount . multiply ( new BigDecimal ( periodList . size ( ) ) ) ;
// TODO:校验返利参数
// 移除旧的返利费用
// 转化为费用申请
TbsCostApply costApply = new TbsCostApply ( ) ;
costApply . setRebateId ( rebateId ) ;
costApply . setCode ( rebate . getRebateCode ( ) ) ;
costApply . setMatchType ( 1 ) ;
costApply . setChargeTheme ( rebate . getRebateName ( ) ) ;
@ -81,11 +97,17 @@ public class TzcRebateCheckApplicationServiceImpl implements TzcRebateCheckAppli
costApply . setTotalActivityAmount ( totalAllAmount ) ;
costApply . setSubmitTime ( LocalDateTime . now ( ) ) ;
costApply . setRemark ( rebate . getRemark ( ) ) ;
tbsCostApplyService . save ( costApply ) ;
Long costApplyId = costApply . getId ( ) ;
Map < Long , List < TzcRebateCenter > > centerMap = centerList . stream ( )
. collect ( Collectors . groupingBy ( TzcRebateCenter : : getRebateSubjectId ) ) ;
for ( int i = 0 ; i < periodList . size ( ) ; i + + ) {
TzcRebatePeriod period = periodList . get ( i ) ;
//构建活动
TbsActivity activity = new TbsActivity ( ) ;
activity . setCostApplyId ( costApplyId ) ;
activity . setActivityCode ( rebate . getRebateCode ( ) + "_" + ( i + 1 ) ) ;
activity . setActivityState ( TbsActivityState . STATE_1_Finished ) ;
activity . setCostPassFlag ( 0 ) ;
@ -95,13 +117,130 @@ public class TzcRebateCheckApplicationServiceImpl implements TzcRebateCheckAppli
activity . setSupplierCode ( rebate . getSupplierCode ( ) ) ;
activity . setActStartDate ( period . getPeriodStartDate ( ) ) ;
activity . setActEndDate ( period . getPeriodEndDate ( ) ) ;
activity . setPreStartDate ( s tartDate) ;
activity . setPreEndDate ( endDate ) ;
activity . setPreCheckDate ( nowDate ) ;
activity . setPreStartDate ( period . getPeriodS tartDate( ) . plusDays ( preCheckDays ) ) ;
activity . setPreEndDate ( p eriod . getPeriodE ndDate( ) . plusDays ( preCheckDays ) ) ;
activity . setPreCheckDate ( period . getPeriodEndDate ( ) . plusDays ( preCheckDays ) ) ;
activity . setTotalAmount ( maxLevelsAmount ) ;
tbsActivityService . save ( activity ) ;
Long activityId = activity . getId ( ) ;
// 构建活动商品(ActivityGoods)
this . saveActivityGoodsList ( goodsList , costApplyId , activityId ) ;
for ( TzcRebateSubject rebateSubject : subjectList ) {
TbsActivitySubject activitySubject = new TbsActivitySubject ( ) ;
activitySubject . setSubjectId ( rebateSubject . getSubjectId ( ) ) ;
activitySubject . setSubjectCode ( rebateSubject . getSubjectCode ( ) ) ;
activitySubject . setSubjectName ( rebateSubject . getSubjectName ( ) ) ;
activitySubject . setAmount ( rebateSubject . getSubjectAmount ( ) ) ;
activitySubject . setOrgAmount ( rebateSubject . getSubjectAmount ( ) ) ;
activitySubject . setUsedAmount ( BigDecimal . ZERO ) ;
activitySubject . setCountSession ( 0 ) ;
activitySubject . setCountPerson ( 0 ) ;
activitySubject . setRemark ( rebateSubject . getRemark ( ) ) ;
tbsActivitySubjectService . save ( activitySubject ) ;
List < TbsActivityCenter > activityCenterList = new ArrayList < > ( ) ;
List < TbsActivityCenterGoods > centerGoodsList = new ArrayList < > ( ) ;
for ( TzcRebateCenter rebateCenter : centerMap . get ( rebateSubject . getId ( ) ) ) {
TbsActivityCenter activityCenter = new TbsActivityCenter ( ) ;
activityCenter . setCostApplyId ( costApplyId ) ;
activityCenter . setActivityId ( activityId ) ;
activityCenter . setSubjectId ( activitySubject . getSubjectId ( ) ) ;
activityCenter . setCenterRate ( rebateCenter . getCenterRate ( ) ) ;
activityCenter . setOrgCenterAmount ( rebateCenter . getCenterAmount ( ) ) ;
activityCenter . setCenterAmount ( rebateCenter . getCenterAmount ( ) ) ;
activityCenter . setUsedAmount ( BigDecimal . ZERO ) ;
activityCenter . setPayAmount ( BigDecimal . ZERO ) ;
activityCenter . setCenterType ( rebateCenter . getCenterType ( ) ) ;
activityCenter . setCenterId ( rebateCenter . getCenterId ( ) ) ;
activityCenter . setCenterCode ( rebateCenter . getCenterCode ( ) ) ;
activityCenter . setCenterName ( rebateCenter . getCenterName ( ) ) ;
activityCenter . setRemark ( rebateCenter . getRemark ( ) ) ;
activityCenterList . add ( activityCenter ) ;
// 平摊商品的比重 = 100 / goodsList.size()
BigDecimal goodsAvgRate = n100 . divide ( new BigDecimal ( goodsList . size ( ) ) , 1 , RoundingMode . DOWN ) ;
// 平摊商品的金额 = centerAmount * 平摊商品的比重
BigDecimal goodsAvgAmount = rebateCenter . getCenterAmount ( ) . multiply ( goodsAvgRate ) ;
// 剩余的比重和金额
BigDecimal surplusRate = n100 ;
BigDecimal surplusAmt = rebateCenter . getCenterAmount ( ) ;
for ( int i1 = 0 ; i1 < goodsList . size ( ) ; i1 + + ) {
// 最后一个商品,使用剩余的比重和金额
if ( i1 + 1 = = goodsList . size ( ) ) {
goodsAvgRate = surplusRate ;
goodsAvgAmount = surplusAmt ;
} else {
surplusRate = surplusRate . subtract ( goodsAvgRate ) ;
surplusAmt = surplusAmt . subtract ( goodsAvgAmount ) ;
}
TzcRebateGoods rebateGoods = goodsList . get ( i1 ) ;
TbsActivityCenterGoods centerGoods = new TbsActivityCenterGoods ( ) ;
centerGoods . setCenterGoodsCode ( activity . getActivityCode ( ) + "_" + ( i1 + 1 ) ) ;
centerGoods . setCostApplyId ( costApplyId ) ;
centerGoods . setActivityId ( activityId ) ;
centerGoods . setActivityCode ( activity . getActivityCode ( ) ) ;
centerGoods . setSubjectId ( rebateSubject . getSubjectId ( ) ) ;
centerGoods . setSubjectCode ( rebateSubject . getSubjectCode ( ) ) ;
centerGoods . setSubjectName ( rebateSubject . getSubjectName ( ) ) ;
centerGoods . setCenterGoodsAmount ( goodsAvgAmount ) ;
centerGoods . setOrgCenterGoodsAmount ( goodsAvgAmount ) ;
centerGoods . setCenterGoodsRate ( goodsAvgRate ) ;
centerGoods . setCenterType ( rebateCenter . getCenterType ( ) ) ;
centerGoods . setCenterId ( rebateCenter . getCenterId ( ) ) ;
centerGoods . setCenterCode ( rebateCenter . getCenterCode ( ) ) ;
centerGoods . setCenterName ( rebateCenter . getCenterName ( ) ) ;
centerGoods . setOrgCenterAmount ( rebateCenter . getCenterAmount ( ) ) ;
centerGoods . setCenterAmount ( rebateCenter . getCenterAmount ( ) ) ;
centerGoods . setCenterRate ( rebateCenter . getCenterRate ( ) ) ;
centerGoods . setTargetType ( rebateGoods . getTargetType ( ) ) ;
centerGoods . setTargetId ( rebateGoods . getTargetId ( ) ) ;
centerGoods . setTargetCode ( rebateGoods . getTargetCode ( ) ) ;
centerGoods . setTargetName ( rebateGoods . getTargetName ( ) ) ;
centerGoods . setTargetLevelPathIds ( rebateGoods . getTargetLevelPathIds ( ) ) ;
centerGoods . setTargetLevelPathNames ( rebateGoods . getTargetLevelPathNames ( ) ) ;
centerGoods . setRemark ( rebateGoods . getRemark ( ) ) ;
centerGoods . setActStartDate ( activity . getActStartDate ( ) ) ;
centerGoods . setActEndDate ( activity . getActEndDate ( ) ) ;
centerGoods . setPreStartDate ( activity . getPreStartDate ( ) ) ;
centerGoods . setPreEndDate ( activity . getPreEndDate ( ) ) ;
centerGoods . setPreCheckDate ( activity . getPreCheckDate ( ) ) ;
centerGoods . setSupplierId ( activity . getSupplierId ( ) ) ;
centerGoods . setSupplierCode ( activity . getSupplierCode ( ) ) ;
centerGoods . setSupplierName ( activity . getSupplierName ( ) ) ;
centerGoodsList . add ( centerGoods ) ;
}
}
tbsActivityCenterService . saveBatch ( activityCenterList ) ;
tbsActivityCenterGoodsService . saveBatch ( centerGoodsList ) ;
}
}
}
/ * *
* 构建活动商品 ( ActivityGoods )
* @param goodsList
* @param costApplyId
* @param activityId
* /
private void saveActivityGoodsList ( List < TzcRebateGoods > goodsList , Long costApplyId , Long activityId ) {
List < TbsActivityGoods > activityGoodsList = new ArrayList < > ( ) ;
for ( TzcRebateGoods rebateGoods : goodsList ) {
TbsActivityGoods activityGoods = new TbsActivityGoods ( ) ;
activityGoods . setCostApplyId ( costApplyId ) ;
activityGoods . setActivityId ( activityId ) ;
activityGoods . setTargetType ( rebateGoods . getTargetType ( ) ) ;
activityGoods . setTargetId ( rebateGoods . getTargetId ( ) ) ;
activityGoods . setTargetCode ( rebateGoods . getTargetCode ( ) ) ;
activityGoods . setTargetName ( rebateGoods . getTargetName ( ) ) ;
activityGoods . setTargetLevelPathIds ( rebateGoods . getTargetLevelPathIds ( ) ) ;
activityGoods . setTargetLevelPathNames ( rebateGoods . getTargetLevelPathNames ( ) ) ;
activityGoods . setRemark ( rebateGoods . getRemark ( ) ) ;
activityGoodsList . add ( activityGoods ) ;
}
tbsActivityGoodsService . saveBatch ( activityGoodsList ) ;
}
/ * *