public static Dictionary <int, ProductPriceVM> GetAllLastPrice() { var list = new Dictionary <int, ProductPriceVM>(); var collection = db.GetCollection <MgPrice>("prices"); var data = collection.Find(o => o.isLatest).ToList(); data = data.OrderByDescending(o => o.addDate).ToList(); var marketGroup = data.GroupBy(o => o.marketId); foreach (var g in marketGroup) { var productPrice = g.OrderByDescending(o => o.addDate).First(); var vm = new ProductPriceVM(); vm.AddDate = productPrice.addDate.ToLocalTime(); vm.Change = CommonService.ChangePrice(productPrice.change); vm.LPrice = productPrice.low; vm.HPrice = productPrice.high; vm.ProductId = productPrice.productId; vm.MarketID = productPrice.marketId; vm.ProductName = productPrice.productName; list.Add(g.Key, vm); } return(list); }
public List <ProductPriceVM> GetPricesByMobileAndMarketId(string mobile, int marketId) { var listOrder = new List <ProductPriceVM>(); using (var ctx = new ShtxSms2008Entities()) { CustomerBase cb = ctx.CustomerBases.FirstOrDefault(o => o.Tel.Contains(mobile) && o.SendInterFace == 102); if (cb != null) { mobile = cb.Tel; //change:// //var products = ctx.SmsProducts.Where(o => o.MarketId == marketId).OrderBy(p => p.DisplayOrder).ToList(); var products = CacheService.GetProductByMarketId(marketId).OrderBy(p => p.DisplayOrder).ToList(); var orderIds = ctx.Gps.Where(o => o.Tel == mobile && o.MarketID == marketId).Select(o => o.ProductID.Value).ToList(); foreach (var product in products) { var vm = new ProductPriceVM { ProductId = product.ProductId, ProductName = product.ProductName, ParentName = product.parentName, Comment = product.comment, Spec = product.spec }; if (orderIds.Contains(product.ProductId)) { vm.IsOrder = "YES"; } else { vm.IsOrder = "NO"; } //mongo: //Price price = ctx.Prices.OrderByDescending(o => o.AddDate).FirstOrDefault(o => o.ProductID == product.ProductId); Price price = MongoDBService.GetLastPricesByProductId(product.ProductId); if (price != null) { vm.LPrice = price.LPrice; vm.HPrice = string.IsNullOrEmpty(price.HPrice) ? price.LPrice : price.HPrice; vm.Date = price.AddDate.Value.ToString("yyyy-MM-dd"); vm.Change = CommonService.ChangePrice(price.PriceChange); vm.AddDate = price.AddDate.Value; listOrder.Add(vm); } } } return(listOrder); } }
public List <ProductPriceVM> GetHistoryPrices(int productId, DateTime start, DateTime end) { var list = new List <ProductPriceVM>(); //mongo: //去mongo里取该productId对应的相应日期里的所有价格信息,下边代码为改进的,注释的是原来的代码 var prices = MongoDBService.GetHostoryPrices(productId, start, end); while (start <= end) { DateTime endTime = start.AddDays(1); var price = prices.Where(o => o.ProductID == productId && o.AddDate < endTime && o.AddDate >= start).OrderByDescending(o => o.AddDate).FirstOrDefault(); if (price != null && (!string.IsNullOrEmpty(price.LPrice) || !string.IsNullOrEmpty(price.HPrice))) { ProductPriceVM vm = new ProductPriceVM(); vm.Date = start.ToString("yyyy-MM-dd"); vm.HPrice = string.IsNullOrEmpty(price.HPrice) ? price.LPrice : price.HPrice; vm.LPrice = string.IsNullOrEmpty(price.LPrice) ? price.HPrice : price.LPrice; vm.Change = CommonService.ChangePrice(price.PriceChange); list.Add(vm); } start = start.AddDays(1); } //using (var ctx = new ShtxSms2008Entities()) //{ // while (start <= end) // { // DateTime endTime = start.AddDays(1); // var price = ctx.Prices.Where(o => (o.ProductID ?? 0) == productId && o.AddDate < endTime && o.AddDate >= start).OrderByDescending(o => o.AddDate).FirstOrDefault(); // if (price != null && (!string.IsNullOrEmpty(price.LPrice) || !string.IsNullOrEmpty(price.HPrice))) // { // ProductPriceVM vm = new ProductPriceVM(); // vm.Date = start.ToString("yyyy-MM-dd"); // vm.HPrice = string.IsNullOrEmpty(price.HPrice) ? price.LPrice : price.HPrice; // vm.LPrice = string.IsNullOrEmpty(price.LPrice) ? price.HPrice : price.LPrice; // vm.Change = CommonService.ChangePrice(price.PriceChange); // list.Add(vm); // } // start = start.AddDays(1); // } //} return(list.OrderByDescending(o => o.Date).ToList()); }
public List <SearchPriceVM> GetSearchResult(string key, string mobile) { var list = new List <SearchPriceVM>(); List <int> xhmarketIds = new List <int>(); using (var ctx = new ShtxSms2008Entities()) { //change:// //var groups = ctx.XHMarketGroups.Where(o => o.Flag == 1 && o.IsForApp).Select(o => o.GroupID).ToList(); var groups = CacheService.GetMarketGroupForApp(1).Select(o => o.GroupID).ToList(); CustomerBase cb = ctx.CustomerBases.FirstOrDefault(o => o.Tel.Contains(mobile) && o.SendInterFace == 102); if (cb != null) { mobile = cb.Tel; var orderIds = ctx.Gps.Where(o => o.Tel == mobile).Select(o => o.ProductID.Value).ToList(); foreach (var group in groups) { //change:// //xhmarketIds.AddRange(ctx.Markets.Where(o => o.GroupID == group).Select(o => o.MarketId)); xhmarketIds.AddRange(CacheService.GetMarketByGroupId(group).Select(o => o.MarketId)); } var result = ctx.SmsProducts.Where(o => o.ProductName.Contains(key) && xhmarketIds.Contains(o.MarketId.Value)) .Select(o => new searchProduct { MarketId = o.MarketId.Value, ProductId = o.ProductId, ProductName = o.ProductName, Comment = o.comment }).ToList(); var markets = result.Select(o => o.MarketId).Distinct(); foreach (var market in markets) { var m = ctx.Markets.FirstOrDefault(o => o.MarketId == market); if (m != null) { SearchPriceVM priceVm = new SearchPriceVM(); priceVm.MarketId = m.MarketId; priceVm.MarketName = m.MarketName; priceVm.prices = new List <ProductPriceVM>(); var productList = result.Where(o => o.MarketId == market); foreach (var product in productList) { var vm = new ProductPriceVM { ProductId = product.ProductId, ProductName = product.ProductName, Comment = product.Comment }; if (orderIds.Contains(product.ProductId)) { vm.IsOrder = "YES"; } else { vm.IsOrder = "NO"; } //mongo: //Price price = ctx.Prices.OrderByDescending(o => o.AddDate).FirstOrDefault(o => o.ProductID == product.ProductId); Price price = MongoDBService.GetLastPricesByProductId(product.ProductId); if (price != null) { vm.LPrice = price.LPrice; vm.HPrice = string.IsNullOrEmpty(price.HPrice) ? price.LPrice : price.HPrice; vm.Date = price.AddDate.Value.ToString("yyyy-MM-dd"); vm.Change = CommonService.ChangePrice(price.PriceChange); vm.AddDate = price.AddDate.Value; priceVm.prices.Add(vm); } } if (priceVm.prices.Count > 0) { list.Add(priceVm); } } } } } return(list); }