using Business.Core.Utilities;
using Business.Dto;
using Business.StructuredDB.WMS;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;
using IUnitOfWorkManager = Volo.Abp.Uow.IUnitOfWorkManager;
namespace Business.DOP
{
//DOP
public class SyncDOPAppService : ApplicationService, ISyncDOPAppService, ITransientDependency
{
#region 服务
///
/// 平台规格型号对照表
///
private readonly IRepository _PlatformSpecificationComparison;
///
/// 平台库存表
///
private readonly IRepository _PlatformInventory;
///
/// 雪花算法
///
SnowFlake snowFlake = new SnowFlake();
///
/// 事务
///
private readonly IUnitOfWorkManager _unitOfWorkManager;
#endregion
#region 构造函数
///
/// 构造函数
///
public SyncDOPAppService(
IRepository PlatformSpecificationComparison,
IRepository PlatformInventory,
IUnitOfWorkManager unitOfWorkManager,
ICurrentTenant currentTenant
)
{
_PlatformSpecificationComparison = PlatformSpecificationComparison;
_PlatformInventory = PlatformInventory;
_unitOfWorkManager = unitOfWorkManager;
}
#endregion
///
/// 海王平台成品监控
///
///
///
public async Task SyncPlatformFinishedProductMonitoringHW(List platformInventoryDtoList)
{
var ret = SavePlatformFinishedProductMonitoringAsync(platformInventoryDtoList, 1);
return await ret;
}
///
/// 国科平台成品监控
///
///
///
public async Task SyncPlatformFinishedProductMonitoringGK(List platformInventoryDtoList)
{
var ret = SavePlatformFinishedProductMonitoringAsync(platformInventoryDtoList, 2);
return await ret;
}
///
/// 保存库存信息
///
///
/// 1海王,2国科
public async Task SavePlatformFinishedProductMonitoringAsync(List platformInventoryDtoList, int type)
{
if (platformInventoryDtoList.Count == 0)
{
return JsonConvert.SerializeObject("成品库存为空!");
}
List platformInventoryInsert = new List();
List platformInventoryDel;
if (type == 1)
{
platformInventoryDel = _PlatformInventory.GetListAsync(x => x.Code == "HW0001").Result;
}
else
{
platformInventoryDel = _PlatformInventory.GetListAsync(x => x.Code == "HW0002").Result;
}
foreach (var item in platformInventoryDtoList)
{
PlatformInventory platformInventory = new PlatformInventory();
platformInventory.GenerateNewId(snowFlake.NextId());
platformInventory.Location = item.Location;
platformInventory.SpecificationModel = item.SpecificationModel;
platformInventory.BatchNumber = item.BatchNumber;
platformInventory.InventoryQuantity = item.InventoryQuantity;
platformInventory.PeriodOfValidity = item.PeriodOfValidity;
if (type == 1)
{
platformInventory.Code = "HW0001";
}
else
{
platformInventory.Code = "HW0002";
}
platformInventoryInsert.Add(platformInventory);
}
using (var unitOfWork = _unitOfWorkManager.Begin(false, true))
{
try
{
//先删后插
await _PlatformInventory.DeleteManyAsync(platformInventoryDel);
await _PlatformInventory.InsertManyAsync(platformInventoryInsert);
await unitOfWork.CompleteAsync();
}
catch (Exception e)
{
new NLogHelper("SyncDOPAppService").WriteLog("PlatformFinishedProductMonitoringAsyncMerge", "接口异常:" + e.Message);
unitOfWork.Dispose();
return JsonConvert.SerializeObject(e.Message);
}
}
return JsonConvert.SerializeObject("ok");
}
}
}