using Microsoft.EntityFrameworkCore.Storage; using Microsoft.Extensions.DependencyInjection; using NPOI.POIFS.FileSystem; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web; using WMS.Web.Core.Dto.Erp; using WMS.Web.Core.Dto.Login; using WMS.Web.Core.Internal.Results; using WMS.Web.Domain.Entitys; using WMS.Web.Domain.Infrastructure; using WMS.Web.Domain.IService; using WMS.Web.Domain.IService.Public; using WMS.Web.Domain.Values; namespace WMS.Web.Domain.Services { public class ProductInventoryService : IProductInventoryService { private readonly IErpService _erpService; private readonly ISingleDataService _singleDataService; private readonly IErpBasicDataExtendService _erpBasicDataExtendService; private readonly IProductInventoryRepositories _repositories; private readonly ILingXingService _lingXingService; private readonly IBasicsRepositories _basicsRepositories; public readonly IBasicsRepositories _transactionRepositories; public readonly IJuShuiTanService _juShuiTanService; public readonly IErpInventoryService _erpInventoryService; public ProductInventoryService(IErpService erpService, ISingleDataService singleDataService, IErpBasicDataExtendService erpBasicDataExtendService, IProductInventoryRepositories repositories, ILingXingService lingXingService, IBasicsRepositories basicsRepositories, IBasicsRepositories transactionRepositories, IJuShuiTanService juShuiTanService, IErpInventoryService erpInventoryService) { _erpService = erpService; _singleDataService = singleDataService; _erpBasicDataExtendService = erpBasicDataExtendService; _repositories = repositories; _lingXingService = lingXingService; _basicsRepositories = basicsRepositories; _transactionRepositories = transactionRepositories; _juShuiTanService = juShuiTanService; _erpInventoryService = erpInventoryService; } /// /// 金蝶 /// /// public async Task Erp() { //获取单点配置 var r = await _basicsRepositories.GetUcStockByHeadOfficeAsync("", 1); var codes = r.Where(w => w.ManagementSystem == 1 || w.ManagementSystem == 4).Select(s => (s.Code, s.ErpOrgCode)).ToList(); if (codes.Count() <= 0) return Result.ReSuccess(); var res = await _erpService.BillQueryForInventory(codes); if (!res.IsSuccess) return res; List inventoryList = new List(); foreach (var item in res.Data) { var entity = new ProductInventory() { Type = ProductInventoryType.JinDie, MaterialNumber = item.MaterialNumber, Customer = item.Erp_SubStockName, OrgCode = item.OrgCode, StockCode = item.StockCode, Qty = item.Qty, BeforeQty = item.BeforeQty }; inventoryList.Add(entity); } IDbContextTransaction _transaction = _transactionRepositories.GetTransaction(); Result res_Rollback = Result.ReSuccess(); bool isSuccess = true; //修改库存 //先删除之前的再添加 if (res_Rollback.IsSuccess) { isSuccess = await _repositories.Delete(ProductInventoryType.JinDie, false); if (isSuccess == false) res_Rollback = Result.ReFailure(ResultCodes.DateWriteError); } if (res_Rollback.IsSuccess) { isSuccess = await _repositories.AddRange(inventoryList, false); if (isSuccess == false) res_Rollback = Result.ReFailure(ResultCodes.DateWriteError); } //提交事务 isSuccess = _transactionRepositories.CommitTransaction(res_Rollback.IsSuccess ? false : true, _transaction); if (!res_Rollback.IsSuccess) return res_Rollback; if (!isSuccess) return Result.ReFailure(ResultCodes.DateWriteError); return Result.ReSuccess(); } /// /// 聚水潭 /// /// /// public async Task JuShuiTan() { //获取单点配置 var r = await _basicsRepositories.GetUcStockByHeadOfficeAsync("", 1); var listNames = r.Where(w => w.ManagementSystem == 2 && !string.IsNullOrEmpty(w.WarehouseCodeOfJushuitan)) .Select(s => s.WarehouseCodeOfJushuitan) .ToList(); if (listNames.Count() <= 0) return Result.ReSuccess(); //获取领星仓库 var resStock = await _juShuiTanService.GetStock(); if (!resStock.IsSuccess) return resStock; var ids = resStock.Data.Where(w => listNames.Contains(w.Name)).Select(s => s.Id).ToList(); //获取领星库存 var resInventory = await _juShuiTanService.GetInventory(ids); if (!resInventory.IsSuccess) return resStock; //物料 var materials_result = await _erpService.BillQueryForMaterial(); List materials = new List(); if (materials_result.IsSuccess) materials = materials_result.Data.ToList(); List inventoryList = new List(); foreach (var item in resInventory.Data) { //如果物料不匹配 过滤 var m = materials.FirstOrDefault(f => f.Specifications.Equals(item.sku_id)); if (m == null) continue; //找仓库 var l_stock = resStock.Data.FirstOrDefault(f => f.Id == item.wms_co_id); if (l_stock == null) continue; var stock = r.FirstOrDefault(f => f.WarehouseCodeOfLingxing.Equals(l_stock.Name)); if (stock == null) continue; var entity = new ProductInventory() { Type = ProductInventoryType.JushuiTan, MaterialNumber = m.MaterialNumber, Customer = "", OrgCode = stock.Code, StockCode = stock.ErpOrgCode, Qty = item.ky_qty, BeforeQty = item.qty ?? 0 }; inventoryList.Add(entity); } IDbContextTransaction _transaction = _transactionRepositories.GetTransaction(); Result res_Rollback = Result.ReSuccess(); bool isSuccess = true; //修改库存 //先删除之前的再添加 if (res_Rollback.IsSuccess) { isSuccess = await _repositories.Delete(ProductInventoryType.LingXing, false); if (isSuccess == false) res_Rollback = Result.ReFailure(ResultCodes.DateWriteError); } if (res_Rollback.IsSuccess) { isSuccess = await _repositories.AddRange(inventoryList, false); if (isSuccess == false) res_Rollback = Result.ReFailure(ResultCodes.DateWriteError); } //提交事务 isSuccess = _transactionRepositories.CommitTransaction(res_Rollback.IsSuccess ? false : true, _transaction); if (!res_Rollback.IsSuccess) return res_Rollback; if (!isSuccess) return Result.ReFailure(ResultCodes.DateWriteError); return Result.ReSuccess(); } /// /// 领星 /// /// public async Task LingXing() { //获取单点配置 var r = await _basicsRepositories.GetUcStockByHeadOfficeAsync("", 1); var listNames = r.Where(w => w.ManagementSystem == 3 && !string.IsNullOrEmpty(w.WarehouseCodeOfLingxing)) .Select(s => s.WarehouseCodeOfLingxing) .ToList(); if (listNames.Count() <= 0) return Result.ReSuccess(); //获取领星仓库 var resStock = await _lingXingService.GetStock(); if (!resStock.IsSuccess) return resStock; var ids = resStock.Data.Where(w => listNames.Contains(w.Name)).Select(s => s.Id).ToList(); string strIds = string.Join(",", ids); //获取领星库存 var resInventory = await _lingXingService.GetInventory(new Core.Dto.LingXing.LingXingInventoryRequest() { wid = strIds }); if (!resInventory.IsSuccess) return resStock; //物料 var materials_result = await _erpService.BillQueryForMaterial(); List materials = new List(); if (materials_result.IsSuccess) materials = materials_result.Data.ToList(); //获取店铺 var sellerList = await _lingXingService.GetSeller(); //合并 var group = resInventory.Data.GroupBy(g => (g.StockId, g.SKU, g.SellerId)); List inventoryList = new List(); foreach (var item in group) { //如果物料不匹配 过滤 var m = materials.FirstOrDefault(f => f.Specifications.Equals(item.Key.SKU)); if (m == null) continue; //找仓库 var l_stock = resStock.Data.FirstOrDefault(f => f.Id == item.Key.StockId); if (l_stock == null) continue; var stock = r.FirstOrDefault(f => f.WarehouseCodeOfLingxing.Equals(l_stock.Name)); if (stock == null) continue; //店铺 var seller = sellerList.Data.FirstOrDefault(f => f.Id == item.Key.SellerId); if (seller == null) continue; var pList = resInventory.Data.Where(w => w.SKU == item.Key.SKU && w.StockId == item.Key.StockId && w.SellerId == item.Key.SellerId).ToList(); var entity = new ProductInventory() { Type = ProductInventoryType.LingXing, MaterialNumber = m.MaterialNumber, Customer = seller.Name, OrgCode = stock.Code, StockCode = stock.ErpOrgCode, Qty = pList.Sum(s => s.Product_Valid_Num), BeforeQty = pList.Sum(s => s.Product_Total) }; inventoryList.Add(entity); } IDbContextTransaction _transaction = _transactionRepositories.GetTransaction(); Result res_Rollback = Result.ReSuccess(); bool isSuccess = true; //修改库存 //先删除之前的再添加 if (res_Rollback.IsSuccess) { isSuccess = await _repositories.Delete(ProductInventoryType.LingXing, false); if (isSuccess == false) res_Rollback = Result.ReFailure(ResultCodes.DateWriteError); } if (res_Rollback.IsSuccess) { isSuccess = await _repositories.AddRange(inventoryList, false); if (isSuccess == false) res_Rollback = Result.ReFailure(ResultCodes.DateWriteError); } //提交事务 isSuccess = _transactionRepositories.CommitTransaction(res_Rollback.IsSuccess ? false : true, _transaction); if (!res_Rollback.IsSuccess) return res_Rollback; if (!isSuccess) return Result.ReFailure(ResultCodes.DateWriteError); return Result.ReSuccess(); } } }