1. 程式人生 > >確認訂單按鈕和取消訂單按鈕中的事物學習

確認訂單按鈕和取消訂單按鈕中的事物學習

首先這是確認收貨的model的程式碼

		 DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionStatus status = transactionManager.getTransaction(def);
        try {

            //引數校驗
            if (ordersId == null || ordersId == 0) {
                log.error("訂單ID為空。");
                throw new BusinessException("訂單ID為空,請重試!");
            }

            //獲取訂單
            Orders orders = ordersWriteDao.get(ordersId);
            if (orders == null) {
                log.error("訂單資訊獲取失敗。");
                throw new BusinessException("訂單資訊獲取失敗,請重試!");
            } else if (!orders.getOrderState().equals(Orders.ORDER_STATE_4)) {
                log.error("訂單不處於已發貨狀態,不能確認收貨。");
                throw new BusinessException("訂單不處於已發貨狀態,不能確認收貨!");
            }
			//狀態為5表示已完成的訂單,這裡把訂單狀態改變
            orders.setOrderState(Orders.ORDER_STATE_5);
            //設定完成時間為當前時間
            orders.setFinishTime(new Date());
            //更新訂單資訊
            int count = ordersWriteDao.update(orders);
            //如果成功的話count起碼是1
            if (count == 0) {
                log.error("訂單更新失敗。");
                throw new BusinessException("訂單更新失敗!");
            }

            OrderLog orderLog = new OrderLog(member.getId(), member.getName(), orders.getId(),
                orders.getOrderSn(), "您已簽收訂單", new Date());
                //儲存進訂單操作日誌

            int logCount = orderLogWriteDao.save(orderLog);
            if (logCount == 0) {
                throw new BusinessException("訂單日誌儲存失敗,請重試!");
            }
			//事務管理的提交,如果有異常就回滾
            transactionManager.commit(status);
            return true;
        } catch (BusinessException be) {
            transactionManager.rollback(status);
            throw be;
        } catch (Exception e) {
            transactionManager.rollback(status);
            log.error("[OrderService][goodsReceipt]訂單確認收貨時發生異常:", e);
            throw e;
        }

這是取消訂單的model層

DefaultTransactionDefinition def = new DefaultTransactionDefinition();
        def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        TransactionStatus status = transactionManager.getTransaction(def);
        try {
            //引數校驗
            if (ordersId == null || ordersId == 0) {
                throw new BusinessException("訂單ID為空,請重試!");
            }
            //獲取訂單
            Orders ordersDb = ordersWriteDao.get(ordersId);

            if (!ordersDb.getMemberId().equals(optId)) {
                throw new BusinessException("您不能操作別人的訂單,謝謝!");
            }

            // 取消訂單
            this.doCancelOrder(ordersDb, optId, optName, "您取消了訂單");

            transactionManager.commit(status);
            return true;
        } catch (Exception e) {
            transactionManager.rollback(status);
            throw e;
        }
