using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using JNPF.Common.Core.Manager; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.Systems.Interfaces.System; using JNPF.VisualDev; using JNPF.VisualDev.Entitys.Dto.VisualDevModelData; using JNPF.VisualDev.Interfaces; using SqlSugar; using Tnb.WarehouseMgr.Entities; using Tnb.WarehouseMgr.Entities.Enums; using Tnb.WarehouseMgr.Interfaces; using JNPF.Common.Extension; using JNPF.VisualDev.Entitys; using Aspose.Cells.Drawing; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; using JNPF.Logging; using JNPF.FriendlyException; using JNPF.Common.Security; using Tnb.BasicData.Entities; using Mapster; using Aop.Api.Domain; using JNPF.VisualDev.Entitys.Dto.VisualDev; namespace Tnb.WarehouseMgr { /// /// 载具台账服务 /// [OverideVisualDev(ModuleConsts.MODULE_WMSCARRY_ID)] public class WmsCarryLedgerService : BaseWareHouseService, IWmsCarryLedgerService { private const string ModuleId = ""; private readonly ISqlSugarClient _db; private readonly IRunService _runService; private readonly IVisualDevService _visualDevService; private readonly IUserManager _userManager; public WmsCarryLedgerService(ISqlSugarRepository repository, IRunService runService, IVisualDevService visualDevService, IUserManager userManager) { _db = repository.AsSugarClient(); _runService = runService; _visualDevService = visualDevService; _userManager = userManager; OverideFuncs.ImportDataAsync = DataImport; } /// /// 载具导入 /// /// /// private async Task DataImport(VisualDevImportDataInput input) { int row = 0; try { List> dics = input.list; List carrys = new List(); Dictionary? locs = null; List locCodes = new(); List cStdCodes = new(); WmsCarryH carryH = new WmsCarryH(); //遍历字典,找出需要查询数据库拿的相关字段 foreach (var d in dics) { var cStdCode = d["carrystd_id"].ToString() ?? string.Empty; locCodes.Add(d["location_code"].ToString() ?? string.Empty); cStdCodes.Add(cStdCode); d["create_time"] = DateTime.Now; d.Remove("modify_time"); carryH = d.Adapt(); carrys.Add(carryH); } var carryStdDic = await _db.Queryable().Where(it => cStdCodes.Contains(it.carrystd_code)).ToDictionaryAsync(x => x.carrystd_code, x => x.id); if (locCodes?.Count > 0) locs = await _db.Queryable().Where(it => locCodes.Contains(it.location_code)).ToDictionaryAsync(x => x.location_code, x => x.id); carrys.ForEach(x => { if (!carryStdDic.Keys.Contains(x.carrystd_id)) throw new AppFriendlyException($"第{carrys.IndexOf(x) + 1}个数据的载具规格有误", 500); x.id = SnowflakeIdHelper.NextId(); x.org_id = _userManager.User.OrganizeId; x.status = 1; x.is_lock = 0; x.carrystd_id = carryStdDic[x.carrystd_id].ToString() ?? throw new AppFriendlyException($"第{carrys.IndexOf(x) + 1}个数据的载具规格有误", 500); x.carry_status = ((int)EnumCarryStatus.空闲).ToString(); if (locs != null && x.location_code != null && x.location_code != string.Empty) x.location_id = locs[x.location_code].ToString() ?? throw new AppFriendlyException($"第{carrys.IndexOf(x) + 1}个数据的库位编号有误", 500); x.out_status = ((int)EnumOutStatus.正常).ToString(); x.is_check = 1; x.create_id = _userManager.UserId; x.modify_id = null; x.modify_time = null; }); if (carrys.Count > 1000) { await _db.Fastest().BulkCopyAsync(carrys); } else if (carrys.Count > 400) { _db.Utilities.PageEach(carrys, 100, async pageList => { await _db.Insertable(pageList).ExecuteCommandAsync(); }); } else { row = await _db.Insertable(carrys).ExecuteCommandAsync(); } } catch (Exception ex) { throw Oops.Bah(ex.Message); } return row > 0; } } }