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.
1812 lines
88 KiB
1812 lines
88 KiB
using BBWYB.Common.Extensions;
|
|
using BBWYB.Common.Http;
|
|
using BBWYB.Common.Log;
|
|
using BBWYB.Common.Models;
|
|
using BBWYB.Server.Model;
|
|
using BBWYB.Server.Model.Db;
|
|
using BBWYB.Server.Model.Db.HY;
|
|
using BBWYB.Server.Model.Dto;
|
|
using FreeSql;
|
|
using MySqlX.XDevAPI;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using SDKAdapter;
|
|
using SDKAdapter.OperationPlatform.Client;
|
|
using SDKAdapter.OperationPlatform.Models;
|
|
using SDKAdapter.PurchasePlatform.Client;
|
|
using SDKAdapter.PurchasePlatform.Models;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Net.Http.Headers;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using Yitter.IdGenerator;
|
|
|
|
namespace BBWYB.Server.Business
|
|
{
|
|
public class DataRepairBusiness : BaseBusiness, IDenpendency
|
|
{
|
|
private KuaiDi100Manager kuaiDi100Manager;
|
|
private PurchaseSchemeBusiness purchaseSchemeBusiness;
|
|
private PurchaseOrderBusiness purchaseOrderBusiness;
|
|
private FreeSqlMultiDBManager freeSqlMultiDBManager;
|
|
private VenderBusiness venderBusiness;
|
|
private OP_PlatformClientFactory opPlatformClientFactory;
|
|
private PP_PlatformClientFactory pp_PlatformClientFactory;
|
|
private RestApiService restApiService;
|
|
private TimeLimitRules timeLimitRules;
|
|
public DataRepairBusiness(IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator, KuaiDi100Manager kuaiDi100Manager, PurchaseSchemeBusiness purchaseSchemeBusiness, PurchaseOrderBusiness purchaseOrderBusiness, FreeSqlMultiDBManager freeSqlMultiDBManager, VenderBusiness venderBusiness, OP_PlatformClientFactory opPlatformClientFactory, PP_PlatformClientFactory pp_PlatformClientFactory, RestApiService restApiService, TimeLimitRules timeLimitRules) : base(fsql, nLogManager, idGenerator)
|
|
{
|
|
this.kuaiDi100Manager = kuaiDi100Manager;
|
|
this.purchaseSchemeBusiness = purchaseSchemeBusiness;
|
|
this.purchaseOrderBusiness = purchaseOrderBusiness;
|
|
this.freeSqlMultiDBManager = freeSqlMultiDBManager;
|
|
this.venderBusiness = venderBusiness;
|
|
this.opPlatformClientFactory = opPlatformClientFactory;
|
|
this.pp_PlatformClientFactory = pp_PlatformClientFactory;
|
|
this.restApiService = restApiService;
|
|
this.timeLimitRules = timeLimitRules;
|
|
}
|
|
|
|
|
|
public void SubscribeKD100(string waybillNo, string targetCompanyCode)
|
|
{
|
|
//订阅快递100
|
|
kuaiDi100Manager.SubscribeKuaiDi100(waybillNo, targetCompanyCode, "http://bbwyb.qiyue666.com/api/purchaseorder/kuaidi100publish");
|
|
}
|
|
|
|
public void SupplementOrderSkuCost()
|
|
{
|
|
var orderSkuCostList = fsql.Select<OrderCostDetail, Order>().InnerJoin((ocd, o) => ocd.OrderId == o.Id)
|
|
.Where((ocd, o) => ocd.IsEnabled == true &&
|
|
o.IsPurchased == true &&
|
|
o.OrderState != Enums.OrderState.已取消 &&
|
|
!fsql.Select<OrderSkuCost>().As("osc").Where(osc => osc.OrderId == o.Id).Any())
|
|
.GroupBy((ocd, o) => new { ocd.OrderId, ocd.SkuId })
|
|
.ToList(g => new
|
|
{
|
|
OrderId = g.Key.OrderId,
|
|
SkuId = g.Key.SkuId,
|
|
SkuAmount = g.Sum(g.Value.Item1.SkuAmount),
|
|
PurchaseFreight = g.Sum(g.Value.Item1.PurchaseFreight)
|
|
});
|
|
var orderIds = orderSkuCostList.Select(x => x.OrderId).Distinct().ToList();
|
|
var orderSkuList = fsql.Select<OrderSku>().Where(osku => orderIds.Contains(osku.OrderId)).ToList();
|
|
|
|
var insertOrderSkuCostList = new List<OrderSkuCost>();
|
|
foreach (var osc in orderSkuCostList)
|
|
{
|
|
var osku = orderSkuList.FirstOrDefault(osku => osku.SkuId == osc.SkuId && osku.OrderId == osc.OrderId);
|
|
var insert = new OrderSkuCost()
|
|
{
|
|
Id = idGenerator.NewLong(),
|
|
CreateTime = DateTime.Now,
|
|
DeliveryExpressFreight = 0M,
|
|
OrderId = osc.OrderId,
|
|
SkuId = osc.SkuId,
|
|
OutPackAmount = 0M,
|
|
PurchaseFreight = osc.PurchaseFreight,
|
|
SkuAmount = osc.SkuAmount,
|
|
TotalCost = osc.PurchaseFreight + osc.SkuAmount,
|
|
Profit = ((osku.Price * osku.ItemTotal) ?? 0M) + osku.BuyerPayFreight - osc.PurchaseFreight - osc.SkuAmount
|
|
};
|
|
insertOrderSkuCostList.Add(insert);
|
|
}
|
|
fsql.Transaction(() =>
|
|
{
|
|
fsql.Insert(insertOrderSkuCostList).ExecuteAffrows();
|
|
});
|
|
}
|
|
|
|
public void SupplementPurchaserInfo()
|
|
{
|
|
var purlList = fsql.Select<PurchaseSchemeProduct, Purchaser>()
|
|
.InnerJoin((psp, p) => psp.PurchaserId == p.Id)
|
|
.Where((psp, p) => p.Platform == Enums.Platform.阿里巴巴 && string.IsNullOrEmpty(p.MemberId))
|
|
.ToList((psp, p) => new
|
|
{
|
|
//psp.PurchaseUrl,
|
|
psp.PurchaseProductId,
|
|
p.Id
|
|
});
|
|
var groups = purlList.GroupBy(p => p.Id);
|
|
Console.WriteLine(groups.Count());
|
|
IList<IUpdate<Purchaser>> updatePurchaserList = new List<IUpdate<Purchaser>>();
|
|
int i = 1;
|
|
foreach (var group in groups)
|
|
{
|
|
var purchaseId = group.Key;
|
|
var productId = group.Max(p => p.PurchaseProductId);
|
|
Thread.Sleep(1000);
|
|
Console.WriteLine();
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
Console.WriteLine($"index {i}");
|
|
Console.ResetColor();
|
|
Console.WriteLine();
|
|
var response = purchaseSchemeBusiness.GetPurchaseSkuBasicInfo(new PurchaseSkuBasicInfoRequest()
|
|
{
|
|
FirstApiMode = Enums.PurchaseProductAPIMode.Spider,
|
|
Platform = Enums.Platform.阿里巴巴,
|
|
PriceMode = Enums.PurchaseOrderMode.批发,
|
|
PurchaseProductId = productId
|
|
});
|
|
if (!string.IsNullOrEmpty(response?.Purchaser?.MemberId))
|
|
{
|
|
if (response.Purchaser.MemberId.Length > 5)
|
|
Console.WriteLine(response.Purchaser.MemberId);
|
|
var update = fsql.Update<Purchaser>(purchaseId).Set(p => p.MemberId, response.Purchaser.MemberId);
|
|
updatePurchaserList.Add(update);
|
|
}
|
|
i++;
|
|
|
|
if (updatePurchaserList.Count >= 10)
|
|
{
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var update in updatePurchaserList)
|
|
update.ExecuteAffrows();
|
|
});
|
|
updatePurchaserList.Clear();
|
|
Console.WriteLine();
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
Console.WriteLine("更新数据库更新");
|
|
Console.ResetColor();
|
|
Console.WriteLine();
|
|
}
|
|
}
|
|
|
|
if (updatePurchaserList.Count() > 0)
|
|
{
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var update in updatePurchaserList)
|
|
update.ExecuteAffrows();
|
|
});
|
|
}
|
|
}
|
|
|
|
public void SupplementPurchaseSchemeGroup()
|
|
{
|
|
fsql.Transaction(() =>
|
|
{
|
|
fsql.Delete<PurchaseSchemeGroup>().Where(psg => 1 == 1).ExecuteAffrows();
|
|
fsql.Delete<PurchaseSchemeGroupIndex>().Where(psgi => 1 == 1).ExecuteAffrows();
|
|
fsql.Update<PurchaseScheme>().Set(ps => ps.SchemeGroupId == 0).Where(ps => 1 == 1).ExecuteAffrows();
|
|
});
|
|
|
|
List<PurchaseSchemeGroup> insertSchemeGroups = new List<PurchaseSchemeGroup>();
|
|
List<PurchaseSchemeGroupIndex> insertSchemeGroupIndexs = new List<PurchaseSchemeGroupIndex>();
|
|
Queue<IUpdate<PurchaseScheme>> updatePurchaseSchemeQueue = new Queue<IUpdate<PurchaseScheme>>();
|
|
|
|
var purchaseSchemeList = fsql.Select<PurchaseScheme>().OrderBy(ps => ps.Id).ToList();
|
|
var purchaseSchemeSpuGroups = purchaseSchemeList.GroupBy(ps => ps.ProductId);
|
|
foreach (var purchaseSchemeSpuGroup in purchaseSchemeSpuGroups)
|
|
{
|
|
Console.WriteLine($"正在处理spu {purchaseSchemeSpuGroup.Key}");
|
|
var purchaseSchemeSkuGroups = purchaseSchemeSpuGroup.GroupBy(ps => ps.SkuId);
|
|
var maxPsGroupCount = purchaseSchemeSkuGroups.Max(g => g.Count()); //该spu最大的采购分组数
|
|
|
|
var groupIndex = new PurchaseSchemeGroupIndex()
|
|
{
|
|
CreateTime = DateTime.Now,
|
|
Index = maxPsGroupCount,
|
|
ShopId = purchaseSchemeSpuGroup.First().ShopId,
|
|
ProductId = purchaseSchemeSpuGroup.First().ProductId
|
|
};
|
|
insertSchemeGroupIndexs.Add(groupIndex);
|
|
|
|
for (var i = 0; i < maxPsGroupCount; i++)
|
|
{
|
|
Console.WriteLine($"正在处理第{i + 1}组");
|
|
IList<PurchaseScheme> currentIndexSchemeList = new List<PurchaseScheme>();
|
|
//取每个sku的第i个方案
|
|
foreach (var purchaseSchemeSkuGroup in purchaseSchemeSkuGroups)
|
|
{
|
|
var currentSkuPurchaseSchemeList = purchaseSchemeSkuGroup.ToList();
|
|
|
|
if (currentSkuPurchaseSchemeList.Count() >= (i + 1))
|
|
{
|
|
var scheme = currentSkuPurchaseSchemeList[i];
|
|
currentIndexSchemeList.Add(scheme);
|
|
}
|
|
}
|
|
|
|
var schemeGroupId = idGenerator.NewLong();
|
|
var insertSchemeGroup = new PurchaseSchemeGroup()
|
|
{
|
|
Id = schemeGroupId,
|
|
//GroupName = schemeGroupId.Value.ToString(),
|
|
GroupName = $"{purchaseSchemeSpuGroup.Key}-{i + 1}",
|
|
CreateTime = DateTime.Now,
|
|
ProductId = currentIndexSchemeList.First().ProductId,
|
|
ShopId = currentIndexSchemeList.First().ShopId
|
|
};
|
|
insertSchemeGroups.Add(insertSchemeGroup);
|
|
|
|
|
|
foreach (var purchaseScheme in currentIndexSchemeList)
|
|
{
|
|
if (purchaseScheme.SchemeGroupId != schemeGroupId)
|
|
{
|
|
var update = fsql.Update<PurchaseScheme>(purchaseScheme.Id).Set(ps => ps.SchemeGroupId, schemeGroupId);
|
|
updatePurchaseSchemeQueue.Enqueue(update);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
Console.WriteLine("数据库处理中");
|
|
fsql.Transaction(() =>
|
|
{
|
|
if (insertSchemeGroups.Count() > 0)
|
|
fsql.Insert(insertSchemeGroups).ExecuteAffrows();
|
|
if (insertSchemeGroupIndexs.Count() > 0)
|
|
fsql.Insert(insertSchemeGroupIndexs).ExecuteAffrows();
|
|
});
|
|
|
|
var updatePurchaseSchemeList = new List<IUpdate<PurchaseScheme>>();
|
|
while (true)
|
|
{
|
|
if (updatePurchaseSchemeQueue.TryDequeue(out var update))
|
|
updatePurchaseSchemeList.Add(update);
|
|
else
|
|
break;
|
|
if (updatePurchaseSchemeList.Count() == 10)
|
|
{
|
|
Console.WriteLine($"执行数据库 {DateTime.Now}");
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var _update in updatePurchaseSchemeList)
|
|
_update.ExecuteAffrows();
|
|
});
|
|
updatePurchaseSchemeList.Clear();
|
|
}
|
|
}
|
|
if (updatePurchaseSchemeList.Count() > 0)
|
|
{
|
|
Console.WriteLine($"最后执行数据库 {DateTime.Now}");
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var _update in updatePurchaseSchemeList)
|
|
_update.ExecuteAffrows();
|
|
});
|
|
}
|
|
}
|
|
|
|
public void RepairOrderStateToCheckComputation()
|
|
{
|
|
List<TimeLimitTask> insertComputationTaskList = new List<TimeLimitTask>();
|
|
var time = DateTime.Parse("2023-09-01");
|
|
var orderList = fsql.Select<Order>().Where(o => o.StartTime <= time && o.OrderState == Enums.OrderState.待验收).ToList();
|
|
var orderIdsList = orderList.Select(o => o.Id).ToList();
|
|
var checmComputationTaskList = fsql.Select<TimeLimitTask>().Where(t => t.TaskType == Enums.TimeLimitTaskType.待核算任务 && orderIdsList.Contains(t.OrderId)).ToList();
|
|
foreach (var order in orderList)
|
|
{
|
|
if (!checmComputationTaskList.Any(x => x.OrderId == order.Id))
|
|
{
|
|
insertComputationTaskList.Add(new TimeLimitTask()
|
|
{
|
|
Id = idGenerator.NewLong(),
|
|
OrderId = order.Id,
|
|
CreateTme = DateTime.Now,
|
|
//ExpirationTime = DateTime.Now.AddDays(1),
|
|
OrderSn = order.OrderSn,
|
|
Remark = "手工处理待核算",
|
|
ShopId = order.ShopId,
|
|
TaskType = Enums.TimeLimitTaskType.待核算任务,
|
|
ExpirationTime = timeLimitRules.CalculateExpirationTime(Enums.TimeLimitTaskType.待核算任务, DateTime.Now)
|
|
});
|
|
}
|
|
}
|
|
|
|
fsql.Transaction(() =>
|
|
{
|
|
if (insertComputationTaskList.Count() > 0)
|
|
fsql.Insert(insertComputationTaskList).ExecuteAffrows();
|
|
fsql.Update<Order>().Set(o => o.OrderState, Enums.OrderState.待核算)
|
|
.Where(o => o.StartTime <= time && o.OrderState == Enums.OrderState.待验收)
|
|
.ExecuteAffrows();
|
|
});
|
|
}
|
|
|
|
public void SyncCCancelOrder()
|
|
{
|
|
var c_cancel_orderlist = freeSqlMultiDBManager.BBWYCfsql.Select<Model.Db.BBWY.PurchaseOrderV2>().Where(p => p.OrderState == Enums.OrderState.已取消).ToList();
|
|
var c_cancel_orderIdlist = c_cancel_orderlist.Select(p => p.Id).ToList();
|
|
if (c_cancel_orderIdlist.Count() > 0)
|
|
{
|
|
fsql.Transaction(() =>
|
|
{
|
|
fsql.Update<Order>(c_cancel_orderIdlist).Set(o => o.OrderState, Enums.OrderState.已取消).ExecuteAffrows();
|
|
});
|
|
}
|
|
|
|
}
|
|
|
|
public void BSyncToC()
|
|
{
|
|
var time = DateTime.Parse("2023-10-29");
|
|
var orderList = fsql.Select<Order>().Where(o => o.StartTime > time).OrderByDescending(o => o.Id).ToList(o => new { o.Id, o.OrderState });
|
|
var count = orderList.Count();
|
|
var i = 1;
|
|
foreach (var order in orderList)
|
|
{
|
|
Thread.Sleep(500);
|
|
try
|
|
{
|
|
var url = "http://bbwytest.qiyue666.com";
|
|
var result_orderState = restApiService.SendRequest(url, "api/BatchPurchase/UpdatePurchaseOrderState", new
|
|
{
|
|
OrderId = order.Id,
|
|
PurchaseOrderState = order.OrderState
|
|
}, null, HttpMethod.Post);
|
|
|
|
|
|
var result_editPrice = restApiService.SendRequest(url,
|
|
"/Api/PurchaseOrder/QuanTanEditPriceCallback",
|
|
new { orderId = order.Id },
|
|
null,
|
|
HttpMethod.Post);
|
|
|
|
Console.WriteLine($"{i}/{count},{order.Id},{result_orderState.StatusCode},{result_editPrice.StatusCode}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
finally
|
|
{
|
|
i++;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SyncHistoryPurchaseScheme()
|
|
{
|
|
var qtShops = venderBusiness.GetShopList(platform: Enums.Platform.拳探);
|
|
foreach (var shop in qtShops)
|
|
{
|
|
var psList = purchaseSchemeBusiness.GetPurchaseSchemeList(new QuerySchemeRequest()
|
|
{
|
|
ShopId = long.Parse(shop.ShopId)
|
|
});
|
|
|
|
List<HistoryPurchaseScheme> insertHistoryPSList = new List<HistoryPurchaseScheme>();
|
|
List<HistoryPurchaseSchemeProduct> insertHistoryPSPList = new List<HistoryPurchaseSchemeProduct>();
|
|
List<HistoryPurchaseSchemeProductSku> insertHistoryPSSList = new List<HistoryPurchaseSchemeProductSku>();
|
|
|
|
|
|
foreach (var ps in psList)
|
|
{
|
|
//var newVersion = ps.Version + 1;
|
|
var newVersion = ps.Version;
|
|
foreach (var psp in ps.PurchaseSchemeProductList)
|
|
{
|
|
foreach (var pss in psp.PurchaseSchemeProductSkuList)
|
|
{
|
|
#region 处理历史版本
|
|
var historyPss = pss.Map<HistoryPurchaseSchemeProductSku>();
|
|
historyPss.HistoryId = idGenerator.NewLong();
|
|
historyPss.Version = newVersion;
|
|
insertHistoryPSSList.Add(historyPss);
|
|
#endregion
|
|
}
|
|
#region 处理历史版本
|
|
var historyPsp = psp.Map<HistoryPurchaseSchemeProduct>();
|
|
historyPsp.HistoryId = idGenerator.NewLong();
|
|
historyPsp.Version = newVersion;
|
|
insertHistoryPSPList.Add(historyPsp);
|
|
#endregion
|
|
}
|
|
|
|
#region 处理历史版本
|
|
var historyPs = ps.Map<HistoryPurchaseScheme>();
|
|
historyPs.HistoryId = idGenerator.NewLong();
|
|
historyPs.Version = newVersion;
|
|
insertHistoryPSList.Add(historyPs);
|
|
|
|
//updatePurchaseSchemeIdList.Add(ps.Id);
|
|
#endregion
|
|
}
|
|
|
|
|
|
fsql.Transaction(() =>
|
|
{
|
|
if (insertHistoryPSList.Count() > 0)
|
|
fsql.Insert(insertHistoryPSList).ExecuteAffrows();
|
|
if (insertHistoryPSPList.Count() > 0)
|
|
fsql.Insert(insertHistoryPSPList).ExecuteAffrows();
|
|
if (insertHistoryPSSList.Count() > 0)
|
|
fsql.Insert(insertHistoryPSSList).ExecuteAffrows();
|
|
//if (updatePurchaseSchemeIdList.Count() > 0)
|
|
// fsql.Update<PurchaseScheme>(updatePurchaseSchemeIdList).Set(ps => ps.Version + 1).ExecuteAffrows();
|
|
});
|
|
}
|
|
}
|
|
|
|
public void BatchCompleteOrder()
|
|
{
|
|
var time = DateTime.Parse("2023-09-01");
|
|
//var orderStateList = new List<Enums.OrderState?>() { Enums.OrderState.待付款, Enums.OrderState.已取消, Enums.OrderState.已完成 };
|
|
//var orderList = fsql.Select<Order>().Where(o => o.StartTime < time && !orderStateList.Contains(o.OrderState)).ToList();
|
|
|
|
var orderList = fsql.Select<Order>().Where(o => o.StartTime < time && o.OrderState == Enums.OrderState.待付款).ToList();
|
|
var shops = venderBusiness.GetShopList(platform: Enums.Platform.拳探);
|
|
var count = orderList.Count();
|
|
var i = 1;
|
|
|
|
Queue<IUpdate<Order>> updateOrderQueue = new Queue<IUpdate<Order>>();
|
|
List<IUpdate<Order>> updateOrderList = new List<IUpdate<Order>>();
|
|
foreach (var order in orderList)
|
|
{
|
|
Console.WriteLine($"{i}/{count},{order.Id}");
|
|
try
|
|
{
|
|
var shop = shops.FirstOrDefault(s => s.ShopId == order.ShopId.ToString());
|
|
if (shop == null)
|
|
{
|
|
i++;
|
|
continue;
|
|
}
|
|
var qtOrderList = opPlatformClientFactory.GetClient(AdapterEnums.PlatformType.拳探).GetOrderList(new OP_QueryOrderRequest()
|
|
{
|
|
AppKey = shop.AppKey,
|
|
AppSecret = shop.AppSecret,
|
|
AppToken = shop.AppToken,
|
|
OrderId = order.Id,
|
|
PageIndex = 1,
|
|
PageSize = 100,
|
|
Platform = AdapterEnums.PlatformType.拳探,
|
|
SortTimeField = AdapterEnums.SortTimeField.Modify,
|
|
SortType = AdapterEnums.SortType.Desc,
|
|
StartDate = null,
|
|
EndDate = null
|
|
});
|
|
if (qtOrderList.Count == 0)
|
|
{
|
|
var update = fsql.Update<Order>(order.Id).Set(o => o.OrderState, Enums.OrderState.已取消);
|
|
updateOrderQueue.Enqueue(update);
|
|
|
|
i++;
|
|
continue;
|
|
}
|
|
|
|
var qtOrder = qtOrderList.Items[0];
|
|
if (qtOrder.OrderState != "-1" && qtOrder.IsPay)
|
|
{
|
|
var update = fsql.Update<Order>(order.Id).Set(o => o.OrderState, Enums.OrderState.已完成);
|
|
updateOrderQueue.Enqueue(update);
|
|
}
|
|
|
|
if ((qtOrder.OrderState == "-1" || !qtOrder.IsPay) && order.OrderState != Enums.OrderState.已取消)
|
|
{
|
|
var update = fsql.Update<Order>(order.Id).Set(o => o.OrderState, Enums.OrderState.已取消);
|
|
updateOrderQueue.Enqueue(update);
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
|
|
}
|
|
finally
|
|
{
|
|
i++;
|
|
}
|
|
}
|
|
|
|
if (updateOrderQueue.Count() > 0)
|
|
{
|
|
while (true)
|
|
{
|
|
if (updateOrderQueue.TryDequeue(out var update))
|
|
updateOrderList.Add(update);
|
|
else
|
|
break;
|
|
|
|
if (updateOrderList.Count() == 10)
|
|
{
|
|
Console.Write($"执行数据库 {DateTime.Now}");
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var u in updateOrderList)
|
|
{
|
|
u.ExecuteAffrows();
|
|
}
|
|
});
|
|
updateOrderList.Clear();
|
|
}
|
|
}
|
|
|
|
if (updateOrderList.Count() > 0)
|
|
{
|
|
Console.Write($"最后执行数据库 {DateTime.Now}");
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var u in updateOrderList)
|
|
{
|
|
u.ExecuteAffrows();
|
|
}
|
|
});
|
|
updateOrderList.Clear();
|
|
}
|
|
}
|
|
}
|
|
|
|
public void RepairHistoryItemCount()
|
|
{
|
|
List<SpuTotalSaleInfo> insertSpuSaleInfoList = new List<SpuTotalSaleInfo>();
|
|
List<SkuTotalSaleInfo> insertSkuSaleInfoList = new List<SkuTotalSaleInfo>();
|
|
List<PurchaseScheme> updatePurchaseSchemeList = new List<PurchaseScheme>();
|
|
List<Purchaser> updatePurchaserList = new List<Purchaser>();
|
|
List<SkuHistoryPurchaserRelation> insertSkuHistoryPurchaserRelationList = new List<SkuHistoryPurchaserRelation>();
|
|
var noOrderStateList = new List<Enums.OrderState?>()
|
|
{
|
|
Enums.OrderState.已取消,
|
|
Enums.OrderState.待付款
|
|
};
|
|
|
|
#region 补销量
|
|
{
|
|
var spuSaleGroups = fsql.Select<OrderSku, Order>()
|
|
.InnerJoin((osku, o) => osku.OrderId == o.Id)
|
|
.Where((osku, o) => !noOrderStateList.Contains(o.OrderState) && o.ShopId != 11)
|
|
.GroupBy((osku, o) => new { osku.ProductId, o.ShopId })
|
|
.ToList(g => new
|
|
{
|
|
g.Key.ProductId,
|
|
g.Key.ShopId,
|
|
ItemCount = g.Sum(g.Value.Item1.ItemTotal)
|
|
});
|
|
|
|
var skuSaleGroups = fsql.Select<OrderSku, Order>()
|
|
.InnerJoin((osku, o) => osku.OrderId == o.Id)
|
|
.Where((osku, o) => !noOrderStateList.Contains(o.OrderState) && o.ShopId != 11)
|
|
.GroupBy((osku, o) => new { osku.ProductId, osku.SkuId, o.ShopId })
|
|
.ToList(g => new
|
|
{
|
|
g.Key.ProductId,
|
|
g.Key.SkuId,
|
|
g.Key.ShopId,
|
|
ItemCount = g.Sum(g.Value.Item1.ItemTotal)
|
|
});
|
|
|
|
foreach (var spuSale in spuSaleGroups)
|
|
{
|
|
var itemCount = Convert.ToInt64(spuSale.ItemCount);
|
|
var insertSpuSale = new SpuTotalSaleInfo()
|
|
{
|
|
ProductId = spuSale.ProductId,
|
|
ShopId = spuSale.ShopId,
|
|
CreateTime = DateTime.Now,
|
|
IsEnabled = true,
|
|
ItemCount = itemCount,
|
|
UpdateTime = DateTime.Now,
|
|
LastOptimizationItemCount = itemCount,
|
|
LastOptimizationTime = DateTime.Now,
|
|
FirstOptimizationCompletedItemCount = itemCount,
|
|
FirstPurchaseCompletedItemCount = 0,
|
|
IsFirstOptimizationCompleted = true,
|
|
IsFirstPurchaseCompleted = true
|
|
};
|
|
insertSpuSaleInfoList.Add(insertSpuSale);
|
|
}
|
|
|
|
foreach (var skuSale in skuSaleGroups)
|
|
{
|
|
var insertSkuSale = new SkuTotalSaleInfo()
|
|
{
|
|
ProductId = skuSale.ProductId,
|
|
SkuId = skuSale.SkuId,
|
|
ShopId = skuSale.ShopId,
|
|
CreateTime = DateTime.Now,
|
|
IsEnabled = true,
|
|
ItemCount = Convert.ToInt64(skuSale.ItemCount),
|
|
UpdateTime = DateTime.Now
|
|
};
|
|
insertSkuSaleInfoList.Add(insertSkuSale);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 采购方案累计信息
|
|
|
|
#region 采购件数
|
|
/*
|
|
select opi.SchemeId,sum(osku.ItemTotal) from orderpurchaserelationinfo opi
|
|
inner join `order` o on opi.OrderId=o.Id
|
|
inner join ordersku osku on osku.OrderId=opi.OrderId and osku.SkuId=opi.BelongSkuId
|
|
where o.OrderState not in (0,6) and o.ShopId!=11
|
|
group by opi.SchemeId;
|
|
*/
|
|
|
|
{
|
|
var psItemCountGroups = fsql.Select<OrderPurchaseRelationInfo, Order, OrderSku>()
|
|
.InnerJoin((opi, o, osku) => opi.OrderId == o.Id)
|
|
.InnerJoin((opi, o, osku) => osku.OrderId == opi.OrderId && osku.SkuId == opi.BelongSkuId)
|
|
.Where((opi, o, osku) => !noOrderStateList.Contains(o.OrderState) && o.ShopId != 11)
|
|
.GroupBy((opi, o, osku) => opi.SchemeId)
|
|
.ToList(g => new
|
|
{
|
|
SchemeId = g.Key,
|
|
ItemTotal = g.Sum(g.Value.Item3.ItemTotal)
|
|
});
|
|
foreach (var psItemCount in psItemCountGroups)
|
|
{
|
|
updatePurchaseSchemeList.Add(new PurchaseScheme()
|
|
{
|
|
Id = psItemCount.SchemeId.Value,
|
|
PurchasedItemCount = Convert.ToInt32(psItemCount.ItemTotal),
|
|
PurchasedAmount = 0M
|
|
});
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 采购金额
|
|
/*
|
|
select opi.SchemeId,sum(osc.SkuAmount) from orderpurchaserelationinfo opi
|
|
inner join `order` o on opi.OrderId=o.Id
|
|
inner join orderSkuCost osc on osc.OrderId=opi.OrderId and osc.SkuId = opi.BelongSkuId
|
|
where o.OrderState not in (0,6) and o.ShopId!=11
|
|
group by opi.SchemeId;
|
|
*/
|
|
{
|
|
var psAmountGroups = fsql.Select<OrderPurchaseRelationInfo, Order, OrderSkuCost>()
|
|
.InnerJoin((opi, o, osc) => opi.OrderId == o.Id)
|
|
.InnerJoin((opi, o, osc) => osc.OrderId == opi.OrderId && osc.SkuId == opi.BelongSkuId)
|
|
.Where((opi, o, osc) => !noOrderStateList.Contains(o.OrderState) && o.ShopId != 11)
|
|
.GroupBy((opi, o, osc) => opi.SchemeId)
|
|
.ToList(g => new
|
|
{
|
|
SchemeId = g.Key,
|
|
Amount = g.Sum(g.Value.Item3.SkuAmount)
|
|
});
|
|
foreach (var psAmount in psAmountGroups)
|
|
{
|
|
var ps = updatePurchaseSchemeList.FirstOrDefault(x => x.Id == psAmount.SchemeId);
|
|
if (ps == null)
|
|
{
|
|
ps = new PurchaseScheme() { Id = psAmount.SchemeId.Value, PurchasedItemCount = 0 };
|
|
updatePurchaseSchemeList.Add(ps);
|
|
}
|
|
ps.PurchasedAmount = psAmount.Amount;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#endregion
|
|
|
|
//#region 采购商累计信息
|
|
//{
|
|
// #region 采购SKU数
|
|
// var purchaserPurchasedList = fsql.Select<OrderPurchaseRelationInfo, Order, OrderPurchaseInfo>()
|
|
// .InnerJoin((opri, o, opi) => opri.OrderId == o.Id)
|
|
// .InnerJoin((opri, o, opi) => opi.OrderId == opri.OrderId && opi.IsEnabled == true)
|
|
// .Where((opri, o, opi) => !noOrderStateList.Contains(o.OrderState) && o.ShopId != 11 && !string.IsNullOrEmpty(opi.PurchaserId))
|
|
// .GroupBy((opri, o, opi) => new { o.ShopId, opi.PurchaserId, opri.BelongSkuId })
|
|
// .ToList(g => new
|
|
// {
|
|
// g.Key.ShopId,
|
|
// g.Key.PurchaserId,
|
|
// g.Key.BelongSkuId
|
|
// });
|
|
|
|
// var purchaserPurchasedGroups = purchaserPurchasedList.GroupBy(x => x.PurchaserId);
|
|
// foreach (var g in purchaserPurchasedGroups)
|
|
// {
|
|
// updatePurchaserList.Add(new Purchaser()
|
|
// {
|
|
// Id = g.Key,
|
|
// PurchasedSkuCount = g.Count(),
|
|
// PurchasedCount = 0
|
|
// });
|
|
// insertSkuHistoryPurchaserRelationList.AddRange(g.Select(x => new SkuHistoryPurchaserRelation()
|
|
// {
|
|
// CreateTime = DateTime.Now,
|
|
// Id = idGenerator.NewLong(),
|
|
// PurchaserId = x.PurchaserId,
|
|
// ShopId = x.ShopId,
|
|
// SkuId = x.BelongSkuId
|
|
// }));
|
|
// }
|
|
// #endregion
|
|
|
|
// #region 采购次数
|
|
// /*
|
|
// select opi.PurchaserId,count(DISTINCT(opi.OrderId)) from orderpurchaserelationinfo opri
|
|
// inner join `order` o on opri.OrderId=o.Id
|
|
// inner join orderpurchaseinfo opi on opi.OrderId=opri.OrderId and opi.IsEnabled=1
|
|
// where o.OrderState not in (0,6) and o.ShopId !=11 and opi.PurchaserId is not null
|
|
// group by opi.PurchaserId;
|
|
// */
|
|
// var purchaserPurchasedCountList = fsql.Select<OrderPurchaseRelationInfo, Order, OrderPurchaseInfo>()
|
|
// .InnerJoin((opri, o, opi) => opri.OrderId == o.Id)
|
|
// .InnerJoin((opri, o, opi) => opi.OrderId == opri.OrderId && opi.IsEnabled == true)
|
|
// .Where((opri, o, opi) => !noOrderStateList.Contains(o.OrderState) && o.ShopId != 11 && !string.IsNullOrEmpty(opi.PurchaserId))
|
|
// .GroupBy((opri, o, opi) => opi.PurchaserId)
|
|
// .ToList(g => new
|
|
// {
|
|
// PurchaserId = g.Key,
|
|
// //Count = g.Count(g.Value.Item3.OrderId.Distinct())
|
|
// Count = SqlExt.DistinctCount(g.Value.Item3.OrderId)
|
|
// });
|
|
|
|
// foreach (var pcount in purchaserPurchasedCountList)
|
|
// {
|
|
// var p = updatePurchaserList.FirstOrDefault(x => x.Id == pcount.PurchaserId);
|
|
// if (p == null)
|
|
// {
|
|
// p = new Purchaser() { Id = pcount.PurchaserId, PurchasedSkuCount = 0 };
|
|
// updatePurchaserList.Add(p);
|
|
// }
|
|
// p.PurchasedCount = Convert.ToInt32(pcount.Count);
|
|
// }
|
|
|
|
// //var purchaserPurchasedCountDataTable = fsql.Ado.ExecuteDataTable(@"select opi.PurchaserId,count(DISTINCT(opi.OrderId)) from orderpurchaserelationinfo opri
|
|
// // inner join `order` o on opri.OrderId = o.Id
|
|
// // inner join orderpurchaseinfo opi on opi.OrderId = opri.OrderId and opi.IsEnabled = 1
|
|
// // where o.OrderState not in (0, 6) and o.ShopId != 11 and opi.PurchaserId is not null
|
|
// // group by opi.PurchaserId;");
|
|
|
|
// //foreach (DataRow dataRow in purchaserPurchasedCountDataTable.Rows)
|
|
// //{
|
|
// // var purchaserId = dataRow[0].ToString();
|
|
// // var count = dataRow[1].ToInt32() ?? 0;
|
|
|
|
// // var p = updatePurchaserList.FirstOrDefault(x => x.Id == purchaserId);
|
|
// // if (p == null)
|
|
// // {
|
|
// // p = new Purchaser() { Id = purchaserId, PurchasedSkuCount = 0 };
|
|
// // updatePurchaserList.Add(p);
|
|
// // }
|
|
// // p.PurchasedCount = count;
|
|
|
|
// //}
|
|
// #endregion
|
|
//}
|
|
//#endregion
|
|
|
|
fsql.Transaction(() =>
|
|
{
|
|
fsql.Delete<SpuTotalSaleInfo>().Where(s => 1 == 1).ExecuteAffrows();
|
|
fsql.Delete<SkuTotalSaleInfo>().Where(s => 1 == 1).ExecuteAffrows();
|
|
fsql.Delete<SkuOptimizationHistory>().Where(s => 1 == 1).ExecuteAffrows();
|
|
fsql.Delete<TimeLimitTask>().Where(t => t.TaskType == Enums.TimeLimitTaskType.待议价任务).ExecuteAffrows();
|
|
fsql.Delete<SkuHistoryPurchaserRelation>().Where(s => 1 == 1).ExecuteAffrows();
|
|
fsql.Update<OrderSku>().Set(osku => osku.IsNeedOptimization, 0)
|
|
.Set(osku => osku.IsOptimizationCompleted, 0)
|
|
.Where(osku => osku.IsNeedOptimization == 1)
|
|
.ExecuteAffrows();
|
|
foreach (var updatePs in updatePurchaseSchemeList)
|
|
{
|
|
fsql.Update<PurchaseScheme>(updatePs.Id).Set(ps => ps.PurchasedItemCount, updatePs.PurchasedItemCount)
|
|
.Set(ps => ps.PurchasedAmount, updatePs.PurchasedAmount)
|
|
.ExecuteAffrows();
|
|
}
|
|
//foreach (var updatePurchaser in updatePurchaserList)
|
|
//{
|
|
// fsql.Update<Purchaser>(updatePurchaser.Id).Set(p => p.PurchasedSkuCount, updatePurchaser.PurchasedSkuCount)
|
|
// .Set(p => p.PurchasedCount, updatePurchaser.PurchasedCount)
|
|
// .ExecuteAffrows();
|
|
//}
|
|
fsql.Insert(insertSpuSaleInfoList).ExecuteAffrows();
|
|
fsql.Insert(insertSkuSaleInfoList).ExecuteAffrows();
|
|
fsql.Insert(insertSkuHistoryPurchaserRelationList).ExecuteAffrows();
|
|
});
|
|
}
|
|
|
|
public void RepairPurchaseSkuLastPurchasePriceCost()
|
|
{
|
|
//IDictionary<string, IUpdate<PurchaseSchemeProductSku>> updatePssDictionary = new Dictionary<string, IUpdate<PurchaseSchemeProductSku>>();
|
|
List<string> updatePssKeyList = new List<string>();
|
|
|
|
var oriList = fsql.Select<OrderPurchaseRelationInfo, PurchaseScheme, PurchaseSchemeProduct, Purchaser>()
|
|
.InnerJoin((ori, ps, psp, pr) => ori.SchemeId == ps.Id)
|
|
.InnerJoin((ori, ps, psp, pr) => ps.Id == psp.SkuPurchaseSchemeId)
|
|
.InnerJoin((ori, ps, psp, pr) => pr.Id == psp.PurchaserId)
|
|
.Where((ori, ps, psp, pr) => pr.Platform == Enums.Platform.阿里巴巴 && fsql.Select<PurchaseSchemeProductSku>().Where(pss => pss.PurchaseSkuId == ori.PurchaseSkuId && pss.LastPurchasePriceCost == null).Any())
|
|
.OrderByDescending((ori, ps, psp, pr) => ori.CreateTime)
|
|
.ToList((ori, ps, psp, pr) => new
|
|
{
|
|
ori.OrderId,
|
|
ori.PurchaseOrderId,
|
|
ori.PurchaseSkuId,
|
|
ori.SchemeId,
|
|
ps.ShopId
|
|
});
|
|
|
|
|
|
var purchaseAccountList = freeSqlMultiDBManager.MDSfsql.Select<Model.Db.MDS.Purchaseaccount, Model.Db.Mds.Shops>()
|
|
.InnerJoin((pa, s) => pa.ShopId == s.Id)
|
|
.Where((pa, s) => s.PlatformId == 10 && pa.PurchasePlatformId == "2")
|
|
.ToList((pa, s) => new
|
|
{
|
|
s.ShopId,
|
|
pa.AppKey,
|
|
pa.AppSecret,
|
|
pa.AppToken
|
|
});
|
|
|
|
var client = pp_PlatformClientFactory.GetClient(AdapterEnums.PlatformType.阿里巴巴);
|
|
|
|
var puchaseOrderGroups = oriList.GroupBy(ori => ori.PurchaseOrderId);
|
|
var count = puchaseOrderGroups.Count();
|
|
var poIndex = 0;
|
|
foreach (var poGroup in puchaseOrderGroups)
|
|
{
|
|
var poId = poGroup.Key;
|
|
poIndex++;
|
|
Console.WriteLine($"查询采购单 {poId}, {poIndex}/{puchaseOrderGroups.Count()}");
|
|
try
|
|
{
|
|
var shopId = poGroup.FirstOrDefault().ShopId.ToString();
|
|
var purchaseAccount = purchaseAccountList.FirstOrDefault(s => s.ShopId == shopId);
|
|
Thread.Sleep(1500);
|
|
var purchaseOrderSimpleInfo = client.QueryOrderDetail(new PP_QueryOrderDetailRequest()
|
|
{
|
|
AppKey = purchaseAccount.AppKey,
|
|
AppSecret = purchaseAccount.AppSecret,
|
|
AppToken = purchaseAccount.AppToken,
|
|
OrderId = poId
|
|
});
|
|
|
|
foreach (var ori in poGroup)
|
|
{
|
|
var key = $"{ori.PurchaseSkuId}_{ori.SchemeId}";
|
|
if (updatePssKeyList.Contains(key))
|
|
continue;
|
|
|
|
var purchaseSku = purchaseOrderSimpleInfo.ItemList.FirstOrDefault(x => x.SkuId == ori.PurchaseSkuId);
|
|
if (purchaseSku != null)
|
|
{
|
|
Console.WriteLine($"更新 {key}");
|
|
var update = fsql.Update<PurchaseSchemeProductSku>().Set(pss => pss.LastPurchasePriceCost, purchaseSku.Price)
|
|
.Where(pss => pss.PurchaseSkuId == ori.PurchaseSkuId && pss.SkuPurchaseSchemeId == ori.SchemeId);
|
|
update.ExecuteAffrows();
|
|
updatePssKeyList.Add(key);
|
|
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"{poId},{ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
|
|
public void RepairPurchaseSchemeLastPurchasePrice()
|
|
{
|
|
/*
|
|
select opi.SchemeId,opi.PurchaseOrderId from orderpurchaserelationinfo opi
|
|
inner join (select max(opi1.OrderId) as maxOrderId,opi1.SchemeId from orderpurchaserelationinfo opi1 group by opi1.SchemeId) as temp
|
|
on opi.OrderId=temp.maxOrderId and opi.SchemeId=temp.SchemeId
|
|
group by opi.SchemeId,opi.PurchaseOrderId
|
|
*/
|
|
|
|
var opiList = fsql.Select<OrderPurchaseRelationInfo>().ToList();
|
|
var pssList = fsql.Select<PurchaseSchemeProductSku>().ToList();
|
|
var poList = fsql.Select<OrderPurchaseInfo>().ToList();
|
|
|
|
var purchaseAccountList = freeSqlMultiDBManager.MDSfsql.Select<Model.Db.MDS.Purchaseaccount, Model.Db.Mds.Shops>()
|
|
.InnerJoin((pa, s) => pa.ShopId == s.Id)
|
|
.Where((pa, s) => s.PlatformId == 10 && pa.PurchasePlatformId == "2")
|
|
.ToList((pa, s) => new
|
|
{
|
|
s.ShopId,
|
|
pa.AppKey,
|
|
pa.AppSecret,
|
|
pa.AppToken
|
|
});
|
|
|
|
var schemeGroups = fsql.Select<OrderPurchaseRelationInfo>()
|
|
.GroupBy(opi1 => opi1.SchemeId)
|
|
.WithTempQuery(g => new { MaxOrderId = g.Max(g.Value.OrderId), SchemeId = g.Value.SchemeId })
|
|
.From<OrderPurchaseRelationInfo>()
|
|
.InnerJoin((opi1, opi2) => opi1.MaxOrderId == opi2.OrderId && opi1.SchemeId == opi2.SchemeId)
|
|
.GroupBy((opi1, opi2) => new { opi2.SchemeId, opi2.PurchaseOrderId })
|
|
.ToList(g => new
|
|
{
|
|
g.Value.Item2.SchemeId,
|
|
g.Value.Item2.PurchaseOrderId
|
|
}).GroupBy(x => x.SchemeId);
|
|
|
|
var client = pp_PlatformClientFactory.GetClient(AdapterEnums.PlatformType.阿里巴巴);
|
|
|
|
IDictionary<string, IList<PP_QueryOrderDetailSkuResponse>> purchaseOrderDictionary = new Dictionary<string, IList<PP_QueryOrderDetailSkuResponse>>();
|
|
var index = 1;
|
|
foreach (var schemeGroup in schemeGroups)
|
|
{
|
|
var schemeId = schemeGroup.Key;
|
|
var lastPurchasePriceCost = 0M;
|
|
foreach (var schemeItem in schemeGroup)
|
|
{
|
|
Thread.Sleep(1500);
|
|
try
|
|
{
|
|
var poId = schemeItem.PurchaseOrderId;
|
|
var currentOpiList = opiList.Where(x => x.SchemeId == schemeId && x.PurchaseOrderId == poId).ToList();
|
|
if (!purchaseOrderDictionary.TryGetValue(poId, out var _1688itemList))
|
|
{
|
|
var po = poList.FirstOrDefault(x => x.PurchaseOrderId == poId);
|
|
if (po == null)
|
|
continue;
|
|
var shopId = po.ShopId.ToString();
|
|
var pa = purchaseAccountList.FirstOrDefault(x => x.ShopId == shopId);
|
|
|
|
var purchaseOrderSimpleInfo = client.QueryOrderDetail(new PP_QueryOrderDetailRequest()
|
|
{
|
|
AppKey = pa.AppKey,
|
|
AppSecret = pa.AppSecret,
|
|
AppToken = pa.AppToken,
|
|
OrderId = poId
|
|
});
|
|
_1688itemList = purchaseOrderSimpleInfo.ItemList;
|
|
purchaseOrderDictionary.TryAdd(poId, _1688itemList);
|
|
}
|
|
foreach (var ppsku in _1688itemList)
|
|
{
|
|
if (currentOpiList.Any(x => x.PurchaseSkuId == ppsku.SkuId))
|
|
{
|
|
var pss = pssList.FirstOrDefault(x => x.SkuPurchaseSchemeId == schemeId && x.PurchaseSkuId == ppsku.SkuId);
|
|
if (pss != null)
|
|
{
|
|
lastPurchasePriceCost += ppsku.Price * (pss.PurchaseRatio ?? 1);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"{schemeItem.PurchaseOrderId},{ex.Message}");
|
|
}
|
|
}
|
|
fsql.Update<PurchaseScheme>(schemeId).Set(ps => ps.LastPurchasePriceCost, lastPurchasePriceCost).ExecuteAffrows();
|
|
Console.WriteLine($"更新进度 {index}/{schemeGroups.Count()}");
|
|
index++;
|
|
}
|
|
Console.WriteLine(schemeGroups.Count());
|
|
}
|
|
|
|
public void SyncProductCategory()
|
|
{
|
|
List<PurchaserExtendedInfo> insertList = new List<PurchaserExtendedInfo>();
|
|
List<long> deletePurchaserExtendedIdList = new List<long>();
|
|
|
|
var dbCategoryList = fsql.Select<PurchaserExtendedInfo>()
|
|
.Where(x => x.Type == Enums.PurchaserBasicInfoType.主营类目)
|
|
.ToList();
|
|
|
|
var client = opPlatformClientFactory.GetClient(AdapterEnums.PlatformType.拳探);
|
|
var request = new OP_QueryProductCategoryRequest()
|
|
{
|
|
PageIndex = 1,
|
|
PageSize = 500,
|
|
AppKey = "BBWYB2023022501",
|
|
AppSecret = "3a1521deccc6819d61788a0d6baae49e9",
|
|
AppToken = "9",
|
|
Level = 0
|
|
};
|
|
|
|
var L1_qtCategoryList = new List<OP_ProductCategoryResponse>();
|
|
var L1_response = client.GetProductCategoryList(request);
|
|
if (L1_response.Items != null)
|
|
L1_qtCategoryList.AddRange(L1_response.Items);
|
|
|
|
request.Level = 1;
|
|
var L2_qtCategoryList = new List<OP_ProductCategoryResponse>();
|
|
var L2_response = client.GetProductCategoryList(request);
|
|
if (L2_response.Items != null)
|
|
L2_qtCategoryList.AddRange(L2_response.Items);
|
|
|
|
request.Level = 2;
|
|
var L3_qtCategoryList = new List<OP_ProductCategoryResponse>();
|
|
var L3_response = client.GetProductCategoryList(request);
|
|
if (L3_response.Items != null)
|
|
L3_qtCategoryList.AddRange(L3_response.Items);
|
|
|
|
foreach (var l3Category in L3_qtCategoryList)
|
|
{
|
|
var parentPathIdArray = l3Category.CategoryPath.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
|
if (parentPathIdArray.Length != 3)
|
|
continue;
|
|
var l2Category = L2_qtCategoryList.FirstOrDefault(x => x.CategoryId == parentPathIdArray[1]);
|
|
if (l2Category == null)
|
|
continue;
|
|
var l1Category = L1_qtCategoryList.FirstOrDefault(x => x.CategoryId == parentPathIdArray[0]);
|
|
if (l1Category == null)
|
|
continue;
|
|
|
|
l3Category.CategoryPath = $"{l1Category.CategoryName},{l2Category.CategoryName}";
|
|
}
|
|
|
|
|
|
//找出新增的类目
|
|
//var newCategoryList = qtCategoryList.Where(c => !dbCategoryList.Any(dc => dc.Id == long.Parse(c.CategoryId))).ToList();
|
|
//if (newCategoryList.Count() > 0)
|
|
//{
|
|
insertList.AddRange(L3_qtCategoryList.Select(c => new PurchaserExtendedInfo()
|
|
{
|
|
Id = long.Parse(c.CategoryId),
|
|
Name = c.CategoryName,
|
|
CreateTime = DateTime.Now,
|
|
Type = Enums.PurchaserBasicInfoType.主营类目,
|
|
ExtendInfo = c.CategoryPath
|
|
}).ToList());
|
|
//}
|
|
|
|
|
|
fsql.Transaction(() =>
|
|
{
|
|
fsql.Delete<PurchaserExtendedInfo>().Where(x => x.Type == Enums.PurchaserBasicInfoType.主营类目).ExecuteAffrows();
|
|
fsql.Insert(insertList).ExecuteAffrows();
|
|
});
|
|
|
|
|
|
//找出接口丢失的类目
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 同步采购商名字
|
|
/// </summary>
|
|
public void SyncPurchaserName()
|
|
{
|
|
List<IUpdate<Purchaser>> updatePurchaserList = new List<IUpdate<Purchaser>>();
|
|
|
|
var pspList = fsql.Select<PurchaseSchemeProduct, Purchaser>()
|
|
.InnerJoin((psp2, p) => psp2.PurchaserId == p.Id)
|
|
.Where((psp2, p) => string.IsNullOrEmpty(p.Location))
|
|
.GroupBy((psp2, p) => psp2.PurchaserId)
|
|
.WithTempQuery(g => new { MaxId = g.Max(g.Value.Item1.Id), PurchaserId = g.Key })
|
|
.From<PurchaseSchemeProduct>()
|
|
.InnerJoin((psp2, psp1) => psp2.MaxId == psp1.Id)
|
|
.ToList((psp2, psp1) => psp1);
|
|
|
|
//var pspList = fsql.Select<PurchaseSchemeProduct>(432883091529819).ToList();
|
|
var purchaserIdList = pspList.Select(x => x.PurchaserId).Distinct().ToList();
|
|
var dbPurchaserList = fsql.Select<Purchaser>(purchaserIdList).ToList();
|
|
|
|
var index = 0;
|
|
foreach (var psp in pspList)
|
|
{
|
|
index++;
|
|
Console.WriteLine($"Scaning PurchaserProduct {psp.PurchaseProductId},PurchaserId {psp.PurchaserId},{index}/{pspList.Count()}");
|
|
Thread.Sleep(1000);
|
|
var data = purchaseSchemeBusiness.GetPurchaseSkuBasicInfo(new PurchaseSkuBasicInfoRequest()
|
|
{
|
|
FirstApiMode = Enums.PurchaseProductAPIMode.Spider,
|
|
PriceMode = Enums.PurchaseOrderMode.批发,
|
|
Platform = Enums.Platform.阿里巴巴,
|
|
PurchaseProductId = psp.PurchaseProductId,
|
|
});
|
|
if (data == null || data.IsInvalid == true || data.Purchaser == null)
|
|
continue;
|
|
var dbPurchaser = dbPurchaserList.FirstOrDefault(p => p.Id == data.Purchaser.Id);
|
|
if (dbPurchaser == null)
|
|
continue;
|
|
if (dbPurchaser.Name != data.Purchaser.Name ||
|
|
dbPurchaser.Id2 != data.Purchaser.Id2 ||
|
|
dbPurchaser.MemberId != data.Purchaser.MemberId ||
|
|
dbPurchaser.Location != data.Purchaser.Location ||
|
|
dbPurchaser.Tag != data.Purchaser.Tag)
|
|
{
|
|
var update = fsql.Update<Purchaser>(dbPurchaser.Id)
|
|
.SetIf(dbPurchaser.Name != data.Purchaser.Name, p => p.Name, data.Purchaser.Name)
|
|
.SetIf(dbPurchaser.Id2 != data.Purchaser.Id2, p => p.Id2, data.Purchaser.Id2)
|
|
.SetIf(dbPurchaser.MemberId != data.Purchaser.MemberId, p => p.MemberId, data.Purchaser.MemberId)
|
|
.SetIf(dbPurchaser.Location != data.Purchaser.Location, p => p.Location, data.Purchaser.Location)
|
|
.SetIf(dbPurchaser.Tag != data.Purchaser.Tag, p => p.Tag, data.Purchaser.Tag);
|
|
updatePurchaserList.Add(update);
|
|
}
|
|
}
|
|
|
|
List<IUpdate<Purchaser>> tempUpdateList = new List<IUpdate<Purchaser>>();
|
|
for (var i = 0; i < updatePurchaserList.Count(); i++)
|
|
{
|
|
tempUpdateList.Add(updatePurchaserList[i]);
|
|
if (tempUpdateList.Count() == 10)
|
|
{
|
|
Console.WriteLine($"{DateTime.Now},{i}/{updatePurchaserList.Count()},执行数据库更新");
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var update in tempUpdateList)
|
|
update.ExecuteAffrows();
|
|
});
|
|
tempUpdateList.Clear();
|
|
}
|
|
}
|
|
|
|
if (tempUpdateList.Count() > 0)
|
|
{
|
|
Console.WriteLine($"{DateTime.Now},最后执行数据库更新");
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var update in tempUpdateList)
|
|
update.ExecuteAffrows();
|
|
});
|
|
}
|
|
}
|
|
|
|
public void SyncPurchaserCategory()
|
|
{
|
|
var purchaserIdList = fsql.Select<Purchaser>()
|
|
.Where(p => !fsql.Select<Purchaser_ExtendedInfo_Relation>().Where(per => per.ExtendedType == Enums.PurchaserBasicInfoType.主营类目 &&
|
|
per.PurchaserId == p.Id).Any())
|
|
.ToList(p => p.Id);
|
|
if (purchaserIdList.Count() == 0)
|
|
return;
|
|
|
|
var pspList = fsql.Select<PurchaseSchemeProduct, Product>()
|
|
.InnerJoin((psp, p) => psp.ProductId == p.Id)
|
|
.Where((psp, p) => p.CategoryId != null && purchaserIdList.Contains(psp.PurchaserId))
|
|
.ToList((psp, p) => new
|
|
{
|
|
psp.PurchaserId,
|
|
psp.ProductId,
|
|
p.CategoryId
|
|
});
|
|
|
|
if (pspList.Count() == 0)
|
|
return;
|
|
|
|
List<Purchaser_ExtendedInfo_Relation> insertList = new List<Purchaser_ExtendedInfo_Relation>();
|
|
|
|
foreach (var purchaserId in purchaserIdList)
|
|
{
|
|
var currentPspList = pspList.Where(x => x.PurchaserId == purchaserId).ToList();
|
|
if (currentPspList.Count() > 0)
|
|
{
|
|
var categoryList = currentPspList.GroupBy(x => x.CategoryId).Select(g => new Purchaser_ExtendedInfo_Relation()
|
|
{
|
|
Id = idGenerator.NewLong(),
|
|
ExtendedInfoId = g.Key,
|
|
ExtendedType = Enums.PurchaserBasicInfoType.主营类目,
|
|
PurchaserId = purchaserId
|
|
}).ToList();
|
|
insertList.AddRange(categoryList);
|
|
Console.WriteLine($"insert count {insertList.Count()}");
|
|
}
|
|
}
|
|
|
|
if (insertList.Count() > 0)
|
|
{
|
|
fsql.Transaction(() =>
|
|
{
|
|
fsql.Insert(insertList).ExecuteAffrows();
|
|
});
|
|
}
|
|
|
|
}
|
|
|
|
public void RepairPurchaserLocation()
|
|
{
|
|
var purchaserList = fsql.Select<Purchaser>().ToList();
|
|
List<IUpdate<Purchaser>> updateList = new List<IUpdate<Purchaser>>();
|
|
|
|
foreach (var purchaser in purchaserList)
|
|
{
|
|
var location = FormatteLocation(purchaser.Location);
|
|
if (location != purchaser.Location)
|
|
{
|
|
updateList.Add(fsql.Update<Purchaser>(purchaser.Id).Set(p => p.Location, location));
|
|
}
|
|
}
|
|
List<IUpdate<Purchaser>> tempUpdateList = new List<IUpdate<Purchaser>>();
|
|
for (var i = 0; i < updateList.Count(); i++)
|
|
{
|
|
tempUpdateList.Add(updateList[i]);
|
|
if (i != 0 && i % 10 == 0)
|
|
{
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var update in tempUpdateList)
|
|
update.ExecuteAffrows();
|
|
});
|
|
tempUpdateList.Clear();
|
|
}
|
|
}
|
|
|
|
if (tempUpdateList.Count() > 0)
|
|
{
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var update in tempUpdateList)
|
|
update.ExecuteAffrows();
|
|
});
|
|
tempUpdateList.Clear();
|
|
}
|
|
}
|
|
|
|
private string FormatteLocation(string location)
|
|
{
|
|
if (string.IsNullOrEmpty(location))
|
|
return location;
|
|
location = location.Trim().Replace(" ", string.Empty);
|
|
|
|
IDictionary<string, List<string>> provinceDictionary = new Dictionary<string, List<string>>()
|
|
{
|
|
{ "省", new List<string>()
|
|
{
|
|
"黑龙江","吉林","辽宁","河北","山西","陕西","甘肃","青海","河南","湖北","湖南","山东","江苏","安徽","浙江","江西","福建","台湾","贵州","四川","云南","广东","海南"
|
|
}
|
|
},
|
|
{ "自治区", new List<string>(){ "内蒙古","西藏"}},
|
|
{ "回族自治区", new List<string>(){"宁夏"}},
|
|
{ "维吾尔自治区", new List<string>(){"新疆"}},
|
|
{ "壮族自治区", new List<string>(){"广西"}},
|
|
{ "特别行政区",new List<string>(){ "香港","澳门"}},
|
|
{ "市",new List<string>(){ "重庆","北京","天津","上海"}}
|
|
};
|
|
|
|
foreach (var provincekey in provinceDictionary.Keys)
|
|
{
|
|
var province = provinceDictionary[provincekey].FirstOrDefault(p => location.StartsWith(p));
|
|
if (string.IsNullOrEmpty(province))
|
|
continue;
|
|
if (location.Contains(provincekey))
|
|
return location;
|
|
location = location.Replace(province, $"{province}{provincekey}");
|
|
if (location.EndsWith(provincekey))
|
|
return location;
|
|
if (!location.EndsWith("市"))
|
|
location = $"{location}市";
|
|
}
|
|
//Console.WriteLine(location);
|
|
return location;
|
|
}
|
|
|
|
private IDictionary<string, (string Id, string parentId, string name, int lev)?> categoryCache = new Dictionary<string, (string Id, string parentId, string name, int lev)?>();
|
|
|
|
public (string Id, string parentId, string name, int lev)? GetCategoryById(string categoryId)
|
|
{
|
|
if (categoryCache.ContainsKey(categoryId))
|
|
return categoryCache[categoryId];
|
|
|
|
var httpResult = restApiService.SendRequest("http://yunding.qiyue666.com", "api/platformsdk/GetCategoryInfoById", new
|
|
{
|
|
AppKey = "120EA9EC65AB017567D78CC1139EEEA5",
|
|
AppSecret = "8a42bc2301e8439b896e99f5475e0a9b",
|
|
AppToken = "523ad68443184436965ca4304daff409tizy",
|
|
Platform = Enums.Platform.京东,
|
|
CategoryId = categoryId
|
|
}, null, HttpMethod.Post);
|
|
if (httpResult.StatusCode == System.Net.HttpStatusCode.OK)
|
|
{
|
|
var jtoken = JToken.Parse(httpResult.Content);
|
|
string fid = null;
|
|
if (jtoken["Data"].Value<string>("fid") != "0")
|
|
fid = jtoken["Data"].Value<string>("fid");
|
|
|
|
(string Id, string parentId, string name, int lev)? c = (jtoken["Data"].Value<string>("id"),
|
|
fid,
|
|
jtoken["Data"].Value<string>("name"),
|
|
jtoken["Data"].Value<int>("lev"));
|
|
|
|
if (!categoryCache.ContainsKey(c.Value.Id))
|
|
categoryCache.TryAdd(c.Value.Id, c);
|
|
return c;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public void SyncHyCats()
|
|
{
|
|
var l3CategoryIdList = freeSqlMultiDBManager.BBWYCfsql.Select<Model.Db.BBWY.ProductSku>().Where(s => s.CategoryId != null).Distinct().ToList(s => s.CategoryId);
|
|
var hycatsList = freeSqlMultiDBManager.HYfsql.Select<Model.Db.HY.HyCats>().ToList();
|
|
|
|
List<Model.Db.HY.HyCats> insertHyCatsList = new List<Model.Db.HY.HyCats>();
|
|
|
|
var count = l3CategoryIdList.Count();
|
|
var index = 1;
|
|
foreach (var l3CategoryId in l3CategoryIdList)
|
|
{
|
|
Console.ForegroundColor = ConsoleColor.Green;
|
|
Console.WriteLine($"{index}/{count} {l3CategoryId}");
|
|
Console.ResetColor();
|
|
index++;
|
|
var l3Info = GetCategoryById(l3CategoryId.Value.ToString());
|
|
if (l3Info != null)
|
|
{
|
|
if (!hycatsList.Any(x => x.Id == l3Info.Value.Id) && !insertHyCatsList.Any(x => x.Id == l3Info.Value.Id))
|
|
{
|
|
insertHyCatsList.Add(new HyCats()
|
|
{
|
|
Id = l3Info.Value.Id,
|
|
IsLeaf = 1,
|
|
Level = 3,
|
|
Name = l3Info.Value.name,
|
|
ParentId = l3Info.Value.parentId
|
|
});
|
|
}
|
|
|
|
var l2Info = GetCategoryById(l3Info.Value.parentId);
|
|
if (l2Info != null)
|
|
{
|
|
if (!hycatsList.Any(x => x.Id == l2Info.Value.Id) && !insertHyCatsList.Any(x => x.Id == l2Info.Value.Id))
|
|
{
|
|
insertHyCatsList.Add(new HyCats()
|
|
{
|
|
Id = l2Info.Value.Id,
|
|
IsLeaf = 0,
|
|
Level = 2,
|
|
Name = l2Info.Value.name,
|
|
ParentId = l2Info.Value.parentId
|
|
});
|
|
}
|
|
var l1Info = GetCategoryById(l2Info.Value.parentId);
|
|
if (l1Info != null)
|
|
{
|
|
if (!hycatsList.Any(x => x.Id == l1Info.Value.Id) && !insertHyCatsList.Any(x => x.Id == l1Info.Value.Id))
|
|
{
|
|
insertHyCatsList.Add(new HyCats()
|
|
{
|
|
Id = l1Info.Value.Id,
|
|
IsLeaf = 0,
|
|
Level = 1,
|
|
Name = l1Info.Value.name,
|
|
ParentId = l1Info.Value.parentId
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (insertHyCatsList.Count() > 0)
|
|
freeSqlMultiDBManager.HYfsql.Insert(insertHyCatsList).ExecuteAffrows();
|
|
}
|
|
|
|
public void ExportQTSpuAndJDSku()
|
|
{
|
|
var qtlist = fsql.Select<OrderSku>()
|
|
.Where(s1 => !string.IsNullOrEmpty(s1.BelongSkuId))
|
|
.GroupBy(s1 => s1.ProductId)
|
|
.WithTempQuery(g => new { MaxId = g.Max(g.Value.Id) })
|
|
.From<OrderSku>()
|
|
.InnerJoin((s1, s2) => s1.MaxId == s2.Id)
|
|
.ToList((s1, s2) => new { s2.ProductId, s2.BelongSkuId, s2.CreateTime });
|
|
|
|
var qtSpuList = fsql.Select<Product>().ToList();
|
|
|
|
var jdSkuIdList = qtlist.Select(x => x.BelongSkuId).Distinct().ToList();
|
|
var jdSkuList = freeSqlMultiDBManager.BBWYCfsql.Select<BBWYB.Server.Model.Db.BBWY.ProductSku>(jdSkuIdList).ToList();
|
|
var hycatsList = freeSqlMultiDBManager.HYfsql.Select<Model.Db.HY.HyCats>().ToList();
|
|
|
|
#region 拳探商品类目
|
|
var client = opPlatformClientFactory.GetClient(AdapterEnums.PlatformType.拳探);
|
|
var request = new OP_QueryProductCategoryRequest()
|
|
{
|
|
PageIndex = 1,
|
|
PageSize = 500,
|
|
AppKey = "BBWYB2023022501",
|
|
AppSecret = "3a1521deccc6819d61788a0d6baae49e9",
|
|
AppToken = "9",
|
|
Level = 0
|
|
};
|
|
|
|
var L1_qtCategoryList = new List<OP_ProductCategoryResponse>();
|
|
var L1_response = client.GetProductCategoryList(request);
|
|
if (L1_response.Items != null)
|
|
L1_qtCategoryList.AddRange(L1_response.Items);
|
|
|
|
request.Level = 1;
|
|
var L2_qtCategoryList = new List<OP_ProductCategoryResponse>();
|
|
var L2_response = client.GetProductCategoryList(request);
|
|
if (L2_response.Items != null)
|
|
L2_qtCategoryList.AddRange(L2_response.Items);
|
|
|
|
request.Level = 2;
|
|
var L3_qtCategoryList = new List<OP_ProductCategoryResponse>();
|
|
var L3_response = client.GetProductCategoryList(request);
|
|
if (L3_response.Items != null)
|
|
L3_qtCategoryList.AddRange(L3_response.Items);
|
|
#endregion
|
|
|
|
var result = new List<string>();
|
|
foreach (var qt in qtlist)
|
|
{
|
|
HyCats l1category = null;
|
|
HyCats l2category = null;
|
|
HyCats l3category = null;
|
|
|
|
OP_ProductCategoryResponse qt_l1category = null;
|
|
OP_ProductCategoryResponse qt_l2category = null;
|
|
OP_ProductCategoryResponse qt_l3category = null;
|
|
|
|
var qtspu = qtSpuList.FirstOrDefault(x => x.Id == qt.ProductId);
|
|
if (qtspu != null)
|
|
{
|
|
qt_l3category = L3_qtCategoryList.FirstOrDefault(x => x.CategoryId == qtspu.CategoryId?.ToString());
|
|
if (qt_l3category != null)
|
|
{
|
|
var parentPathIdArray = qt_l3category.CategoryPath.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
|
if (parentPathIdArray.Length != 3)
|
|
continue;
|
|
qt_l2category = L2_qtCategoryList.FirstOrDefault(x => x.CategoryId == parentPathIdArray[1]);
|
|
qt_l1category = L1_qtCategoryList.FirstOrDefault(x => x.CategoryId == parentPathIdArray[0]);
|
|
}
|
|
}
|
|
|
|
var jdps = jdSkuList.FirstOrDefault(x => x.Id == qt.BelongSkuId);
|
|
|
|
if (jdps != null && jdps.CategoryId != null)
|
|
{
|
|
l3category = hycatsList.FirstOrDefault(x => x.Id == jdps.CategoryId.ToString());
|
|
if (l3category != null && !string.IsNullOrEmpty(l3category.ParentId))
|
|
{
|
|
l2category = hycatsList.FirstOrDefault(x => x.Id == l3category.ParentId);
|
|
if (l2category != null && !string.IsNullOrEmpty(l2category.ParentId))
|
|
l1category = hycatsList.FirstOrDefault(x => x.Id == l2category.ParentId);
|
|
}
|
|
}
|
|
result.Add($"{qt.ProductId},{qt_l1category?.CategoryName},{qt_l2category?.CategoryName},{qt_l3category?.CategoryName},{qt.BelongSkuId},{l1category?.Name},{l2category?.Name},{l3category?.Name}");
|
|
}
|
|
result.Insert(0, "拳探spu,拳探1级类目,拳探2级类目,拳探3级类目,京东sku,京东1级类目,京东2级类目,京东3级类目");
|
|
var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "ExportQTSpuAndJDSku.csv");
|
|
System.IO.File.WriteAllLines(path, result, System.Text.Encoding.UTF8);
|
|
}
|
|
|
|
public void BelongBarginTeam()
|
|
{
|
|
var lines = System.IO.File.ReadAllLines("C:\\Users\\pengcong\\Desktop\\供应商.csv", Encoding.UTF8).ToList();
|
|
lines.RemoveAt(0);
|
|
|
|
var purchaseRelations = lines.Select(x =>
|
|
{
|
|
var array = x.Split(",", StringSplitOptions.RemoveEmptyEntries);
|
|
var teamId = string.Empty;
|
|
var teamName = string.Empty;
|
|
if (array[3] == "陈默")
|
|
{
|
|
teamId = "1760971589383360512";
|
|
teamName = "1组";
|
|
}
|
|
else
|
|
{
|
|
teamId = "1760971688964526080";
|
|
teamName = "2组";
|
|
}
|
|
return new
|
|
{
|
|
PurchaserName = array[0],
|
|
TeamId = teamId,
|
|
TeamName = teamName
|
|
};
|
|
});
|
|
|
|
var errorPurchaserList = new List<string>();
|
|
//var errorSchemeList = new List<string>();
|
|
var errorSchemeGroupList = new List<string>();
|
|
|
|
|
|
var dbPurchaserList = fsql.Select<Purchaser>().ToList();
|
|
var dbPurchaseGroupList = fsql.Select<PurchaseSchemeGroup>().ToList();
|
|
var dbSchemeList = fsql.Select<PurchaseScheme>().ToList();
|
|
var dbSchemeProductList = fsql.Select<PurchaseSchemeProduct>().ToList();
|
|
|
|
var updatePurchaserList = new List<Purchaser>();
|
|
var updateSchemeList = new List<PurchaseScheme>();
|
|
var updateSchemeGroupList = new List<PurchaseSchemeGroup>();
|
|
var deletePsgIdList = new List<long>();
|
|
|
|
|
|
|
|
foreach (var purchaseRelation in purchaseRelations)
|
|
{
|
|
var dbPurchaser = dbPurchaserList.FirstOrDefault(p => p.Name == purchaseRelation.PurchaserName);
|
|
if (dbPurchaser == null)
|
|
{
|
|
errorPurchaserList.Add($"未从数据表中找到供应商 {purchaseRelation}");
|
|
continue;
|
|
}
|
|
|
|
if (dbPurchaser.BelongBargainTeamId != purchaseRelation.TeamId)
|
|
{
|
|
dbPurchaser.BelongBargainTeamId = purchaseRelation.TeamId;
|
|
dbPurchaser.BelongBargainTeamName = purchaseRelation.TeamName;
|
|
dbPurchaser.BelongType = Enums.PurchaserBelongType.临时;
|
|
updatePurchaserList.Add(dbPurchaser);
|
|
}
|
|
}
|
|
|
|
List<BelongSchemeTestModel> compareNullBelongSchemeList = new List<BelongSchemeTestModel>();
|
|
var errorBelongSchemeList = new List<BelongSchemeTestModel>();
|
|
foreach (var purchaseRelation in purchaseRelations)
|
|
{
|
|
var dbPurchaser = dbPurchaserList.FirstOrDefault(p => p.Name == purchaseRelation.PurchaserName);
|
|
if (dbPurchaser == null)
|
|
continue;
|
|
|
|
//查询使用了该采购商的采购方案
|
|
var usePurchaserSchemeIdList = dbSchemeProductList.Where(psp => psp.PurchaserId == dbPurchaser.Id)
|
|
.Select(psp => psp.SkuPurchaseSchemeId)
|
|
.Distinct()
|
|
.ToList();
|
|
var usePurchaserSchemeList = dbSchemeList.Where(ps => usePurchaserSchemeIdList.Contains(ps.Id)).ToList();
|
|
|
|
|
|
foreach (var scheme in usePurchaserSchemeList)
|
|
{
|
|
var pspList = dbSchemeProductList.Where(psp => psp.SkuPurchaseSchemeId == scheme.Id).ToList();
|
|
if (pspList.Count() == 0)
|
|
continue;
|
|
var usePurchaserIdList = pspList.Select(psp => psp.PurchaserId).Distinct().ToList();
|
|
var currentSchemePurchaserList = dbPurchaserList.Where(p => usePurchaserIdList.Contains(p.Id)).ToList();
|
|
var currentSchemePurchaserBelongGroups = currentSchemePurchaserList.GroupBy(p => p.BelongBargainTeamId);
|
|
if (currentSchemePurchaserBelongGroups.Count() > 1)
|
|
{
|
|
//if (currentSchemePurchaserBelongGroups.Any(g => string.IsNullOrEmpty(g.Key)))
|
|
//{
|
|
//采购方案包含多个不同议价组的采购商且议价组Id为空
|
|
var schemeModel = new BelongSchemeTestModel()
|
|
{
|
|
SchemeId = scheme.Id,
|
|
BelongPurchaserTestModelList = new List<BelongPurchaserTestModel>()
|
|
};
|
|
compareNullBelongSchemeList.Add(schemeModel);
|
|
|
|
foreach (var belongGroup in currentSchemePurchaserBelongGroups)
|
|
{
|
|
foreach (var purchaser in belongGroup)
|
|
{
|
|
schemeModel.BelongPurchaserTestModelList.Add(new BelongPurchaserTestModel()
|
|
{
|
|
BelongTeam = string.IsNullOrEmpty(purchaser.BelongBargainTeamName) ? "空" : purchaser.BelongBargainTeamName,
|
|
PurchaserName = purchaser.Name
|
|
});
|
|
}
|
|
}
|
|
}
|
|
else if (currentSchemePurchaserBelongGroups.Count() == 1)
|
|
{
|
|
var firstPurchaser = currentSchemePurchaserBelongGroups.FirstOrDefault()?.FirstOrDefault();
|
|
if (scheme.BelongBargainTeamId != firstPurchaser.BelongBargainTeamId)
|
|
{
|
|
scheme.BelongBargainTeamId = firstPurchaser?.BelongBargainTeamId;
|
|
scheme.BelongBargainTeamName = firstPurchaser?.BelongBargainTeamName;
|
|
updateSchemeList.Add(scheme);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#region 筛选有没有混合空组采购商的情况
|
|
foreach (var belongScheme in compareNullBelongSchemeList)
|
|
{
|
|
if (belongScheme.BelongPurchaserTestModelList.Any(p => p.BelongTeam == "空"))
|
|
{
|
|
var nullBelongPurchaserList = belongScheme.BelongPurchaserTestModelList.Where(p => p.BelongTeam == "空").ToList();
|
|
var otherSchemeList = compareNullBelongSchemeList.Where(c => c.SchemeId != belongScheme.SchemeId &&
|
|
c.BelongPurchaserTestModelList.Any(p => p.BelongTeam == "空") &&
|
|
c.BelongGroupKey != belongScheme.BelongGroupKey &&
|
|
c.BelongPurchaserTestModelList.Any(x => nullBelongPurchaserList.Any(y => y.PurchaserName == x.PurchaserName))).ToList();
|
|
if (otherSchemeList.Count() > 0)
|
|
{
|
|
foreach (var belongPurchaser in nullBelongPurchaserList)
|
|
{
|
|
belongPurchaser.RelationSchemeIdList = otherSchemeList.Where(c => c.BelongPurchaserTestModelList.Any(x => belongPurchaser.PurchaserName == x.PurchaserName)).Select(c => c.SchemeId).Distinct().ToList();
|
|
}
|
|
errorBelongSchemeList.Add(belongScheme);
|
|
}
|
|
else
|
|
{
|
|
//没有混合空组
|
|
var firstNoNullBelongPurchaserName = belongScheme.BelongPurchaserTestModelList.FirstOrDefault(p => p.BelongTeam != "空")?.PurchaserName;
|
|
var firstNoNullBelongPurchaser = dbPurchaserList.FirstOrDefault(p => p.Name == firstNoNullBelongPurchaserName);
|
|
if (firstNoNullBelongPurchaser != null)
|
|
{
|
|
foreach (var nullBelongPurchaser in nullBelongPurchaserList)
|
|
{
|
|
var dbPurchaser = dbPurchaserList.FirstOrDefault(p => p.Name == nullBelongPurchaser.PurchaserName);
|
|
if (dbPurchaser != null)
|
|
{
|
|
dbPurchaser.BelongBargainTeamId = firstNoNullBelongPurchaser.BelongBargainTeamId;
|
|
dbPurchaser.BelongBargainTeamName = firstNoNullBelongPurchaser.BelongBargainTeamName;
|
|
dbPurchaser.BelongType = Enums.PurchaserBelongType.临时;
|
|
updatePurchaserList.Add(dbPurchaser);
|
|
}
|
|
}
|
|
|
|
var dbScheme = dbSchemeList.FirstOrDefault(s => s.Id == belongScheme.SchemeId);
|
|
if (dbScheme != null)
|
|
{
|
|
dbScheme.BelongBargainTeamId = firstNoNullBelongPurchaser.BelongBargainTeamId;
|
|
dbScheme.BelongBargainTeamName = firstNoNullBelongPurchaser.BelongBargainTeamName;
|
|
updateSchemeList.Add(dbScheme);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
errorBelongSchemeList.Add(belongScheme);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
foreach (var psg in dbPurchaseGroupList)
|
|
{
|
|
var schemeList = dbSchemeList.Where(ps => ps.SchemeGroupId == psg.Id).ToList();
|
|
if (schemeList.Count() == 0)
|
|
{
|
|
deletePsgIdList.Add(psg.Id);
|
|
continue;
|
|
}
|
|
|
|
var currentPsgPurchaserBelongGroups = schemeList.GroupBy(ps => ps.BelongBargainTeamId);
|
|
if (currentPsgPurchaserBelongGroups.Count() > 1)
|
|
{
|
|
//var sb = new StringBuilder($"采购分组{psg.GroupName}使用了{currentPsgPurchaserBelongGroups.Count()}个不同归属的采购方案");
|
|
//foreach (var belongGroup in currentPsgPurchaserBelongGroups)
|
|
//{
|
|
// var teamName = belongGroup.FirstOrDefault()?.BelongBargainTeamName;
|
|
// sb.Append($"{teamName}:{string.Join(",", belongGroup.Select(x => x.Id))},");
|
|
//}
|
|
//errorSchemeGroupList.Add(sb.ToString());
|
|
|
|
var ps = currentPsgPurchaserBelongGroups.SelectMany(g => g.ToList()).FirstOrDefault(ps => !string.IsNullOrEmpty(ps.BelongBargainTeamId));
|
|
|
|
foreach (var schemeGroup in currentPsgPurchaserBelongGroups)
|
|
{
|
|
foreach (var scheme in schemeGroup)
|
|
{
|
|
if (string.IsNullOrEmpty(scheme.BelongBargainTeamId))
|
|
{
|
|
scheme.BelongBargainTeamId = ps?.BelongBargainTeamId;
|
|
scheme.BelongBargainTeamName = ps?.BelongBargainTeamName;
|
|
updateSchemeList.Add(scheme);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (psg.BelongBargainTeamId != ps?.BelongBargainTeamId)
|
|
{
|
|
psg.BelongBargainTeamId = ps?.BelongBargainTeamId;
|
|
updateSchemeGroupList.Add(psg);
|
|
}
|
|
}
|
|
else if (currentPsgPurchaserBelongGroups.Count() == 1)
|
|
{
|
|
var firstScheme = schemeList[0];
|
|
if (psg.BelongBargainTeamId != firstScheme.BelongBargainTeamId)
|
|
{
|
|
psg.BelongBargainTeamId = firstScheme.BelongBargainTeamId;
|
|
updateSchemeGroupList.Add(psg);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (errorPurchaserList.Count() > 0 ||
|
|
errorBelongSchemeList.Count() > 0 ||
|
|
errorSchemeGroupList.Count() > 0)
|
|
{
|
|
if (errorBelongSchemeList.Count() > 0)
|
|
{
|
|
var msg2 = JsonConvert.SerializeObject(errorBelongSchemeList);
|
|
throw new BusinessException(msg2);
|
|
}
|
|
var msg = JsonConvert.SerializeObject(errorPurchaserList.Union(errorSchemeGroupList));
|
|
throw new BusinessException(msg);
|
|
}
|
|
|
|
#region 更新采购商
|
|
{
|
|
var updateList = new List<IUpdate<Purchaser>>();
|
|
for (var i = 0; i < updatePurchaserList.Count(); i++)
|
|
{
|
|
var purchaser = updatePurchaserList[i];
|
|
var update = fsql.Update<Purchaser>(purchaser.Id)
|
|
.Set(p => p.BelongBargainTeamId, purchaser.BelongBargainTeamId)
|
|
.Set(p => p.BelongBargainTeamName, purchaser.BelongBargainTeamName);
|
|
updateList.Add(update);
|
|
if (updateList.Count() == 10)
|
|
{
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var _u in updateList)
|
|
_u.ExecuteAffrows();
|
|
});
|
|
updateList.Clear();
|
|
Console.WriteLine($"UpdatePurchaser {i + 1}/{updatePurchaserList.Count()}");
|
|
}
|
|
}
|
|
|
|
if (updateList.Count() > 0)
|
|
{
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var _u in updateList)
|
|
_u.ExecuteAffrows();
|
|
});
|
|
updateList.Clear();
|
|
Console.WriteLine($"LastUpdatePurchaser");
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 更新采购方案
|
|
{
|
|
var updateList = new List<IUpdate<PurchaseScheme>>();
|
|
for (var i = 0; i < updateSchemeList.Count(); i++)
|
|
{
|
|
var scheme = updateSchemeList[i];
|
|
var update = fsql.Update<PurchaseScheme>(scheme.Id)
|
|
.Set(ps => ps.BelongBargainTeamId, scheme.BelongBargainTeamId)
|
|
.Set(ps => ps.BelongBargainTeamName, scheme.BelongBargainTeamName);
|
|
updateList.Add(update);
|
|
if (updateList.Count() == 10)
|
|
{
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var _u in updateList)
|
|
_u.ExecuteAffrows();
|
|
});
|
|
updateList.Clear();
|
|
Console.WriteLine($"UpdateScheme {i + 1}/{updateSchemeList.Count()}");
|
|
}
|
|
}
|
|
|
|
if (updateList.Count() > 0)
|
|
{
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var _u in updateList)
|
|
_u.ExecuteAffrows();
|
|
});
|
|
updateList.Clear();
|
|
Console.WriteLine($"LastUpdateScheme");
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region 更新采购分组
|
|
{
|
|
var updateList = new List<IUpdate<PurchaseSchemeGroup>>();
|
|
for (var i = 0; i < updateSchemeGroupList.Count(); i++)
|
|
{
|
|
var psg = updateSchemeGroupList[i];
|
|
var update = fsql.Update<PurchaseSchemeGroup>(psg.Id)
|
|
.Set(g => g.BelongBargainTeamId, psg.BelongBargainTeamId);
|
|
updateList.Add(update);
|
|
if (updateList.Count() == 10)
|
|
{
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var _u in updateList)
|
|
_u.ExecuteAffrows();
|
|
});
|
|
updateList.Clear();
|
|
Console.WriteLine($"UpdateSchemeGroup {i + 1}/{updateSchemeGroupList.Count()}");
|
|
}
|
|
}
|
|
|
|
if (updateList.Count() > 0)
|
|
{
|
|
fsql.Transaction(() =>
|
|
{
|
|
foreach (var _u in updateList)
|
|
_u.ExecuteAffrows();
|
|
});
|
|
updateList.Clear();
|
|
Console.WriteLine($"LastUpdateSchemeGroup");
|
|
}
|
|
}
|
|
#endregion
|
|
}
|
|
|
|
public List<string> RepairCBOrder()
|
|
{
|
|
var startTime = DateTime.Now.Date.AddDays(-3);
|
|
var endTime = startTime.AddDays(1).AddSeconds(-1);
|
|
var corderList = freeSqlMultiDBManager.BBWYCfsql.Select<Model.Db.BBWY.PurchaseOrderV2>()
|
|
.Where(c => c.CreateTime >= startTime && c.CreateTime <= endTime)
|
|
.ToList(c => c.Id);
|
|
|
|
var borderlist = fsql.Select<Order>(corderList).ToList(o => o.Id);
|
|
|
|
var exceptList = corderList.Except(borderlist).ToList();
|
|
return exceptList;
|
|
}
|
|
}
|
|
}
|
|
|
|
|