示例#1
0
        public async Task <ActionResult> PosttbOrder(viOrder value)
        {
            ctx.BeginTransaction();

            try
            {
                tbOrderHeader or;
                tbAddress     adr;

                if (value.Id == null)
                {
                    or               = new tbOrderHeader();
                    or.CreateDate    = DateTime.Now;
                    or.CreateUser    = User.GetId();
                    or.OrderStatusId = 1;
                }
                else
                {
                    return(BadRequest("Бундай ID лик заказ бор..."));
                    //or = await ctx.tbOrders.FindAsync(value.Id);
                    //or.UpdateUser = User.GetId();
                    //or.UpdateDate = DateTime.Now;
                }

                var dt = await ctx.spDeliveryTypes.FindAsync(value.DeliveryTypeId);

                var phone = await ctx.tbUser.FindAsync(User.GetId());

                or.Status         = 1;
                or.DrugStoreId    = 1001;
                or.DeliveryTime   = DateTime.Now.AddHours(dt.DeliveryTime);
                or.Description    = value.Description;
                or.PaymentId      = value.PaymentId;
                or.UserAgentId    = value.UserAgentId;
                or.DeliveryTypeId = value.DeliveryTypeId;
                or.Phone          = value.Phone;

                if (value.AddressId == null || value.AddressId == 0)
                {
                    adr            = new tbAddress();
                    adr.RegionId   = value.RegionId ?? value.RegionId.Value;
                    adr.DistrictId = value.DistrictId ?? value.DistrictId.Value;
                    adr.Street     = value.Street;
                    adr.House      = value.House;
                    adr.Flat       = value.Flat;
                    adr.Waymark    = value.Waymark;
                    adr.Latitude   = value.Latitude;
                    adr.Longitude  = value.Longitude;

                    adr.CreateDate = DateTime.Now;
                    adr.CreateUser = User.GetId();
                    adr.Status     = 1;

                    await ctx.tbAddress.AddAsync(adr);

                    await ctx.SaveChangesAsync();

                    or.AddressId = adr.Id;
                }
                else
                {
                    or.AddressId = value.AddressId.Value;
                }

                or.ItemQty = value.OrderDetails.Count();
                or.Summa   = value.OrderDetails.Sum(x => x.TotalSum);

                or.OrderDetails = new List <tbOrderDetails>();
                foreach (var it in value.OrderDetails)
                {
                    var od = new tbOrderDetails();
                    od.CreateDate = DateTime.Now;
                    od.CreateUser = value.CreateUser;

                    if (it.ProductId == null || it.ProductId == 0)
                    {
                        od.ProductId = await GetDrugIdAsync(it.DrugName);
                    }
                    else
                    {
                        od.ProductId = it.ProductId ?? it.ProductId.Value;
                    }

                    od.Qty      = it.Qty;
                    od.Price    = it.Price;
                    od.TotalSum = it.TotalSum;
                    od.Status   = 1;

                    or.OrderDetails.Add(od);
                }

                await ctx.tbOrders.AddAsync(or);

                await ctx.SaveChangesAsync();

                ctx.Commit();
                return(Ok(new { OrderId = or.Id, DeliveryTime = or.DeliveryTime }));
            }
            catch (Exception)
            {
                ctx.Rollback();
                return(BadRequest());
            }
        }