shanji 3 years ago
parent
commit
c1cecd6867
  1. 29
      BBWY.Server.API/Controllers/JDPopularizeSyncController.cs
  2. 78
      BBWY.Server.Business/Sync/JDPopularizeSyncBusiness.cs
  3. 6
      BBWY.Server.Business/Vender/VenderBusiness.cs
  4. 29
      BBWY.Server.Model/Db/Shop/ShopPopularize.cs

29
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);
}
}
}

78
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<decimal>("amount"),
//CreateTime = DateTime.Now,
Date = j.Value<long>("createTime").StampToDateTime().Date,
ItemName = j.Value<string>("orderType"),
ShopId = shopId
});
var group = sourceList.GroupBy(s => new { s.Date, s.ItemName });
var insertList = new List<Shoppopularize>();
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
}
}
/// <summary>
/// 俺也书
/// </summary>
/// <param name="shop"></param>
/// <param name="startDate"></param>
/// <param name="endDate"></param>
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<ShopResponse> shops, DateTime startDate, DateTime endDate)
{
var shopIds = shops.Select(s => Convert.ToInt64(s.ShopId)).ToList();
fsql.Delete<Shoppopularize>().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);
}
}
}
}

6
BBWY.Server.Business/Vender/VenderBusiness.cs

@ -221,9 +221,11 @@ namespace BBWY.Server.Business
return departmentList;
}
public IList<ShopResponse> GetShopList()
public IList<ShopResponse> GetShopList(long? shopId = null, Enums.Platform? platform = null)
{
return freeSqlMultiDBManager.MDSfsql.Select<Shops>().Where(s => !string.IsNullOrEmpty(s.ShopId)).ToList<ShopResponse>();
return freeSqlMultiDBManager.MDSfsql.Select<Shops>().Where(s => !string.IsNullOrEmpty(s.ShopId))
.WhereIf(shopId != null, s => s.ShopId == shopId.ToString())
.WhereIf(platform != null, s => s.PlatformId == (int)platform).ToList<ShopResponse>();
}
public ShopResponse GetShopByShopId(string shopId)

29
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; }
}
}
Loading…
Cancel
Save