步步为盈
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
17 KiB

3 years ago
using BBWY.Common.Http;
using BBWY.Common.Models;
using BBWY.Server.Model;
2 years ago
using BBWY.Server.Model.Db;
3 years ago
using BBWY.Server.Model.Db.Mds;
2 years ago
using BBWY.Server.Model.Dto;
3 years ago
using FreeSql;
using Microsoft.Extensions.Options;
2 years ago
using System;
3 years ago
using System.Collections.Generic;
2 years ago
using System.IO;
using System.Linq;
2 years ago
using System.Threading;
2 years ago
using BBWY.Server.Business.Extensions;
2 years ago
using Newtonsoft.Json;
using System.Net.Http;
using System.Threading.Tasks;
3 years ago
namespace BBWY.Server.Business
{
public class TestBusiness : BasePlatformRelayBusiness, IDenpendency
{
private FreeSqlMultiDBManager freeSqlMultiDBManager;
private VenderBusiness venderBusiness;
2 years ago
private IFreeSql fsql;
2 years ago
private OrderSyncBusiness orderSyncBusiness;
2 years ago
private TaskSchedulerManager taskSchedulerManager;
3 years ago
public TestBusiness(RestApiService restApiService,
IOptions<GlobalConfig> options,
FreeSqlMultiDBManager freeSqlMultiDBManager,
2 years ago
VenderBusiness venderBusiness,
YunDingBusiness yunDingBusiness,
IFreeSql fsql,
2 years ago
OrderSyncBusiness orderSyncBusiness,
TaskSchedulerManager taskSchedulerManager) : base(restApiService, options, yunDingBusiness)
3 years ago
{
this.freeSqlMultiDBManager = freeSqlMultiDBManager;
this.venderBusiness = venderBusiness;
2 years ago
this.fsql = fsql;
2 years ago
this.orderSyncBusiness = orderSyncBusiness;
2 years ago
this.taskSchedulerManager = taskSchedulerManager;
3 years ago
}
public void SyncVenderId()
{
var noVenderShops = freeSqlMultiDBManager.MDSfsql.Select<Shops>().Where(s => !string.IsNullOrEmpty(s.AppToken) && !string.IsNullOrEmpty(s.ShopId) && string.IsNullOrEmpty(s.VenderId)).ToList();
//IList < IUpdate < Shops >> updateShops = new List<IUpdate<Shops>>();
foreach (var noVenderShop in noVenderShops)
{
if (string.IsNullOrEmpty(noVenderShop.AppKey))
2 years ago
{
3 years ago
}
var venderResponse = venderBusiness.GetVenderInfo(new Model.Dto.PlatformRequest()
{
AppKey = noVenderShop.AppKey,
AppSecret = noVenderShop.AppSecret,
AppToken = noVenderShop.AppToken,
Platform = (Enums.Platform)noVenderShop.PlatformId
});
freeSqlMultiDBManager.MDSfsql.Update<Shops>(noVenderShop.Id).Set(s => s.VenderId, venderResponse.VenderId).ExecuteAffrows();
}
}
2 years ago
public void StatisticsProduct()
{
var shopList = venderBusiness.GetShopList(platform: Enums.Platform.);
var list = new List<string>();
foreach (var shop in shopList)
{
long shopId = long.Parse(shop.ShopId);
var productCount = fsql.Select<Product>().Where(p => p.ShopId == shopId)
.Where(p => p.State == 8)
.Count();
var productSkuCount = fsql.Select<ProductSku>().Where(p => p.ShopId == shopId)
.Where(p => p.State == 1)
.Count();
2 years ago
var jpspu_sql = $"select count(1) as Count,t.ShopId from (select max(osku1.Id) as oskuId,max(osku1.CreateTime) as maxTime,osku1.ProductId,o.ShopId from OrderSku osku1 inner join `Order` o on osku1.OrderId=o.Id where o.OrderState !=6 and o.IsGift=0 and o.StartTime>='2022-05-01' and o.ShopId='{shopId}' Group By osku1.ProductId,o.ShopId order by maxTime desc,osku1.ProductId desc,o.ShopId desc) as t inner join orderSku osku on t.oskuId=osku.Id inner join `order` o on osku.OrderId=o.Id inner join product p on t.ProductId = p.Id where o.StorageType in (0,1) and p.state=8 group by t.ShopId";
2 years ago
//京配spu数
var jpspu = fsql.Ado.QuerySingle<JPCount>(jpspu_sql);
2 years ago
var jpsku_sql = $"select count(1) as Count,t.ShopId from (select max(osku1.Id) as oskuId,max(osku1.CreateTime) as maxTime,osku1.SkuId,o.ShopId from OrderSku osku1 inner join `Order` o on osku1.OrderId=o.Id where o.OrderState !=6 and o.IsGift=0 and o.StartTime>='2022-05-01' and o.ShopId='{shopId}' Group By osku1.SkuId,o.ShopId order by maxTime desc,osku1.SkuId desc,o.ShopId desc) as t inner join orderSku osku on t.oskuId=osku.Id inner join `order` o on osku.OrderId=o.Id inner join productsku psku on t.SkuId = psku.Id where o.StorageType in (0,1) and psku.State=1 group by t.ShopId";
2 years ago
var jpsku = fsql.Ado.QuerySingle<JPCount>(jpsku_sql);
//list.Add($"{shop.ShopName},{productCount},{productSkuCount},{productSkuCount}/{productCount},{jpspu?.Count ?? 0},{jpsku?.Count ?? 0},{(productCount == 0 ? 0 : Math.Round(1.0 * jpspu?.Count ?? 0 / productCount * 100, 2))}%,{(productSkuCount == 0 ? 0 : Math.Round(1.0 * jpsku?.Count ?? 0 / productSkuCount * 100, 2))}%");
var jpspuCount = jpspu?.Count ?? 0;
var jpskuCount = jpsku?.Count ?? 0;
var jpspuPercent = productCount == 0 ? 0 : Math.Round(1.0 * jpspuCount / productCount * 100, 2);
var jpskuPercent = productSkuCount == 0 ? 0 : Math.Round(1.0 * jpskuCount / productSkuCount * 100, 2);
list.Add($"{shop.ShopName},{productCount},{productSkuCount},{productSkuCount}/{productCount},{jpspuCount},{jpskuCount},{jpspuPercent}%,{jpskuPercent}%");
}
list.Insert(0, "店铺,SPU数,SKU数,SKU/SPU,京配SPU数,京配SKU数,京配SPU占比,京配SKU占比");
var filePath = Path.Combine(AppContext.BaseDirectory, "result.csv");
File.WriteAllLines(filePath, list, System.Text.Encoding.UTF8);
}
public void XiuFu()
{
var st = DateTime.Parse("2023-09-01");
var orderList = fsql.Select<OrderCoupon, Order>().InnerJoin((oc, o) => oc.OrderId == o.Id)
.Where((oc, o) => o.OrderState != Enums.OrderState. && o.StartTime >= st && o.PreferentialAmount == 0)
.GroupBy((oc, o) => oc.OrderId)
.ToList(g => new Order
{
Id = g.Key,
PreferentialAmount = g.Sum(g.Value.Item1.CouponPrice)
});
List<IUpdate<Order>> updateOrderList = new List<IUpdate<Order>>();
var totalCount = orderList.Count();
for (var i = 0; i < totalCount; i++)
{
var order = orderList[i];
updateOrderList.Add(fsql.Update<Order>(order.Id).Set(o => o.PreferentialAmount, order.PreferentialAmount));
if (updateOrderList.Count() == 50)
{
Console.WriteLine($"执行数据库更新 {i}/{totalCount}");
fsql.Transaction(() =>
{
foreach (var update in updateOrderList)
update.ExecuteAffrows();
});
updateOrderList.Clear();
}
}
if (updateOrderList.Count() > 0)
{
fsql.Transaction(() =>
{
foreach (var update in updateOrderList)
update.ExecuteAffrows();
});
updateOrderList.Clear();
}
Console.WriteLine("更新完毕");
}
2 years ago
public void RepairSkuDetailAndSkuGriooProfit(RepairSkuDetailAndSkuGriooProfit request)
{
request.EndTime = request.EndTime.Date.AddDays(1).AddSeconds(-1);
var banedList = new List<string>()
{
"森王汽车用品专营店",
"森王玩具专营店",
"广发玩具专营店",
"广发汽车用品专营店",
"言义汽车用品专营店",
"杰博玩具专营店",
"安吉玩具专营店",
"布莱特玩具专营店",
"畅阳汽车用品专营店",
"熊猴汽车用品专营店",
"布莱特汽车用品专营店",
"越驭玩具专营店",
"言义玩具专营店",
"瑞源汽车用品专营店",
"熊猴玩具专营店",
"奥德汽车用品专营店",
"创天户外专营店",
"桑妮玩具专营店",
"瑞源玩具专营店"
};
var shopList = venderBusiness.GetShopList(request.ShopId, Enums.Platform.);
2 years ago
//var shopIndex = 0;
2 years ago
foreach (var shop in shopList)
{
2 years ago
Task.Factory.StartNew(() =>
2 years ago
{
2 years ago
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<Order>()
.Where(o => o.ShopId == shopId &&
o.OrderState != Enums.OrderState. &&
2 years ago
o.StorageType != null && o.StorageType != Enums.StorageType.SD &&
2 years ago
o.StartTime >= request.StartTime &&
o.StartTime <= request.EndTime)
.WhereIf(!string.IsNullOrEmpty(request.OrderId), o => o.Id == request.OrderId)
.WhereIf(request.OrderIdList != null && request.OrderIdList.Count() > 0, o => request.OrderIdList.Contains(o.Id))
2 years ago
.OrderByDescending(o => o.StartTime)
.ToList(o => o.Id);
2 years ago
2 years ago
if (orderIdList.Count() == 0)
return;
2 years ago
fsql.Transaction(() =>
{
fsql.Delete<OrderTotalInfo>(orderIdList).ExecuteAffrows();
fsql.Delete<OrderCoupon_New>().Where(on => orderIdList.Contains(on.OrderId)).ExecuteAffrows();
fsql.Delete<OrderPromotion>().Where(op => orderIdList.Contains(op.OrderId)).ExecuteAffrows();
fsql.Update<OrderSku>().Set(osku => osku.ShouldPay, 0).Where(osku => orderIdList.Contains(osku.OrderId)).ExecuteAffrows();
});
2 years ago
2 years ago
for (var i = 0; i < orderIdList.Count(); i++)
{
var orderId = orderIdList[i];
orderSyncBusiness.SyncOrder(shop, orderId);
2 years ago
Console.WriteLine($"SyncOrder {shop.ShopName},SyncProgress {i + 1}/{orderIdList.Count()}");
2 years ago
}
2 years ago
2 years ago
var orderList = fsql.Select<Order>(orderIdList).ToList();
var orderSkuList = fsql.Select<OrderSku>().Where(osku => osku.Price > 0 && orderIdList.Contains(osku.OrderId)).ToList();
var orderCostDetailList = fsql.Select<OrderCostDetail>().Where(ocd => orderIdList.Contains(ocd.OrderId) && ocd.IsEnabled == true).ToList();
var orderCostList = fsql.Select<OrderCost>(orderIdList).ToList();
var afterOrderList = fsql.Select<AfterSaleOrder>().Where(af => orderIdList.Contains(af.OrderId)).ToList();
var orderTotalList = fsql.Select<OrderTotalInfo>().Where(on => orderIdList.Contains(on.Id)).ToList();
var orderIndex = 0;
foreach (var order in orderList)
2 years ago
{
2 years ago
orderIndex++;
2 years ago
Console.WriteLine($"Calculation {shop.ShopName}, CalculationProgress {orderIndex}/{orderList.Count()}");
2 years ago
var currentOrderCostDetailList = orderCostDetailList.Where(ocd => ocd.OrderId == order.Id).ToList();
if (currentOrderCostDetailList.Count() > 0)
2 years ago
{
2 years ago
List<IUpdate<OrderCostDetail>> updateOrderCostDetailList = new List<IUpdate<OrderCostDetail>>();
var currentOrderSkuList = orderSkuList.Where(osku => osku.OrderId == order.Id).ToList();
foreach (var orderSku in currentOrderSkuList)
{
2 years ago
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<OrderCostDetail>(ocd.Id).Set(x => x.SkuGrossProfit, ocd.SkuGrossProfit);
updateOrderCostDetailList.Add(update);
}
}
2 years ago
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)
2 years ago
{
2 years ago
orderCost.CalculationOrderProfitAndCost(order.ActualProductAmount ?? 0M,
currentAfterOrderList);
if (updateOrderCostDetailList.Count() > 0)
{
2 years ago
fsql.Transaction(() =>
{
foreach (var update in updateOrderCostDetailList)
update.ExecuteAffrows();
fsql.Update<OrderCost>(orderCost.OrderId).Set(oc => oc.Profit, orderCost.Profit)
.Set(oc => oc.PlatformCommissionAmount, orderCost.PlatformCommissionAmount).ExecuteAffrows();
});
}
}
2 years ago
}
2 years ago
}
2 years ago
}, CancellationToken.None, TaskCreationOptions.LongRunning, taskSchedulerManager.SyncOrderTaskScheduler);
2 years ago
}
}
2 years ago
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<ApiResponse>(httpResult.Content);
if (!response.Success)
throw new BusinessException(response.Msg);
}
3 years ago
}
2 years ago
public class JPCount
{
public int Count { get; set; }
public long ShopId { get; set; }
}
3 years ago
}