using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using JNPF.Common.Core.Manager; using JNPF.Common.Security; using JNPF.FriendlyException; using JNPF.VisualDev; using Mapster; using Microsoft.AspNetCore.Http; using SqlSugar; using Tnb.BasicData.Entities; using Tnb.WarehouseMgr.Entities.Enums; using Tnb.WarehouseMgr.Entities; using JNPF.Common.Extension; using JNPF.Logging; using System.Reflection.Emit; namespace Tnb.WarehouseMgr { [OverideVisualDev(ModuleConsts.MODULE_LOCATIONDEFINITION_ID)] public class LocationDefinitionService : ExcelDataImportManager { private readonly ISqlSugarClient _db; private readonly IUserManager _userManager; public LocationDefinitionService(ISqlSugarRepository repository, IUserManager userManager) { _db = repository.AsSugarClient(); _userManager = userManager; OverideFuncs.ImportAsync = DataImport; } private async Task DataImport(IFormFile file) { int row = 0; try { List> dics = await ImportExcelToMemory(file); List locs = new List(); BasLocation loc = new BasLocation(); var carryStdDic = await _db.Queryable().ToDictionaryAsync(x => x.carrystd_code, x => x.id); if (carryStdDic?.Count > 0) { string carryStdId = string.Empty; foreach (var d in dics) { var stdCodeKey = d["carrystd_id"].ToString(); carryStdId = carryStdDic.ContainsKey(stdCodeKey) ? carryStdDic[stdCodeKey]?.ToString() ?? "" : ""; d["carrystd_id"] = carryStdId; loc = d.Adapt(); locs.Add(loc); } } locs.ForEach(x => { x.id = SnowflakeIdHelper.NextId(); x.org_id = _userManager.User.OrganizeId; x.is_lock = 0; x.create_id = _userManager.UserId; x.create_time = DateTime.Now; x.modify_id = null; x.modify_time = null; x.is_mix = 1; }); await _db.Ado.BeginTranAsync(); row = await _db.Insertable(locs).ExecuteCommandAsync(); await _db.Ado.CommitTranAsync(); } catch (Exception ex) { await _db.Ado.RollbackTranAsync(); Log.Error("导入失败", ex); throw Oops.Bah("导入失败"); } return row > 0; } } }