using System.Text; using JNPF; using JNPF.Common.Extension; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using SqlSugar; using Tnb.WarehouseMgr.Entities; using Tnb.WarehouseMgr.Entities.Dto.Queries; using Tnb.WarehouseMgr.Entities.Entity; namespace Tnb.WarehouseMgr { public class DevServBase : BaseWareHouseService { protected static Dictionary s_elevatorMap = new(); private static readonly Lazy initializationTask; private static SqlSugarScope context; private readonly ISqlSugarClient _db; public static Dictionary s_eleUseStatusDic = new(); public static Dictionary s_loadedStatusDic = new(); static DevServBase() { //initializationTask = new Lazy(InitializeAsync); _ = Task.Run(() => InitializeAsync()); } public DevServBase(ISqlSugarClient db) { _db = db; } private static async Task InitializeAsync() { ConnectionStringsOptions connectionOpts = App.GetConfig("ConnectionStrings", true); ConnectionConfig cfg = new() { ConfigId = connectionOpts.ConfigId, ConnectionString = connectionOpts.ConnectString, DbType = DbType.PostgreSQL, IsAutoCloseConnection = true, }; context = new(cfg); s_elevatorMap = await context.Queryable().ToDictionaryAsync(x => x.elevator_id, x => x.elevator_code); s_loadedStatusDic = context.Queryable().ToList().ToDictionary(x => x.elevator_id, x => x.is_use); if (s_eleUseStatusDic.Count < 1) { foreach (var (k, _) in s_elevatorMap) { s_loadedStatusDic[k] = 0; } } } //public static Task InitializationTask => initializationTask.Value; /// /// 获取电梯根据任务单号 /// /// /// taskCode:子任务编号 /// endlocation_id:目标库位ID /// /// protected async Task FindElevatorFromPars(ElevagorInfoQuery input) { var whereExpable = Expressionable.Create() .And((a, b, c) => a.enabled == 1); if (!input.taskCode.IsNullOrEmpty()) { whereExpable.AndIF(!SqlFunc.IsNullOrEmpty(input.taskCode), (a, b, c) => c.bill_code == input.taskCode); } if (!input.endlocation_id.IsNullOrEmpty()) { whereExpable.AndIF(!SqlFunc.IsNullOrEmpty(input.endlocation_id), (a, b, c) => b.location_id == input.endlocation_id); } if (!input.startlocation_id.IsNullOrEmpty()) { whereExpable.AndIF(!SqlFunc.IsNullOrEmpty(input.startlocation_id), (a, b, c) => b.location_id == input.startlocation_id); } var ele = await _db.CopyNew().Queryable().InnerJoin((a, b) => a.id == b.bill_id) .InnerJoin((a, b, c) => b.location_code == c.endlocation_code || b.location_code == c.startlocation_code) .Where(whereExpable.ToExpression()) .WhereIF(!SqlFunc.IsNullOrEmpty(input.sourceName) && SqlFunc.StartsWith("DT-R", input.sourceName), (a, b, c) => c.startpoint_code == input.sourceName) .WhereIF(!SqlFunc.IsNullOrEmpty(input.sourceName) && SqlFunc.StartsWith("DT-C", input.sourceName), (a, b, c) => c.endpoint_code == input.sourceName) .Select((a, b, c) => new WmsElevatorH { bill_code = c.bill_code, device_id = a.elevator_id, end_floor = c.end_floor }, true) .FirstAsync(); return ele; } } public static class CustomLoggerExtenstions { public static void Debug(this ILogger logger, string message, params object[] parameters) { logger.Debug(message, parameters); } public static void Information(this ILogger logger, string message, params object[] parameters) { logger.LogInformation(message, parameters); } public static void Error(this ILogger logger, string message, Exception ex, params object[] parameters) { logger.LogError(ex, message, parameters); } public static void Error(this ILogger logger, string message, params object[] parameters) { logger.LogError(message, parameters); } } }