diff --git a/BBWY.Server.API/Controllers/JDPopularizeSyncController.cs b/BBWY.Server.API/Controllers/JDPopularizeSyncController.cs new file mode 100644 index 00000000..7be00822 --- /dev/null +++ b/BBWY.Server.API/Controllers/JDPopularizeSyncController.cs @@ -0,0 +1,29 @@ +using BBWY.Server.Business.Sync; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using System; + +namespace BBWY.Server.API.Controllers +{ + + public class JDPopularizeSyncController : BaseApiController + { + private JDPopularizeSyncBusiness jdPopularizeSyncBusiness; + public JDPopularizeSyncController(IHttpContextAccessor httpContextAccessor, JDPopularizeSyncBusiness jdPopularizeSyncBusiness) : base(httpContextAccessor) + { + this.jdPopularizeSyncBusiness = jdPopularizeSyncBusiness; + } + + [HttpGet] + public void SyncAllShopPopularizeRecord() + { + jdPopularizeSyncBusiness.SyncAllShopPopularizeRecord(); + } + + [HttpGet] + public void SyncShopPopularizeRecordByDate(long? shopId, DateTime startDate, DateTime endDate) + { + jdPopularizeSyncBusiness.SyncShopPopularizeRecordByDate(shopId, startDate, endDate); + } + } +} diff --git a/BBWY.Server.Business/Sync/JDPopularizeSyncBusiness.cs b/BBWY.Server.Business/Sync/JDPopularizeSyncBusiness.cs index 5dca9e8f..64f2067a 100644 --- a/BBWY.Server.Business/Sync/JDPopularizeSyncBusiness.cs +++ b/BBWY.Server.Business/Sync/JDPopularizeSyncBusiness.cs @@ -8,10 +8,13 @@ using Newtonsoft.Json.Linq; using NLog; using System; using System.Collections.Generic; +using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; using Yitter.IdGenerator; +using BBWY.Common.Extensions; +using BBWY.Server.Model.Db; namespace BBWY.Server.Business.Sync { @@ -36,7 +39,32 @@ namespace BBWY.Server.Business.Sync private void SyncShopPopularizeRecord(long shopId, JArray jArray) { - + if (jArray == null || !jArray.HasValues) + return; + var sourceList = jArray.Select(j => new Shoppopularize() + { + //Id = idGenerator.NewLong(), + Cost = j.Value("amount"), + //CreateTime = DateTime.Now, + Date = j.Value("createTime").StampToDateTime().Date, + ItemName = j.Value("orderType"), + ShopId = shopId + }); + var group = sourceList.GroupBy(s => new { s.Date, s.ItemName }); + var insertList = new List(); + foreach (var g in group) + { + insertList.Add(new Shoppopularize() + { + Id = idGenerator.NewLong(), + Cost = g.Sum(s => s.Cost), + CreateTime = DateTime.Now, + Date = g.Key.Date, + ItemName = g.Key.ItemName, + ShopId = shopId + }); + } + fsql.Insert(insertList).ExecuteAffrows(); } private void SyncShopPopularizeRecord(ShopResponse shop, DateTime startDate, DateTime endDate) @@ -69,19 +97,59 @@ namespace BBWY.Server.Business.Sync } } + /// + /// 俺也书 + /// + /// + /// + /// + private void SyncAllShopPopularizeRecordByDate(ShopResponse shop, DateTime startDate, DateTime endDate) + { + var currentDate = startDate; + while (true) + { + SyncShopPopularizeRecord(shop, currentDate, currentDate); + currentDate = currentDate.AddDays(1); + if (currentDate > endDate) + break; + } + } + + private void DeleteOldData(IList shops, DateTime startDate, DateTime endDate) + { + var shopIds = shops.Select(s => Convert.ToInt64(s.ShopId)).ToList(); + fsql.Delete().Where(s => shopIds.Contains(s.ShopId.Value) && s.Date >= startDate && s.Date <= endDate).ExecuteAffrows(); + } public void SyncAllShopPopularizeRecord() { - var shopList = venderBusiness.GetShopList(); + var shopList = venderBusiness.GetShopList(shopId: null, Enums.Platform.京东); + var date = DateTime.Now.Date.AddDays(-1); + DeleteOldData(shopList, date, date); foreach (var shop in shopList) { - Task.Factory.StartNew(() => SyncShopPopularizeRecord(shop, - DateTime.Now.Date.AddDays(-1), - DateTime.Now.Date.AddDays(-1)), + Task.Factory.StartNew(() => SyncShopPopularizeRecord(shop, date, date), System.Threading.CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.JDPopularizeTaskScheduler); } } + + public void SyncShopPopularizeRecordByDate(long? shopId, DateTime startDate, DateTime endDate) + { + startDate = startDate.Date; + endDate = endDate.Date; + var shopList = venderBusiness.GetShopList(shopId, Enums.Platform.京东); + DeleteOldData(shopList, startDate, endDate); + foreach (var shop in shopList) + { + Task.Factory.StartNew(() => SyncAllShopPopularizeRecordByDate(shop, startDate, endDate), + System.Threading.CancellationToken.None, + TaskCreationOptions.LongRunning, + taskSchedulerManager.JDPopularizeTaskScheduler); + } + } + + } } diff --git a/BBWY.Server.Business/Vender/VenderBusiness.cs b/BBWY.Server.Business/Vender/VenderBusiness.cs index acef3750..332f0948 100644 --- a/BBWY.Server.Business/Vender/VenderBusiness.cs +++ b/BBWY.Server.Business/Vender/VenderBusiness.cs @@ -221,9 +221,11 @@ namespace BBWY.Server.Business return departmentList; } - public IList GetShopList() + public IList GetShopList(long? shopId = null, Enums.Platform? platform = null) { - return freeSqlMultiDBManager.MDSfsql.Select().Where(s => !string.IsNullOrEmpty(s.ShopId)).ToList(); + return freeSqlMultiDBManager.MDSfsql.Select().Where(s => !string.IsNullOrEmpty(s.ShopId)) + .WhereIf(shopId != null, s => s.ShopId == shopId.ToString()) + .WhereIf(platform != null, s => s.PlatformId == (int)platform).ToList(); } public ShopResponse GetShopByShopId(string shopId) diff --git a/BBWY.Server.Model/Db/Shop/ShopPopularize.cs b/BBWY.Server.Model/Db/Shop/ShopPopularize.cs new file mode 100644 index 00000000..d860bfbd --- /dev/null +++ b/BBWY.Server.Model/Db/Shop/ShopPopularize.cs @@ -0,0 +1,29 @@ +using FreeSql.DataAnnotations; +using System; + +namespace BBWY.Server.Model.Db +{ + + [Table(Name = "shoppopularize", DisableSyncStructure = true)] + public partial class Shoppopularize { + + [Column(IsPrimary = true)] + public long Id { get; set; } + + [Column(DbType = "decimal(18,2)")] + public decimal Cost { get; set; } = 0.0M; + + [Column(DbType = "datetime")] + public DateTime? CreateTime { get; set; } + + [Column(DbType = "datetime")] + public DateTime? Date { get; set; } + + [Column(StringLength = 50)] + public string ItemName { get; set; } + + public long? ShopId { get; set; } + + } + +}