diff --git a/BBWY.Server.API/Controllers/TestController.cs b/BBWY.Server.API/Controllers/TestController.cs index c706ab87..fce9a488 100644 --- a/BBWY.Server.API/Controllers/TestController.cs +++ b/BBWY.Server.API/Controllers/TestController.cs @@ -30,5 +30,11 @@ namespace BBWY.Server.API.Controllers { testBusiness.RepairSkuDetailAndSkuGriooProfit(request); } + + [HttpPost] + public void DeleteJDTask() + { + testBusiness.DeleteJDTask(); + } } } diff --git a/BBWY.Server.API/Properties/launchSettings.json b/BBWY.Server.API/Properties/launchSettings.json index e044e444..90f5837e 100644 --- a/BBWY.Server.API/Properties/launchSettings.json +++ b/BBWY.Server.API/Properties/launchSettings.json @@ -21,7 +21,7 @@ "commandName": "Project", "launchBrowser": true, "launchUrl": "weatherforecast", - "applicationUrl": "http://localhost:5000", + "applicationUrl": "http://localhost:5002", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/BBWY.Server.Business/TestBusiness.cs b/BBWY.Server.Business/TestBusiness.cs index 8b892e70..f48022af 100644 --- a/BBWY.Server.Business/TestBusiness.cs +++ b/BBWY.Server.Business/TestBusiness.cs @@ -12,6 +12,9 @@ using System.IO; using System.Linq; using System.Threading; using BBWY.Server.Business.Extensions; +using Newtonsoft.Json; +using System.Net.Http; +using System.Threading.Tasks; namespace BBWY.Server.Business { @@ -21,18 +24,21 @@ namespace BBWY.Server.Business private VenderBusiness venderBusiness; private IFreeSql fsql; private OrderSyncBusiness orderSyncBusiness; + private TaskSchedulerManager taskSchedulerManager; public TestBusiness(RestApiService restApiService, IOptions options, FreeSqlMultiDBManager freeSqlMultiDBManager, VenderBusiness venderBusiness, YunDingBusiness yunDingBusiness, IFreeSql fsql, - OrderSyncBusiness orderSyncBusiness) : base(restApiService, options, yunDingBusiness) + OrderSyncBusiness orderSyncBusiness, + TaskSchedulerManager taskSchedulerManager) : base(restApiService, options, yunDingBusiness) { this.freeSqlMultiDBManager = freeSqlMultiDBManager; this.venderBusiness = venderBusiness; this.fsql = fsql; this.orderSyncBusiness = orderSyncBusiness; + this.taskSchedulerManager = taskSchedulerManager; } public void SyncVenderId() @@ -164,101 +170,130 @@ namespace BBWY.Server.Business "瑞源玩具专营店" }; var shopList = venderBusiness.GetShopList(request.ShopId, Enums.Platform.京东); - var shopIndex = 0; + //var shopIndex = 0; foreach (var shop in shopList) { - shopIndex++; - Console.WriteLine($"shopProgress {shopIndex}/{shopList.Count()}"); - var shopId = long.Parse(shop.ShopId); - if (banedList.Contains(shop.ShopName)) - continue; - var orderIdList = fsql.Select() - .Where(o => o.ShopId == shopId && - o.OrderState != Enums.OrderState.已取消 && - o.StorageType != Enums.StorageType.SD && - o.StartTime >= request.StartTime && - o.StartTime <= request.EndTime) - .WhereIf(!string.IsNullOrEmpty(request.OrderId), o => o.Id == request.OrderId) - .OrderByDescending(o => o.StartTime) - .ToList(o => o.Id); - - fsql.Transaction(() => - { - fsql.Delete(orderIdList).ExecuteAffrows(); - fsql.Delete().Where(on => orderIdList.Contains(on.OrderId)).ExecuteAffrows(); - fsql.Delete().Where(op => orderIdList.Contains(op.OrderId)).ExecuteAffrows(); - fsql.Update().Set(osku => osku.ShouldPay, 0).Where(osku => orderIdList.Contains(osku.OrderId)).ExecuteAffrows(); - }); - - for (var i = 0; i < orderIdList.Count(); i++) + Task.Factory.StartNew(() => { - var orderId = orderIdList[i]; - orderSyncBusiness.SyncOrder(shop, orderId); - Console.WriteLine($"shopProgress {shopIndex}/{shopList.Count()} {shop.ShopName},orderProgress {i + 1}/{orderIdList.Count()}"); - } - // foreach (var orderId in orderIdList) - // { - // orderSyncBusiness.SyncOrder(shop, orderId); - // Thread.Sleep(1000); - // } + Console.WriteLine(shop.ShopName); + //shopIndex++; + //Console.WriteLine($"shopProgress {shopIndex}/{shopList.Count()}"); + var shopId = long.Parse(shop.ShopId); + if (banedList.Contains(shop.ShopName)) + return; + var orderIdList = fsql.Select() + .Where(o => o.ShopId == shopId && + o.OrderState != Enums.OrderState.已取消 && + o.StorageType != Enums.StorageType.SD && + o.StartTime >= request.StartTime && + o.StartTime <= request.EndTime) + .WhereIf(!string.IsNullOrEmpty(request.OrderId), o => o.Id == request.OrderId) + .OrderByDescending(o => o.StartTime) + .ToList(o => o.Id); - var orderList = fsql.Select(orderIdList).ToList(); - var orderSkuList = fsql.Select().Where(osku => osku.Price > 0 && orderIdList.Contains(osku.OrderId)).ToList(); - var orderCostDetailList = fsql.Select().Where(ocd => orderIdList.Contains(ocd.OrderId) && ocd.IsEnabled == true).ToList(); - var orderCostList = fsql.Select(orderIdList).ToList(); - var afterOrderList = fsql.Select().Where(af => orderIdList.Contains(af.OrderId)).ToList(); - var orderTotalList = fsql.Select().Where(on => orderIdList.Contains(on.Id)).ToList(); - var orderIndex = 0; - foreach (var order in orderList) - { - orderIndex++; - Console.WriteLine($"shopProgress {shopIndex}/{shopList.Count()}, orderProgress {orderIndex}/{orderList.Count()}"); + fsql.Transaction(() => + { + fsql.Delete(orderIdList).ExecuteAffrows(); + fsql.Delete().Where(on => orderIdList.Contains(on.OrderId)).ExecuteAffrows(); + fsql.Delete().Where(op => orderIdList.Contains(op.OrderId)).ExecuteAffrows(); + fsql.Update().Set(osku => osku.ShouldPay, 0).Where(osku => orderIdList.Contains(osku.OrderId)).ExecuteAffrows(); + }); - var currentOrderCostDetailList = orderCostDetailList.Where(ocd => ocd.OrderId == order.Id).ToList(); + for (var i = 0; i < orderIdList.Count(); i++) + { + var orderId = orderIdList[i]; + orderSyncBusiness.SyncOrder(shop, orderId); + Console.WriteLine($"SyncOrder {shop.ShopName},orderProgress {i + 1}/{orderIdList.Count()}"); + } + // foreach (var orderId in orderIdList) + // { + // orderSyncBusiness.SyncOrder(shop, orderId); + // Thread.Sleep(1000); + // } - if (currentOrderCostDetailList.Count() > 0) + var orderList = fsql.Select(orderIdList).ToList(); + var orderSkuList = fsql.Select().Where(osku => osku.Price > 0 && orderIdList.Contains(osku.OrderId)).ToList(); + var orderCostDetailList = fsql.Select().Where(ocd => orderIdList.Contains(ocd.OrderId) && ocd.IsEnabled == true).ToList(); + var orderCostList = fsql.Select(orderIdList).ToList(); + var afterOrderList = fsql.Select().Where(af => orderIdList.Contains(af.OrderId)).ToList(); + var orderTotalList = fsql.Select().Where(on => orderIdList.Contains(on.Id)).ToList(); + var orderIndex = 0; + foreach (var order in orderList) { - List> updateOrderCostDetailList = new List>(); - var currentOrderSkuList = orderSkuList.Where(osku => osku.OrderId == order.Id).ToList(); - foreach (var orderSku in currentOrderSkuList) + orderIndex++; + Console.WriteLine($"Calculation {shop.ShopName}, orderProgress {orderIndex}/{orderList.Count()}"); + + var currentOrderCostDetailList = orderCostDetailList.Where(ocd => ocd.OrderId == order.Id).ToList(); + + if (currentOrderCostDetailList.Count() > 0) { - var ocd = currentOrderCostDetailList.FirstOrDefault(x => x.SkuId == orderSku.SkuId); - if (ocd != null) + List> updateOrderCostDetailList = new List>(); + var currentOrderSkuList = orderSkuList.Where(osku => osku.OrderId == order.Id).ToList(); + foreach (var orderSku in currentOrderSkuList) { - ocd.CalculationSkuGrossProfit(orderSku.ShouldPay ?? 0M, - orderSku.PingTaiChengDanYouHuiQuan ?? 0M, - orderSku.SuperRedEnvelope ?? 0M, - orderSku.XianPinLeiDongQuan ?? 0M, - orderSku.VenderFee ?? 0M, - shop.PlatformCommissionRatio ?? 0.05M); - var update = fsql.Update(ocd.Id).Set(x => x.SkuGrossProfit, ocd.SkuGrossProfit); - updateOrderCostDetailList.Add(update); + var ocd = currentOrderCostDetailList.FirstOrDefault(x => x.SkuId == orderSku.SkuId); + if (ocd != null) + { + ocd.CalculationSkuGrossProfit(orderSku.ShouldPay ?? 0M, + orderSku.PingTaiChengDanYouHuiQuan ?? 0M, + orderSku.SuperRedEnvelope ?? 0M, + orderSku.XianPinLeiDongQuan ?? 0M, + orderSku.VenderFee ?? 0M, + shop.PlatformCommissionRatio ?? 0.05M); + var update = fsql.Update(ocd.Id).Set(x => x.SkuGrossProfit, ocd.SkuGrossProfit); + updateOrderCostDetailList.Add(update); + } } - } - var currentAfterOrderList = afterOrderList.Where(af => af.OrderId == order.Id).ToList(); - var orderTotal = orderTotalList.FirstOrDefault(on => on.Id == order.Id); - var orderCost = orderCostList.FirstOrDefault(oc => oc.OrderId == order.Id); - if (orderCost != null && orderTotal != null) - { - orderCost.CalculationOrderProfitAndCost(order.ActualProductAmount ?? 0M, - currentAfterOrderList); - if (updateOrderCostDetailList.Count() > 0) + var currentAfterOrderList = afterOrderList.Where(af => af.OrderId == order.Id).ToList(); + var orderTotal = orderTotalList.FirstOrDefault(on => on.Id == order.Id); + var orderCost = orderCostList.FirstOrDefault(oc => oc.OrderId == order.Id); + if (orderCost != null && orderTotal != null) { - fsql.Transaction(() => + orderCost.CalculationOrderProfitAndCost(order.ActualProductAmount ?? 0M, + currentAfterOrderList); + if (updateOrderCostDetailList.Count() > 0) { - foreach (var update in updateOrderCostDetailList) - update.ExecuteAffrows(); - fsql.Update(orderCost.OrderId).Set(oc => oc.Profit, orderCost.Profit) - .Set(oc => oc.PlatformCommissionAmount, orderCost.PlatformCommissionAmount).ExecuteAffrows(); - }); + fsql.Transaction(() => + { + foreach (var update in updateOrderCostDetailList) + update.ExecuteAffrows(); + fsql.Update(orderCost.OrderId).Set(oc => oc.Profit, orderCost.Profit) + .Set(oc => oc.PlatformCommissionAmount, orderCost.PlatformCommissionAmount).ExecuteAffrows(); + }); + } } - } + } } - } + }, CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.SyncOrderTaskScheduler); } } + + public void DeleteJDTask() + { + var appKey = "E1AA9247D5583A6D87449CE6AB290185"; + var appSecret = "e479558ddd9e40f8929cfc00c6cbbc9c"; + var appToken = "578d4074d5c04272af72de7f5fbe0ff2qwnz"; + var promotionId = 6106235467; + var httpResult = restApiService.SendRequest(GetPlatformRelayAPIHost(Enums.Platform.京东), + "api/platformsdk/DeleteJDPromotionTask", + new DeleteJDPromotionTaskRequest() + { + AppKey = appKey, + AppSecret = appSecret, + AppToken = appToken, + Platform = Enums.Platform.京东, + PromotionId = promotionId + }, + GetYunDingRequestHeader(), + HttpMethod.Post); + if (httpResult.StatusCode != System.Net.HttpStatusCode.OK) + throw new BusinessException(httpResult.Content); + var response = JsonConvert.DeserializeObject(httpResult.Content); + if (!response.Success) + throw new BusinessException(response.Msg); + } } public class JPCount