校验订单
网络异常,图片无法展示
|
private void checkOrder(TradeOrder order) { //1.校验订单是否存在 if(order==null){ CastException.cast(ShopCode.SHOP_ORDER_INVALID); } //2.校验订单中的商品是否存在 TradeGoods goods = goodsService.findOne(order.getGoodsId()); if(goods==null){ CastException.cast(ShopCode.SHOP_GOODS_NO_EXIST); } //3.校验下单用户是否存在 TradeUser user = userService.findOne(order.getUserId()); if(user==null){ CastException.cast(ShopCode.SHOP_USER_NO_EXIST); } //4.校验商品单价是否合法 if(order.getGoodsPrice().compareTo(goods.getGoodsPrice())!=0){ CastException.cast(ShopCode.SHOP_GOODS_PRICE_INVALID); } //5.校验订单商品数量是否合法 if(order.getGoodsNumber()>=goods.getGoodsNumber()){ CastException.cast(ShopCode.SHOP_GOODS_NUM_NOT_ENOUGH); } log.info("校验订单通过"); } 复制代码
生成预订单
网络异常,图片无法展示
|
private Long savePreOrder(TradeOrder order) { //1.设置订单状态为不可见 order.setOrderStatus(ShopCode.SHOP_ORDER_NO_CONFIRM.getCode()); //2.订单ID order.setOrderId(idWorker.nextId()); //核算运费是否正确 BigDecimal shippingFee = calculateShippingFee(order.getOrderAmount()); if (order.getShippingFee().compareTo(shippingFee) != 0) { CastException.cast(ShopCode.SHOP_ORDER_SHIPPINGFEE_INVALID); } //3.计算订单总价格是否正确 BigDecimal orderAmount = order.getGoodsPrice().multiply(new BigDecimal(order.getGoodsNumber())); orderAmount.add(shippingFee); if (orderAmount.compareTo(order.getOrderAmount()) != 0) { CastException.cast(ShopCode.SHOP_ORDERAMOUNT_INVALID); } //4.判断优惠券信息是否合法 Long couponId = order.getCouponId(); if (couponId != null) { TradeCoupon coupon = couponService.findOne(couponId); //优惠券不存在 if (coupon == null) { CastException.cast(ShopCode.SHOP_COUPON_NO_EXIST); } //优惠券已经使用 if ((ShopCode.SHOP_COUPON_ISUSED.getCode().toString()) .equals(coupon.getIsUsed().toString())) { CastException.cast(ShopCode.SHOP_COUPON_INVALIED); } order.setCouponPaid(coupon.getCouponPrice()); } else { order.setCouponPaid(BigDecimal.ZERO); } //5.判断余额是否正确 BigDecimal moneyPaid = order.getMoneyPaid(); if (moneyPaid != null) { //比较余额是否大于0 int r = order.getMoneyPaid().compareTo(BigDecimal.ZERO); //余额小于0 if (r == -1) { CastException.cast(ShopCode.SHOP_MONEY_PAID_LESS_ZERO); } //余额大于0 if (r == 1) { //查询用户信息 TradeUser user = userService.findOne(order.getUserId()); if (user == null) { CastException.cast(ShopCode.SHOP_USER_NO_EXIST); } //比较余额是否大于用户账户余额 if (user.getUserMoney().compareTo(order.getMoneyPaid().longValue()) == -1) { CastException.cast(ShopCode.SHOP_MONEY_PAID_INVALID); } order.setMoneyPaid(order.getMoneyPaid()); } } else { order.setMoneyPaid(BigDecimal.ZERO); } //计算订单支付总价 order.setPayAmount(orderAmount.subtract(order.getCouponPaid()) .subtract(order.getMoneyPaid())); //设置订单添加时间 order.setAddTime(new Date()); //保存预订单 int r = orderMapper.insert(order); if (ShopCode.SHOP_SUCCESS.getCode() != r) { CastException.cast(ShopCode.SHOP_ORDER_SAVE_ERROR); } log.info("订单:["+order.getOrderId()+"]预订单生成成功"); return order.getOrderId(); } 复制代码
扣减库存
- 通过dubbo调用商品服务完成扣减库存
private void reduceGoodsNum(TradeOrder order) { TradeGoodsNumberLog goodsNumberLog = new TradeGoodsNumberLog(); goodsNumberLog.setGoodsId(order.getGoodsId()); goodsNumberLog.setOrderId(order.getOrderId()); goodsNumberLog.setGoodsNumber(order.getGoodsNumber()); Result result = goodsService.reduceGoodsNum(goodsNumberLog); if (result.getSuccess().equals(ShopCode.SHOP_FAIL.getSuccess())) { CastException.cast(ShopCode.SHOP_REDUCE_GOODS_NUM_FAIL); } log.info("订单:["+order.getOrderId()+"]扣减库存["+order.getGoodsNumber()+"个]成功"); } 复制代码
商品服务GoodsService扣减库存
@Override public Result reduceGoodsNum(TradeGoodsNumberLog goodsNumberLog) { if (goodsNumberLog == null || goodsNumberLog.getGoodsNumber() == null || goodsNumberLog.getOrderId() == null || goodsNumberLog.getGoodsNumber() == null || goodsNumberLog.getGoodsNumber().intValue() <= 0) { CastException.cast(ShopCode.SHOP_REQUEST_PARAMETER_VALID); } TradeGoods goods = goodsMapper.selectByPrimaryKey(goodsNumberLog.getGoodsId()); if(goods.getGoodsNumber()<goodsNumberLog.getGoodsNumber()){ //库存不足 CastException.cast(ShopCode.SHOP_GOODS_NUM_NOT_ENOUGH); } //减库存 goods.setGoodsNumber(goods.getGoodsNumber()-goodsNumberLog.getGoodsNumber()); goodsMapper.updateByPrimaryKey(goods); //记录库存操作日志 goodsNumberLog.setGoodsNumber(-(goodsNumberLog.getGoodsNumber())); goodsNumberLog.setLogTime(new Date()); goodsNumberLogMapper.insert(goodsNumberLog); return new Result(ShopCode.SHOP_SUCCESS.getSuccess(),ShopCode.SHOP_SUCCESS.getMessage()); } 复制代码
扣减优惠券
private void changeCoponStatus(TradeOrder order) { //判断用户是否使用优惠券 if (!StringUtils.isEmpty(order.getCouponId())) { //封装优惠券对象 TradeCoupon coupon = couponService.findOne(order.getCouponId()); coupon.setIsUsed(ShopCode.SHOP_COUPON_ISUSED.getCode()); coupon.setUsedTime(new Date()); coupon.setOrderId(order.getOrderId()); Result result = couponService.changeCouponStatus(coupon); //判断执行结果 if (result.getSuccess().equals(ShopCode.SHOP_FAIL.getSuccess())) { //优惠券使用失败 CastException.cast(ShopCode.SHOP_COUPON_USE_FAIL); } log.info("订单:["+order.getOrderId()+"]使用扣减优惠券["+coupon.getCouponPrice()+"元]成功"); } } 复制代码
优惠券服务CouponService更改优惠券状态
@Override public Result changeCouponStatus(TradeCoupon coupon) { try { //判断请求参数是否合法 if (coupon == null || StringUtils.isEmpty(coupon.getCouponId())) { CastException.cast(ShopCode.SHOP_REQUEST_PARAMETER_VALID); } //更新优惠券状态为已使用 couponMapper.updateByPrimaryKey(coupon); return new Result(ShopCode.SHOP_SUCCESS.getSuccess(), ShopCode.SHOP_SUCCESS.getMessage()); } catch (Exception e) { return new Result(ShopCode.SHOP_FAIL.getSuccess(), ShopCode.SHOP_FAIL.getMessage()); } } 复制代码
扣减用户余额
- 通过用户服务完成扣减余额通过用户服务完成扣减余额
private void reduceMoneyPaid(TradeOrder order) { //判断订单中使用的余额是否合法 if (order.getMoneyPaid() != null && order.getMoneyPaid().compareTo(BigDecimal.ZERO) == 1) { TradeUserMoneyLog userMoneyLog = new TradeUserMoneyLog(); userMoneyLog.setOrderId(order.getOrderId()); userMoneyLog.setUserId(order.getUserId()); userMoneyLog.setUseMoney(order.getMoneyPaid()); userMoneyLog.setMoneyLogType(ShopCode.SHOP_USER_MONEY_PAID.getCode()); //扣减余额 Result result = userService.changeUserMoney(userMoneyLog); if (result.getSuccess().equals(ShopCode.SHOP_FAIL.getSuccess())) { CastException.cast(ShopCode.SHOP_USER_MONEY_REDUCE_FAIL); } log.info("订单:["+order.getOrderId()+"扣减余额["+order.getMoneyPaid()+"元]成功]"); } } 复制代码
用户服务UserService,
网络异常,图片无法展示
|
@Override public Result changeUserMoney(TradeUserMoneyLog userMoneyLog) { //判断请求参数是否合法 if (userMoneyLog == null || userMoneyLog.getUserId() == null || userMoneyLog.getUseMoney() == null || userMoneyLog.getOrderId() == null || userMoneyLog.getUseMoney().compareTo(BigDecimal.ZERO) <= 0) { CastException.cast(ShopCode.SHOP_REQUEST_PARAMETER_VALID); } //查询该订单是否存在付款记录 TradeUserMoneyLogExample userMoneyLogExample = new TradeUserMoneyLogExample(); userMoneyLogExample.createCriteria() .andUserIdEqualTo(userMoneyLog.getUserId()) .andOrderIdEqualTo(userMoneyLog.getOrderId()); int count = userMoneyLogMapper.countByExample(userMoneyLogExample); TradeUser tradeUser = new TradeUser(); tradeUser.setUserId(userMoneyLog.getUserId()); tradeUser.setUserMoney(userMoneyLog.getUseMoney().longValue()); //判断余额操作行为 //【付款操作】 if (userMoneyLog.getMoneyLogType().equals(ShopCode.SHOP_USER_MONEY_PAID.getCode())) { //订单已经付款,则抛异常 if (count > 0) { CastException.cast(ShopCode.SHOP_ORDER_PAY_STATUS_IS_PAY); } //用户账户扣减余额 userMapper.reduceUserMoney(tradeUser); } //【退款操作】 if (userMoneyLog.getMoneyLogType().equals(ShopCode.SHOP_USER_MONEY_REFUND.getCode())) { //如果订单未付款,则不能退款,抛异常 if (count == 0) { CastException.cast(ShopCode.SHOP_ORDER_PAY_STATUS_NO_PAY); } //防止多次退款 userMoneyLogExample = new TradeUserMoneyLogExample(); userMoneyLogExample.createCriteria() .andUserIdEqualTo(userMoneyLog.getUserId()) .andOrderIdEqualTo(userMoneyLog.getOrderId()) .andMoneyLogTypeEqualTo(ShopCode.SHOP_USER_MONEY_REFUND.getCode()); count = userMoneyLogMapper.countByExample(userMoneyLogExample); if (count > 0) { CastException.cast(ShopCode.SHOP_USER_MONEY_REFUND_ALREADY); } //用户账户添加余额 userMapper.addUserMoney(tradeUser); } //记录用户使用余额日志 userMoneyLog.setCreateTime(new Date()); userMoneyLogMapper.insert(userMoneyLog); return new Result(ShopCode.SHOP_SUCCESS.getSuccess(),ShopCode.SHOP_SUCCESS.getMessage()); } 复制代码
确认订单
private void updateOrderStatus(TradeOrder order) { order.setOrderStatus(ShopCode.SHOP_ORDER_CONFIRM.getCode()); order.setPayStatus(ShopCode.SHOP_ORDER_PAY_STATUS_NO_PAY.getCode()); order.setConfirmTime(new Date()); int r = orderMapper.updateByPrimaryKey(order); if (r <= 0) { CastException.cast(ShopCode.SHOP_ORDER_CONFIRM_FAIL); } log.info("订单:["+order.getOrderId()+"]状态修改成功"); } 复制代码