From 24c74d28881ce4573253b1d382777ca9b1205863 Mon Sep 17 00:00:00 2001 From: qianjiawei <1184704771@qq.com> Date: Thu, 14 Dec 2023 09:33:04 +0800 Subject: [PATCH] ctu --- .../Tnb.WarehouseMgr/WareHouseService.cs | 303 ++++++++++++++++-- 1 file changed, 278 insertions(+), 25 deletions(-) diff --git a/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs b/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs index f6e9f64a..4a9b337f 100644 --- a/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs +++ b/WarehouseMgr/Tnb.WarehouseMgr/WareHouseService.cs @@ -1,6 +1,8 @@ using System.Diagnostics; using System.Dynamic; using System.Linq.Expressions; +using System.Security.Policy; +using Aop.Api.Domain; using JNPF; using JNPF.Common.Contracts; using JNPF.Common.Core.Manager; @@ -15,14 +17,17 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.CodeAnalysis; using Microsoft.Extensions.Configuration; +using NetTaste; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using NPOI.OpenXmlFormats.Dml; +using Org.BouncyCastle.Crypto; using SqlSugar; using Tnb.BasicData.Entities; using Tnb.Common.Extension; using Tnb.Common.Redis; using Tnb.Common.Utils; +using Tnb.ProductionMgr.Entities; using Tnb.WarehouseMgr.Entities; using Tnb.WarehouseMgr.Entities.Configs; using Tnb.WarehouseMgr.Entities.Consts; @@ -50,10 +55,11 @@ namespace Tnb.WarehouseMgr private readonly ElevatorControlConfiguration _eleCtlCfg = App.Configuration.Build(); private static Dictionary locMap = new Dictionary(StringComparer.OrdinalIgnoreCase); private readonly IConfiguration _configuration; + private readonly RedisData _redisData; public Func AddUnExecuteTask { get; set; } - public WareHouseService(ISqlSugarRepository repository, IDictionaryDataService dictionaryDataService, + public WareHouseService(ISqlSugarRepository repository, IDictionaryDataService dictionaryDataService, RedisData redisData, IBillRullService billRullService, IUserManager userManager, ICacheManager cacheManager, IElevatorControlService elevatorControlService //IConfiguration configuration ) : base(repository.AsSugarClient()) @@ -64,6 +70,7 @@ namespace Tnb.WarehouseMgr _userManager = userManager; _cacheManager = cacheManager; _elevatorControlService = elevatorControlService; + _redisData = redisData; //_configuration = configuration; } /// @@ -270,11 +277,6 @@ namespace Tnb.WarehouseMgr SqlSugarClient db = _db.CopyNew(); try { - //一楼中储仓CTU - CTUTaskExecute(db); - - - //获取电梯数据 /*List elevatorList = await db.Queryable().InnerJoin((a, b) => a.id == b.bill_id) .Where((a, b) => a.enabled == 1) @@ -328,7 +330,8 @@ namespace Tnb.WarehouseMgr } preTasks = normalPreTasks.Concat(agvElevatorTasks).Concat(elePreTasks).ToList(); - + //一楼中储仓CTU + CTUTaskExecute(); List ids = preTasks.Select(x => x.id).Distinct().ToList(); List? preTaskCodes = await db.Queryable().Where(it => ids.Contains(it.bill_id)).ToListAsync(); if (preTasks.Count > 0) @@ -501,6 +504,8 @@ namespace Tnb.WarehouseMgr } } } + + } catch (Exception ex) when (ex is HttpRequestException hReqEx) { @@ -519,30 +524,278 @@ namespace Tnb.WarehouseMgr agvCts.Dispose(); } } + #region CTU /// /// 生成CTU任务执行 /// /// - private async Task CTUTaskExecute(SqlSugarClient db) + private async Task CTUTaskExecute() { - List preTasks = await db.Queryable() - .InnerJoin((a, b) => a.startlocation_id == b.location_id && a.carry_id == b.id) - .InnerJoin((a, b, c) => a.area_id == c.id) - .InnerJoin((a, b, c, d) => a.endlocation_id == d.id && d.is_use == "0") - .Where(a => a.status != WmsWareHouseConst.PRETASK_BILL_STATUS_DXF_ID && !string.IsNullOrWhiteSpace(a.startlocation_id)) - .OrderBy(a => new { priority = SqlFunc.Desc(a.priority), a.bill_code }) - .Select((a, b, c, d) => new WmsPretaskH - { - move_num = c.move_num, - third_eqp_type = c.third_eqp_type, - }, true) - .ToListAsync(); - List CTUTasks = preTasks - .Where(it => it.endlocation_code.StartsWith("DT", StringComparison.OrdinalIgnoreCase) && - !it.area_code.Contains("ELE", StringComparison.OrdinalIgnoreCase)) - .ToList(); - } + try + { + var db = _db.CopyNew(); + Logger.Information("进入CTUTaskExecute"); + List CTUTasks = await db.Queryable() + .InnerJoin((a, b) => a.area_id == b.id) + .Where(a => a.status == WmsWareHouseConst.PRETASK_BILL_STATUS_DXF_ID) + .Where((a, b) => b.code == "B")//一楼中储仓 + .OrderBy(a => a.create_id) + .Select((a, b) => new WmsPretaskH + { + move_num = b.move_num, + third_eqp_type = b.third_eqp_type, + }, true).ToListAsync(); + List TaskCodes = await db.Queryable().Where(it => CTUTasks.Select(p => p.id).ToList().Contains(it.bill_id)).ToListAsync(); + var InTasks = CTUTasks.Where(a => a.task_type == WmsWareHouseConst.WMS_PRETASK_INSTOCK_TYPE_ID).ToList(); + var OutTasks = CTUTasks.Where(a => a.task_type == WmsWareHouseConst.WMS_PRETASK_OUTSTOCK_TYPE_ID).ToList(); + var OriginDistaskHs = await db.Queryable() + .InnerJoin((a, b) => a.area_id == b.id) + .Where((a, b) => b.code == "B" && a.status == WmsWareHouseConst.TASK_BILL_STATUS_DZX_ID) + .OrderBy(a => a.bill_code) + .ToListAsync(); + List DistaskHs = new List(); + List UpDistaskHs = new List(); + List DistaskCodes = new List(); + var inCtuExec = new List(); + var outCtuExec = new List(); + foreach (var item in InTasks) + { + WmsDistaskH distaskH = item.Adapt(); + distaskH.id = SnowflakeIdHelper.NextId(); + distaskH.status = WmsWareHouseConst.TASK_BILL_STATUS_DZX_ID; + distaskH.is_chain = 1; + distaskH.create_time = DateTime.Now; + var billcode = GetBillCode(OriginDistaskHs, DistaskHs, distaskH); + distaskH.groups = billcode.Substring(0, billcode.Length - 2); + distaskH.bill_code = billcode; + var num = int.Parse(distaskH.bill_code.Substring(billcode.Length - 1, 1)); + if (num == item.move_num) + { + distaskH.status = WmsWareHouseConst.TASK_BILL_STATUS_YXD_ID; + inCtuExec.Add(distaskH); + if (OriginDistaskHs.Where(p => p.groups == distaskH.groups).Any()) + { + OriginDistaskHs.Where(p => p.groups == distaskH.groups).ToList().ForEach(p => p.status = WmsWareHouseConst.TASK_BILL_STATUS_YXD_ID); + UpDistaskHs.AddRange(OriginDistaskHs.Where(p => p.groups == distaskH.groups).ToList()); + inCtuExec.AddRange(OriginDistaskHs.Where(p => p.groups == distaskH.groups).ToList()); + } + if (DistaskHs.Where(p => p.groups == distaskH.groups).Any()) + { + DistaskHs.Where(p => p.groups == distaskH.groups).ToList().ForEach(p => p.status = WmsWareHouseConst.TASK_BILL_STATUS_YXD_ID); + inCtuExec.AddRange(DistaskHs.Where(p => p.groups == distaskH.groups).ToList()); + } + } + List preTaskCodes = TaskCodes.FindAll(x => x.bill_id == distaskH.pretask_id); + List disTaskCodes = preTaskCodes.Adapt>(); + disTaskCodes.ForEach(x => + { + x.id = SnowflakeIdHelper.NextId(); + x.bill_id = distaskH.id; + x.create_time = DateTime.Now; + }); + DistaskHs.Add(distaskH); + DistaskCodes.AddRange(disTaskCodes); + } + foreach (var item in OutTasks) + { + WmsDistaskH distaskH = item.Adapt(); + distaskH.id = SnowflakeIdHelper.NextId(); + distaskH.status = WmsWareHouseConst.TASK_BILL_STATUS_DZX_ID; + distaskH.is_chain = 1; + distaskH.create_time = DateTime.Now; + var billcode = GetBillCode(OriginDistaskHs, DistaskHs, distaskH); + distaskH.groups = billcode.Substring(0, billcode.Length - 2); + distaskH.bill_code = billcode; + var num = int.Parse(distaskH.bill_code.Substring(billcode.Length - 1, 1)); + if (num == item.move_num) + { + distaskH.status = WmsWareHouseConst.TASK_BILL_STATUS_YXD_ID; + outCtuExec.Add(distaskH); + if (OriginDistaskHs.Where(p => p.groups == distaskH.groups).Any()) + { + OriginDistaskHs.Where(p => p.groups == distaskH.groups).ToList().ForEach(p => p.status = WmsWareHouseConst.TASK_BILL_STATUS_YXD_ID); + + UpDistaskHs.AddRange(OriginDistaskHs.Where(p => p.groups == distaskH.groups).ToList()); + outCtuExec.AddRange(OriginDistaskHs.Where(p => p.groups == distaskH.groups).ToList()); + } + if (DistaskHs.Where(p => p.groups == distaskH.groups).Any()) + { + DistaskHs.Where(p => p.groups == distaskH.groups).ToList().ForEach(p => p.status = WmsWareHouseConst.TASK_BILL_STATUS_YXD_ID); + outCtuExec.AddRange(DistaskHs.Where(p => p.groups == distaskH.groups).ToList()); + } + } + List preTaskCodes = TaskCodes.FindAll(x => x.bill_id == distaskH.pretask_id); + List disTaskCodes = preTaskCodes.Adapt>(); + disTaskCodes.ForEach(x => + { + x.id = SnowflakeIdHelper.NextId(); + x.bill_id = distaskH.id; + x.create_time = DateTime.Now; + }); + DistaskHs.Add(distaskH); + DistaskCodes.AddRange(disTaskCodes); + } + await db.Ado.BeginTranAsync(); + int row = 0; + if (UpDistaskHs.Count > 0) + { + await db.Updateable().SetColumns(it => new WmsDistaskH { status = WmsWareHouseConst.TASK_BILL_STATUS_YXD_ID }).Where(it => UpDistaskHs.Select(p => p.id).ToList().Contains(it.id)).ExecuteCommandAsync(); + } + if (DistaskHs.Count > 0) + { + row = await db.Insertable(DistaskHs).ExecuteCommandAsync(); + } + if (DistaskCodes.Count > 0) + { + await db.Insertable(DistaskCodes).ExecuteCommandAsync(); + } + if (row > 0) + { + List preTaskIds = DistaskHs.Select(x => x.pretask_id).ToList(); + await db.Updateable().SetColumns(it => new WmsPretaskH { status = WmsWareHouseConst.PRETASK_BILL_STATUS_YXF_ID }).Where(it => preTaskIds.Contains(it.id)).ExecuteCommandAsync(); + } + await db.Ado.CommitTranAsync(); + //判断 + if (DistaskHs.Where(p => p.status == WmsWareHouseConst.TASK_BILL_STATUS_DZX_ID).Any()) + { + timer = new Timer(TimerExec, null, TimeSpan.Zero, TimeSpan.FromMinutes(10)); + } + if (inCtuExec.Count > 0) + { + //呼叫ctu入库 + CancellationTokenSource Ctu = new(); + await CallingCTU(inCtuExec, Ctu.Token, 1); + Ctu.Dispose(); + } + if (outCtuExec.Count > 0) + { + //呼叫ctu出库 + CancellationTokenSource Ctu = new(); + await CallingCTU(outCtuExec, Ctu.Token, 0); + Ctu.Dispose(); + } + } + catch(Exception ex) + { + Logger.Information("CTUTaskExecute方法报错"+ex.Message); + } + } + //判断生成bill_code + private string GetBillCode(List OriginDistaskH, List NewdistaskHs, WmsDistaskH distaskH) + { + string BillCode = string.Empty; + //入库 + if (distaskH.task_type == WmsWareHouseConst.WMS_PRETASK_INSTOCK_TYPE_ID) + { + var orgdistaskHs = OriginDistaskH.Where(p => p.status == WmsWareHouseConst.TASK_BILL_STATUS_DZX_ID && p.task_type == WmsWareHouseConst.WMS_PRETASK_INSTOCK_TYPE_ID && p.startpoint_id == distaskH.startpoint_id).ToList(); + var newdistaskHs = NewdistaskHs = OriginDistaskH.Where(p => p.status == WmsWareHouseConst.TASK_BILL_STATUS_DZX_ID && p.task_type == WmsWareHouseConst.WMS_PRETASK_INSTOCK_TYPE_ID && p.startpoint_id == distaskH.startpoint_id).ToList(); + if ((orgdistaskHs.Count + newdistaskHs.Count) == 0) + { + var groups = _billRullService.GetBillNumber(WmsWareHouseConst.WMS_TASK_EXECUTE_ENCODE).Result; + BillCode = $"{groups}-1"; + } + else + { + var groups = orgdistaskHs.Count > 0 ? orgdistaskHs.First().groups : newdistaskHs.First().groups; + BillCode = $"{groups}-" + (orgdistaskHs.Count + newdistaskHs.Count + 1); + } + } + else if (distaskH.task_type == WmsWareHouseConst.WMS_PRETASK_OUTSTOCK_TYPE_ID) + { + var orgdistaskHs = OriginDistaskH.Where(p => p.status == WmsWareHouseConst.TASK_BILL_STATUS_DZX_ID && p.task_type == WmsWareHouseConst.WMS_PRETASK_OUTSTOCK_TYPE_ID && p.endpoint_id == distaskH.endpoint_id).ToList(); + var newdistaskHs = NewdistaskHs = OriginDistaskH.Where(p => p.status == WmsWareHouseConst.TASK_BILL_STATUS_DZX_ID && p.task_type == WmsWareHouseConst.WMS_PRETASK_OUTSTOCK_TYPE_ID && p.endpoint_id == distaskH.endpoint_id).ToList(); + if ((orgdistaskHs.Count + newdistaskHs.Count) == 0) + { + var groups = _billRullService.GetBillNumber(WmsWareHouseConst.WMS_TASK_EXECUTE_ENCODE).Result; + BillCode = $"{groups}-1"; + } + else + { + var groups = orgdistaskHs.Count > 0 ? orgdistaskHs.First().groups : newdistaskHs.First().groups; + BillCode = $"{groups}-" + (orgdistaskHs.Count + newdistaskHs.Count + 1); + } + } + return BillCode; + } + private Timer? timer; + private async void TimerExec(object e) + { + try + { + Logger.Information("CTU任务执行TimerExec"); + SqlSugarClient db = _db.CopyNew(); + var list = db.Queryable().InnerJoin((a, b) => a.area_id == b.id) + .Where((a, b) => b.code == "B" && a.status == WmsWareHouseConst.TASK_BILL_STATUS_DZX_ID) + .OrderBy(a => a.bill_code) + .ToList(); + var data = list.GroupBy(p => p.groups).Select(p => new + { + groups = p.Key, + time = p.Max(a => a.create_time) + + }).ToList(); + var date = DateTime.Now; + foreach (var item in data) + { + if (date.Subtract(item.time).Minutes >= 10) + { + var execlist = list.Where(p => p.groups == item.groups).ToList(); + await db.Updateable().SetColumns(it => new WmsDistaskH { status = WmsWareHouseConst.TASK_BILL_STATUS_YXD_ID }).Where(it => execlist.Select(p => p.id).ToList().Contains(it.id)).ExecuteCommandAsync(); + await db.Updateable().SetColumns(it => new WmsPretaskH { status = WmsWareHouseConst.PRETASK_BILL_STATUS_YXF_ID }).Where(it => execlist.Select(x => x.pretask_id).ToList().Contains(it.id)).ExecuteCommandAsync(); + CancellationTokenSource Ctu = new(); + int type = execlist.First().task_type == WmsWareHouseConst.WMS_PRETASK_INSTOCK_TYPE_ID ? 1 : 0; + await CallingCTU(execlist, Ctu.Token, type); + Ctu.Dispose(); + } + } + } + catch (Exception ex) + { + Logger.Information("CTU任务执行TimerExec报错" + ex.Message); + } + finally + { + timer?.Dispose(); + } + + } + private async Task CallingCTU(List distaskHs, CancellationToken token,int type) + { + Logger.Information("CTU任务执行请求第三方接口"); + try + { + AgvRequestConfig requestCfg = App.Configuration.Build(); + string url = requestCfg.AgvRequestUrls.CreateTaskChainUrl; + var taskChainCodeDic = distaskHs.Where(t => !t.groups.IsNullOrWhiteSpace()).GroupBy(g => g.groups!) + .ToDictionary(x => x.Key, x => x.Select(it => new + { + taskCode = it.bill_code, + sourceName = it.startpoint_code, + targetName = it.endpoint_code, + containerCode = it.carry_code, + })); + + foreach ((string k, object v) in taskChainCodeDic) + { + dynamic reqBody = new ExpandoObject(); + reqBody.taskChainCode = k; + reqBody.type = (int)EnumTaskChainType.CTU; + reqBody.sequential = false; + reqBody.taskChainPriority = 0; + reqBody.taskList = v; + reqBody.inOut = type; + Logger.Information($"请求参数:{JsonConvert.SerializeObject(reqBody)}"); + dynamic respBody = await HttpClientHelper.PostStreamAsync(url, reqBody, token); + Logger.Information($"调用Agv接口响应结果:{respBody}"); + } + } + catch (Exception ex) + { + Logger.Information("CTU任务执行请求第三方接口报错"+ex.Message); + } + } + #endregion /// /// 呼梯操作 ///