|
|
@ -7,9 +7,9 @@ using Coravel.Invocable; |
|
|
|
using EFCore.BulkExtensions; |
|
|
|
using LinqToDB; |
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
using Newtonsoft.Json; |
|
|
|
using SettleAccount.Domain.BQ; |
|
|
|
using Volo.Abp.Application.Services; |
|
|
|
using Win.Sfs.SettleAccount.Entities.BQ.Managers; |
|
|
|
using Win.Sfs.SettleAccount.Entities.BQ.Vmi; |
|
|
|
using Win.Sfs.SettleAccount.EntityFrameworkCore; |
|
|
|
using Win.Sfs.SettleAccount.MaterialRelationships; |
|
|
@ -27,52 +27,28 @@ public class JitSeSyncAppService : ApplicationService, IInvocable |
|
|
|
/// WMS数据上下文
|
|
|
|
/// </summary>
|
|
|
|
private readonly WMSBJBMPTDbContext _wmsBJBMPTContext; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 数据上下文
|
|
|
|
/// </summary>
|
|
|
|
private readonly SettleAccountDbContext _settleAccountDbContext; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 同步位置标记
|
|
|
|
/// </summary>
|
|
|
|
private readonly INormalEfCoreRepository<SyncPositionFlag, Guid> _syncPositionFlagRepository; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Jit发运数据仓储
|
|
|
|
/// </summary>
|
|
|
|
private readonly INormalEfCoreRepository<PUB_SE_DETAIL, Guid> _pubSeDetailRepository; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 客户零件关系领域
|
|
|
|
/// </summary>
|
|
|
|
private readonly MaterialRelationshipManager _materialRelationshipManager; |
|
|
|
|
|
|
|
//private readonly IVmiService _vmiService;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 构造
|
|
|
|
/// </summary>
|
|
|
|
public JitSeSyncAppService( |
|
|
|
WMSBJBMPTDbContext wmsBJBMPTContext, |
|
|
|
SettleAccountDbContext settleAccountDbContext, |
|
|
|
INormalEfCoreRepository<SyncPositionFlag, Guid> syncPositionFlagRepository, |
|
|
|
INormalEfCoreRepository<PUB_SE_DETAIL, Guid> pubSeDetailRepository, |
|
|
|
MaterialRelationshipManager materialRelationshipManager, |
|
|
|
VmiAppService vmiService) |
|
|
|
public JitSeSyncAppService(WMSBJBMPTDbContext wmsBJBMPTContext, SettleAccountDbContext settleAccountDbContext) |
|
|
|
{ |
|
|
|
_wmsBJBMPTContext = wmsBJBMPTContext; |
|
|
|
_settleAccountDbContext = settleAccountDbContext; |
|
|
|
_syncPositionFlagRepository = syncPositionFlagRepository; |
|
|
|
_pubSeDetailRepository = pubSeDetailRepository; |
|
|
|
_materialRelationshipManager = materialRelationshipManager; |
|
|
|
//_vmiService = vmiService;
|
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 发运同步配置
|
|
|
|
/// </summary>
|
|
|
|
public SeSyncConfig SeSyncConfigInfo { get; set; } |
|
|
|
/// <summary>
|
|
|
|
/// Seed
|
|
|
|
/// </summary>
|
|
|
|
public int Seed { get; set; } |
|
|
|
|
|
|
|
[HttpPost] |
|
|
|
public async Task Invoke() |
|
|
@ -90,30 +66,70 @@ public class JitSeSyncAppService : ApplicationService, IInvocable |
|
|
|
//业务类别
|
|
|
|
var businessType = SeSyncConfigInfo.BusinessType; |
|
|
|
|
|
|
|
Expression<Func<TM_BJBMPT_OTHER_RECORD, bool>> predicate = (t) => t.DeliverBillType == deliverBillType && (!deliverSubBillTypes.Any() || deliverSubBillTypes.Contains(t.DeliverSubBillType)); |
|
|
|
var syncPositionFlag = await _syncPositionFlagRepository.FindAsync(t => t.TableName == syncTableName).ConfigureAwait(false); |
|
|
|
if (syncPositionFlag != null) |
|
|
|
while (Seed < 10) |
|
|
|
{ |
|
|
|
Seed++; |
|
|
|
var syncPositionFlag = await _settleAccountDbContext.Set<SyncPositionFlag>().FirstOrDefaultAsync(t => t.TableName == syncTableName).ConfigureAwait(false); |
|
|
|
var syncPosition = syncPositionFlag?.Position ?? "0"; |
|
|
|
|
|
|
|
Expression<Func<TM_BJBMPT_OTHER_RECORD, bool>> predicate = (t) => t.DeliverBillType == deliverBillType && t.UID > int.Parse(syncPosition) && (!deliverSubBillTypes.Any() || deliverSubBillTypes.Contains(t.DeliverSubBillType)); |
|
|
|
//WMS发运记录
|
|
|
|
var wmsSeRecords = _wmsBJBMPTContext.TM_BJBMPT_OTHER_RECORD.Where(predicate).Take(100_000).OrderBy(b => b.UID).ToList(); |
|
|
|
var pubSeDetails = ObjectMapper.Map<List<TM_BJBMPT_OTHER_RECORD>, List<PUB_SE_DETAIL>>(wmsSeRecords); |
|
|
|
if (pubSeDetails.Any()) |
|
|
|
{ |
|
|
|
await SeDataPutInStorageAsync(pubSeDetails).ConfigureAwait(false); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#region 私有方法
|
|
|
|
/// <summary>
|
|
|
|
/// 添加零件关系
|
|
|
|
/// </summary>
|
|
|
|
private async Task AddNewMaterialRelationshipsAsync(IEnumerable<MaterialRelationship> materialRelationships) |
|
|
|
{ |
|
|
|
//新客户零件号和厂内零件号
|
|
|
|
var noHaveLuRePartCodes = from item1 in materialRelationships |
|
|
|
join item2 in _settleAccountDbContext.Set<MaterialRelationship>() |
|
|
|
on new { item1.ErpMaterialCode, item1.SettleMaterialCode } equals new { item2.ErpMaterialCode, item2.SettleMaterialCode } |
|
|
|
into temp |
|
|
|
from item3 in temp.DefaultIfEmpty() |
|
|
|
where item3 == null |
|
|
|
select item1; |
|
|
|
|
|
|
|
if (noHaveLuRePartCodes.Any()) |
|
|
|
{ |
|
|
|
predicate = (t) => t.DeliverBillType == deliverBillType && t.UID > int.Parse(syncPositionFlag.Position) && (!deliverSubBillTypes.Any() || deliverSubBillTypes.Contains(t.DeliverSubBillType)); |
|
|
|
await _settleAccountDbContext.BulkInsertAsync(noHaveLuRePartCodes.ToList()).ConfigureAwait(false); |
|
|
|
} |
|
|
|
//WMS发运记录
|
|
|
|
var wmsSeRecords = _wmsBJBMPTContext.TM_BJBMPT_OTHER_RECORD.Where(predicate).OrderBy(b => b.UID).ToList(); |
|
|
|
var pubSeDetails = ObjectMapper.Map<List<TM_BJBMPT_OTHER_RECORD>, List<PUB_SE_DETAIL>>(wmsSeRecords); |
|
|
|
if (pubSeDetails.Any()) |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 发运数据入库
|
|
|
|
/// </summary>
|
|
|
|
private async Task SeDataPutInStorageAsync(List<PUB_SE_DETAIL> seDetails) |
|
|
|
{ |
|
|
|
var syncTableName = SeSyncConfigInfo.SyncTableName; |
|
|
|
var businessType = SeSyncConfigInfo.BusinessType; |
|
|
|
if (seDetails.Any()) |
|
|
|
{ |
|
|
|
if (businessType != EnumBusinessType.BeiJian) |
|
|
|
{ |
|
|
|
//备件以外其他业务添加客户零件号与厂内零件号关系
|
|
|
|
//客户零件号和厂内零件号
|
|
|
|
var luRePartCodes = pubSeDetails.Where(t => !string.IsNullOrEmpty(t.LU) && !string.IsNullOrEmpty(t.FactoryPartCode)).Select(t => new { t.LU, t.FactoryPartCode }).Distinct().ToList(); |
|
|
|
var luRePartCodes = seDetails.Where(t => !string.IsNullOrEmpty(t.LU) && !string.IsNullOrEmpty(t.FactoryPartCode)).Select(t => new { t.LU, t.FactoryPartCode }).Distinct().ToList(); |
|
|
|
if (luRePartCodes.Any()) |
|
|
|
{ |
|
|
|
var materialRelationships = luRePartCodes.Select(t => new MaterialRelationship(GuidGenerator.Create(), t.FactoryPartCode, "", t.LU, businessType)); |
|
|
|
await this.AddNewMaterialRelationships(materialRelationships).ConfigureAwait(false); |
|
|
|
await this.AddNewMaterialRelationshipsAsync(materialRelationships).ConfigureAwait(false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
pubSeDetails.ForEach(t => |
|
|
|
seDetails.ForEach(t => |
|
|
|
{ |
|
|
|
t.SetId(GuidGenerator.Create()); |
|
|
|
t.BusinessType = businessType; |
|
|
@ -124,108 +140,101 @@ public class JitSeSyncAppService : ApplicationService, IInvocable |
|
|
|
t.Version = int.Parse(t.BillTime?.ToString("yyyyMM")); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
if (syncPositionFlag != null) |
|
|
|
{ |
|
|
|
syncPositionFlag.Position = wmsSeRecords.Last().UID.ToString(); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
syncPositionFlag = new SyncPositionFlag() |
|
|
|
{ |
|
|
|
TableName = syncTableName, |
|
|
|
Position = wmsSeRecords.Last().UID.ToString() |
|
|
|
}; |
|
|
|
} |
|
|
|
var syncPosition = seDetails.Last().UID.ToString(); |
|
|
|
|
|
|
|
using var transaction = await _settleAccountDbContext.Database.BeginTransactionAsync().ConfigureAwait(false); |
|
|
|
try |
|
|
|
{ |
|
|
|
_settleAccountDbContext.Set<SyncPositionFlag>().AddOrUpdate(t => t.TableName == syncPositionFlag.TableName, syncPositionFlag); |
|
|
|
await _settleAccountDbContext.BulkInsertAsync(pubSeDetails).ConfigureAwait(false); |
|
|
|
await transaction.CommitAsync().ConfigureAwait(false); |
|
|
|
} |
|
|
|
catch (Exception) |
|
|
|
{ |
|
|
|
await transaction.RollbackAsync().ConfigureAwait(false); |
|
|
|
throw; |
|
|
|
} |
|
|
|
|
|
|
|
foreach (var pubSeDetail in pubSeDetails) |
|
|
|
{ |
|
|
|
if (pubSeDetail.TransType == EnumDelTransType.发货) |
|
|
|
var syncPositionFlag = await _settleAccountDbContext.Set<SyncPositionFlag>().FirstOrDefaultAsync(t => t.TableName == syncTableName).ConfigureAwait(false); |
|
|
|
if (syncPositionFlag != null) |
|
|
|
{ |
|
|
|
var vmiBalance = new VmiBalance() |
|
|
|
{ |
|
|
|
RealPartCode = pubSeDetail.PartCode, |
|
|
|
VinCode = pubSeDetail.PN, |
|
|
|
CustPartCode = default, |
|
|
|
CodeType = default, |
|
|
|
DeliverBillType = pubSeDetail.DeliverBillType, |
|
|
|
Qty = pubSeDetail.Qty, |
|
|
|
BillTime = pubSeDetail.BillTime ?? new DateTime(), |
|
|
|
AssembleData = pubSeDetail.AssembleData, |
|
|
|
ErpToLoc = pubSeDetail.ToErpLocCode, |
|
|
|
Seq = default, |
|
|
|
OrderNum = pubSeDetail.DeliveryIndex, |
|
|
|
UniqueCode = default, |
|
|
|
MatchNumber = default, |
|
|
|
PjsNum = default, |
|
|
|
Configcode = default, |
|
|
|
factory = default, |
|
|
|
DeliverSubBillType = pubSeDetail.DeliverSubBillType, |
|
|
|
RealCode = default, |
|
|
|
ReMark = pubSeDetail.Remark, |
|
|
|
}; |
|
|
|
//await _vmiService.In(logType: Vmi.VmiLogType.Type100, changedNumber: pubSeDetail.DnBillNum, data: vmiBalance).ConfigureAwait(false);
|
|
|
|
syncPositionFlag.Position = syncPosition; |
|
|
|
_settleAccountDbContext.Update<SyncPositionFlag>(syncPositionFlag); |
|
|
|
} |
|
|
|
else if (pubSeDetail.TransType == EnumDelTransType.退货) |
|
|
|
else |
|
|
|
{ |
|
|
|
var vmiLog = new VmiLog() |
|
|
|
syncPositionFlag = new SyncPositionFlag() |
|
|
|
{ |
|
|
|
RealPartCode = pubSeDetail.PartCode, |
|
|
|
VinCode = pubSeDetail.PN, |
|
|
|
CustPartCode = default, |
|
|
|
CodeType = default, |
|
|
|
DeliverBillType = pubSeDetail.DeliverBillType, |
|
|
|
Qty = pubSeDetail.Qty, |
|
|
|
BillTime = pubSeDetail.BillTime ?? new DateTime(), |
|
|
|
AssembleData = pubSeDetail.DnBillTime, |
|
|
|
ErpToLoc = pubSeDetail.ToErpLocCode, |
|
|
|
Seq = default, |
|
|
|
OrderNum = pubSeDetail.DnBillNum, |
|
|
|
UniqueCode = default, |
|
|
|
MatchNumber = default, |
|
|
|
PjsNum = default, |
|
|
|
Configcode = default, |
|
|
|
factory = default, |
|
|
|
DeliverSubBillType = pubSeDetail.DeliverSubBillType, |
|
|
|
RealCode = default, |
|
|
|
ReMark = pubSeDetail.Remark, |
|
|
|
TableName = syncTableName, |
|
|
|
Position = syncPosition |
|
|
|
}; |
|
|
|
//await _vmiService.Out(VmiLogType.Type400, pubSeDetail.DnBillNum, vmiLog).ConfigureAwait(false);
|
|
|
|
_settleAccountDbContext.Add<SyncPositionFlag>(syncPositionFlag); |
|
|
|
} |
|
|
|
await _settleAccountDbContext.BulkInsertAsync(seDetails).ConfigureAwait(false); |
|
|
|
await SaveVmiLogsAsync(seDetails).ConfigureAwait(false); |
|
|
|
await _settleAccountDbContext.SaveChangesAsync().ConfigureAwait(false); |
|
|
|
await transaction.CommitAsync().ConfigureAwait(false); |
|
|
|
} |
|
|
|
catch (Exception) |
|
|
|
{ |
|
|
|
await transaction.RollbackAsync().ConfigureAwait(false); |
|
|
|
throw; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 添加零件关系
|
|
|
|
/// 保存寄售库Log
|
|
|
|
/// </summary>
|
|
|
|
public async Task AddNewMaterialRelationships(IEnumerable<MaterialRelationship> materialRelationships) |
|
|
|
private async Task SaveVmiLogsAsync(List<PUB_SE_DETAIL> seDetails) |
|
|
|
{ |
|
|
|
//新客户零件号和厂内零件号
|
|
|
|
var noHaveLuRePartCodes = from item1 in materialRelationships |
|
|
|
join item2 in _settleAccountDbContext.Set<MaterialRelationship>() |
|
|
|
on new { item1.ErpMaterialCode, item1.SettleMaterialCode } equals new { item2.ErpMaterialCode, item2.SettleMaterialCode } |
|
|
|
into temp |
|
|
|
from item3 in temp.DefaultIfEmpty() |
|
|
|
where item3 == null |
|
|
|
select item1; |
|
|
|
var deliverSeDetails = seDetails.FindAll(t => t.TransType == EnumDelTransType.发货); |
|
|
|
var returnSeDetails = seDetails.FindAll(t => t.TransType == EnumDelTransType.退货); |
|
|
|
var dateTimeNow = DateTime.Now; |
|
|
|
|
|
|
|
if (noHaveLuRePartCodes.Any()) |
|
|
|
var vmiLogList = deliverSeDetails.Select(t => new VmiLog(Guid.NewGuid()) |
|
|
|
{ |
|
|
|
await _settleAccountDbContext.BulkInsertAsync(noHaveLuRePartCodes.ToList()).ConfigureAwait(false); |
|
|
|
} |
|
|
|
LogType = VmiLogType.Type100, |
|
|
|
ChangedQty = t.Qty, |
|
|
|
ChangedType = VmiType.In, |
|
|
|
ChangedBy = "WMS", |
|
|
|
ChangedNumber = t.PN, |
|
|
|
ChangedTime = dateTimeNow, |
|
|
|
AssembleData = t.AssembleData, |
|
|
|
BillTime = t.BillTime, |
|
|
|
CustPartCode = t.CustPartCode, |
|
|
|
DeliverBillType = t.DeliverBillType, |
|
|
|
DeliverSubBillType = t.DeliverSubBillType, |
|
|
|
ErpToLoc = t.ToErpLocCode, |
|
|
|
OrderNum = t.DnBillNum, |
|
|
|
Qty = t.Qty, |
|
|
|
ReMark = t.Remark, |
|
|
|
RealCode = t.PartCode |
|
|
|
}).ToList(); |
|
|
|
var returnVmiLogList = returnSeDetails.Select(t => new VmiLog(Guid.NewGuid()) |
|
|
|
{ |
|
|
|
LogType = VmiLogType.Type400, |
|
|
|
ChangedQty = t.Qty, |
|
|
|
ChangedType = VmiType.In, |
|
|
|
ChangedBy = "WMS", |
|
|
|
ChangedNumber = t.PN, |
|
|
|
ChangedTime = dateTimeNow, |
|
|
|
AssembleData = t.AssembleData, |
|
|
|
BillTime = t.BillTime, |
|
|
|
CustPartCode = t.CustPartCode, |
|
|
|
DeliverBillType = t.DeliverBillType, |
|
|
|
DeliverSubBillType = t.DeliverSubBillType, |
|
|
|
ErpToLoc = t.ToErpLocCode, |
|
|
|
OrderNum = t.DnBillNum, |
|
|
|
Qty = -t.Qty, |
|
|
|
ReMark = t.Remark, |
|
|
|
RealCode = t.PartCode |
|
|
|
}).ToList(); |
|
|
|
vmiLogList.AddRange(returnVmiLogList); |
|
|
|
await SaveVmiMessagesAsync(vmiLogList).ConfigureAwait(false); |
|
|
|
await _settleAccountDbContext.BulkInsertAsync(vmiLogList).ConfigureAwait(false); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 保存寄售库Message
|
|
|
|
/// </summary>
|
|
|
|
public async Task SaveVmiMessagesAsync(List<VmiLog> vmiLogs) |
|
|
|
{ |
|
|
|
var vmiMessages = vmiLogs.Select(t => new VmiMessage(Guid.NewGuid()) |
|
|
|
{ |
|
|
|
Message = JsonConvert.SerializeObject(t) |
|
|
|
}).ToList(); |
|
|
|
await _settleAccountDbContext.BulkInsertAsync(vmiMessages).ConfigureAwait(false); |
|
|
|
} |
|
|
|
#endregion
|
|
|
|
} |
|
|
|