前言
完整版演示 http://120.26.95.195/
开发版演示 http://120.26.95.195:8889/
在之前的开发进程中,我们完成商品的出入库及收银台库存优化,今天文章主要完成订单的销单,及销单后会员卡余额及库存的优化。
主要考虑三种思路 商品订单销单 开卡销单 续卡销单
1.商品订单销单
销单确认 并填写必要的原因
第一步 订单减少的商品库存 重新回退
第二部 扣除的会员卡余额返回 这里有一个点 当卡金足够支付时直接回退卡金 当同时用了卡金赠送金 则返回相应余额
关键代码
java
public void cancel(Sell sell) {
List<SellDetail> sellDetailList = sellDetailService.selectSellDetailList(sell.getSellId());
//增加库存
addInventory(sellDetailList);
MemberCard memberCard = memberCardService.getById(sell.getMemberCardId());
if(memberCard!=null){
BigDecimal cardAmount = memberCard.getAmount();
log.info("当前卡余额:"+cardAmount);
BigDecimal cardGiveAmount = memberCard.getGiveAmount();
log.info("当前卡赠送金余额:"+cardAmount);
List<Sellpay> sellPayVoList = sellpayService.selectSellPayList(sell.getSellId());
//计算订单卡金支付的金额
Sellpay memberCardPay = sellPayVoList.stream().filter(p->
p.getPayType().equals(PAY_TYPE_CARD)
).findFirst().orElse(null);
Sellpay memberGivePay= sellPayVoList.stream().filter(p->
p.getPayType().equals(PAY_TYPE_CARD_GIVE)
).findFirst().orElse(null);
if(memberCardPay!=null){
memberCard.setAmount(BigDecimalUtil.add(cardAmount,memberCardPay.getAmount()));
}
if(memberGivePay!=null){
memberCard.setGiveAmount(BigDecimalUtil.add(cardGiveAmount,memberGivePay.getAmount()));
}
Boolean b = memberCardService.updateById(memberCard);
if(!b){
throw new ApiException(CARD_AMOUNT_CHANGE);
}
}
}
2.开卡订单销单
首选判断是否消费过 消费过禁止销单
需要把开卡后的所有消费平账才可进行销单
销单后卡删除
关键代码
java
public void cancel(Sell sell) {
Sell extraSellInfo = sellService.getById(sell.getSellId());
//消费时用的哪张会员卡
MemberCard membercard = memberCardService.getById(extraSellInfo.getMemberCardId());
BigDecimal sellCardAmount = extraSellInfo.getMemberCardAfterAmount();
BigDecimal sellCardGiveAmount = extraSellInfo.getMemberCardAfterGiveAmount();
log.info("订单显示的卡余额:"+sellCardAmount);
BigDecimal cardAmount = membercard.getAmount();
BigDecimal cardGiveAmount = membercard.getGiveAmount();
log.info("当前卡余额:"+cardAmount);
//当前余额和订单充值余额比较
if(sellCardAmount.compareTo(cardAmount) != 0 || cardGiveAmount.compareTo(sellCardGiveAmount) != 0){
throw new ApiException(PAY_SELL_CARD_AMOUNT_CHANGE);
}
//删除卡
memberCardService.removeById(membercard);
}
3.续卡订单销单
续卡仅增加或减少余额即可 需要注意赠送余额也减少 如果当前余额不够 减少则给出错误提示
java
public void cancel(Sell sell) {
Sell extraSellInfo = sellService.getById(sell.getSellId());
//消费时用的哪张会员卡
MemberCard memberCard = memberCardService.getById(extraSellInfo.getMemberCardId());
List<SellDetail> sellDetailList = sellDetailService.selectSellDetailList(sell.getSellId());
SellDetail sellDetail = sellDetailList.get(0);
BigDecimal sellCardAmount = sellDetail.getAmount();
BigDecimal sellCardGiveAmount = sellDetail.getGiveAmount();
BigDecimal cardAmount = memberCard.getAmount();
log.info("当前卡余额:"+cardAmount);
BigDecimal cardGiveAmount = memberCard.getGiveAmount();
log.info("当前卡余额:"+cardAmount);
//消费后余额
BigDecimal nowCardAmount = BigDecimalUtil.sub(cardAmount,sellCardAmount);
BigDecimal nowGiveAmount = BigDecimalUtil.sub(cardGiveAmount,sellCardGiveAmount);
if (nowCardAmount.signum() == -1|nowGiveAmount.signum() == -1) {
throw new ApiException(PAY_SELL_CARD_AMOUNT_NOT_SUFFICIENT_FUNDS);
}
//修改当前卡金赠送金
memberCard.setAmount(nowCardAmount);
memberCard.setGiveAmount(nowGiveAmount);
Boolean b = memberCardService.updateById(memberCard);
if(!b){
throw new ApiException(CARD_AMOUNT_CHANGE);
}
}
后续功能是 修改订单 挂单 取单。