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.
141 lines
7.2 KiB
141 lines
7.2 KiB
using BBWY.Common.Http;
|
|
using BBWY.Common.Models;
|
|
using BBWY.Server.Model;
|
|
using BBWY.Server.Model.Db;
|
|
using BBWY.Server.Model.Db.Mds;
|
|
using FreeSql;
|
|
using Microsoft.Extensions.Options;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
|
|
namespace BBWY.Server.Business
|
|
{
|
|
public class TestBusiness : BasePlatformRelayBusiness, IDenpendency
|
|
{
|
|
private FreeSqlMultiDBManager freeSqlMultiDBManager;
|
|
private VenderBusiness venderBusiness;
|
|
private IFreeSql fsql;
|
|
public TestBusiness(RestApiService restApiService,
|
|
IOptions<GlobalConfig> options,
|
|
FreeSqlMultiDBManager freeSqlMultiDBManager,
|
|
VenderBusiness venderBusiness, YunDingBusiness yunDingBusiness, IFreeSql fsql) : base(restApiService, options, yunDingBusiness)
|
|
{
|
|
this.freeSqlMultiDBManager = freeSqlMultiDBManager;
|
|
this.venderBusiness = venderBusiness;
|
|
this.fsql = fsql;
|
|
}
|
|
|
|
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))
|
|
{
|
|
|
|
}
|
|
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();
|
|
}
|
|
}
|
|
|
|
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();
|
|
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";
|
|
|
|
//京配spu数
|
|
var jpspu = fsql.Ado.QuerySingle<JPCount>(jpspu_sql);
|
|
|
|
|
|
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";
|
|
|
|
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("更新完毕");
|
|
}
|
|
}
|
|
|
|
public class JPCount
|
|
{
|
|
public int Count { get; set; }
|
|
public long ShopId { get; set; }
|
|
}
|
|
|
|
|
|
}
|
|
|