private bool CreateReport(StatisticType statisticType, int statisticValue, int year) { OnUIMessageEventHandle($"{ServerName}-{statisticType.ToString()}-{statisticValue}-客户报表开始统计"); try { using (var db = new OMSContext()) { List <OrderEntity> orderEntities = new List <OrderEntity>(); switch (statisticType) { case StatisticType.Day: DateTime start = new DateTime(year, 1, 1).AddDays(statisticValue - 1); DateTime end = start.AddDays(1); orderEntities = db.OrderSet.Include(o => o.OrderDateInfo).Include(o => o.OrderRepurchase).Include(o => o.OrderExtendInfo).Include(o => o.Consignee).Where(o => (o.OrderType & 1) == 0 && (o.OrderType & 4) == 0 && o.OrderDateInfo.CreateTime >= start && o.OrderDateInfo.CreateTime <= end && o.CreatedDate.Year == year).ToList(); break; case StatisticType.Week: orderEntities = db.OrderSet.Include(o => o.OrderDateInfo).Include(o => o.OrderRepurchase).Include(o => o.OrderExtendInfo).Include(o => o.Consignee).Where(s => (s.OrderType & 1) == 0 && (s.OrderType & 4) == 0 && s.OrderDateInfo.WeekNum == statisticValue && s.CreatedDate.Year == year).ToList(); break; case StatisticType.Month: orderEntities = db.OrderSet.Include(o => o.OrderDateInfo).Include(o => o.OrderRepurchase).Include(o => o.OrderExtendInfo).Include(o => o.Consignee).Where(s => (s.OrderType & 1) == 0 && (s.OrderType & 4) == 0 && s.OrderDateInfo.MonthNum == statisticValue && s.CreatedDate.Year == year).ToList(); break; case StatisticType.Quarter: orderEntities = db.OrderSet.Include(o => o.OrderDateInfo).Include(o => o.OrderRepurchase).Include(o => o.OrderExtendInfo).Include(o => o.Consignee).Where(s => (s.OrderType & 1) == 0 && (s.OrderType & 4) == 0 && s.OrderDateInfo.SeasonNum == statisticValue && s.CreatedDate.Year == year).ToList(); break; case StatisticType.Year: orderEntities = db.OrderSet.Include(o => o.OrderDateInfo).Include(o => o.OrderRepurchase).Include(o => o.OrderExtendInfo).Include(o => o.Consignee).Where(s => (s.OrderType & 1) == 0 && (s.OrderType & 4) == 0 && s.OrderDateInfo.Year == statisticValue).ToList(); break; default: break; } if (ServerName != OrderSource.ALL) { orderEntities = orderEntities.Where(o => o.Source == ServerName).ToList(); } var glst = orderEntities.GroupBy(o => o.Consignee).OrderByDescending(g => g.Count()).Take(20); if (glst != null && glst.Any()) { List <StatisticCustomer> lst = new List <StatisticCustomer>(); foreach (var item in glst) { StatisticCustomer statistic = new StatisticCustomer() { CreateDate = DateTime.Now, Year = year, Source = ServerName, SourceDesc = Util.Helpers.Reflection.GetDescription <OrderSource>(ServerName), StatisticType = (int)statisticType, StatisticValue = statisticValue, OrderCount = item.Count(), ProductCount = item.Sum(o => o.OrderExtendInfo.TotalProductCount), Customer = item.Key, Name = item.Key.Name, Phone = item.Key.Phone }; lst.Add(statistic); } var foo = db.StatisticCustomers.Where(s => s.Source == ServerName && s.StatisticType == (int)statisticType && s.StatisticValue == statisticValue && s.Year == year); if (foo == null) { db.Set <StatisticCustomer>().AddRange(lst); } else { db.Set <StatisticCustomer>().RemoveRange(foo); db.Set <StatisticCustomer>().AddRange(lst); } db.SaveChanges(); OnUIMessageEventHandle($"{ServerName}-{statisticType.ToString()}-{statisticValue}-客户统计完毕"); } else { OnUIMessageEventHandle($"{ServerName}-{statisticType.ToString()}-{statisticValue}-客户方面无统计结果"); } return(true); } } catch (Exception ex) { Util.Logs.Log.GetLog(nameof(DistrictStatisticServerBase)).Error($":error:{ex.Message},statck:{ex.StackTrace}"); return(false); } }
// Create a statistic definition and add it to the collection based on the user selection in the combo boxes private void AddStatisticClicked(object sender, RoutedEventArgs e) { // Verify that a field name and statistic type has been selected if (FieldsComboBox.SelectedValue == null || StatTypeComboBox.SelectedValue == null) { return; } // Get the chosen field name and statistic type from the combo boxes string fieldName = FieldsComboBox.SelectedValue.ToString(); StatisticType statType = (StatisticType)StatTypeComboBox.SelectedValue; // Check if this statistic definition has already be created (same field name and statistic type) StatisticDefinition existingStatDefinition = _statDefinitions.FirstOrDefault(def => def.OnFieldName == fieldName && def.StatisticType == statType); // If it doesn't exist, create it and add it to the collection (use the field name and statistic type to build the output alias) if (existingStatDefinition == null) { StatisticDefinition statDefinition = new StatisticDefinition(fieldName, statType, fieldName + "_" + statType.ToString()); _statDefinitions.Add(statDefinition); } }
private bool CreateReport(StatisticType statisticType, int statisticValue, int year) { OnUIMessageEventHandle($"{ServerName}-{statisticType.ToString()}-{statisticValue}-商品报表开始统计"); try { using (var db = new OMSContext()) { db.Database.CommandTimeout = 600000; List <OrderEntity> orderEntities = new List <OrderEntity>(); switch (statisticType) { case StatisticType.Day: DateTime start = new DateTime(year, 1, 1).AddDays(statisticValue - 1); DateTime end = start.AddDays(1); orderEntities = db.OrderSet.Include(o => o.Consignee).Include(o => o.OrderDateInfo).Include(o => o.OrderRepurchase).Include(o => o.Products).Where(s => (s.OrderType & 1) == 0 && (s.OrderType & 4) == 0 && s.OrderDateInfo.CreateTime >= start && s.OrderDateInfo.CreateTime < end && s.CreatedDate.Year == year).ToList(); break; case StatisticType.Week: orderEntities = db.OrderSet.Include(o => o.Consignee).Include(o => o.OrderDateInfo).Include(o => o.OrderRepurchase).Include(o => o.Products).Where(s => (s.OrderType & 1) == 0 && (s.OrderType & 4) == 0 && s.OrderDateInfo.WeekNum == statisticValue && s.CreatedDate.Year == year).ToList(); break; case StatisticType.Month: orderEntities = db.OrderSet.Include(o => o.Consignee).Include(o => o.OrderDateInfo).Include(o => o.OrderRepurchase).Include(o => o.Products).Where(s => (s.OrderType & 1) == 0 && (s.OrderType & 4) == 0 && s.OrderDateInfo.MonthNum == statisticValue && s.CreatedDate.Year == year).ToList(); break; case StatisticType.Quarter: orderEntities = db.OrderSet.Include(o => o.Consignee).Include(o => o.OrderDateInfo).Include(o => o.OrderRepurchase).Include(o => o.Products).Where(s => (s.OrderType & 1) == 0 && (s.OrderType & 4) == 0 && s.OrderDateInfo.SeasonNum == statisticValue && s.CreatedDate.Year == year).ToList(); break; case StatisticType.Year: orderEntities = db.OrderSet.Include(o => o.Consignee).Include(o => o.OrderDateInfo).Include(o => o.OrderRepurchase).Include(o => o.Products).Where(s => (s.OrderType & 1) == 0 && (s.OrderType & 4) == 0 && s.OrderDateInfo.Year == statisticValue).ToList(); break; default: break; } if (ServerName != OrderSource.ALL) { orderEntities = orderEntities.Where(o => o.Source == ServerName).ToList(); } if (orderEntities.Any()) { var plst = orderEntities.SelectMany(o => o.Products).Where(o => o.sku != null); var q = plst.GroupBy(p => new { p.weightCode, p.sku }); List <StatisticProduct> smp = new List <StatisticProduct>(); foreach (var item in q) { var lst = orderEntities.Where(o => o.Products.Any(p => p.sku == item.Key.sku)); StatisticProduct statistic = new StatisticProduct() { Source = ServerName, SourceDesc = Util.Helpers.Reflection.GetDescription <OrderSource>(ServerName), Year = year, ProductPlatName = db.ProductsSet.Find(item.Key.sku)?.ShortName ?? db.ProductsSet.Find(item.Key.sku)?.Name, SKU = item.Key.sku, ProductTotalAmount = item.Sum(o => o.TotalAmount), ProductTotalCostAmount = item.Sum(o => o.TotalCostPrice), ProductTotalFlatAmount = item.Sum(o => o.TotalFlatAmount), weightCode = item.Key.weightCode, weightCodeDesc = db.WeightCodeSet.Find(item.Key.weightCode)?.Value.ToString(), ProductTotalWeight = item.Sum(o => o.ProductWeight) / 1000, ProductCount = item.Sum(o => o.ProductCount), CreateDate = DateTime.Now, StatisticType = (int)statisticType, StatisticValue = statisticValue, TotalCustomers = lst.GroupBy(o => o.Consignee).Count(),//总计客户数量 TotalOrders = lst.Count() }; //复购统计 switch (statisticType) { case StatisticType.Day: DateTime start = new DateTime(year, 1, 1).AddDays(statisticValue - 1); statistic.CreateDate = start; statistic.TotalOrderRepurchase = lst.Where(o => o.OrderRepurchase.DailyRepurchase == true).Count(); statistic.TotalCustomerRepurchase = lst.Where(o => o.OrderRepurchase.DailyRepurchase == true).GroupBy(o => o.Consignee).ToList().Count(); statistic.TotalProductRepurchase = lst.Where(o => o.OrderRepurchase.DailyRepurchase == true).SelectMany(o => o.Products.Where(P => P.sku == item.Key.sku)).Sum(p => p.ProductCount); break; case StatisticType.Week: statistic.TotalOrderRepurchase = lst.Where(o => o.OrderRepurchase.WeekRepurchase == true).Count(); statistic.TotalCustomerRepurchase = lst.Where(o => o.OrderRepurchase.WeekRepurchase == true).GroupBy(o => o.Consignee).ToList().Count(); statistic.TotalProductRepurchase = lst.Where(o => o.OrderRepurchase.WeekRepurchase == true).SelectMany(o => o.Products.Where(P => P.sku == item.Key.sku)).Sum(p => p.ProductCount); break; case StatisticType.Month: statistic.TotalOrderRepurchase = lst.Where(o => o.OrderRepurchase.MonthRepurchase == true).Count(); statistic.TotalCustomerRepurchase = lst.Where(o => o.OrderRepurchase.MonthRepurchase == true).GroupBy(o => o.Consignee).ToList().Count(); statistic.TotalProductRepurchase = lst.Where(o => o.OrderRepurchase.MonthRepurchase == true).SelectMany(o => o.Products.Where(P => P.sku == item.Key.sku)).Sum(p => p.ProductCount); break; case StatisticType.Quarter: statistic.TotalOrderRepurchase = lst.Where(o => o.OrderRepurchase.SeasonRepurchase == true).Count(); statistic.TotalCustomerRepurchase = lst.Where(o => o.OrderRepurchase.SeasonRepurchase == true).GroupBy(o => o.Consignee).ToList().Count(); statistic.TotalProductRepurchase = lst.Where(o => o.OrderRepurchase.SeasonRepurchase == true).ToList().SelectMany(o => o.Products.Where(P => P.sku == item.Key.sku)).Sum(p => p.ProductCount); break; case StatisticType.Year: statistic.TotalOrderRepurchase = lst.Where(o => o.OrderRepurchase.YearRepurchase == true).Count(); statistic.TotalCustomerRepurchase = lst.Where(o => o.OrderRepurchase.YearRepurchase == true).GroupBy(o => o.Consignee).ToList().Count(); statistic.TotalProductRepurchase = lst.Where(o => o.OrderRepurchase.YearRepurchase == true).SelectMany(o => o.Products.Where(P => P.sku == item.Key.sku)).Sum(p => p.ProductCount); break; default: break; } smp.Add(statistic); } //foreach (var item in smp) //{ // OnUIMessageEventHandle($"{item.SourceDesc}\t{item.ProductPlatName}\t{item.weightCodeDesc}\t{item.ProductTotalWeight}\t{item.ProductTotalAmount}\t{item.ProductCount}"); //} var removeobj = db.StatisticProductSet.Where(s => s.Year == year && s.Source == ServerName && s.StatisticType == (int)statisticType && s.StatisticValue == statisticValue); if (removeobj.Any()) { //bool saveFailed; //do //{ // saveFailed = false; // try // { // db.SaveChanges(); // } // catch (Db ex) // { // saveFailed = true; // // Update original values from the database // var entry = ex.Entries.Single(); // entry.OriginalValues.SetValues(entry.GetDatabaseValues()); // } //} while (saveFailed); db.Set <StatisticProduct>().RemoveRange(removeobj); db.SaveChanges(); } db.Set <StatisticProduct>().AddRange(smp); db.SaveChanges(); OnUIMessageEventHandle($"{ServerName}-{statisticType.ToString()}-{statisticValue}-商品统计完毕"); } else { OnUIMessageEventHandle($"{ServerName}-{statisticType.ToString()}-{statisticValue}-产品方面无统计结果"); return(false); } return(true); } } catch (Exception ex) { Util.Logs.Log.GetLog(nameof(ProductStatisticServerBase)).Error($"{ServerName}:error:{ex.Message},statck:{ex.StackTrace}"); return(false); } }