private void doCancelOrder(Orders ordersDb, Integer optId, String optName, String logContent) {
        if (ordersDb == null) {
            throw new BusinessException("獲取訂單資訊失敗,請重試!");
        } else if (!(ordersDb.getOrderState().equals(Orders.ORDER_STATE_1)
                     || (ordersDb.getOrderState().equals(Orders.ORDER_STATE_2))
                     || (ordersDb.getOrderState().equals(Orders.ORDER_STATE_3)))) {
            throw new BusinessException("訂單已發貨不能取消!");
        }
        if (ordersDb.getOrderType().intValue() == Orders.ORDER_TYPE_5) {
            // 競價尾款訂單不能取消
            throw new BusinessException("集合競價尾款訂單不能取消!");
        }

        //設定訂單狀態
        Orders orders = new Orders();
        orders.setId(ordersDb.getId());
        orders.setFinishTime(new Date());
        orders.setOrderState(Orders.ORDER_STATE_6);
        int count = ordersWriteDao.update(orders);
        if (count == 0) {
            throw new BusinessException("訂單更新失敗,請重試!");
        }
        //記錄訂單日誌
        OrderLog orderLog = new OrderLog();
        orderLog.setContent(logContent);
        orderLog.setOperatingId(optId);
        orderLog.setOrdersId(ordersDb.getId());
        orderLog.setOrdersSn(ordersDb.getOrderSn());
        orderLog.setOperatingName(optName);

        int logCount = orderLogWriteDao.save(orderLog);
        if (logCount == 0) {
            throw new BusinessException("訂單日誌儲存失敗,請重試!");
        }

        // 如果是父訂單,則修改子訂單的狀態
        if (ordersDb.getIsParent().intValue() == Orders.IS_PARENT_1) {
            ordersWriteDao.cancelByPsn(ordersDb.getOrderSn());
        }

        // 返還積分
        this.cancelOrderBackIntegral(ordersDb);

        // 返回優惠券
        this.cancelOrderBackCoupon(ordersDb, optId, optName);

        // 退回付款金額
        this.cancelOrderBackMoney(ordersDb, optId, optName);

        // 還原庫存和實際銷量
        this.cancelOrderBackStockAndActualSales(ordersDb);
    }
 /**
     * 取消訂單時退回使用者優惠券
     * @param order
     * @param optId
     * @param optName
     */
    private void cancelOrderBackCoupon(Orders orderDb, Integer optId, String optName) {

        List<Orders> list = null;
        // 如果是子訂單則只退這個訂單的優惠券,如果是父訂單,則退回該父訂單下所有子訂單的優惠券
        if (orderDb.getIsParent().intValue() == Orders.IS_PARENT_1) {
            list = ordersReadDao.getByOrderPsn(orderDb.getOrderSn());
        } else {
            list = new ArrayList<>();
            list.add(orderDb);
        }
        for (Orders order : list) {
            if (order.getCouponUserId() != null && order.getCouponUserId() > 0) {
                CouponUser couponUser = couponUserReadDao.get(order.getCouponUserId());
                if (couponUser == null) {
                    log.error("使用者優惠券獲取失敗。");
                    throw new BusinessException("返還使用者優惠券時失敗,請重試!");
                }
                Integer backCouponUser = couponUserWriteDao.backCouponUser(order.getMemberId(),
                    couponUser.getId());
                if (backCouponUser < 1) {
                    log.error("修改使用者優惠券使用次數失敗。");
                    throw new BusinessException("返還使用者優惠券時失敗,請重試!");
                }
                // 設定優惠券使用日誌
                CouponOptLog couponOptLog = new CouponOptLog();
                couponOptLog.setCouponUserId(couponUser.getId());
                couponOptLog.setMemberId(couponUser.getMemberId());
                couponOptLog.setSellerId(couponUser.getSellerId());
                couponOptLog.setCouponId(couponUser.getCouponId());
                couponOptLog.setOptType(CouponOptLog.OPT_TYPE_3);
                couponOptLog.setOrderId(order.getId());
                couponOptLog.setCreateUserId(optId);
                couponOptLog.setCreateUserName(optName);
                couponOptLog.setCreateTime(new Date());
                couponOptLogWriteDao.insert(couponOptLog);
            }
        }
    }

    /**
     * 訂單取消時,1、返還該訂單消耗的積分;2、追回付款時送給使用者的積分
     * @param order
     */
    private void cancelOrderBackIntegral(Orders order) {
        // 最終需要修改的積分數量
        int backValue = 0;

        // 1、返還該訂單消耗的積分
        // 消耗了積分才返還
        if (order.getIntegral() > 0) {
            // 返還積分是增加
            backValue = order.getIntegral();

            MemberGradeIntegralLogs memberGradeIntegralLogs = new MemberGradeIntegralLogs();
            memberGradeIntegralLogs.setMemberId(order.getMemberId());
            memberGradeIntegralLogs.setMemberName(order.getMemberName());
            memberGradeIntegralLogs.setValue(order.getIntegral());
            memberGradeIntegralLogs.setOptType(MemberGradeIntegralLogs.MEMBER_GRD_INT_LOG_OPT_T_11);
            memberGradeIntegralLogs.setOptDes("取消訂單返還積分(訂單號:" + order.getOrderSn() + ")");
            memberGradeIntegralLogs.setRefCode(order.getOrderSn());
            memberGradeIntegralLogs.setType(MemberGradeIntegralLogs.MEMBER_GRD_INT_LOG_T_2);
            memberGradeIntegralLogs.setCreateTime(new Date());
            Integer save = memberGradeIntegralLogsWriteDao.save(memberGradeIntegralLogs);
            if (save == 0) {
                throw new BusinessException("記錄使用者積分消費日誌失敗(取消訂單返還積分),請重試!");
            }
        }

        // 2、追回付款時送給使用者的積分,只有當訂單是子訂單時才需要追回
        if (order.getPaymentStatus().intValue() == Orders.PAYMENT_STATUS_1
            && order.getIsParent().intValue() == Orders.IS_PARENT_0) {
            // 訂單已付款才會發生此型別積分追回

            // 計算購物時平臺送出的積分追回為減少使用者的積分
            MemberGradeIntegralLogs sendIntLog = memberGradeIntegralLogsReadDao
                .getIntLogByMIdAndOrderSnAndOptType(order.getMemberId(),
                    MemberGradeIntegralLogs.MEMBER_GRD_INT_LOG_OPT_T_3, order.getOrderSn(),
                    MemberGradeIntegralLogs.MEMBER_GRD_INT_LOG_T_2);
            if (sendIntLog != null && sendIntLog.getValue() > 0) {
                //  追回積分是減少
                backValue = backValue - sendIntLog.getValue();

                MemberGradeIntegralLogs memberGradeIntegralLogs = new MemberGradeIntegralLogs();
                memberGradeIntegralLogs.setMemberId(order.getMemberId());
                memberGradeIntegralLogs.setMemberName(order.getMemberName());
                memberGradeIntegralLogs.setValue(sendIntLog.getValue());
                memberGradeIntegralLogs
                    .setOptType(MemberGradeIntegralLogs.MEMBER_GRD_INT_LOG_OPT_T_13);
                memberGradeIntegralLogs.setOptDes("取消訂單追回積分(訂單號:" + order.getOrderSn() + ")");
                memberGradeIntegralLogs.setRefCode(order.getOrderSn());
                memberGradeIntegralLogs.setType(MemberGradeIntegralLogs.MEMBER_GRD_INT_LOG_T_2);
                memberGradeIntegralLogs.setCreateTime(new Date());
                Integer saveLog = memberGradeIntegralLogsWriteDao.save(memberGradeIntegralLogs);
                if (saveLog == 0) {
                    throw new BusinessException("記錄使用者積分消費日誌失敗(取消訂單追回積分),請重試!");
                }
            }
        }

        // 3、修改使用者積分數量,記錄積分消耗日誌
        Member memberNew = new Member();
        memberNew.setId(order.getMemberId());
        memberNew.setIntegral(backValue);
        Integer updateIntegral = memberWriteDao.updateIntegral(memberNew);
        if (updateIntegral == 0) {
            throw new BusinessException("取消訂單修改使用者積分時失敗,請重試!");
        }
    }

    /**
     * 取消訂單還原庫存和實際銷量公共方法
     * @param orderType
     * @param ordersId
     */
    private void cancelOrderBackStockAndActualSales(Orders orderDb) {
        List<OrdersProduct> opList = null;
        if (orderDb.getIsParent().intValue() == Orders.IS_PARENT_1) {
            opList = ordersProductReadDao.getByOrdersPsn(orderDb.getOrderSn());
        } else {
            opList = ordersProductReadDao.getByOrderId(orderDb.getId());
        }
        Integer orderType = orderDb.getOrderType();
        //還原庫存和實際銷量
        if (orderType.intValue() == Orders.ORDER_TYPE_2) {
            // 限時搶購訂單時還原活動商品的庫存
            // 限時搶購只有一個網單
            this.updateFlashProductStockAndActualSales(opList.get(0).getActFlashSaleProductId(),
                opList.get(0).getNumber(), true);
        } else if (orderType.intValue() == Orders.ORDER_TYPE_3) {
            // 團購訂單時還原活動商品的庫存
            // 團購只有一個網單
            this.updateGroupStockAndActualSales(opList.get(0).getActGroupId(),
                opList.get(0).getNumber(), true);
        } else if (orderType.intValue() == Orders.ORDER_TYPE_4) {
            // 集合競價訂單時還原活動商品的庫存
            // 競價定金訂單時不會進入此邏輯,因為定金訂單支付後訂單狀態直接設定為已完成
            this.updateBiddingStockAndActualSales(opList.get(0).getActBiddingId(),
                opList.get(0).getNumber(), true);
        } else if (orderType.intValue() == Orders.ORDER_TYPE_5) {
            // 競價尾款訂單時不會進入此邏輯,因為尾款訂單不能取消
            // 此處為邏輯清晰、程式碼易讀增加此邏輯
        } else if (orderType.intValue() == Orders.ORDER_TYPE_6) {
            // 積分換購訂單時還原活動商品的庫存
            // 積分換購只有一個網單
            this.updateIntegralStockAndActualSales(opList.get(0).getActIntegralId(),
                opList.get(0).getNumber(), true);
        } else {
            // 普通訂單
            // 更新貨品和商品的庫存
            if (opList != null && opList.size() > 0) {
                for (OrdersProduct op : opList) {
                    this.updateProductActualSalesAndStock(op.getProductId(), op.getProductGoodsId(),
                        op.getNumber(), true);
                }
            }
        }
    }