| @@ -252,6 +252,12 @@ public class WxVtwoActivityOrder extends TenantEntity { | |||
| @TableField(exist = false) | |||
| private Long activityId; | |||
| /** | |||
| * 是否可以发奖 | |||
| */ | |||
| @TableField(exist = false) | |||
| private Integer grantStatus; | |||
| /** | |||
| * 是否参与总金额计算 | |||
| */ | |||
| @@ -4,6 +4,7 @@ import com.iformall.domain.po.WxVtwoActivityGrant; | |||
| import com.iformall.domain.po.WxVtwoActivityGrantExport; | |||
| import com.baomidou.mybatisplus.extension.service.IService; | |||
| import com.iformall.domain.po.WxVtwoActivityGrantSettlement; | |||
| import com.iformall.domain.po.WxVtwoCashBackGrant; | |||
| import java.util.List; | |||
| @@ -15,4 +16,6 @@ import java.util.List; | |||
| public interface WxVtwoActivityGrantExportService extends IService<WxVtwoActivityGrantExport> { | |||
| void saveGrantExport(WxVtwoActivityGrant activityGrant, List<WxVtwoActivityGrantSettlement> settlementList); | |||
| void saveGrantExport(WxVtwoCashBackGrant activityGrant, List<WxVtwoActivityGrantSettlement> settlementList); | |||
| } | |||
| @@ -3,6 +3,7 @@ package com.iformall.service; | |||
| import com.iformall.domain.po.WxVtwoActivityGrant; | |||
| import com.iformall.domain.po.WxVtwoActivityGrantSettlement; | |||
| import com.baomidou.mybatisplus.extension.service.IService; | |||
| import com.iformall.domain.po.WxVtwoCashBackGrant; | |||
| /** | |||
| * @author xiaohu | |||
| @@ -12,4 +13,6 @@ import com.baomidou.mybatisplus.extension.service.IService; | |||
| public interface WxVtwoActivityGrantSettlementService extends IService<WxVtwoActivityGrantSettlement> { | |||
| void saveGrantSettlement(WxVtwoActivityGrant activityGrant); | |||
| void saveGrantSettlement(WxVtwoCashBackGrant activityGrant); | |||
| } | |||
| @@ -1,8 +1,13 @@ | |||
| package com.iformall.service; | |||
| import com.iformall.domain.po.WxVtwoActivityOrder; | |||
| import com.iformall.domain.po.WxVtwoCashBackActivity; | |||
| import com.iformall.domain.po.WxVtwoCashBackGrant; | |||
| import com.iformall.domain.po.WxVtwoCashBackGrantAward; | |||
| import com.baomidou.mybatisplus.extension.service.IService; | |||
| import java.util.List; | |||
| /** | |||
| * @author xiaohu | |||
| * @description 针对表【wx_vtwo_cash_back_grant_award(线下返现活动发放奖励)】的数据库操作Service | |||
| @@ -10,4 +15,7 @@ import com.baomidou.mybatisplus.extension.service.IService; | |||
| */ | |||
| public interface WxVtwoCashBackGrantAwardService extends IService<WxVtwoCashBackGrantAward> { | |||
| void handleGrantAwardList(WxVtwoCashBackActivity cashBackActivity, WxVtwoCashBackGrant activityGrant, List<WxVtwoActivityOrder> orderList); | |||
| void saveGrantAward(WxVtwoCashBackGrant activityGrant); | |||
| } | |||
| @@ -3,10 +3,7 @@ package com.iformall.service.impl; | |||
| import com.alibaba.fastjson.JSONObject; | |||
| import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | |||
| import com.iformall.common.IdWorker; | |||
| import com.iformall.domain.po.WxVtwoActivityGrant; | |||
| import com.iformall.domain.po.WxVtwoActivityGrantExport; | |||
| import com.iformall.domain.po.WxVtwoActivityGrantSettlement; | |||
| import com.iformall.domain.po.WxVtwoActivityOrder; | |||
| import com.iformall.domain.po.*; | |||
| import com.iformall.service.WxVtwoActivityGrantExportService; | |||
| import com.iformall.mapper.WxVtwoActivityGrantExportMapper; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| @@ -107,6 +104,31 @@ public class WxVtwoActivityGrantExportServiceImpl extends ServiceImpl<WxVtwoActi | |||
| grantExport.setTaxAmount(taxAmount); | |||
| return grantExport; | |||
| } | |||
| @Override | |||
| public void saveGrantExport(WxVtwoCashBackGrant activityGrant, List<WxVtwoActivityGrantSettlement> settlementList) { | |||
| Map<Long, List<WxVtwoActivityGrantSettlement>> orderSettlementMap = settlementList.stream().collect(Collectors.groupingBy(WxVtwoActivityGrantSettlement::getOrderId)); | |||
| Map<Long, WxVtwoActivityOrder> orderMap = activityGrant.getOrderList().stream().collect(Collectors.toMap(WxVtwoActivityOrder::getId, Function.identity())); | |||
| List<WxVtwoActivityGrantExport> grantExportList = new ArrayList<>(); | |||
| final IdWorker idWorker = IdWorker.get(); | |||
| orderSettlementMap.keySet().forEach(orderId -> { | |||
| List<WxVtwoActivityGrantSettlement> orderSettlementList = orderSettlementMap.get(orderId); | |||
| WxVtwoActivityOrder order = orderMap.get(orderId); | |||
| WxVtwoActivityGrantExport grantExport = getGrantExport(order,orderSettlementList); | |||
| grantExport.setId(idWorker.nextId()); | |||
| grantExport.setActivityId(activityGrant.getActivityId()); | |||
| grantExport.setGrantId(activityGrant.getId()); | |||
| grantExport.setGrantUserName(activityGrant.getRealName()); | |||
| grantExport.setGrantUserCard(activityGrant.getRealCard()); | |||
| grantExport.setGrantBy(activityGrant.getCreateBy()); | |||
| grantExport.setGrantByName(activityGrant.getCreateByName()); | |||
| grantExport.setGrantTime(activityGrant.getCreateDate()); | |||
| grantExport.setGrantRemark(activityGrant.getRemark()); | |||
| grantExport.setSignImg(activityGrant.getSignImg()); | |||
| }); | |||
| wxVtwoActivityGrantExportMapper.insertList(grantExportList); | |||
| } | |||
| } | |||
| @@ -79,6 +79,40 @@ public class WxVtwoActivityGrantSettlementServiceImpl extends ServiceImpl<WxVtwo | |||
| wxVtwoActivityGrantExportService.saveGrantExport(activityGrant,settlementList); | |||
| } | |||
| private WxVtwoActivityGrantSettlement getGrantSettlement(WxVtwoActivityGrantAward grant,WxVtwoActivityOrder order,IdWorker idWorker,boolean isOneOrder){ | |||
| WxVtwoActivityGrantSettlement settlement = new WxVtwoActivityGrantSettlement(); | |||
| settlement.setId(idWorker.nextId()); | |||
| settlement.updateTenantInfo(grant); | |||
| settlement.setActivityId(grant.getActivityId()); | |||
| settlement.setGrantId(grant.getGrantId()); | |||
| settlement.setGrantAwardId(grant.getId()); | |||
| settlement.setAwardId(grant.getAwardId()); | |||
| settlement.setAwardCount(grant.getAwardCount()); | |||
| settlement.setGrantPrice(grant.getGrantPrice()); | |||
| settlement.setOrderId(order.getId()); | |||
| settlement.setOrderNo(order.getOrderNo()); | |||
| settlement.setOrderAmount(order.getOrderMoney()); | |||
| settlement.setMerchantId(order.getMerchantId()); | |||
| if(isOneOrder){ | |||
| settlement.setOrderRatio(new BigDecimal(100)); | |||
| }else{ | |||
| settlement.setOrderRatio(order.getOrderRatio()); | |||
| } | |||
| settlement.setOrderGrantPrice(settlement.getGrantPrice() | |||
| .multiply(settlement.getOrderRatio().divide(new BigDecimal(100))) | |||
| .setScale(2, RoundingMode.HALF_UP)); | |||
| settlement.setMallShareRate(grant.getMallShareRate()); | |||
| settlement.setMerchantShareRate(grant.getMerchantShareRate()); | |||
| settlement.setOrderMallSharePrice(settlement.getOrderGrantPrice() | |||
| .multiply(settlement.getMallShareRate().divide(new BigDecimal(100))) | |||
| .setScale(2, RoundingMode.HALF_UP)); | |||
| settlement.setOrderMerchantSharePrice(settlement.getOrderGrantPrice().subtract(settlement.getOrderMallSharePrice())); | |||
| settlement.setCreateDate(grant.getUpdateDate()); | |||
| settlement.setUpdateDate(grant.getUpdateDate()); | |||
| return settlement; | |||
| } | |||
| private void handleOrderRatio(List<WxVtwoActivityOrder> orderList){ | |||
| BigDecimal orderSumMoney = orderList.stream().map(WxVtwoActivityOrder::getOrderMoney).reduce(BigDecimal.ZERO, BigDecimal::add); | |||
| int size = orderList.size() - 1; | |||
| @@ -94,7 +128,52 @@ public class WxVtwoActivityGrantSettlementServiceImpl extends ServiceImpl<WxVtwo | |||
| orderList.get(size).setOrderRatio(lastRatio); | |||
| } | |||
| private WxVtwoActivityGrantSettlement getGrantSettlement(WxVtwoActivityGrantAward grant,WxVtwoActivityOrder order,IdWorker idWorker,boolean isOneOrder){ | |||
| @Override | |||
| public void saveGrantSettlement(WxVtwoCashBackGrant activityGrant) { | |||
| List<WxVtwoActivityOrder> amountOrderList = activityGrant.getOrderList().stream().filter(o -> EnumYesOrNo.YES.getCode().equals(o.getIsAmount())) | |||
| .sorted(Comparator.comparing(WxVtwoActivityOrder::getOrderMoney).reversed()).collect(Collectors.toList()); | |||
| List<WxVtwoActivityOrder> cmountOrderList = activityGrant.getOrderList().stream().sorted(Comparator.comparing(WxVtwoActivityOrder::getOrderMoney).reversed()).collect(Collectors.toList()); | |||
| List<WxVtwoCashBackGrantAward> grantAwardList = activityGrant.getGrantAwardList(); | |||
| Map<Integer, List<WxVtwoCashBackGrantAward>> grantAwardMap = grantAwardList.stream().collect(Collectors.groupingBy(WxVtwoCashBackGrantAward::getModeType)); | |||
| List<WxVtwoCashBackGrantAward> amountGrantAwardList = grantAwardMap.get(EnumOfflineActivityAwardsModeType.AMOUNT.getCode()); | |||
| List<WxVtwoCashBackGrantAward> countGrantAwardList = grantAwardMap.get(EnumOfflineActivityAwardsModeType.COUNT.getCode()); | |||
| List<WxVtwoActivityGrantSettlement> settlementList = new ArrayList<>(); | |||
| final IdWorker idWorker = IdWorker.get(); | |||
| if(amountGrantAwardList != null && !amountGrantAwardList.isEmpty()){ | |||
| handleOrderRatio(amountOrderList); | |||
| Map<Long, WxVtwoActivityOrder> amountOrderMap = amountOrderList.stream().collect(Collectors.toMap(WxVtwoActivityOrder::getId, Function.identity())); | |||
| for (WxVtwoCashBackGrantAward grant : amountGrantAwardList) { | |||
| if(grant.getOrderId() != null){ | |||
| WxVtwoActivityOrder order = amountOrderMap.get(grant.getOrderId()); | |||
| WxVtwoActivityGrantSettlement settlement = getGrantSettlement(grant,order,idWorker,true); | |||
| settlementList.add(settlement); | |||
| }else{ | |||
| for (WxVtwoActivityOrder order : amountOrderList){ | |||
| WxVtwoActivityGrantSettlement settlement = getGrantSettlement(grant,order,idWorker,false); | |||
| settlementList.add(settlement); | |||
| } | |||
| } | |||
| } | |||
| } | |||
| if(countGrantAwardList != null && !countGrantAwardList.isEmpty()){ | |||
| handleOrderRatio(cmountOrderList); | |||
| for (WxVtwoCashBackGrantAward grant : countGrantAwardList) { | |||
| for (WxVtwoActivityOrder order : amountOrderList){ | |||
| WxVtwoActivityGrantSettlement settlement = getGrantSettlement(grant,order,idWorker,false); | |||
| settlementList.add(settlement); | |||
| } | |||
| } | |||
| } | |||
| wxVtwoActivityGrantSettlementMapper.insertList(settlementList); | |||
| wxVtwoActivityGrantExportService.saveGrantExport(activityGrant,settlementList); | |||
| } | |||
| private WxVtwoActivityGrantSettlement getGrantSettlement(WxVtwoCashBackGrantAward grant,WxVtwoActivityOrder order,IdWorker idWorker,boolean isOneOrder){ | |||
| WxVtwoActivityGrantSettlement settlement = new WxVtwoActivityGrantSettlement(); | |||
| settlement.setId(idWorker.nextId()); | |||
| settlement.updateTenantInfo(grant); | |||
| @@ -102,8 +181,7 @@ public class WxVtwoActivityGrantSettlementServiceImpl extends ServiceImpl<WxVtwo | |||
| settlement.setGrantId(grant.getGrantId()); | |||
| settlement.setGrantAwardId(grant.getId()); | |||
| settlement.setAwardId(grant.getAwardId()); | |||
| settlement.setAwardCount(grant.getAwardCount()); | |||
| settlement.setGrantPrice(grant.getGrantPrice()); | |||
| settlement.setGrantPrice(grant.getCashBack()); | |||
| settlement.setOrderId(order.getId()); | |||
| settlement.setOrderNo(order.getOrderNo()); | |||
| settlement.setOrderAmount(order.getOrderMoney()); | |||
| @@ -143,6 +143,9 @@ public class WxVtwoActivityOrderServiceImpl extends ServiceImpl<WxVtwoActivityOr | |||
| return; | |||
| } | |||
| for (WxVtwoActivityOrder record : recordList) { | |||
| if(EnumOfflineActivityRecordStatus.AUDI_SUCCESS.getCode().equals(record.getAuditStatus())){ | |||
| record.setGrantStatus(EnumYesOrNo.YES.getCode()); | |||
| } | |||
| WxVtwoActivityGrantOrder grantOrder = new WxVtwoActivityGrantOrder(); | |||
| grantOrder.updateTenantInfo(record); | |||
| grantOrder.setActivityId(activityId); | |||
| @@ -65,6 +65,15 @@ public class WxVtwoCashBackActivityServiceImpl extends ServiceImpl<WxVtwoCashBac | |||
| @Autowired | |||
| private WxVtwoCashBackGrantService wxVtwoCashBackGrantService; | |||
| @Autowired | |||
| private WxVtwoCashBackGrantAwardService wxVtwoCashBackGrantAwardService; | |||
| @Autowired | |||
| private WxVtwoActivityGrantOrderService wxVtwoActivityGrantOrderService; | |||
| @Autowired | |||
| private WxVtwoActivityGrantSettlementService wxVtwoActivityGrantSettlementService; | |||
| @Autowired | |||
| private ExcelService excelService; | |||
| @@ -250,162 +259,10 @@ public class WxVtwoCashBackActivityServiceImpl extends ServiceImpl<WxVtwoCashBac | |||
| wxVtwoCashBackGrantService.handleFromOrder(cashBackActivity.getName(),activityGrant,orderList,false); | |||
| List<WxVtwoCashBackGrantAward> grantAwardList = new ArrayList<>(); | |||
| BigDecimal cashBackTotal = BigDecimal.ZERO; | |||
| List<WxVtwoCashBackAward> sortAmountAwardList = cashBackActivity.getAmountRulesObject().stream().sorted(Comparator.comparing(WxVtwoCashBackAward::getMinAmount)).collect(toList()); | |||
| if(EnumOfflineActivityMode.ONE.getCode().equals(cashBackActivity.getAmountModel())){ | |||
| for(WxVtwoActivityOrder order:orderList){ | |||
| WxVtwoCashBackAward cashBackAward = null; | |||
| for(WxVtwoCashBackAward award:sortAmountAwardList){ | |||
| if((award.getMinAmount() == null || order.getOrderMoney().compareTo(award.getMinAmount()) >= 0) | |||
| && (award.getMaxAmount() == null || order.getOrderMoney().compareTo(award.getMaxAmount()) <= 0)){ | |||
| cashBackAward = award; | |||
| } | |||
| } | |||
| if(cashBackAward != null && cashBackAward.getCashBackRatio() != null){ | |||
| BigDecimal cashBackPrice = order.getOrderMoney().multiply(cashBackAward.getCashBackRatio().divide(new BigDecimal(100))); | |||
| if(cashBackAward.getCashBackLimit() != null && cashBackAward.getCashBackLimit().compareTo(BigDecimal.ZERO) > 0 | |||
| && cashBackAward.getCashBackLimit().compareTo(cashBackPrice) < 0 ){ | |||
| cashBackPrice = cashBackAward.getCashBackLimit(); | |||
| } | |||
| cashBackPrice = awardsRulePrice(cashBackPrice,cashBackActivity.getCashBackRule()); | |||
| if(cashBackPrice.compareTo(BigDecimal.ZERO) > 0){ | |||
| cashBackTotal = cashBackTotal.add(cashBackPrice); | |||
| WxVtwoCashBackGrantAward grantAward = new WxVtwoCashBackGrantAward(); | |||
| grantAward.setName("订单返现("+order.getOrderNo()+")"); | |||
| grantAward.setOrderId(order.getId()); | |||
| grantAward.setOrderAmount(order.getOrderMoney()); | |||
| grantAward.setModeType(EnumOfflineActivityAwardsModeType.AMOUNT.getCode()); | |||
| grantAward.setCashBackRule(cashBackActivity.getCashBackRule()); | |||
| grantAward.setCashBack(cashBackPrice); | |||
| handleGrantAward(grantAward,cashBackAward); | |||
| grantAwardList.add(grantAward); | |||
| } | |||
| } | |||
| } | |||
| }else{ | |||
| BigDecimal orderMoneySum = orderList.stream().filter(order -> EnumYesOrNo.YES.getCode().equals(order.getIsAmount())) | |||
| .map(WxVtwoActivityOrder::getOrderMoney).reduce(BigDecimal.ZERO, BigDecimal::add); | |||
| WxVtwoCashBackAward cashBackAward = null; | |||
| for(WxVtwoCashBackAward award:sortAmountAwardList){ | |||
| if((award.getMinAmount() == null || orderMoneySum.compareTo(award.getMinAmount()) >= 0) | |||
| && (award.getMaxAmount() == null || orderMoneySum.compareTo(award.getMaxAmount()) <= 0)){ | |||
| cashBackAward = award; | |||
| } | |||
| } | |||
| if(cashBackAward != null && cashBackAward.getCashBackRatio() != null){ | |||
| BigDecimal cashBackPrice = orderMoneySum.multiply(cashBackAward.getCashBackRatio().divide(new BigDecimal(100))); | |||
| cashBackPrice = awardsRulePrice(cashBackPrice,cashBackActivity.getCashBackRule()); | |||
| if(cashBackPrice.compareTo(BigDecimal.ZERO) > 0){ | |||
| cashBackTotal = cashBackTotal.add(cashBackPrice); | |||
| WxVtwoCashBackGrantAward grantAward = new WxVtwoCashBackGrantAward(); | |||
| grantAward.setName("订单金额返现"); | |||
| grantAward.setOrderAmount(orderMoneySum); | |||
| grantAward.setModeType(EnumOfflineActivityAwardsModeType.AMOUNT.getCode()); | |||
| grantAward.setCashBackRule(cashBackActivity.getCashBackRule()); | |||
| grantAward.setCashBack(cashBackPrice); | |||
| handleGrantAward(grantAward,cashBackAward); | |||
| grantAwardList.add(grantAward); | |||
| } | |||
| } | |||
| } | |||
| //联单 | |||
| Integer jointCount = wxVtwoActivityOrderService.getJointCount(cashBackActivity.getRelatedModel(),orderList); | |||
| if(jointCount != 0){ | |||
| Map<Integer, WxVtwoCashBackAward> jointCountMap = cashBackActivity.getRelatedRulesObject().stream().collect(toMap(WxVtwoCashBackAward::getRelatedCount, Function.identity())); | |||
| WxVtwoCashBackAward cashBackAward = jointCountMap.get(jointCount); | |||
| if(cashBackAward != null && cashBackAward.getCashBackRatio() != null){ | |||
| BigDecimal orderMoneySum = orderList.stream().map(WxVtwoActivityOrder::getOrderMoney).reduce(BigDecimal.ZERO, BigDecimal::add); | |||
| BigDecimal cashBackPrice = orderMoneySum.multiply(cashBackAward.getCashBackRatio().divide(new BigDecimal(100))); | |||
| cashBackPrice = awardsRulePrice(cashBackPrice,cashBackActivity.getCashBackRule()); | |||
| if(cashBackPrice.compareTo(BigDecimal.ZERO) > 0){ | |||
| cashBackTotal = cashBackTotal.add(cashBackPrice); | |||
| WxVtwoCashBackGrantAward grantAward = new WxVtwoCashBackGrantAward(); | |||
| grantAward.setName("联单返现"); | |||
| grantAward.setOrderAmount(orderMoneySum); | |||
| grantAward.setModeType(EnumOfflineActivityAwardsModeType.COUNT.getCode()); | |||
| grantAward.setCashBackRule(cashBackActivity.getCashBackRule()); | |||
| grantAward.setCashBack(cashBackPrice); | |||
| handleGrantAward(grantAward,cashBackAward); | |||
| grantAwardList.add(grantAward); | |||
| } | |||
| } | |||
| } | |||
| //计算返现金额最大限制 | |||
| BigDecimal prop = BigDecimal.ONE; | |||
| if(cashBackTotal.compareTo(cashBackActivity.getCashBackLimit()) >= 0){ | |||
| prop = cashBackActivity.getCashBackLimit().divide(cashBackTotal,4,RoundingMode.HALF_UP); | |||
| } | |||
| BigDecimal realCashBackTotal = BigDecimal.ZERO; | |||
| if(prop.compareTo(BigDecimal.ONE) < 0){ | |||
| for (WxVtwoCashBackGrantAward grantAward:grantAwardList){ | |||
| grantAward.setCashBack(awardsRulePrice(grantAward.getCashBack().multiply(prop),cashBackActivity.getCashBackRule())); | |||
| realCashBackTotal = realCashBackTotal.add(grantAward.getCashBack()); | |||
| } | |||
| } | |||
| activityGrant.setCashBackLimit(cashBackActivity.getCashBackLimit()); | |||
| activityGrant.setCashBack(realCashBackTotal); | |||
| if(EnumOfflineActivityMode.ONE.getCode().equals(cashBackActivity.getAmountModel())){ | |||
| //单独算税点 | |||
| BigDecimal taxAmount = BigDecimal.ZERO; | |||
| for (WxVtwoCashBackGrantAward grantAward:grantAwardList){ | |||
| grantAward.setTaxDeductionRules(cashBackActivity.getTaxDeductionRules()); | |||
| WxVtwoActivityTaxDeductionRule taxDeductionRulesObject = cashBackActivity.getTaxDeductionRulesObject(); | |||
| if(taxDeductionRulesObject.getStartingTax() != null && grantAward.getCashBack().compareTo(taxDeductionRulesObject.getStartingTax()) > 0 | |||
| && taxDeductionRulesObject.getTaxPoints() != null){ | |||
| grantAward.setTaxAmount(grantAward.getCashBack().multiply(taxDeductionRulesObject.getTaxPoints()) | |||
| .divide(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP)); | |||
| taxAmount = taxAmount.add(grantAward.getTaxAmount()); | |||
| } | |||
| } | |||
| activityGrant.setTaxAmount(taxAmount); | |||
| }else{ | |||
| activityGrant.setTaxDeductionRules(cashBackActivity.getTaxDeductionRules()); | |||
| WxVtwoActivityTaxDeductionRule taxDeductionRulesObject = cashBackActivity.getTaxDeductionRulesObject(); | |||
| if(taxDeductionRulesObject.getStartingTax() != null && activityGrant.getCashBack().compareTo(taxDeductionRulesObject.getStartingTax()) > 0 | |||
| && taxDeductionRulesObject.getTaxPoints() != null){ | |||
| activityGrant.setTaxAmount(activityGrant.getCashBack().multiply(taxDeductionRulesObject.getTaxPoints()) | |||
| .divide(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP)); | |||
| //税点分摊到发奖上面 | |||
| } | |||
| } | |||
| wxVtwoCashBackGrantAwardService.handleGrantAwardList(cashBackActivity, activityGrant, orderList); | |||
| activityGrant.setGrantAwardList(grantAwardList); | |||
| } | |||
| private BigDecimal awardsRulePrice(BigDecimal awardsPrice,Integer awardsRule){ | |||
| BigDecimal awardsRulePrice = BigDecimal.ZERO; | |||
| if(EnumOfflineActivityAwardsRule.DEF.getCode().equals(awardsRule)){ | |||
| awardsRulePrice = awardsPrice.setScale(2, RoundingMode.HALF_UP); | |||
| } else if (EnumOfflineActivityAwardsRule.YUAN.getCode().equals(awardsRule)) { | |||
| awardsRulePrice = awardsPrice.setScale(0, RoundingMode.HALF_UP); | |||
| }else if (EnumOfflineActivityAwardsRule.TEN.getCode().equals(awardsRule)){ | |||
| awardsRulePrice = awardsPrice.divide(new BigDecimal(10)) | |||
| .setScale(0, RoundingMode.HALF_UP).multiply(new BigDecimal(10)); | |||
| } | |||
| return awardsRulePrice; | |||
| } | |||
| private void handleGrantAward(WxVtwoCashBackGrantAward grantAward,WxVtwoCashBackAward cashBackAward){ | |||
| grantAward.setMinAmount(cashBackAward.getMinAmount()); | |||
| grantAward.setMaxAmount(cashBackAward.getMaxAmount()); | |||
| grantAward.setAwardId(cashBackAward.getId()); | |||
| grantAward.setAwardName(cashBackAward.getName()); | |||
| grantAward.setCashBackRatio(cashBackAward.getCashBackRatio()); | |||
| grantAward.setShareRule(cashBackAward.getShareRule()); | |||
| grantAward.setMallShareRate(cashBackAward.getMallShareRate()); | |||
| grantAward.setMerchantShareRate(cashBackAward.getMerchantShareRate()); | |||
| } | |||
| @Override | |||
| @Transactional(rollbackFor = Exception.class) | |||
| @@ -413,15 +270,17 @@ public class WxVtwoCashBackActivityServiceImpl extends ServiceImpl<WxVtwoCashBac | |||
| List<WxVtwoActivityOrder> orderList = wxVtwoActivityOrderService.findGrantOrder(activityGrant,activityGrant.getOrderIdList(),activityGrant.getOrderFilterIdList()); | |||
| // wxVtwoActivityGrantService.handleFromOrder(amountGiftActivity.getName(),activityGrant,orderList,false); | |||
| // | |||
| // wxVtwoActivityGrantService.saveOrUpdate(activityGrant); | |||
| // | |||
| // wxVtwoActivityGrantOrderService.saveGrantOrder(activityGrant.getId(),activityGrant.getActivityId(),orderList); | |||
| // | |||
| // wxVtwoActivityGrantAwardService.saveGrantAward(activityGrant); | |||
| // | |||
| // wxVtwoActivityGrantSettlementService.saveGrantSettlement(activityGrant); | |||
| wxVtwoCashBackGrantService.handleFromOrder(cashBackActivity.getName(),activityGrant,orderList,false); | |||
| wxVtwoCashBackGrantAwardService.handleGrantAwardList(cashBackActivity, activityGrant, orderList); | |||
| wxVtwoCashBackGrantService.saveOrUpdate(activityGrant); | |||
| wxVtwoActivityGrantOrderService.saveGrantOrder(activityGrant.getId(),activityGrant.getActivityId(),orderList); | |||
| wxVtwoCashBackGrantAwardService.saveGrantAward(activityGrant); | |||
| wxVtwoActivityGrantSettlementService.saveGrantSettlement(activityGrant); | |||
| } | |||
| } | |||
| @@ -1,11 +1,30 @@ | |||
| package com.iformall.service.impl; | |||
| import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | |||
| import com.iformall.domain.po.WxVtwoCashBackGrantAward; | |||
| import com.iformall.common.IdWorker; | |||
| import com.iformall.domain.po.*; | |||
| import com.iformall.domain.vo.WxVtwoActivityTaxDeductionRule; | |||
| import com.iformall.enums.EnumOfflineActivityAwardsModeType; | |||
| import com.iformall.enums.EnumOfflineActivityAwardsRule; | |||
| import com.iformall.enums.EnumOfflineActivityMode; | |||
| import com.iformall.enums.EnumYesOrNo; | |||
| import com.iformall.service.WxVtwoActivityOrderService; | |||
| import com.iformall.service.WxVtwoCashBackGrantAwardService; | |||
| import com.iformall.mapper.WxVtwoCashBackGrantAwardMapper; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.stereotype.Service; | |||
| import java.math.BigDecimal; | |||
| import java.math.RoundingMode; | |||
| import java.util.ArrayList; | |||
| import java.util.Comparator; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import java.util.function.Function; | |||
| import static java.util.stream.Collectors.toList; | |||
| import static java.util.stream.Collectors.toMap; | |||
| /** | |||
| * @author xiaohu | |||
| * @description 针对表【wx_vtwo_cash_back_grant_award(线下返现活动发放奖励)】的数据库操作Service实现 | |||
| @@ -15,6 +34,188 @@ import org.springframework.stereotype.Service; | |||
| public class WxVtwoCashBackGrantAwardServiceImpl extends ServiceImpl<WxVtwoCashBackGrantAwardMapper, WxVtwoCashBackGrantAward> | |||
| implements WxVtwoCashBackGrantAwardService{ | |||
| @Autowired | |||
| private WxVtwoCashBackGrantAwardMapper wxVtwoCashBackGrantAwardMapper; | |||
| @Autowired | |||
| private WxVtwoActivityOrderService wxVtwoActivityOrderService; | |||
| @Override | |||
| public void handleGrantAwardList(WxVtwoCashBackActivity cashBackActivity, WxVtwoCashBackGrant activityGrant, List<WxVtwoActivityOrder> orderList) { | |||
| List<WxVtwoCashBackGrantAward> grantAwardList = new ArrayList<>(); | |||
| BigDecimal cashBackTotal = BigDecimal.ZERO; | |||
| List<WxVtwoCashBackAward> sortAmountAwardList = cashBackActivity.getAmountRulesObject().stream().sorted(Comparator.comparing(WxVtwoCashBackAward::getMinAmount)).collect(toList()); | |||
| if(EnumOfflineActivityMode.ONE.getCode().equals(cashBackActivity.getAmountModel())){ | |||
| for(WxVtwoActivityOrder order:orderList){ | |||
| WxVtwoCashBackAward cashBackAward = null; | |||
| for(WxVtwoCashBackAward award:sortAmountAwardList){ | |||
| if((award.getMinAmount() == null || order.getOrderMoney().compareTo(award.getMinAmount()) >= 0) | |||
| && (award.getMaxAmount() == null || order.getOrderMoney().compareTo(award.getMaxAmount()) <= 0)){ | |||
| cashBackAward = award; | |||
| } | |||
| } | |||
| if(cashBackAward != null && cashBackAward.getCashBackRatio() != null){ | |||
| BigDecimal cashBackPrice = order.getOrderMoney().multiply(cashBackAward.getCashBackRatio().divide(new BigDecimal(100))); | |||
| if(cashBackAward.getCashBackLimit() != null && cashBackAward.getCashBackLimit().compareTo(BigDecimal.ZERO) > 0 | |||
| && cashBackAward.getCashBackLimit().compareTo(cashBackPrice) < 0 ){ | |||
| cashBackPrice = cashBackAward.getCashBackLimit(); | |||
| } | |||
| cashBackPrice = awardsRulePrice(cashBackPrice,cashBackActivity.getCashBackRule()); | |||
| if(cashBackPrice.compareTo(BigDecimal.ZERO) > 0){ | |||
| cashBackTotal = cashBackTotal.add(cashBackPrice); | |||
| WxVtwoCashBackGrantAward grantAward = new WxVtwoCashBackGrantAward(); | |||
| grantAward.setName("订单返现("+order.getOrderNo()+")"); | |||
| grantAward.setOrderId(order.getId()); | |||
| grantAward.setOrderAmount(order.getOrderMoney()); | |||
| grantAward.setModeType(EnumOfflineActivityAwardsModeType.AMOUNT.getCode()); | |||
| grantAward.setCashBackRule(cashBackActivity.getCashBackRule()); | |||
| grantAward.setCashBack(cashBackPrice); | |||
| handleGrantAward(grantAward,cashBackAward); | |||
| grantAwardList.add(grantAward); | |||
| } | |||
| } | |||
| } | |||
| }else{ | |||
| BigDecimal orderMoneySum = orderList.stream().filter(order -> EnumYesOrNo.YES.getCode().equals(order.getIsAmount())) | |||
| .map(WxVtwoActivityOrder::getOrderMoney).reduce(BigDecimal.ZERO, BigDecimal::add); | |||
| WxVtwoCashBackAward cashBackAward = null; | |||
| for(WxVtwoCashBackAward award:sortAmountAwardList){ | |||
| if((award.getMinAmount() == null || orderMoneySum.compareTo(award.getMinAmount()) >= 0) | |||
| && (award.getMaxAmount() == null || orderMoneySum.compareTo(award.getMaxAmount()) <= 0)){ | |||
| cashBackAward = award; | |||
| } | |||
| } | |||
| if(cashBackAward != null && cashBackAward.getCashBackRatio() != null){ | |||
| BigDecimal cashBackPrice = orderMoneySum.multiply(cashBackAward.getCashBackRatio().divide(new BigDecimal(100))); | |||
| cashBackPrice = awardsRulePrice(cashBackPrice,cashBackActivity.getCashBackRule()); | |||
| if(cashBackPrice.compareTo(BigDecimal.ZERO) > 0){ | |||
| cashBackTotal = cashBackTotal.add(cashBackPrice); | |||
| WxVtwoCashBackGrantAward grantAward = new WxVtwoCashBackGrantAward(); | |||
| grantAward.setName("订单金额返现"); | |||
| grantAward.setOrderAmount(orderMoneySum); | |||
| grantAward.setModeType(EnumOfflineActivityAwardsModeType.AMOUNT.getCode()); | |||
| grantAward.setCashBackRule(cashBackActivity.getCashBackRule()); | |||
| grantAward.setCashBack(cashBackPrice); | |||
| handleGrantAward(grantAward,cashBackAward); | |||
| grantAwardList.add(grantAward); | |||
| } | |||
| } | |||
| } | |||
| //联单 | |||
| Integer jointCount = wxVtwoActivityOrderService.getJointCount(cashBackActivity.getRelatedModel(),orderList); | |||
| if(jointCount != 0){ | |||
| Map<Integer, WxVtwoCashBackAward> jointCountMap = cashBackActivity.getRelatedRulesObject().stream().collect(toMap(WxVtwoCashBackAward::getRelatedCount, Function.identity())); | |||
| WxVtwoCashBackAward cashBackAward = jointCountMap.get(jointCount); | |||
| if(cashBackAward != null && cashBackAward.getCashBackRatio() != null){ | |||
| BigDecimal orderMoneySum = orderList.stream().map(WxVtwoActivityOrder::getOrderMoney).reduce(BigDecimal.ZERO, BigDecimal::add); | |||
| BigDecimal cashBackPrice = orderMoneySum.multiply(cashBackAward.getCashBackRatio().divide(new BigDecimal(100))); | |||
| cashBackPrice = awardsRulePrice(cashBackPrice,cashBackActivity.getCashBackRule()); | |||
| if(cashBackPrice.compareTo(BigDecimal.ZERO) > 0){ | |||
| cashBackTotal = cashBackTotal.add(cashBackPrice); | |||
| WxVtwoCashBackGrantAward grantAward = new WxVtwoCashBackGrantAward(); | |||
| grantAward.setName("联单返现"); | |||
| grantAward.setOrderAmount(orderMoneySum); | |||
| grantAward.setModeType(EnumOfflineActivityAwardsModeType.COUNT.getCode()); | |||
| grantAward.setCashBackRule(cashBackActivity.getCashBackRule()); | |||
| grantAward.setCashBack(cashBackPrice); | |||
| handleGrantAward(grantAward,cashBackAward); | |||
| grantAwardList.add(grantAward); | |||
| } | |||
| } | |||
| } | |||
| //计算返现金额最大限制 | |||
| BigDecimal prop = BigDecimal.ONE; | |||
| if(cashBackTotal.compareTo(cashBackActivity.getCashBackLimit()) >= 0){ | |||
| prop = cashBackActivity.getCashBackLimit().divide(cashBackTotal,4, RoundingMode.HALF_UP); | |||
| } | |||
| BigDecimal realCashBackTotal = BigDecimal.ZERO; | |||
| if(prop.compareTo(BigDecimal.ONE) < 0){ | |||
| for (WxVtwoCashBackGrantAward grantAward:grantAwardList){ | |||
| grantAward.setCashBack(awardsRulePrice(grantAward.getCashBack().multiply(prop),cashBackActivity.getCashBackRule())); | |||
| realCashBackTotal = realCashBackTotal.add(grantAward.getCashBack()); | |||
| } | |||
| } | |||
| activityGrant.setCashBackLimit(cashBackActivity.getCashBackLimit()); | |||
| activityGrant.setCashBack(realCashBackTotal); | |||
| if(EnumOfflineActivityMode.ONE.getCode().equals(cashBackActivity.getAmountModel())){ | |||
| //单独算税点 | |||
| BigDecimal taxAmount = BigDecimal.ZERO; | |||
| for (WxVtwoCashBackGrantAward grantAward:grantAwardList){ | |||
| grantAward.setTaxDeductionRules(cashBackActivity.getTaxDeductionRules()); | |||
| WxVtwoActivityTaxDeductionRule taxDeductionRulesObject = cashBackActivity.getTaxDeductionRulesObject(); | |||
| if(taxDeductionRulesObject.getStartingTax() != null && grantAward.getCashBack().compareTo(taxDeductionRulesObject.getStartingTax()) > 0 | |||
| && taxDeductionRulesObject.getTaxPoints() != null){ | |||
| grantAward.setTaxAmount(grantAward.getCashBack().multiply(taxDeductionRulesObject.getTaxPoints()) | |||
| .divide(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP)); | |||
| taxAmount = taxAmount.add(grantAward.getTaxAmount()); | |||
| } | |||
| } | |||
| activityGrant.setTaxAmount(taxAmount); | |||
| }else{ | |||
| activityGrant.setTaxDeductionRules(cashBackActivity.getTaxDeductionRules()); | |||
| WxVtwoActivityTaxDeductionRule taxDeductionRulesObject = cashBackActivity.getTaxDeductionRulesObject(); | |||
| if(taxDeductionRulesObject.getStartingTax() != null && activityGrant.getCashBack().compareTo(taxDeductionRulesObject.getStartingTax()) > 0 | |||
| && taxDeductionRulesObject.getTaxPoints() != null){ | |||
| activityGrant.setTaxAmount(activityGrant.getCashBack().multiply(taxDeductionRulesObject.getTaxPoints()) | |||
| .divide(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP)); | |||
| //税点分摊到发奖上面 | |||
| for (WxVtwoCashBackGrantAward grantAward:grantAwardList){ | |||
| BigDecimal taxAmountProp = grantAward.getCashBack().divide(activityGrant.getCashBack(),4,RoundingMode.HALF_UP);; | |||
| grantAward.setTaxAmount(activityGrant.getTaxAmount().multiply(taxAmountProp).setScale(2, RoundingMode.HALF_UP)); | |||
| } | |||
| } | |||
| } | |||
| activityGrant.setGrantAwardList(grantAwardList); | |||
| } | |||
| private BigDecimal awardsRulePrice(BigDecimal awardsPrice,Integer awardsRule){ | |||
| BigDecimal awardsRulePrice = BigDecimal.ZERO; | |||
| if(EnumOfflineActivityAwardsRule.DEF.getCode().equals(awardsRule)){ | |||
| awardsRulePrice = awardsPrice.setScale(2, RoundingMode.HALF_UP); | |||
| } else if (EnumOfflineActivityAwardsRule.YUAN.getCode().equals(awardsRule)) { | |||
| awardsRulePrice = awardsPrice.setScale(0, RoundingMode.HALF_UP); | |||
| }else if (EnumOfflineActivityAwardsRule.TEN.getCode().equals(awardsRule)){ | |||
| awardsRulePrice = awardsPrice.divide(new BigDecimal(10)) | |||
| .setScale(0, RoundingMode.HALF_UP).multiply(new BigDecimal(10)); | |||
| } | |||
| return awardsRulePrice; | |||
| } | |||
| private void handleGrantAward(WxVtwoCashBackGrantAward grantAward,WxVtwoCashBackAward cashBackAward){ | |||
| grantAward.setMinAmount(cashBackAward.getMinAmount()); | |||
| grantAward.setMaxAmount(cashBackAward.getMaxAmount()); | |||
| grantAward.setAwardId(cashBackAward.getId()); | |||
| grantAward.setAwardName(cashBackAward.getName()); | |||
| grantAward.setCashBackRatio(cashBackAward.getCashBackRatio()); | |||
| grantAward.setShareRule(cashBackAward.getShareRule()); | |||
| grantAward.setMallShareRate(cashBackAward.getMallShareRate()); | |||
| grantAward.setMerchantShareRate(cashBackAward.getMerchantShareRate()); | |||
| } | |||
| @Override | |||
| public void saveGrantAward(WxVtwoCashBackGrant activityGrant) { | |||
| List<WxVtwoCashBackGrantAward> grantAwardList = activityGrant.getGrantAwardList(); | |||
| final IdWorker idWorker = IdWorker.get(); | |||
| for (WxVtwoCashBackGrantAward grantAward : grantAwardList) { | |||
| grantAward.setId(idWorker.nextId()); | |||
| grantAward.updateTenantInfo(activityGrant); | |||
| grantAward.setActivityId(activityGrant.getActivityId()); | |||
| grantAward.setActivityName(activityGrant.getActivityName()); | |||
| grantAward.setGrantId(activityGrant.getId()); | |||
| grantAward.setCreateDate(activityGrant.getUpdateDate()); | |||
| grantAward.setUpdateDate(activityGrant.getUpdateDate()); | |||
| } | |||
| wxVtwoCashBackGrantAwardMapper.insertList(grantAwardList); | |||
| activityGrant.setGrantAwardList(grantAwardList); | |||
| } | |||
| } | |||