Пример #1
0
    //提交订单
    protected void AddOrder_Btn_Click(object sender, EventArgs e)
    {
        if (Address_Div.Visible && DataConvert.CLng(Request.Form["address_rad"]) < 1)
        {
            function.WriteErrMsg("尚未选定收货地址");
        }
        //1,生成订单,2,关联购物车中商品为已绑定订单
        M_UserInfo  mu      = buser.GetLogin(false);
        M_UserRecei receMod = receBll.GetSelect(Convert.ToInt32(Request.Form["address_rad"]), mu.UserID);
        DataTable   cartDT  = cartBll.SelByCookID(CartCookID, ProClass, ids);//需要购买的商品

        if (cartDT.Rows.Count < 1)
        {
            function.WriteErrMsg("你尚未选择商品,<a href='/User/Order/OrderList.aspx'>查看我的订单</a>");
        }
        //------生成订单前检测区
        foreach (DataRow dr in cartDT.Rows)
        {
            if (!HasStock(dr["Allowed"], DataConvert.CLng(dr["stock"]), Convert.ToInt32(dr["Pronum"])))
            {
                function.WriteErrMsg("购买失败," + dr["proname"] + "的库存数量不足");
            }
        }
        //------检测End
        //按店铺生成订单,统一存ZL_Orderinfo
        DataTable storeDT = cartDT.DefaultView.ToTable(true, "StoreID");
        M_Payment payMod  = new M_Payment();

        payMod.PayNo = payBll.CreatePayNo();
        List <M_OrderList> orderList = new List <M_OrderList>();//用于生成临时订单,统计计算(Disuse)

        foreach (DataRow dr in storeDT.Rows)
        {
            #region 暂不使用字段
            //Odata.province = this.DropDownList1.SelectedValue;
            //Odata.city = this.DropDownList2.SelectedValue;//将地址省份与市ID存入,XML数据源
            //Odata.Guojia = "";//国家
            //Odata.Chengshi = DropDownList2.SelectedItem.Text;//城市
            //Odata.Diqu = DropDownList3.SelectedItem.Text;//地区
            //Odata.Delivery = DataConverter.CLng(Request.Form["Delivery"]);
            //Odata.Deliverytime = DataConverter.CLng(this.Deliverytime.Text);
            //Odata.Mobile = receMod.MobileNum;
            #endregion
            M_OrderList Odata = new M_OrderList();
            Odata.Ordertype = OrderHelper.GetOrderType(ProClass);
            Odata.OrderNo   = B_OrderList.CreateOrderNo((M_OrderList.OrderEnum)Odata.Ordertype);
            Odata.StoreID   = Convert.ToInt32(dr["StoreID"]);
            cartDT.DefaultView.RowFilter = "StoreID=" + Odata.StoreID;
            DataTable storeCartDT = cartDT.DefaultView.ToTable();
            switch (ProClass)//旅游机票等,以联系人信息为地址
            {
            case 7:
            case 8:
                M_Cart_Travel   model = JsonConvert.DeserializeObject <M_Cart_Travel>(storeCartDT.Rows[0]["Additional"].ToString());
                M_Cart_Contract user  = model.Contract[0];
                Odata.Receiver  = user.Name;
                Odata.Reuser    = user.Name;
                Odata.Phone     = user.Mobile;
                Odata.MobileNum = user.Mobile;
                Odata.Email     = user.Email;
                break;

            default:    //普通订单
                Odata.Receiver  = receMod.ReceivName;
                Odata.Reuser    = receMod.ReceivName;
                Odata.Phone     = receMod.phone;
                Odata.MobileNum = receMod.MobileNum;
                Odata.Email     = receMod.Email;
                Odata.Shengfen  = receMod.Provinces;
                Odata.Jiedao    = receMod.Street;
                Odata.ZipCode   = receMod.Zipcode;
                break;
            }
            Odata.Invoiceneeds    = DataConverter.CLng(Request.Form["invoice_rad"]);//是否需开发票
            Odata.Invoice         = Odata.Invoiceneeds == 0 ? "" : InvoTitle_T.Text + "||" + Invoice_T.Text;
            Odata.Rename          = mu.UserName;
            Odata.Outstock        = 0;              //缺货处理
            Odata.Ordermessage    = ORemind_T.Text; //订货留言
            Odata.Merchandiser    = "";             //跟单员
            Odata.Internalrecords = "";             //内部记录
            Odata.IsCount         = false;
            //-----金额计算
            Odata.Balance_price     = GetTotalMoney(storeCartDT);
            Odata.Freight           = GetFarePrice(storeCartDT, Odata.StoreID); //运费计算
            Odata.Ordersamount      = Odata.Balance_price + Odata.Freight;      //订单金额
            Odata.AllMoney_Json     = orderCom.GetTotalJson(storeCartDT);       //附加需要的虚拟币
            Odata.Specifiedprice    = Odata.Ordersamount;                       //订单金额;
            Odata.Receivablesamount = 0;                                        //收款金额
            Odata.Developedvotes    = 0;
            Odata.OrderStatus       = (int)M_OrderList.StatusEnum.Normal;       //订单状态
            Odata.Paymentstatus     = (int)M_OrderList.PayEnum.NoPay;           //付款状态
            Odata.StateLogistics    = 0;                                        //物流状态
            Odata.Signed            = 0;                                        //签收
            Odata.Settle            = 0;                                        //结清
            Odata.Aside             = 0;                                        //作废
            Odata.Suspended         = 0;                                        //暂停
            Odata.AddTime           = DateTime.Now;
            Odata.AddUser           = mu.UserName;;
            Odata.Userid            = mu.UserID;
            Odata.Integral          = DataConverter.CLng(Request.QueryString["jifen"]);
            Odata.Freight_remark    = " ";
            Odata.Balance_remark    = "";
            Odata.Promoter          = 0;
            Odata.id = orderBll.Adds(Odata);
            cartProBll.CopyToCartPro(mu, storeCartDT, Odata.id);
            orderList.Add(Odata);
            orderCom.SendMessage(Odata, null, "ordered");
            userappMod = userappBll.SelModelByUid(buser.GetLogin().UserID, "wechat");
            if (userappMod != null)
            {
                //function.WriteErrMsg(Odata.AddTime.ToString("yyyy年MM月dd日 HH:mm:ss"));
                WxAPI  wxapi = WxAPI.Code_Get(1);
                string json  = "{ \"touser\":\"" + userappMod.OpenID + "\",\"template_id\":\"6kw4rUCX3IpE7k8_kn3ocmaaQBZF4blWq25ualnz1qw\",\"url\":\"http://www.wodian8.com/User/Order/OrderList1.aspx\",\"data\":{\"first\": {\"value\":\"订单提交成功!\",\"color\":\"#173177\"},\"keyword1\":{\"value\":\"" + Odata.AddTime.ToString("yyyy年MM月dd日 HH:mm:ss") + "\",\"color\":\"#173177\"},\"keyword2\": {\"value\":\"" + Odata.Ordersamount.ToString("0.00") + "元\",\"color\":\"#173177\"},\"keyword3\": {\"value\":\"" + GetProname(Odata.id) + "\",\"color\":\"#173177\"},\"keyword4\": {\"value\":\"" + Odata.Shengfen + Odata.Jiedao + "\",\"color\":\"#173177\"},\"remark\":{\"value\":\"您好,订单提交成功,请尽快付款!\",\"color\":\"#173177\"}}}";
                wxapi.SendTlpMsg(json);
                //wxapi.SendMsg(userappMod.OpenID, "你已下订单,订单号为:" + Odata.OrderNo);
            }
        }
        cartBll.DelByids(ids);
        //-----------------订单生成后处理
        //进行减库存等操作
        foreach (DataRow dr in cartDT.Rows)
        {
            M_Product model = proBll.GetproductByid(Convert.ToInt32(dr["Proid"]));
            model.Stock = model.Stock - DataConvert.CLng(dr["Pronum"]);
            SqlHelper.ExecuteSql("Update ZL_Commodities Set Stock=" + model.Stock + " Where ID=" + model.ID);
        }
        //生成支付单,处理优惠券,并进入付款步骤
        foreach (M_OrderList model in orderList)
        {
            payMod.PaymentNum += model.OrderNo + ",";
            payMod.MoneyPay   += model.Ordersamount;
        }
        //优惠券
        if (!string.IsNullOrEmpty(Arrive_T.Text))
        {
            double arriveAmount = arriveBll.UserArrive(Arrive_T.Text, Arrive_Pwd.Text);
            payMod.MoneyPay     = payMod.MoneyPay - arriveAmount;
            payMod.ArriveMoney  = arriveAmount;
            payMod.ArriveDetail = Arrive_T.Text + "|" + Arrive_Pwd.Text;
        }
        //积分处理
        if (point_body.Visible && DataConvert.CLng(Point_T.Text) > 0)
        {
            int point    = DataConvert.CLng(Point_T.Text);
            int maxPoint = (int)((SiteConfig.ShopConfig.PointRatiot * 0.01) * (double)payMod.MoneyPay / SiteConfig.ShopConfig.PointRate);
            //if (point <= 0) { function.WriteErrMsg("积分数值不正确"); }
            if (point > mu.UserExp)
            {
                function.WriteErrMsg("您的积分不足!");
            }
            if (point > maxPoint)
            {
                function.WriteErrMsg("积分不能大于可兑换金额!");
            }
            //生成支付单时扣除用户积分
            buser.ChangeVirtualMoney(mu.UserID, new M_UserExpHis()
            {
                ScoreType = (int)M_UserExpHis.SType.Point, score = -point, detail = "积分抵扣,支付单号:" + payMod.PayNo
            });
            payMod.MoneyPay = payMod.MoneyPay - (point * SiteConfig.ShopConfig.PointRate);
            payMod.UsePoint = point;
        }
        if (payMod.MoneyPay <= 0)
        {
            payMod.MoneyPay = 0.01;
        }
        payMod.PaymentNum = payMod.PaymentNum.TrimEnd(',');
        payMod.Remark     = cartDT.Rows.Count > 1 ? "[" + cartDT.Rows[0]["ProName"] as string + "]等" : cartDT.Rows[0]["ProName"] as string;
        payMod.UserID     = mu.UserID;
        payMod.Status     = 1;
        payMod.PaymentID  = payBll.Add(payMod);

        string redirect_uri = HttpUtility.UrlEncode("http://www.wodian8.com/Payonline/wxpayonline.aspx?PayNo=" + payMod.PayNo);
        string url          = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8c5c2dc7b10a36f3&redirect_uri=" + redirect_uri + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect";
        //Response.Redirect(url);
        Response.Redirect("/PayOnline/Orderpay.aspx?PayNo=" + payMod.PayNo);
    }