예제 #1
0
        /// <summary>
        /// Все позиции заказа в строковое значение для сообщения
        /// Пример.
        /// 1) Товар1 52руб. x 3 шт. = 156 руб.
        /// ......
        /// n) Товар2 40руб. x 1 шт. = 40 руб.
        /// </summary>
        /// <returns></returns>
        public string PositionToString()
        {
            MarketBotDbContext db = new MarketBotDbContext();

            try
            {
                int counter = 1;

                ///групируем позиции по id товара
                List <IGrouping <int, OrderProduct> > PositionGroup = new List <IGrouping <int, OrderProduct> >();

                string Positions = String.Empty;


                foreach (OrderProduct op in OrderProduct)
                {
                    if (op.Product == null)
                    {
                        op.Product = db.Product.Where(p => p.Id == op.ProductId).Include(p => p.Unit).FirstOrDefault();
                    }

                    if (op.Product != null && op.Product.Unit == null)
                    {
                        op.Product.Unit = db.Units.Find(op.Product.UnitId);
                    }

                    if (op.Price == null)
                    {
                        op.Price = db.ProductPrice.Where(p => p.Id == op.PriceId).Include(p => p.Currency).FirstOrDefault();
                    }

                    if (op.Price != null && op.Price.Currency == null)
                    {
                        op.Price.Currency = db.Currency.Find(op.Price.CurrencyId);
                    }
                }

                PositionGroup = OrderProduct.GroupBy(p => p.ProductId).ToList();

                foreach (var pos in PositionGroup)
                {
                    Positions += counter.ToString() + ") " + pos.FirstOrDefault().Product.Name + " " + pos.FirstOrDefault().Price.Value.ToString() +
                                 pos.FirstOrDefault().Price.Currency.ShortName + " x " + pos.Count().ToString() + " " + pos.FirstOrDefault().Product.Unit.ShortName + " = "
                                 + (pos.Count() * pos.FirstOrDefault().Price.Value).ToString() + " " + pos.FirstOrDefault().Price.Currency.ShortName + " | " + Bot.ProductBot.ProductCmd + pos.FirstOrDefault().ProductId.ToString() +
                                 "\r\n";
                    counter++;
                }

                return(Positions);
            }

            catch
            {
                return(String.Empty);
            }

            finally
            {
                db.Dispose();
            }
        }