|
|
@ -1,14 +1,13 @@ |
|
|
|
using System; |
|
|
|
using System.Collections.Generic; |
|
|
|
using System.Linq; |
|
|
|
using System.Linq.Expressions; |
|
|
|
using System.Threading.Tasks; |
|
|
|
using Coravel.Invocable; |
|
|
|
using EFCore.BulkExtensions; |
|
|
|
using LinqToDB; |
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
using SettleAccount.Domain.BQ; |
|
|
|
using Volo.Abp.Application.Services; |
|
|
|
using Win.Sfs.SettleAccount.Entities.BQ.Managers; |
|
|
|
using Win.Sfs.SettleAccount.EntityFrameworkCore; |
|
|
|
using Win.Sfs.SettleAccount.MaterialRelationships; |
|
|
|
using Win.Sfs.Shared.RepositoryBase; |
|
|
@ -25,28 +24,14 @@ public class JisBBACSeSyncBaseAppService : ApplicationService, IInvocable |
|
|
|
/// WMS数据上下文
|
|
|
|
/// </summary>
|
|
|
|
private readonly WMSBJBMPTDbContext _wmsBJBMPTContext; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 数据上下文
|
|
|
|
/// </summary>
|
|
|
|
private readonly SettleAccountDbContext _settleAccountDbContext; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 同步位置标记
|
|
|
|
/// 发运同步扩展
|
|
|
|
/// </summary>
|
|
|
|
private readonly INormalEfCoreRepository<SyncPositionFlag, Guid> _syncPositionFlagRepository; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Jis发运数据仓储
|
|
|
|
/// </summary>
|
|
|
|
private readonly INormalEfCoreRepository<BBAC_SE_DETAIL, Guid> _jisSeDetailRepository; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 客户零件关系领域
|
|
|
|
/// </summary>
|
|
|
|
private readonly MaterialRelationshipManager _materialRelationshipManager; |
|
|
|
|
|
|
|
//private readonly IVmiService _vmiService;
|
|
|
|
private readonly SeSyncExtendManager _syncExtendManager; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 构造
|
|
|
@ -54,17 +39,11 @@ public class JisBBACSeSyncBaseAppService : ApplicationService, IInvocable |
|
|
|
public JisBBACSeSyncBaseAppService( |
|
|
|
WMSBJBMPTDbContext wmsBJBMPTContext, |
|
|
|
SettleAccountDbContext settleAccountDbContext, |
|
|
|
INormalEfCoreRepository<SyncPositionFlag, Guid> syncPositionFlagRepository, |
|
|
|
INormalEfCoreRepository<BBAC_SE_DETAIL, Guid> jisSeDetailRepository, |
|
|
|
MaterialRelationshipManager materialRelationshipManager, |
|
|
|
VmiAppService vmiService) |
|
|
|
SeSyncExtendManager syncExtendManager) |
|
|
|
{ |
|
|
|
_wmsBJBMPTContext = wmsBJBMPTContext; |
|
|
|
_settleAccountDbContext = settleAccountDbContext; |
|
|
|
_syncPositionFlagRepository = syncPositionFlagRepository; |
|
|
|
_jisSeDetailRepository = jisSeDetailRepository; |
|
|
|
_materialRelationshipManager = materialRelationshipManager; |
|
|
|
//_vmiService = vmiService;
|
|
|
|
_syncExtendManager = syncExtendManager; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
@ -80,7 +59,6 @@ public class JisBBACSeSyncBaseAppService : ApplicationService, IInvocable |
|
|
|
return; |
|
|
|
} |
|
|
|
await SyncJitRecordAsync().ConfigureAwait(false); |
|
|
|
await _settleAccountDbContext.SaveChangesAsync().ConfigureAwait(false); |
|
|
|
await SyncJisRecordAsync().ConfigureAwait(false); |
|
|
|
} |
|
|
|
|
|
|
@ -97,53 +75,27 @@ public class JisBBACSeSyncBaseAppService : ApplicationService, IInvocable |
|
|
|
var deliverSubBillTypes = SeSyncConfigInfo.SyncDeliverSubBillTypes; |
|
|
|
//业务类别
|
|
|
|
var businessType = SeSyncConfigInfo.BusinessType; |
|
|
|
|
|
|
|
Expression<Func<TM_BJBMPT_JIT_RECORD, bool>> predicate = (t) => t.DeliverBillType == deliverBillType && deliverSubBillTypes.Contains(t.DeliverSubBillType); |
|
|
|
var syncPositionFlag = await _syncPositionFlagRepository.FindAsync(t => t.TableName == syncTableName).ConfigureAwait(false); |
|
|
|
if (syncPositionFlag != null) |
|
|
|
{ |
|
|
|
predicate = (t) => t.UID > int.Parse(syncPositionFlag.Position) && t.DeliverBillType == deliverBillType && deliverSubBillTypes.Contains(t.DeliverSubBillType); |
|
|
|
} |
|
|
|
|
|
|
|
//WMS发运记录
|
|
|
|
var wmsSeRecords = _wmsBJBMPTContext.TM_BJBMPT_JIT_RECORD.Where(predicate).OrderBy(b => b.UID).Take(100000).ToList(); |
|
|
|
var jisSeDetails = ObjectMapper.Map<List<TM_BJBMPT_JIT_RECORD>, List<BBAC_SE_DETAIL>>(wmsSeRecords); |
|
|
|
if (jisSeDetails.Any()) |
|
|
|
var seed = 0; |
|
|
|
while (seed < 10) |
|
|
|
{ |
|
|
|
//客户零件号和厂内零件号
|
|
|
|
var luRePartCodes = jisSeDetails.Where(t => !string.IsNullOrEmpty(t.LU) && !string.IsNullOrEmpty(t.FactoryPartCode)).Select(t => new { t.LU, t.FactoryPartCode }).Distinct().ToList(); |
|
|
|
if (luRePartCodes.Any()) |
|
|
|
seed++; |
|
|
|
var syncPositionFlag = await _settleAccountDbContext.Set<SyncPositionFlag>().FirstOrDefaultAsync(t => t.TableName == syncTableName).ConfigureAwait(false); |
|
|
|
var syncPosition = syncPositionFlag?.Position ?? "0"; |
|
|
|
|
|
|
|
//WMS发运记录
|
|
|
|
var wmsSeRecords = _wmsBJBMPTContext.TM_BJBMPT_JIT_RECORD |
|
|
|
.Where(t => t.UID > int.Parse(syncPosition)) |
|
|
|
.Where(t => t.DeliverBillType == deliverBillType) |
|
|
|
.Where(t => deliverSubBillTypes.Contains(t.DeliverSubBillType)) |
|
|
|
.OrderBy(b => b.UID).Take(100_000).ToList(); |
|
|
|
var jisSeDetails = ObjectMapper.Map<List<TM_BJBMPT_JIT_RECORD>, List<BBAC_SE_DETAIL>>(wmsSeRecords); |
|
|
|
if (jisSeDetails.Any()) |
|
|
|
{ |
|
|
|
var materialRelationships = luRePartCodes.Select(t => new MaterialRelationship(GuidGenerator.Create(), t.FactoryPartCode, "", t.LU, businessType)); |
|
|
|
await this.AddNewMaterialRelationships(materialRelationships).ConfigureAwait(false); |
|
|
|
} |
|
|
|
|
|
|
|
jisSeDetails.ForEach(t => |
|
|
|
{ |
|
|
|
t.SetId(GuidGenerator.Create()); |
|
|
|
t.BusinessType = businessType; |
|
|
|
t.CustomerPartCodeNoSpace = t.LU.Replace(" ", ""); |
|
|
|
t.KeyCode = t.PN + t.LU; |
|
|
|
if (t.BillTime != null) |
|
|
|
{ |
|
|
|
t.Version = int.Parse(t.BillTime?.ToString("yyyyMM")); |
|
|
|
} |
|
|
|
}); |
|
|
|
await _settleAccountDbContext.BulkInsertAsync(jisSeDetails).ConfigureAwait(false); |
|
|
|
|
|
|
|
if (syncPositionFlag != null) |
|
|
|
{ |
|
|
|
syncPositionFlag.Position = wmsSeRecords.Last().UID.ToString(); |
|
|
|
await _syncPositionFlagRepository.UpdateAsync(syncPositionFlag).ConfigureAwait(false); |
|
|
|
await SeDataPutInStorageAsync(jisSeDetails, false).ConfigureAwait(false); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
syncPositionFlag = new SyncPositionFlag() |
|
|
|
{ |
|
|
|
TableName = syncTableName, |
|
|
|
Position = wmsSeRecords.Last().UID.ToString() |
|
|
|
}; |
|
|
|
await _syncPositionFlagRepository.InsertAsync(syncPositionFlag).ConfigureAwait(false); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -161,74 +113,90 @@ public class JisBBACSeSyncBaseAppService : ApplicationService, IInvocable |
|
|
|
var deliverSubBillTypes = SeSyncConfigInfo.SyncDeliverSubBillTypes; |
|
|
|
//业务类别
|
|
|
|
var businessType = SeSyncConfigInfo.BusinessType; |
|
|
|
var seed = 0; |
|
|
|
while (seed < 10) |
|
|
|
{ |
|
|
|
seed++; |
|
|
|
var syncPositionFlag = await _settleAccountDbContext.Set<SyncPositionFlag>().FirstOrDefaultAsync(t => t.TableName == syncTableName).ConfigureAwait(false); |
|
|
|
var syncPosition = syncPositionFlag?.Position ?? "0"; |
|
|
|
|
|
|
|
//WMS发运记录
|
|
|
|
var wmsSeRecords = _wmsBJBMPTContext.TM_BJBMPT_JIS_RECORD |
|
|
|
.Where(t => t.UID > int.Parse(syncPosition)) |
|
|
|
.Where(t => t.DeliverBillType == deliverBillType) |
|
|
|
.Where(t => deliverSubBillTypes.Contains(t.DeliverSubBillType)) |
|
|
|
.OrderBy(b => b.UID).Take(100_000).ToList(); |
|
|
|
var jisSeDetails = ObjectMapper.Map<List<TM_BJBMPT_JIS_RECORD>, List<BBAC_SE_DETAIL>>(wmsSeRecords); |
|
|
|
if (jisSeDetails.Any()) |
|
|
|
{ |
|
|
|
await SeDataPutInStorageAsync(jisSeDetails, true).ConfigureAwait(false); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 发运数据入库
|
|
|
|
/// </summary>
|
|
|
|
private async Task SeDataPutInStorageAsync(List<BBAC_SE_DETAIL> seDetails, bool isJisTable) |
|
|
|
{ |
|
|
|
//同步表名称
|
|
|
|
var syncTableName = isJisTable ? SeSyncConfigInfo.SyncTableName + |
|
|
|
"_Jis" : SeSyncConfigInfo.SyncTableName + "_Jit"; |
|
|
|
//业务类别
|
|
|
|
var businessType = SeSyncConfigInfo.BusinessType; |
|
|
|
|
|
|
|
Expression<Func<TM_BJBMPT_JIS_RECORD, bool>> predicate = (t) => t.DeliverBillType == deliverBillType && deliverSubBillTypes.Contains(t.DeliverSubBillType); |
|
|
|
var syncPositionFlag = await _syncPositionFlagRepository.FindAsync(t => t.TableName == syncTableName).ConfigureAwait(false); |
|
|
|
if (syncPositionFlag != null) |
|
|
|
//客户零件号和厂内零件号
|
|
|
|
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()) |
|
|
|
{ |
|
|
|
predicate = (t) => t.UID > int.Parse(syncPositionFlag.Position) && t.DeliverBillType == deliverBillType && deliverSubBillTypes.Contains(t.DeliverSubBillType); |
|
|
|
var materialRelationships = luRePartCodes.Select(t => new MaterialRelationship(GuidGenerator.Create(), t.FactoryPartCode, "", t.LU, businessType)); |
|
|
|
await _syncExtendManager.AddNewMaterialRelationshipsAsync(materialRelationships).ConfigureAwait(false); |
|
|
|
} |
|
|
|
|
|
|
|
//WMS发运数据
|
|
|
|
var wmsRecords = _wmsBJBMPTContext.TM_BJBMPT_JIS_RECORD.Where(predicate).OrderBy(b => b.UID).Take(100000).ToList(); |
|
|
|
var jisSeDetails = ObjectMapper.Map<List<TM_BJBMPT_JIS_RECORD>, List<BBAC_SE_DETAIL>>(wmsRecords); |
|
|
|
if (jisSeDetails.Any()) |
|
|
|
seDetails.ForEach(t => |
|
|
|
{ |
|
|
|
//客户零件号和厂内零件号
|
|
|
|
var luRePartCodes = jisSeDetails.Where(t => !string.IsNullOrEmpty(t.LU) && !string.IsNullOrEmpty(t.FactoryPartCode)).Select(t => new { t.LU, t.FactoryPartCode }).Distinct().ToList(); |
|
|
|
if (luRePartCodes.Any()) |
|
|
|
t.SetId(GuidGenerator.Create()); |
|
|
|
t.BusinessType = businessType; |
|
|
|
t.CustomerPartCodeNoSpace = t.LU.Replace(" ", ""); |
|
|
|
t.KeyCode = t.PN + t.LU; |
|
|
|
if (t.BillTime != null) |
|
|
|
{ |
|
|
|
var materialRelationships = luRePartCodes.Select(t => new MaterialRelationship(GuidGenerator.Create(), t.FactoryPartCode, "", t.LU, businessType)); |
|
|
|
await this.AddNewMaterialRelationships(materialRelationships).ConfigureAwait(false); |
|
|
|
t.Version = int.Parse(t.BillTime?.ToString("yyyyMM")); |
|
|
|
} |
|
|
|
}); |
|
|
|
var syncPosition = seDetails.Last().UID.ToString(); |
|
|
|
|
|
|
|
jisSeDetails.ForEach(t => |
|
|
|
{ |
|
|
|
t.SetId(GuidGenerator.Create()); |
|
|
|
t.BusinessType = businessType; |
|
|
|
t.CustomerPartCodeNoSpace = t.LU.Replace(" ", ""); |
|
|
|
t.KeyCode = t.PN + t.LU; |
|
|
|
if (t.BillTime != null) |
|
|
|
{ |
|
|
|
t.Version = int.Parse(t.BillTime?.ToString("yyyyMM")); |
|
|
|
} |
|
|
|
}); |
|
|
|
await _settleAccountDbContext.BulkInsertAsync(jisSeDetails).ConfigureAwait(false); |
|
|
|
|
|
|
|
using var transaction = await _settleAccountDbContext.Database.BeginTransactionAsync().ConfigureAwait(false); |
|
|
|
try |
|
|
|
{ |
|
|
|
var syncPositionFlag = await _settleAccountDbContext.Set<SyncPositionFlag>().FirstOrDefaultAsync(t => t.TableName == syncTableName).ConfigureAwait(false); |
|
|
|
if (syncPositionFlag != null) |
|
|
|
{ |
|
|
|
syncPositionFlag.Position = wmsRecords.Last().UID.ToString(); |
|
|
|
await _syncPositionFlagRepository.UpdateAsync(syncPositionFlag).ConfigureAwait(false); |
|
|
|
syncPositionFlag.Position = syncPosition; |
|
|
|
_settleAccountDbContext.Update<SyncPositionFlag>(syncPositionFlag); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
syncPositionFlag = new SyncPositionFlag() |
|
|
|
{ |
|
|
|
TableName = syncTableName, |
|
|
|
Position = wmsRecords.Last().UID.ToString() |
|
|
|
Position = syncPosition |
|
|
|
}; |
|
|
|
await _syncPositionFlagRepository.InsertAsync(syncPositionFlag).ConfigureAwait(false); ; |
|
|
|
_settleAccountDbContext.Add<SyncPositionFlag>(syncPositionFlag); |
|
|
|
} |
|
|
|
await _settleAccountDbContext.BulkInsertAsync(seDetails).ConfigureAwait(false); |
|
|
|
await _syncExtendManager.JisSeDetailsSaveConsignAsync<BBAC_SE_DETAIL>(seDetails, isJisTable).ConfigureAwait(false); |
|
|
|
await _settleAccountDbContext.SaveChangesAsync().ConfigureAwait(false); |
|
|
|
await transaction.CommitAsync().ConfigureAwait(false); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 添加零件关系
|
|
|
|
/// </summary>
|
|
|
|
public async Task AddNewMaterialRelationships(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()) |
|
|
|
catch (Exception) |
|
|
|
{ |
|
|
|
await _settleAccountDbContext.BulkInsertAsync(noHaveLuRePartCodes.ToList()).ConfigureAwait(false); |
|
|
|
await transaction.RollbackAsync().ConfigureAwait(false); |
|
|
|
throw; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|