using DS.Module.Core; using DS.Module.SqlSugar; using DS.Module.UserModule; using DS.WMS.Core.Code.Dtos; using DS.WMS.Core.Code.Entity; using DS.WMS.Core.Code.Interface; using DS.WMS.Core.Fee.Entity; using DS.WMS.Core.Op.Dtos; using DS.WMS.Core.Op.Entity; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; using MiniExcelLibs; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using static NPOI.HSSF.Util.HSSFColor; namespace DS.WMS.Core.Code.Method { public class CodeInfoImportService: ICodeInfoImportService { private readonly IServiceProvider _serviceProvider; private readonly ISqlSugarClient db; private readonly IUser user; private readonly ISaasDbService saasService; /// /// /// /// public CodeInfoImportService(IServiceProvider serviceProvider) { _serviceProvider = serviceProvider; db = _serviceProvider.GetRequiredService(); user = _serviceProvider.GetRequiredService(); saasService = _serviceProvider.GetRequiredService(); } /// /// 按Excel导入包装 /// /// /// public async Task> ImportPackageByExcel(IFormFile file) { //未上传文件 if (file == null || file.Length == 0) { return await Task.FromResult(DataResult.Failed("请上传Excel!")); } var formFile = file; //DataResult api_Result = new Api_Result(); //获取文件名 var fileName = formFile.FileName; // 获取文件后缀 var fileExtension = Path.GetExtension(fileName); // 判断后缀是否是xlsx或者xls if (fileExtension != ".xlsx" && fileExtension != ".xls") { return DataResult.Failed("文件格式错误"); } var length = formFile.Length; if (length > 1024 * 1024 * 10) { return DataResult.Failed("文件大小不能超过10M"); } var tenantDb = saasService.GetBizDbScopeById(user.TenantId); // MemoryStream 内存流 在内存当中创建一个流(开辟空间) using (var stream = new MemoryStream()) { //将文件写入内存流 formFile.CopyTo(stream); //将流的位置归零 stream.Position = 0; //将内存流转成List集合 var list = await stream.QueryAsync(); var packages = new List(); foreach (var item in list) { var package = new CodePackage() { PackageName = item.PackageName, CnExplain = item.CnExplain, AfrCode = item.AfrCode, EdiCode = item.EdiCode, }; packages.Add(package); } //var codes = configs.Select(c => c.SystemCode).ToList(); //if (tenantDb.Queryable().Where(x => codes.Contains(x.SystemCode)).Any()) //{ // return DataResult.Failed("存在已有的系统编码,请检查"); //} //var configs = list.Adapt(); await db.Insertable(packages).ExecuteCommandAsync(); return await Task.FromResult(DataResult.Failed("导入成功!", MultiLanguageConst.DataImportSuccess)); } } /// /// 按Excel导入集装箱信息 /// /// /// public async Task> ImportCtnByExcel(IFormFile file) { //未上传文件 if (file == null || file.Length == 0) { return await Task.FromResult(DataResult.Failed("请上传Excel!")); } var formFile = file; //DataResult api_Result = new Api_Result(); //获取文件名 var fileName = formFile.FileName; // 获取文件后缀 var fileExtension = Path.GetExtension(fileName); // 判断后缀是否是xlsx或者xls if (fileExtension != ".xlsx" && fileExtension != ".xls") { return DataResult.Failed("文件格式错误"); } var length = formFile.Length; if (length > 1024 * 1024 * 10) { return DataResult.Failed("文件大小不能超过10M"); } var tenantDb = saasService.GetBizDbScopeById(user.TenantId); // MemoryStream 内存流 在内存当中创建一个流(开辟空间) using (var stream = new MemoryStream()) { //将文件写入内存流 formFile.CopyTo(stream); //将流的位置归零 stream.Position = 0; //将内存流转成List集合 var list = await stream.QueryAsync(); var ctns = new List(); foreach (var item in list) { var ctn = new CodeCtn() { CtnSize = item.CtnSize, CtnType = item.CtnType, CnExplain= item.CnExplain, EnExplain= item.EnExplain, CtnName = item.CtnName, LimitWeight= item.LimitWeight, TEU= item.TEU, AfrCode = item.AfrCode, EdiCode = item.EdiCode, }; ctns.Add(ctn); } //var codes = configs.Select(c => c.SystemCode).ToList(); //if (tenantDb.Queryable().Where(x => codes.Contains(x.SystemCode)).Any()) //{ // return DataResult.Failed("存在已有的系统编码,请检查"); //} //var configs = list.Adapt(); await db.Insertable(ctns).ExecuteCommandAsync(); return await Task.FromResult(DataResult.Failed("导入成功!", MultiLanguageConst.DataImportSuccess)); } } /// /// 按Excel导入国家信息 /// /// /// public async Task> ImportCountryByExcel(IFormFile file) { //未上传文件 if (file == null || file.Length == 0) { return await Task.FromResult(DataResult.Failed("请上传Excel!")); } var formFile = file; //DataResult api_Result = new Api_Result(); //获取文件名 var fileName = formFile.FileName; // 获取文件后缀 var fileExtension = Path.GetExtension(fileName); // 判断后缀是否是xlsx或者xls if (fileExtension != ".xlsx" && fileExtension != ".xls") { return DataResult.Failed("文件格式错误"); } var length = formFile.Length; if (length > 1024 * 1024 * 10) { return DataResult.Failed("文件大小不能超过10M"); } var tenantDb = saasService.GetBizDbScopeById(user.TenantId); // MemoryStream 内存流 在内存当中创建一个流(开辟空间) using (var stream = new MemoryStream()) { //将文件写入内存流 formFile.CopyTo(stream); //将流的位置归零 stream.Position = 0; //将内存流转成List集合 var list = await stream.QueryAsync(); var countries = new List(); foreach (var item in list) { var country = new CodeCountry() { CountryCode = item.CountryCode, CountryName = item.CountryName, CountryEnName = item.CountryEnName, Capital = item.Capital, Chau = item.Chau, CountryCode3 = item.CountryCode, Tariff = item.Tariff, Explain = item.Explain, TonnageTax = item.TonnageTax, }; countries.Add(country); } await db.Insertable(countries).ExecuteCommandAsync(); return await Task.FromResult(DataResult.Failed("导入成功!", MultiLanguageConst.DataImportSuccess)); } } /// /// 按Excel导入商品信息 /// /// /// public async Task> ImportGoodsByExcel(IFormFile file) { //未上传文件 if (file == null || file.Length == 0) { return await Task.FromResult(DataResult.Failed("请上传Excel!")); } var formFile = file; //DataResult api_Result = new Api_Result(); //获取文件名 var fileName = formFile.FileName; // 获取文件后缀 var fileExtension = Path.GetExtension(fileName); // 判断后缀是否是xlsx或者xls if (fileExtension != ".xlsx" && fileExtension != ".xls") { return DataResult.Failed("文件格式错误"); } var length = formFile.Length; if (length > 1024 * 1024 * 10) { return DataResult.Failed("文件大小不能超过10M"); } var tenantDb = saasService.GetBizDbScopeById(user.TenantId); // MemoryStream 内存流 在内存当中创建一个流(开辟空间) using (var stream = new MemoryStream()) { //将文件写入内存流 formFile.CopyTo(stream); //将流的位置归零 stream.Position = 0; //将内存流转成List集合 var list = await stream.QueryAsync(); var goods = new List(); foreach (var item in list) { var good = new CodeGoods() { GoodsCode = item.GoodsCode, GoodName = item.GoodName, }; goods.Add(good); } await db.Insertable(goods).ExecuteCommandAsync(); return await Task.FromResult(DataResult.Failed("导入成功!", MultiLanguageConst.DataImportSuccess)); } } /// /// 按Excel导入港口信息 /// /// /// public async Task> ImportPortByExcel(IFormFile file) { //未上传文件 if (file == null || file.Length == 0) { return await Task.FromResult(DataResult.Failed("请上传Excel!")); } var formFile = file; //DataResult api_Result = new Api_Result(); //获取文件名 var fileName = formFile.FileName; // 获取文件后缀 var fileExtension = Path.GetExtension(fileName); // 判断后缀是否是xlsx或者xls if (fileExtension != ".xlsx" && fileExtension != ".xls") { return DataResult.Failed("文件格式错误"); } var length = formFile.Length; if (length > 1024 * 1024 * 10) { return DataResult.Failed("文件大小不能超过10M"); } var tenantDb = saasService.GetBizDbScopeById(user.TenantId); // MemoryStream 内存流 在内存当中创建一个流(开辟空间) using (var stream = new MemoryStream()) { //将文件写入内存流 formFile.CopyTo(stream); //将流的位置归零 stream.Position = 0; //将内存流转成List集合 var list = await stream.QueryAsync(); var ports = new List(); foreach (var item in list) { var good = new CodePort() { PortName = item.PortName, CnName = item.CnName, EdiCode = item.EdiCode, CountryName = item.CountryName, Chau = item.Chau, Explain = item.Explain, PortType = item.PortType, }; ports.Add(good); } await db.Insertable(ports).ExecuteCommandAsync(); return await Task.FromResult(DataResult.Failed("导入成功!", MultiLanguageConst.DataImportSuccess)); } } } }