前端+后端:完整接入订单确认的优惠劵计算

This commit is contained in:
YunaiV 2019-04-20 01:22:39 +08:00
parent cb07eb3f71
commit bc2bf52039
8 changed files with 42 additions and 16 deletions

View File

@ -35,13 +35,14 @@ export function confirmReceiving(orderId) {
});
}
export function getOrderConfirmCreateOrder(skuId, quantity) {
export function getOrderConfirmCreateOrder(skuId, quantity, couponCardId) {
return request({
url: '/order-api/users/order/confirm_create_order',
method: 'get',
params: {
skuId,
quantity,
couponCardId,
}
});
}
@ -109,13 +110,12 @@ export function updateCartSelected(skuIds, selected) {
});
}
export function getCartConfirmCreateOrder(skuId, quantity) {
export function getCartConfirmCreateOrder(couponCardId) {
return request({
url: '/order-api/users/cart/confirm_create_order',
method: 'get',
params: {
skuId,
quantity,
couponCardId
}
});
}

View File

@ -125,6 +125,21 @@
},
methods: {
onCouponChange(index) {
// debugger;
let couponCardId = this.coupons[index].id;
if (this.from === 'direct_order') {
getOrderConfirmCreateOrder(this.skuId, this.quantity, couponCardId).then(data => {
// this.itemGroups = data.itemGroups;
this.fee = data.fee;
this.coupons[index].value = data.couponCardDiscountTotal; //
})
} else if (this.from === 'cart') {
getCartConfirmCreateOrder(couponCardId).then(data => {
// this.itemGroups = data.itemGroups;
this.fee = data.fee;
this.coupons[index].value = data.couponCardDiscountTotal; //
})
}
this.chosenCoupon = index;
this.showCouponPopup = false;
},
@ -205,7 +220,7 @@
endAt: card.validEndTime / 1000,
// description: '',
reason: card.unavailableReason,
value: 0, // TODO
value: 0,
valueDesc: card.preferentialType === 1 ? card.priceOff / 100 : card.percentOff / 10.0,
unitDesc: card.preferentialType === 1 ? '元' : '折'
})
@ -228,6 +243,12 @@
this.itemGroups = data.itemGroups;
this.fee = data.fee;
//
this.coupons = this.convertCouponList(data.couponCards.filter(function (element) {
return element.available;
}));
this.disabledCoupons = this.convertCouponList(data.couponCards.filter(function (element) {
return !element.available;
}));
})
} else if (this.from === 'cart') {
getCartConfirmCreateOrder().then(data => {

View File

@ -105,7 +105,7 @@ public class UsersCartController {
}
@GetMapping("/confirm_create_order")
public CommonResult<UsersOrderConfirmCreateVO> getConfirmCreateOrder(@RequestParam("couponCardId") Integer couponCardId) {
public CommonResult<UsersOrderConfirmCreateVO> getConfirmCreateOrder(@RequestParam(value = "couponCardId", required = false) Integer couponCardId) {
Integer userId = UserSecurityContextHolder.getContext().getUserId();
// 获得购物车中选中的
List<CartItemBO> cartItems = cartService.list(userId, true).getData();

View File

@ -94,7 +94,7 @@ public class UsersOrderController {
@ApiOperation("确认创建订单")
public CommonResult<UsersOrderConfirmCreateVO> getConfirmCreateOrder(@RequestParam("skuId") Integer skuId,
@RequestParam("quantity") Integer quantity,
@RequestParam("couponCardId") Integer couponCardId) {
@RequestParam(value = "couponCardId", required = false) Integer couponCardId) {
// 创建 CalcOrderPriceDTO 对象并执行价格计算
CalcOrderPriceDTO calcOrderPriceDTO = new CalcOrderPriceDTO()
.setUserId(UserSecurityContextHolder.getContext().getUserId())

View File

@ -24,6 +24,10 @@ public class UsersOrderConfirmCreateVO {
* 优惠劵列表 TODO 芋艿后续改改
*/
private List<CouponCardAvailableBO> couponCards;
/**
* 优惠劵优惠金额
*/
private Integer couponCardDiscountTotal;
/**
* 商品分组
*

View File

@ -379,7 +379,8 @@ public class CartServiceImpl implements CartService {
Assert.isTrue(presentTotal > 0, "计算后,价格为负数:" + presentTotal);
} else if (PreferentialTypeEnum.DISCOUNT.getValue().equals(couponCard.getPreferentialType())) { // 打折
presentTotal = originalTotal * couponCard.getPercentOff() / 100;
if (originalTotal - presentTotal > couponCard.getDiscountPriceLimit()) {
if (couponCard.getDiscountPriceLimit() != null // 代表不限制优惠上限
&& originalTotal - presentTotal > couponCard.getDiscountPriceLimit()) {
presentTotal = originalTotal - couponCard.getDiscountPriceLimit();
}
} else {
@ -432,7 +433,7 @@ public class CartServiceImpl implements CartService {
Assert.isTrue(PromotionActivityTypeEnum.FULL_PRIVILEGE.getValue().equals(activity.getActivityType()),
"传入的必须的满减送活动必须是满减送");
// 获得优惠信息
List<CalcOrderPriceBO.Item> items = itemGroup.getItems().stream().filter(item -> !item.getSelected())
List<CalcOrderPriceBO.Item> items = itemGroup.getItems().stream().filter(CalcOrderPriceBO.Item::getSelected)
.collect(Collectors.toList());
Integer itemCnt = items.stream().mapToInt(CalcOrderPriceBO.Item::getBuyQuantity).sum();
Integer originalTotal = items.stream().mapToInt(CalcOrderPriceBO.Item::getPresentTotal).sum();

View File

@ -3,6 +3,7 @@ package cn.iocoder.mall.promotion.api.bo;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
@ -13,7 +14,7 @@ import java.util.List;
*/
@Data
@Accessors(chain = true)
public class CouponCardDetailBO {
public class CouponCardDetailBO implements Serializable {
// ========== 基本信息 BEGIN ==========
/**

View File

@ -20,10 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
@Service // 实际上不用添加添加的原因是必须 Spring 报错提示
@ -279,8 +276,10 @@ public class CouponServiceImpl implements CouponService {
public CommonResult<List<CouponCardAvailableBO>> getCouponCardList(Integer userId, List<CouponCardSpuDTO> spus) {
// 查询用户未使用的优惠劵列表
List<CouponCardDO> cards = couponCardMapper.selectListByUserIdAndStatus(userId, CouponCardStatusEnum.UNUSED.getValue());
// TODO: 2019-04-19 芋艿 如果没有优惠券处理
if (cards.isEmpty()) {
return CommonResult.success(Collections.emptyList());
}
// 查询优惠劵模板集合
Map<Integer, CouponTemplateDO> templates = couponTemplateMapper.selectListByIds(cards.stream().map(CouponCardDO::getTemplateId).collect(Collectors.toSet()))
.stream().collect(Collectors.toMap(CouponTemplateDO::getId, template -> template));
// 逐个判断是否可用