C# TransactionScope 分布式事物使用实例

protected void btnSave_Click(object sender, EventArgs e)
    {
        var order = new BLL.A_OrderInfo().GetModel(this.OrderId);//订单
        if (order != null && IsUpOrder(order.OrderState, order.PayWay, order.PayState))
        {
            var itemIds = Request.Form.GetValues("hdId");
            var proIdList = Request.Form.GetValues("hdProId");//新商品ID
            var proPriceList = Request.Form.GetValues("txtPreProPrice");//优惠价格
            var proNumList = Request.Form.GetValues("txtProNum");//商品数量

            using (var scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted }))
            {
                try
                {
                    //订单原来商品
                    var oldOrderPro = new BLL.A_OrderPro().GetList(null, "OrderId=" + order.OrderId, null);
                    //备份原来订单商品
                    new BLL.A_OrderProBak().Add(oldOrderPro);
                    //还原订单原来商品库存
                    Common.OperGoodsStockAndInsert(order.OrderId, order.OrderCode, 0, (int)Enums.StockRemarkType.修改订单商品增加库存, (int)Enums.StockType.虚拟库存, "虚拟库存-修改订单商品增加库存-正品-入库");
                    var newOrderPro = this.AddNewOrderPro(order.OrderId, order.OrderCode, itemIds, proIdList, proPriceList, proNumList);
                    if (newOrderPro != null)
                    {
                        //删除订单原来商品
                        new BLL.A_OrderPro().DeleteByOrderId(order.OrderId);

                        //修改订单金额
                        int oldStatus = order.OrderState;//保存原始订单状态
                        order.GoodsAmount = Util.GetDecimal(txtGoodsAmount.Text);//商品金额
                        order.IntegralMoney = Util.GetDecimal(txtIntegralMoney.Text);//积分抵扣金额
                        order.CouponAmount = Util.GetInt(txtCouponAmount.Text);//优惠卷抵扣金额
                        order.OrderFreight = Util.GetDecimal(txtOrderFreight.Text);//运费金额
                        order.OrderAmount = Util.GetDecimal(txtOrderAmount.Text);//订单金额
                        order.PayAmount = Util.GetDecimal(txtPayAmount.Text);//已支付金额
                        order.NeedPayAmount = Util.GetDecimal(txtNeedPayAmount.Text);//应支付金额
                        if (order.NeedPayAmount == 0 && order.PayWay != (int)Enums.OrderPayWay.NotPay && order.PayState == (int)Enums.PayState.未支付)
                        {
                            order.PayWay = (int)Enums.OrderPayWay.NotPay;
                            order.PayState = (int)Enums.PayState.已支付;
                            order.OrderState = (int)Enums.OrderState.等待处理中;

                        }
                        if (order.NeedPayAmount > 0 && order.PayWay == (int)Enums.OrderPayWay.NotPay)
                        {
                            order.PayWay = (int)Enums.OrderPayWay.Bill99;
                            order.PayState = (int)Enums.PayState.未支付;
                            order.OrderState = (int)Enums.OrderState.等待支付中;
                        }
                        new BLL.A_OrderInfo().Update(order);

                        //写订单状态日志
                        OperOrderLog(oldStatus, order);

                        //添加订单新商品
                        new BLL.A_OrderPro().Add(newOrderPro);
                        //减少订单新商品库存
                        Common.OperGoodsStockAndInsert(order.OrderId, order.OrderCode, 1, (int)Enums.StockRemarkType.修改订单商品减少库存, (int)Enums.StockType.虚拟库存, "虚拟库存-修改订单商品扣库存-正品-出库");
                        scope.Complete();

                        JScript.Alert("修改订单成功");
                        JScript.Redirect("/SysManager/OrderManager/OrderEdit.aspx");
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception("发送信息异常,原因:" + ex.Message);
                }
                finally
                {
                    scope.Dispose();
                }
            }
        }
        else
        {
            JScript.Alert("此订单不能修改");
            JScript.Redirect("/SysManager/OrderManager/OrderEdit.aspx");
        }
    }

    //是否可以修改
    private bool IsUpOrder(int orderState, int payWay, int payState)
    {
        if (orderState <= (int)Enums.OrderState.已发送到仓储)//&& (payWay == (int)Enums.OrderPayWay.NotPay || payState == (int)Enums.PayState.未支付)
            return true;
        else
            return false;
    }

上一篇:Android系统库豁免列表


下一篇:Why is Go PANICking?