public void ProcessRequest(HttpContext context)
        {
            using (var conn = new SqlConnection(DataAccess.ConnectString))
            {
                conn.Open();
                var trans = conn.BeginTransaction();

                try
                {
                    var strOrderId = context.Request.QueryString["OrderID"];
                    int id;

                    IRepository repo = new Repository();

                    if (!string.IsNullOrEmpty(strOrderId) && int.TryParse(strOrderId, out id))
                    {
                        // Get Current MatchTicket Order
                        var oTicket = (OrdrTicket)Order.Select(id);

                        // Get Order MatchTicket Info
                        var oiMatchTicket = oTicket?.OIMatchTicket;

                        if (oiMatchTicket != null && oiMatchTicket.IsActive)
                        {
                            var mt = MatchTicket.Cache.Load(oiMatchTicket.MatchGuid);
                            var mp = MemberPeriod.GetCurrentMemberPeriodByMemberID(oTicket.MemberID);

                            // isMemberCouldPurchase, should be false
                            if (!mt.CheckMemberCanPurchase(mp))
                            {
                                var currProductType = mt.AllowMemberClass == 2 ?
                                    ProductType.MembershipPremier : ProductType.MembershipCore;

                                var p = Product.Cache.Load(currProductType).Find(x => x.IsActive);

                                var isUpgrade = (mp != null && mp.MemberClass.Equals(MemberClassType.Core) &&
                                                mt.AllowMemberClass.HasValue && mt.AllowMemberClass == 2);

                                #region isRenew
                                var isRenew = false;

                                var mpLast = MemberPeriod.GetCurrentMemberPeriodByMemberID(oTicket.MemberID, -1);

                                if (mpLast != null && mpLast.MemberClass.Equals(MemberClassType.Core))
                                {
                                    isRenew = currProductType.Equals(ProductType.MembershipCore);
                                }
                                if (mpLast != null && mpLast.MemberClass.Equals(MemberClassType.Premier))
                                {
                                    isRenew = currProductType.Equals(ProductType.MembershipPremier);
                                }
                                #endregion

                                #region new membership order

                                var o = new Order
                                {
                                    Mobile = oTicket.Mobile,
                                    UpdateTime = DateTime.Now,
                                    Description = string.Empty,
                                    OrderType = OrderBaseType.Membership,
                                    MemberID = oTicket.MemberID,
                                    MemberName = oTicket.MemberName,
                                    Address = oTicket.Address,
                                    Payment = string.Empty,
                                    Price = 0,
                                    Sale = null,
                                    Deposit = null,
                                    Status = OrderStatusType.Submitted,
                                    Rate = 0,
                                    CreateTime = DateTime.Now,
                                    IsActive = true,
                                    Remark = string.Empty
                                };

                                //Get the Order ID after Insert new one
                                object key;
                                repo.Insert(o, out key, trans);
                                var newId = Convert.ToInt32(key);
                                #endregion

                                #region new membership orderItem
                                //New Order Items
                                var oi = new OrdrItmMembership();

                                // Set AlterMethod & MemberCardNo
                                if (isUpgrade)
                                {
                                    oi.AlterMethod = "Upgrade";
                                    oi.MemberCardNo = mp.MemberCardNo;
                                }
                                else if (isRenew)
                                {
                                    oi.AlterMethod = "Renew";
                                    oi.MemberCardNo = mpLast.MemberCardNo;
                                }
                                else
                                {
                                    oi.AlterMethod = string.Empty;

                                    var rand = new Random(Guid.NewGuid().GetHashCode());
                                    oi.MemberCardNo = rand.Next(100, 999).ToString();
                                }

                                oi.EndDate = CurrSeasonDeadline;

                                oi.OrderID = newId;
                                oi.Quantity = 1;

                                #region set membership sale
                                var pPremier = Product.Cache.Load("iMS2");
                                var pCore = Product.Cache.Load("iMS1");

                                if (isUpgrade)
                                {
                                    oi.Sale = pPremier.PriceCNY - pCore.PriceCNY;
                                }
                                else if (isRenew)
                                {
                                    oi.Sale = currProductType.Equals(ProductType.MembershipPremier)
                                        ? Convert.ToDouble(Math.Floor(pPremier.PriceCNY * 0.88)) : pCore.PriceCNY;
                                }
                                else
                                {
                                    oi.Sale = null;
                                }
                                #endregion

                                oi.Place(Member.Cache.Load(o.MemberID), p, trans);

                                #endregion

                                trans.Commit();

                                context.Response.Redirect($"~/iArsenalOrderView_Membership.aspx?OrderID={newId}", false);
                                context.ApplicationInstance.CompleteRequest();
                            }
                        }
                    }
                }
                catch
                {
                    context.Response.Redirect("iArsenalOrder.aspx", false);
                    context.ApplicationInstance.CompleteRequest();
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            using (var conn = new SqlConnection(DataAccess.ConnectString))
            {
                conn.Open();
                var trans = conn.BeginTransaction();

                try
                {
                    var m = _repo.Single<Member>(Mid);

                    // Update Member Information

                    #region Get Member Nation & Region

                    var nation = ddlNation.SelectedValue;

                    if (!string.IsNullOrEmpty(nation))
                    {
                        if (nation.Equals("中国"))
                        {
                            m.Nation = nation;
                            if (!string.IsNullOrEmpty(tbRegion1.Text.Trim()))
                            {
                                m.Region = !string.IsNullOrEmpty(tbRegion2.Text.Trim()) ? $"{tbRegion1.Text.Trim()}|{tbRegion2.Text.Trim()}" : tbRegion1.Text.Trim();
                            }
                            else
                            {
                                m.Region = string.Empty;
                            }
                        }
                        else if (!string.IsNullOrEmpty(tbNation.Text.Trim()))
                        {
                            m.Nation = tbNation.Text.Trim();
                            m.Region = string.Empty;
                        }
                        else
                        {
                            m.Nation = nation;
                            m.Region = string.Empty;
                        }
                    }
                    else
                    {
                        m.Nation = string.Empty;
                        m.Region = string.Empty;
                    }

                    #endregion

                    if (!string.IsNullOrEmpty(tbIDCardNo.Text.Trim()))
                        m.IDCardNo = tbIDCardNo.Text.Trim();
                    else
                        throw new Exception("请填写会员身份证号码");

                    if (!string.IsNullOrEmpty(tbPassportNo.Text.Trim()))
                        m.PassportNo = tbPassportNo.Text.Trim();
                    else
                        throw new Exception("请填写会员护照编号");

                    if (!string.IsNullOrEmpty(tbPassportName.Text.Trim()))
                        m.PassportName = tbPassportName.Text.Trim();
                    else
                        throw new Exception("请填写会员护照姓名");

                    if (!string.IsNullOrEmpty(tbMobile.Text.Trim()))
                        m.Mobile = tbMobile.Text.Trim();
                    else
                        throw new Exception("请填写会员手机");

                    if (!string.IsNullOrEmpty(tbWeChat.Text.Trim()))
                        m.WeChat = tbWeChat.Text.Trim();
                    else
                        throw new Exception("请填写会员微信/QQ");

                    if (!string.IsNullOrEmpty(tbEmail.Text.Trim()))
                        m.Email = tbEmail.Text.Trim();
                    else
                        throw new Exception("请填写会员邮箱");

                    //m.MemberType = MemberType.Match;

                    _repo.Update(m);

                    // New Order
                    var o = new Order();
                    int newId;

                    if (OrderID > 0)
                    {
                        o = _repo.Single<Order>(OrderID);
                    }

                    o.Mobile = m.Mobile;
                    o.UpdateTime = DateTime.Now;
                    o.Description = tbOrderDescription.Text.Trim();
                    o.OrderType = OrderBaseType.Membership;

                    if (OrderID > 0)
                    {
                        _repo.Update(o, trans);

                        // used by setting OrderItem foreign key
                        newId = OrderID;
                    }
                    else
                    {
                        o.MemberID = m.ID;
                        o.MemberName = m.Name;

                        o.Address = m.Address;
                        o.Payment = string.Empty;

                        o.Price = 0;
                        o.Sale = null;
                        o.Deposit = null;
                        o.Status = OrderStatusType.Draft;
                        o.Rate = 0;
                        o.CreateTime = DateTime.Now;
                        o.IsActive = true;
                        o.Remark = string.Empty;

                        //Get the Order ID after Insert new one
                        object key;
                        _repo.Insert(o, out key, trans);
                        newId = Convert.ToInt32(key);
                    }

                    //New Order Items
                    var oi = new OrdrItmMembership();

                    if (!string.IsNullOrEmpty(tbMemberClass.Text.Trim()))
                    {
                        //Remove Order Item of this Order
                        if (OrderID > 0 && o.ID.Equals(OrderID))
                        {
                            _repo.Query<OrderItem>(x => x.OrderID == OrderID).Delete(trans);
                        }

                        var currProductType = (ProductType)Enum.Parse(typeof(ProductType), tbMemberClass.Text.Trim());
                        var pMembership = Product.Cache.Load(currProductType).Find(p => p.IsActive);

                        if (pMembership == null)
                            throw new Exception("无相关会籍可申请,请联系管理员");

                        // Set AlterMethod
                        if (IsUpgrade)
                        {
                            oi.AlterMethod = "Upgrade";
                        }
                        else if (IsRenew)
                        {
                            oi.AlterMethod = "Renew";
                        }
                        else
                        {
                            oi.AlterMethod = string.Empty;
                        }

                        // Set MemberCardNo
                        if (!string.IsNullOrEmpty(CurrentCardNo))
                        {
                            oi.MemberCardNo = CurrentCardNo;
                        }
                        else
                        {
                            var rand = new Random(Guid.NewGuid().GetHashCode());
                            oi.MemberCardNo = rand.Next(100, 999).ToString();
                        }

                        oi.EndDate = CurrSeasonDeadline;

                        oi.OrderID = newId;
                        oi.Quantity = 1;

                        if (IsUpgrade || IsRenew)
                        {
                            oi.Sale = Convert.ToSingle(tbSale.Text.Trim());
                        }
                        else
                        {
                            oi.Sale = null;
                        }

                        oi.Place(m, pMembership, trans);
                    }
                    else
                    {
                        throw new Exception("此订单未登记会籍信息");
                    }

                    trans.Commit();

                    ClientScript.RegisterClientScriptBlock(typeof(string), "succeed",
                        string.Format("alert('订单({0})保存成功');window.location.href = 'ServerOrderView.ashx?OrderID={0}'",
                            newId), true);
                }
                catch (Exception ex)
                {
                    trans.Rollback();

                    ClientScript.RegisterClientScriptBlock(typeof(string), "failed", $"alert('{ex.Message}')", true);
                }

                //conn.Close();
            }
        }