using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using DapperORMCore.Context.DataContext; using Microsoft.Extensions.Logging; using NXWMS.IService.NXWMS; using NXWMS.Model.Common; using WestDistance.DapperORM.Repository.Repositorys; namespace NXWMS.Service.NXWMS.ERP { [AutoInject(typeof(IErpSyncServer), InjectType.Scope)] public class ErpSyncServer : IErpSyncServer { private IERPServer _ERPServer; //IDataRepositoryContext _context; private IServiceProvider _serviceProvider; private ILogger _logger; public ErpSyncServer(IERPServer ERPServer, ILogger logger, IDataRepositoryContext dataRepositoryContext, IServiceProvider serviceProvider) { _ERPServer = ERPServer; _logger = logger; //this._context = dataRepositoryContext; _serviceProvider = serviceProvider; } public async Task UpdateWarehouse() { try { var _context = (IDataRepositoryContext)this._serviceProvider.GetService(typeof(IDataRepositoryContext)); var Repository = new DataRepository(_context); var list = await _ERPServer.GetERPWareHousesAsync(); var src = list.ToList().ToDictionary(m => { return m.WAREHOUSE_CODE; }); var dst = Repository.Query().ToDictionary(m => { return m.WAREHOUSE_CODE; }); //更新 var update = src.Keys.Intersect(dst.Keys); var addKey = src.Keys.Except(dst.Keys); var deleteKey = dst.Keys.Except(src.Keys); foreach (var key in deleteKey) { Repository.Remove(dst[key], "WAREHOUSE_CODE"); } foreach (var key in addKey) { Repository.Add(src[key], new string[] { "WAREHOUSE_ID" }); } return true; } catch (Exception err) { throw; } } public async Task UpdateBin() { try { var _context = (IDataRepositoryContext)this._serviceProvider.GetService(typeof(IDataRepositoryContext)); var Repository = new DataRepository(_context); var list = await _ERPServer.GetERPERPpBinsAsync(); var src = list.ToList().ToDictionary(m => { return m.BIN_CODE; }); var dst = Repository.Query("Select * from BAS_BIN where REGION_CODE=@regionCode", new { regionCode = "YCLK_Region" }).ToDictionary(m => { return m.BIN_CODE; }); //更新 var update = src.Keys.Intersect(dst.Keys); var addKey = src.Keys.Except(dst.Keys); var deleteKey = dst.Keys.Except(src.Keys); foreach (var key in deleteKey) { Repository.Remove(dst[key], "BIN_CODE"); } foreach (var key in addKey) { src[key].USED_FLAG = 1; src[key].REGION_CODE = "YCLK_Region"; Repository.Add(src[key], new string[] { "BIN_ID" }); } return true; } catch (Exception err) { throw err; } } public async Task UpdateUnit() { try { var _context = (IDataRepositoryContext)this._serviceProvider.GetService(typeof(IDataRepositoryContext)); var Repository = new DataRepository(_context); var list = await _ERPServer.GetERPUnitsAsync(); var src = list.ToList().ToDictionary(m => { return m.UNIT_CODE; }); var dst = Repository.Query().ToDictionary(m => { return m.UNIT_CODE; }); //更新 var update = src.Keys.Intersect(dst.Keys); var addKey = src.Keys.Except(dst.Keys); var deleteKey = dst.Keys.Except(src.Keys); foreach (var key in deleteKey) { Repository.Remove(dst[key], "UNIT_CODE"); } foreach (var key in addKey) { Repository.Add(src[key], new string[] { "UNIT_ID" }); } return true; } catch (Exception err) { throw err; } } public async Task UpdateMaterielType() { try { var _context = (IDataRepositoryContext)this._serviceProvider.GetService(typeof(IDataRepositoryContext)); var Repository = new DataRepository(_context); var list = await _ERPServer.GetERPMaterielTypesAsync(); var src = list.ToList().ToDictionary(m => { return m.MATERIEL_TYPE_CODE; }); var dst = Repository.Query().ToDictionary(m => { return m.MATERIEL_TYPE_CODE; }); //更新 var update = src.Keys.Intersect(dst.Keys); var addKey = src.Keys.Except(dst.Keys); var deleteKey = dst.Keys.Except(src.Keys); foreach (var key in deleteKey) { Repository.Remove(dst[key], "MATERIEL_TYPE_CODE"); } foreach (var key in addKey) { Repository.Add(src[key], new string[] { "MATERIEL_TYPE_ID" }); } return true; } catch (Exception err) { throw err; } } public async Task UpdateMateriel() { try { var _context = (IDataRepositoryContext)this._serviceProvider.GetService(typeof(IDataRepositoryContext)); var Repository = new DataRepository(_context); var list = await _ERPServer.GetERPMaterielsAsync(); var src = list.ToList().ToDictionary(m => { return m.MATERIEL_CODE; }); var dst = Repository.Query().ToDictionary(m => { return m.MATERIEL_CODE; }); //更新 var update = src.Keys.Intersect(dst.Keys); var addKey = src.Keys.Except(dst.Keys); var deleteKey = dst.Keys.Except(src.Keys); foreach (var key in deleteKey) { Repository.Remove(dst[key], "MATERIEL_CODE"); } foreach (var key in addKey) { Repository.Add(src[key], new string[] { "MATERIEL_ID" }); } return true; } catch (Exception) { throw; } } public async Task UpdateSuppliers() { try { var _context = (IDataRepositoryContext)this._serviceProvider.GetService(typeof(IDataRepositoryContext)); var Repository = new DataRepository(_context); var list = await _ERPServer.GetERPSuppliersAsync(); var src = list.ToList().ToDictionary(m => { return m.SUPPLIER_CODE; }); var dst = Repository.Query().ToDictionary(m => { return m.SUPPLIER_CODE; }); //更新 var update = src.Keys.Intersect(dst.Keys); var addKey = src.Keys.Except(dst.Keys); var deleteKey = dst.Keys.Except(src.Keys); foreach (var key in deleteKey) { Repository.Remove(dst[key], "SUPPLIER_CODE"); } foreach (var key in addKey) { Repository.Add(src[key], new string[] { "SUPPLIER_ID" }); } return true; } catch (Exception err) { throw err; } } public async Task UpdateCustomer() { try { var _context = (IDataRepositoryContext)this._serviceProvider.GetService(typeof(IDataRepositoryContext)); var Repository = new DataRepository(_context); var list = await _ERPServer.GetERPCustomersAsync(); var src = list.ToList().ToDictionary(m => { return m.CUSTOMER_CODE; }); var dst = Repository.Query().ToDictionary(m => { return m.CUSTOMER_CODE; }); //更新 var update = src.Keys.Intersect(dst.Keys); var addKey = src.Keys.Except(dst.Keys); var deleteKey = dst.Keys.Except(src.Keys); foreach (var key in deleteKey) { Repository.Remove(dst[key], "CUSTOMER_CODE"); } foreach (var key in addKey) { Repository.Add(src[key], new string[] { "CUSTOMER_ID" }); } return true; } catch (Exception err) { throw err; } } public async Task UpdateArrival(string ArrivalNo) { var _context = (IDataRepositoryContext)this._serviceProvider.GetService(typeof(IDataRepositoryContext)); _context.BeginTran(); try { var Repository = new DataRepository(_context); var RepositoryDtl = new DataRepository(_context); var arrival = Repository.Query().FirstOrDefault(m => m.ARRIVAL_NO == ArrivalNo); if (arrival != null) { var arrivalDtl = RepositoryDtl.Query("ARRIVAL_ID", arrival.ARRIVAL_ID.ToString()).ToList(); var list = await _ERPServer.GetArrival(arrival.ERP_ID); foreach (var _arrival in list) { var (key, detail) = _arrival; key.ARRIVAL_ID = arrival.ARRIVAL_ID; Repository.Update(key, nameof(DataAccess.Entity.WMS_IN_ARRIVAL.ARRIVAL_ID)); foreach (var item in detail) { var _localArrival = arrivalDtl.FirstOrDefault(m => m.ERP_DTL_ID == item.ERP_DTL_ID); if (_localArrival == null) { item.ARRIVAL_ID = arrival.ARRIVAL_ID; item.INSPECTION_RESULT = "OK"; RepositoryDtl.IdentityAdd(item, nameof(DataAccess.Entity.WMS_IN_ARRIVAL_DTL.ARRIVAL_DTL_ID)); } else { item.ARRIVAL_ID = arrival.ARRIVAL_ID; item.INSPECTION_RESULT = "OK"; RepositoryDtl.Update(item, nameof(DataAccess.Entity.WMS_IN_ARRIVAL_DTL.ARRIVAL_DTL_ID)); arrivalDtl.Remove(_localArrival); } } if (arrivalDtl.Count > 0) { foreach (var item in arrivalDtl) { RepositoryDtl.Remove(item, nameof(DataAccess.Entity.WMS_IN_ARRIVAL_DTL.ARRIVAL_DTL_ID)); } } } } _context.Commit(); return true; } catch (Exception err) { _context.Rollback(); throw err; } } public async Task UpdateArrival() { try { var _context = (IDataRepositoryContext)this._serviceProvider.GetService(typeof(IDataRepositoryContext)); var Repository = new DataRepository(_context); var RepositoryDtl = new DataRepository(_context); var listhas = Repository.Query().Select(m => m.ERP_ID).ToHashSet(); var list = await _ERPServer.GetArrival(); foreach (var (key, detail) in list) { if (listhas.Contains(key.ERP_ID)) { continue; } var id = Repository.IdentityAdd(key, nameof(DataAccess.Entity.WMS_IN_ARRIVAL.ARRIVAL_ID)); foreach (var item in detail) { item.ARRIVAL_ID = id; item.INSPECTION_RESULT = "OK"; RepositoryDtl.IdentityAdd(item, nameof(DataAccess.Entity.WMS_IN_ARRIVAL_DTL.ARRIVAL_DTL_ID)); } } return true; } catch (Exception err) { throw err; } } public async Task UpdateOutInvoice() { try { var _context = (IDataRepositoryContext)this._serviceProvider.GetService(typeof(IDataRepositoryContext)); var Repository = new DataRepository(_context); var RepositoryDtl = new DataRepository(_context); var list = await _ERPServer.GetOutInvoice(); foreach (var key in list.Keys) { var detail = list[key]; var id = Repository.IdentityAdd(key, nameof(DataAccess.Entity.WMS_OUT_INVOICE.INVOICE_ID)); foreach (var item in detail) { item.INVOICE_ID = id; RepositoryDtl.IdentityAdd(item, nameof(DataAccess.Entity.WMS_OUT_INVOICE_DTL.INVOICE_DTL_ID)); } } return true; } catch (Exception err) { throw err; } } public void UpdateBaseInfo() { this._logger.LogInformation("ERP同步开始"); //this.UpdateWarehouse(); //this.UpdateMaterielType(); //this.UpdateMateriel(); //this.UpdateSuppliers(); //this.UpdateCustomer(); Task.WaitAll(this.UpdateArrival()); this._logger.LogInformation("ERP同步结束"); } } }