/*
         * 关于订单支付
         * 如果订单只需要消费积分,则在提交订单时,直接扣减积分成单
         * 如果需要现金消费,则先不扣积分,在生成订单后,转到订单详情画面,用户确认支付成功时再作扣减
         * 如果账户余额足够且用户完全使用了账户余额,则扣减余额和积分,成单
         * 如果需要微信支付,则生成微信支付订单,在完成微信支付之后的回调中,再扣减积分、余额,成单
         * 如果微信支付回调时,积分或余额不足,则微信支付的钱转到账户余额
         */

        /// <summary>
        /// 购物车结算,提交订单
        /// CheckoutOrder
        /// </summary>
        /// <returns></returns>
        public ActionResult CreateOrder()
        {
            PointCommodityCheckoutOrderArgs args = RequestArgs <PointCommodityCheckoutOrderArgs>();

            if (args == null)
            {
                return(RespondResult(false, "参数无效。"));
            }

            args.DomainId = DomainContext.Domain.Id;
            args.AppId    = DomainContext.AppId;
            args.MemberId = MemberContext.Member.Id;

            NormalResult <PointCommodityCheckoutOrderResult> result = _pointCommodityManager.CheckoutOrder(args);

            return(RespondDataResult(result));
        }
        /// <summary>
        /// 创建订单
        /// 如果订单中存在库存不足、已下架的商品
        /// 则整个订单都不下单
        /// 0 成功下单 1 积分不足 2 库存不足 3 已下架商品
        /// 默认在1小时后过期
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public NormalResult <PointCommodityCheckoutOrderResult> CheckoutOrder(PointCommodityCheckoutOrderArgs args)
        {
            NormalResult <PointCommodityCheckoutOrderResult> result =
                new NormalResult <PointCommodityCheckoutOrderResult>(false);

            List <CommandParameter> parameterList = new List <CommandParameter>();

            parameterList.Add(new CommandParameter("@domainId", args.DomainId));
            parameterList.Add(new CommandParameter("@appId", args.AppId));
            parameterList.Add(new CommandParameter("@memberId", args.MemberId));

            string xml = XMLHelper.XmlSerialize(args.ItemList);

            parameterList.Add(new CommandParameter("@itemList", xml));

            /*
             * <ArrayOfPointCommodity>
             * <PointCommodity>
             * <Id>61384bd7-18ff-4cfa-80e6-7278ae713e43</Id>
             * <Quantity>1</Quantity>
             * </PointCommodity>
             * <PointCommodity>
             * <Id>5a2606e4-92e3-479b-85a5-594685e87ff7</Id>
             * <Quantity>2</Quantity>
             * </PointCommodity>
             * </ArrayOfPointCommodity>
             */

            DataSet dsResult =
                _dataBase.ExecuteDataSet(CommandType.StoredProcedure, "PointCommodityCreateOrder", parameterList,
                                         new string[] { "result" });

            result.Reason = int.Parse(dsResult.Tables[0].Rows[0]["Result"].ToString());
            if (result.Reason == 0)
            {
                result.Success = true;

                result.Data             = new PointCommodityCheckoutOrderResult();
                result.Data.OrderNumber = dsResult.Tables[0].Rows[0]["OrderNumber"].ToString();
                result.Data.OrderId     = Guid.Parse(dsResult.Tables[0].Rows[0]["OrderId"].ToString());
            }
            return(result);
        }