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; using JNPF.VisualDev.Entitys.Dto.VisualDev; using NPOI.SS.Formula.Functions; namespace Tnb.WarehouseMgr { /// /// 库位定义业务类 /// [OverideVisualDev(ModuleConsts.MODULE_LOCATIONDEFINITION_ID)] public class LocationDefinitionService : BaseWareHouseService { private readonly ISqlSugarClient _db; private readonly IUserManager _userManager; public LocationDefinitionService(ISqlSugarRepository repository, IUserManager userManager) { _db = repository.AsSugarClient(); _userManager = userManager; OverideFuncs.ImportDataAsync = DataImport; } private async Task DataImport(VisualDevImportDataInput input) { int row = 0; try { List> dics = input.list; List locs = new List(); BasLocation loc = new BasLocation(); List cStdCodes = new List(); //遍历字典,找出需要查询数据库拿的相关字段 foreach (var d in dics) { var LCode = d["location_code"].ToString() ?? string.Empty; if (LCode == string.Empty) throw new AppFriendlyException($"第{dics.IndexOf(d) + 1}个数据库位编号不可为空", 500); var isType = d["is_type"].ToString() ?? string.Empty; if (isType == string.Empty) throw new AppFriendlyException($"第{dics.IndexOf(d) + 1}个数据库位类型不可为空", 500); var floor = d["floor"].ToString() ?? string.Empty; if (floor == string.Empty) throw new AppFriendlyException($"第{dics.IndexOf(d) + 1}个数据楼层不可为空", 500); var layers = d["layers"].ToString() ?? string.Empty; var locLine = d["loc_line"].ToString() ?? string.Empty; var locColumn = d["loc_column"].ToString() ?? string.Empty; if (locLine == string.Empty || locColumn == string.Empty || layers == string.Empty) throw new AppFriendlyException($"第{dics.IndexOf(d) + 1}个数据行列层不可为空", 500); var isSign = d["is_sign"].ToString() ?? string.Empty; if(isSign==string.Empty) throw new AppFriendlyException($"第{dics.IndexOf(d) + 1}个数据自动签收不可为空", 500); var cStdCode = d["carrystd_id"].ToString() ?? string.Empty; cStdCodes.Add(cStdCode); d["create_time"] = DateTime.Now; d.Remove("modify_time"); loc = d.Adapt(); locs.Add(loc); } var carryStdDic = await _db.Queryable().Where(it => cStdCodes.Contains(it.carrystd_code)).ToDictionaryAsync(x => x.carrystd_code, x => x.id); locs.ForEach(x => { if (!carryStdDic.Keys.Contains(x.carrystd_id)) throw new AppFriendlyException($"第{locs.IndexOf(x) + 1}个数据的载具规格有误", 500); x.id = SnowflakeIdHelper.NextId(); x.org_id = _userManager.User.OrganizeId; x.is_lock = 0; x.carrystd_id = carryStdDic[x.carrystd_id].ToString() ?? throw new AppFriendlyException($"第{locs.IndexOf(x) + 1}个数据的载具规格有误", 500); x.create_id = _userManager.UserId; x.modify_id = null; x.modify_time = null; x.is_mix = 1; }); row = await _db.Insertable(locs).ExecuteCommandAsync(); } catch (Exception ex) { throw Oops.Bah(ex.Message); } return row > 0; } } }