Пример #1
0
        public IResult CheckOut(List <int> Id, string c_id)
        {
            IResult result = new Result();

            try
            {
                LocalDateTimeService timeService = new LocalDateTimeService();
                Random r           = new Random();
                var    today       = timeService.GetLocalDateTime(LocalDateTimeService.CHINA_STANDARD_TIME);
                string datetimeStr = today.ToString("yyyyMMddHHmmss");
                var    orderId     = string.Format("{0}{1}", datetimeStr, r.Next(1000, 10000));
                var    items       = db.Carts.Where(x => Id.Contains(x.Id)).ToList();
                if (!CheckItemStock(items))
                {
                    result.Message = "有商品庫存不足,請下單前確認庫存量是否足夠";
                    return(result);
                }
                var cartsWithSoapList = items.Join(db.Soaps, c => c.SoapId,
                                                   s => s.Id, (c, s) => new { c, s }).ToList();
                var sumPrice = cartsWithSoapList.Sum(x => x.c.AddCount * x.s.Price);

                var instance = new Orders
                {
                    AddTime    = today,
                    UpdateTime = today,
                    Id         = orderId,
                    TotalPrice = sumPrice,
                    StatusId   = 1,
                    C_Id       = c_id
                };
                db.Orders.Add(instance);
                bool stockNotEnough = cartsWithSoapList.Where(x => x.s.StockCount - x.c.AddCount < 0).SingleOrDefault() != null;
                if (stockNotEnough)
                {
                    result.Message = "有商品庫存不足,請下單前確認庫存量是否足夠";
                    return(result);
                }
                cartsWithSoapList.ForEach(x => x.s.StockCount = x.s.StockCount - x.c.AddCount);
                items.ForEach(x => x.OrderId = orderId);
                db.SaveChanges();
                result.Success = true;
            }
            catch (Exception e)
            {
                result.Message = "系統錯誤,請稍後再試";
                throw;
            }
            return(result);
        }
Пример #2
0
        public IResult CreateNews(News news)
        {
            IResult result = new Result();

            try
            {
                LocalDateTimeService timeService = new LocalDateTimeService();
                var now = timeService.GetLocalDateTime(LocalDateTimeService.CHINA_STANDARD_TIME);
                news.AddTime = now;
                news.Id      = now.ToString("yyyyMMddHHmmss");
                repository.Create(news);

                result.Success = true;
                return(result);
            }
            catch (Exception e)
            {
                result.Message = e.ToString();
                return(result);
            }
        }