diff --git a/BBWY.Server.Business/Sync/OrderSyncBusiness.cs b/BBWY.Server.Business/Sync/OrderSyncBusiness.cs index 11a97fb7..f6bf4a49 100644 --- a/BBWY.Server.Business/Sync/OrderSyncBusiness.cs +++ b/BBWY.Server.Business/Sync/OrderSyncBusiness.cs @@ -207,9 +207,13 @@ namespace BBWY.Server.Business List insertOrderCostDetailList = new List(); List insertOrderSkuList = new List(); List insertOrderCouponList = new List(); + List insertSkuDailySaleDetailList = new List(); IList> updateOrderList = new List>(); IList> updatePurchaseOrderList = new List>(); + IList> updateSkuDailySalesDetailList = new List>(); + + IDictionary> skuDailySalesDetailDictionary = new Dictionary>(); #endregion foreach (var orderJToken in orderTokenList) @@ -274,19 +278,46 @@ namespace BBWY.Server.Business insertOrderList.Add(dbOrder); #region OrderSku - var orderSkuList = orderJToken["itemInfoList"].Where(skuJToken => skuJToken.Value("jdPrice") != 0M).Select(skuToken => new OrderSku() + var itemInfoList = orderJToken["itemInfoList"].Where(skuJToken => skuJToken.Value("jdPrice") != 0M); + foreach (var orderSkuJToken in itemInfoList) { - Id = idGenerator.NewLong(), - ItemTotal = skuToken.Value("itemTotal"), - Price = skuToken.Value("jdPrice"), - ProductId = skuToken.Value("wareId"), - Title = skuToken.Value("skuName").SimplifySkuName(), - ProductNo = skuToken.Value("productNo"), - CreateTime = DateTime.Now, - OrderId = orderId, - SkuId = skuToken.Value("skuId") - }).ToList(); - insertOrderSkuList.AddRange(orderSkuList); + var osku = new OrderSku() + { + Id = idGenerator.NewLong(), + ItemTotal = orderSkuJToken.Value("itemTotal"), + Price = orderSkuJToken.Value("jdPrice"), + ProductId = orderSkuJToken.Value("wareId"), + Title = orderSkuJToken.Value("skuName").SimplifySkuName(), + ProductNo = orderSkuJToken.Value("productNo"), + CreateTime = DateTime.Now, + OrderId = orderId, + SkuId = orderSkuJToken.Value("skuId") + }; + insertOrderSkuList.Add(osku); + + if (!skuDailySalesDetailDictionary.TryGetValue(dbOrder.StartTime.Value, out List skuDailySalesDetailList)) + { + skuDailySalesDetailList = new List(); + skuDailySalesDetailDictionary.Add(dbOrder.StartTime.Value, skuDailySalesDetailList); + } + var skuDailySalesDetail = skuDailySalesDetailList.FirstOrDefault(s => s.Sku == osku.SkuId); + if (skuDailySalesDetail == null) + { + skuDailySalesDetail = new SkuDailySalesDetail() + { + ShopId = shopId, + Spu = osku.ProductId, + Sku = osku.SkuId, + Platform = Enums.Platform.京东, + Price = osku.Price, + Date = dbOrder.StartTime.Value + }; + skuDailySalesDetailList.Add(skuDailySalesDetail); + } + skuDailySalesDetail.Amount += osku.Price.Value * osku.ItemTotal.Value; + skuDailySalesDetail.ItemTotal += osku.ItemTotal.Value; + skuDailySalesDetail.OrderCount++; + } #endregion } #endregion @@ -297,10 +328,6 @@ namespace BBWY.Server.Business var orderConsignee = new OrderConsignee() { OrderId = orderId, - //Address = orderJToken["consigneeInfo"].Value("fullAddress"), - //ContactName = orderJToken["consigneeInfo"].Value("fullname"), - //Mobile = orderJToken["consigneeInfo"].Value("mobile"), - //TelePhone = orderJToken["consigneeInfo"].Value("telephone"), City = orderJToken["consigneeInfo"].Value("city"), Province = orderJToken["consigneeInfo"].Value("province"), County = orderJToken["consigneeInfo"].Value("county"), @@ -618,6 +645,46 @@ namespace BBWY.Server.Business } #endregion + #region sku销量统计 + if (skuDailySalesDetailDictionary.Keys.Count > 0) + { + foreach (var date in skuDailySalesDetailDictionary.Keys) + { + var skuDailySalesDetailList = skuDailySalesDetailDictionary[date]; + var skuDailySalesDetailIds = skuDailySalesDetailList.Select(s => s.Sku).ToList(); + var dbSkuDailySalesDetailIds = fsql.Select().Where(s => s.ShopId == shopId && + s.Date == date && + skuDailySalesDetailIds.Contains(s.Sku)).ToList(s => s.Sku); + var exceptIds = skuDailySalesDetailIds.Except(dbSkuDailySalesDetailIds).ToList(); + if (exceptIds.Count() > 0) + { + var insertList = skuDailySalesDetailList.Where(s => exceptIds.Contains(s.Sku)); + foreach (var s in insertList) + { + s.Id = idGenerator.NewLong(); + s.CreateTime = DateTime.Now; + insertSkuDailySaleDetailList.Add(s); + } + } + + var intersectIds = skuDailySalesDetailIds.Intersect(dbSkuDailySalesDetailIds).ToList(); + if (intersectIds.Count() > 0) + { + var updateList = skuDailySalesDetailList.Where(s => intersectIds.Contains(s.Sku)); + foreach (var s in updateList) + { + var iupdate = fsql.Update().Set(ds => ds.Amount + s.Amount) + .Set(ds => ds.OrderCount + s.OrderCount) + .Set(ds => ds.ItemTotal + s.ItemTotal) + .Where(ds => ds.ShopId == shopId && ds.Date == s.Date && ds.Sku == s.Sku); + updateSkuDailySalesDetailList.Add(iupdate); + } + } + } + } + + #endregion + fsql.Transaction(() => { if (insertOrderList.Count() > 0) @@ -632,6 +699,8 @@ namespace BBWY.Server.Business fsql.Insert(insertOrderCostDetailList).ExecuteAffrows(); if (insertOrderCouponList.Count() > 0) fsql.Insert(insertOrderCouponList).ExecuteAffrows(); + if (insertSkuDailySaleDetailList.Count() > 0) + fsql.Insert(insertSkuDailySaleDetailList).ExecuteAffrows(); if (updatePurchaseOrderList.Count() > 0) { @@ -644,6 +713,10 @@ namespace BBWY.Server.Business foreach (var update in updateOrderList) update.ExecuteAffrows(); } + + if (updateSkuDailySalesDetailList.Count > 0) + foreach (var update in updateSkuDailySalesDetailList) + update.ExecuteAffrows(); }); } diff --git a/BBWY.Server.Model/Db/Statistics/SkuDailySalesDetails.cs b/BBWY.Server.Model/Db/Statistics/SkuDailySalesDetails.cs index cf0a65af..8c1df88f 100644 --- a/BBWY.Server.Model/Db/Statistics/SkuDailySalesDetails.cs +++ b/BBWY.Server.Model/Db/Statistics/SkuDailySalesDetails.cs @@ -7,8 +7,8 @@ namespace BBWY.Server.Model.Db /// /// sku每日销量详情 /// - [Table(Name = "skudailysalesdetails", DisableSyncStructure = true)] - public partial class Skudailysalesdetails + [Table(Name = "skudailysalesdetail", DisableSyncStructure = true)] + public partial class SkuDailySalesDetail { [Column(IsPrimary = true)] @@ -18,7 +18,7 @@ namespace BBWY.Server.Model.Db /// 销售额 /// [Column(DbType = "decimal(18,2)")] - public decimal? Amount { get; set; } + public decimal Amount { get; set; } = 0.0M; [Column(DbType = "datetime")] public DateTime? CreateTime { get; set; } @@ -30,13 +30,13 @@ namespace BBWY.Server.Model.Db /// 销售件数 /// - public int? ItemTotal { get; set; } + public int ItemTotal { get; set; } = 0; /// /// 订单数 /// - public int? OrderCount { get; set; } + public int OrderCount { get; set; } = 0; [Column(MapType = typeof(int?))] public Enums.Platform? Platform { get; set; }