环形库分流点.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. using PlcSiemens.Core.Extension;
  2. using ServiceCenter.Extensions;
  3. using ServiceCenter.Logs;
  4. using ServiceCenter.SqlSugars;
  5. using SqlSugar;
  6. using System.ComponentModel;
  7. using WCS.Core;
  8. using WCS.Entity;
  9. using WCS.Entity.Protocol.Station;
  10. using WCS.WorkEngineering.Extensions;
  11. using WCS.WorkEngineering.WebApi.Controllers;
  12. using WCS.WorkEngineering.Worlds;
  13. using TaskStatus = WCS.Entity.TaskStatus;
  14. namespace WCS.WorkEngineering.Systems
  15. {
  16. /// <summary>
  17. /// 分流点
  18. /// </summary>
  19. [BelongTo(typeof(SortingBranchWorld))]
  20. [Description("环形库分流点")]
  21. public class 环形库分流点 : DeviceSystem<Device<IStation520, IStation521, IStation523, IStation91>>
  22. {
  23. protected override bool ParallelDo => true;
  24. public override void Do(Device<IStation520, IStation521, IStation523, IStation91> obj)
  25. {
  26. if (obj.Data.VoucherNo != obj.Data2.VoucherNo)
  27. {
  28. if (obj.Data2.TaskNumber != 0 && obj.Data2.TaskNumber != 2)
  29. {
  30. obj.Data.TaskNumber = obj.Data2.TaskNumber;
  31. World.Log($"复写:{obj.Data.TaskNumber}");
  32. }
  33. World.Log($"凭证号不一致,DB520:{obj.Data.VoucherNo}-DB521:{obj.Data2.VoucherNo}", LogLevelEnum.Mid);
  34. return;
  35. }
  36. if (obj.Data3.Status.HasFlag(StationStatus.Run))
  37. {
  38. World.Log("设备运行中");
  39. return;
  40. }
  41. ;
  42. if (!obj.Data3.Status.HasFlag(StationStatus.OT_Status))
  43. {
  44. World.Log("站台货物信息与实际占用不一致", LogLevelEnum.Mid);
  45. return;
  46. }
  47. if (!obj.Data3.Status.HasFlag(StationStatus.PH_Status))
  48. {
  49. World.Log("站台无光电信息", LogLevelEnum.Mid);
  50. return;
  51. }
  52. ;
  53. if (obj.Data2.Request != 1)
  54. {
  55. World.Log("无请求", LogLevelEnum.Mid);
  56. return;
  57. }
  58. var isPut = false;
  59. var nextAdd = GetNext(obj);
  60. if (nextAdd == 9999) return;
  61. var goodsType = obj.Data4.Length.ToInt();
  62. int taskId = 0;
  63. SqlSugarHelper.Do(_db =>
  64. {
  65. var db = _db.Default;
  66. WCS_TaskInfo? taskInfo;
  67. if (obj.Data2.TaskNumber == 1 || obj.Data2.TaskNumber == 3 || obj.Data2.TaskNumber == 2)
  68. {
  69. var type = goodsType.ToString();
  70. //找到一条起点是当前位置且状态小于2的任务
  71. taskInfo = db.Queryable<WCS_TaskInfo>().NoLock().OrderBy(x => x.AddTime).First(x => x.Status < TaskStatus.FinishOfShunt && x.AddrFrom == obj.Entity.Code && x.BarCode.Contains("Error") && x.AddrNext == null && x.GoodsType == goodsType);
  72. if (taskInfo == null)
  73. {
  74. switch (obj.Data2.TaskNumber)
  75. {
  76. case 1:
  77. WmsApi.PalletizingCreateseErrorTasks(obj.Entity.Code, type);
  78. break;
  79. case 3: //bc质量
  80. WmsApi.PalletizingCreateseErrorTasks(obj.Entity.Code, type, 3);
  81. break;
  82. case 2: //南北流错
  83. WmsApi.PalletizingCreateseErrorTasks(obj.Entity.Code, type, 2);
  84. break;
  85. }
  86. return;
  87. }
  88. if (taskInfo.Status == TaskStatus.NewBuild)
  89. {
  90. //更新任务状态
  91. taskInfo.Status = TaskStatus.WaitingToExecute;
  92. taskInfo.EditTime =DateTime.Now;
  93. taskInfo.EditWho = "WCS";
  94. db.UpdateableRowLock(taskInfo).ExecuteCommand();
  95. taskInfo.AddWCS_TASK_DTL(db, obj.Entity.Code, $"初始化异常轮入库任务信息");
  96. return;
  97. }
  98. }
  99. taskInfo = db.Queryable<WCS_TaskInfo>().NoLock().First(v => v.ID == obj.Data2.TaskNumber && v.Status < TaskStatus.FinishOfShunt );
  100. if (taskInfo == null)
  101. {
  102. taskInfo = db.Queryable<WCS_TaskInfo>().NoLock().First(x => x.AddrFrom == obj.Entity.Code && x.BarCode.Contains("Error") && x.Status== TaskStatus.WaitingToExecute && x.AddrNext == null && x.GoodsType == goodsType);
  103. if (taskInfo==null)
  104. {
  105. World.Log($"未找到对应的WCS任务{obj.Data2.TaskNumber},1", LogLevelEnum.High);
  106. return;
  107. }
  108. }
  109. if (taskInfo.AddTime < DateTime.Now.AddHours(-12) && obj.Data2.TaskNumber == taskInfo.ID)
  110. {
  111. obj.Data.TaskNumber = 1;
  112. obj.Data.GoodsEnd = obj.Entity.Code.ToShort();
  113. obj.Data.VoucherNo++;
  114. World.Log($"超时执行记录:任务号[{taskInfo.ID}]-[{obj.Data.TaskNumber}]-[{obj.Data.VoucherNo}]");
  115. return;
  116. }
  117. if (taskInfo.Grade != null && (taskInfo.Grade.ToUpper() != "A") && obj.Data2.TaskNumber == taskInfo.ID)
  118. {
  119. obj.Data.TaskNumber = 3;
  120. obj.Data.GoodsEnd = obj.Entity.Code.ToShort();
  121. obj.Data.VoucherNo++;
  122. World.Log($"BC执行记录:任务号[{taskInfo.ID}]-[{obj.Data.TaskNumber}]-[{obj.Data.VoucherNo}]");
  123. return;
  124. }
  125. if (!taskInfo.BarCode.Contains("Error"))
  126. {
  127. switch (goodsType)
  128. {
  129. case 9 when !taskInfo.BarCode.Contains("HAWS09"):
  130. World.Log($"外检结果与任务条码不匹配--外检结果:[{goodsType}]-条码[{taskInfo.BarCode}]", LogLevelEnum.High);
  131. return;
  132. case 18 when !taskInfo.BarCode.Contains("HAWS18"):
  133. World.Log($"外检结果与任务条码不匹配--外检结果:[{goodsType}]-条码[{taskInfo.BarCode}]", LogLevelEnum.High);
  134. return;
  135. case 34 when !taskInfo.BarCode.Contains("HAWS34"):
  136. World.Log($"外检结果与任务条码不匹配--外检结果:[{goodsType}]-条码[{taskInfo.BarCode}]", LogLevelEnum.High);
  137. return;
  138. case 50 when !taskInfo.BarCode.Contains("HAWS50"):
  139. World.Log($"外检结果与任务条码不匹配--外检结果:[{goodsType}]-条码[{taskInfo.BarCode}]", LogLevelEnum.High);
  140. return;
  141. }
  142. }
  143. switch (obj.Entity.Code)
  144. {
  145. case "418":
  146. if (taskInfo.WarehouseCode != "1N")
  147. {
  148. //throw new KnownException($"当前任务应该当前往{taskInfo.WarehouseCode}库", LogLevelEnum.Mid);
  149. if (taskInfo.WarehouseCode.Contains("N"))
  150. {
  151. EditFlow(taskInfo, "1N", db, obj.Entity.Code);
  152. return;
  153. }
  154. WriteInfo(obj, taskInfo);
  155. return;
  156. }
  157. break;
  158. case "618":
  159. if (taskInfo.WarehouseCode != "1S")
  160. {
  161. //throw new KnownException($"当前任务应该当前往{taskInfo.WarehouseCode}库", LogLevelEnum.Mid);
  162. if (taskInfo.WarehouseCode.Contains("S"))
  163. {
  164. EditFlow(taskInfo, "1S", db, obj.Entity.Code);
  165. return;
  166. }
  167. WriteInfo(obj, taskInfo);
  168. return;
  169. }
  170. break;
  171. case "818":
  172. if (taskInfo.WarehouseCode != "2N")
  173. {
  174. //throw new KnownException($"当前任务应该当前往{taskInfo.WarehouseCode}库", LogLevelEnum.Mid);
  175. if (taskInfo.WarehouseCode.Contains("N"))
  176. {
  177. EditFlow(taskInfo, "2N", db, obj.Entity.Code);
  178. return;
  179. }
  180. WriteInfo(obj, taskInfo);
  181. return;
  182. }
  183. break;
  184. case "1018":
  185. if (taskInfo.WarehouseCode != "2S")
  186. {
  187. //throw new KnownException($"当前任务应该当前往{taskInfo.WarehouseCode}库", LogLevelEnum.Mid);
  188. if (taskInfo.WarehouseCode.Contains("S"))
  189. {
  190. EditFlow(taskInfo, "2S", db, obj.Entity.Code);
  191. return;
  192. }
  193. WriteInfo(obj, taskInfo);
  194. return;
  195. }
  196. break;
  197. case "1218":
  198. if (taskInfo.WarehouseCode != "3N")
  199. {
  200. //throw new KnownException($"当前任务应该当前往{taskInfo.WarehouseCode}库", LogLevelEnum.Mid);
  201. if (taskInfo.WarehouseCode.Contains("N"))
  202. {
  203. EditFlow(taskInfo, "3N", db, obj.Entity.Code);
  204. return;
  205. }
  206. WriteInfo(obj, taskInfo);
  207. return;
  208. }
  209. break;
  210. case "1418":
  211. if (taskInfo.WarehouseCode != "3S")
  212. {
  213. //throw new KnownException($"当前任务应该当前往{taskInfo.WarehouseCode}库", LogLevelEnum.Mid);
  214. if (taskInfo.WarehouseCode.Contains("S"))
  215. {
  216. EditFlow(taskInfo, "3S", db, obj.Entity.Code);
  217. return;
  218. }
  219. WriteInfo(obj, taskInfo);
  220. return;
  221. }
  222. break;
  223. }
  224. var isTruss = false; //表示这个工字轮是要去桁架区域码垛的,默认不作处理
  225. if (goodsType == 18)
  226. {
  227. //判断这个垛型是否是到桁架区域进行码垛
  228. var toTruss = db.Queryable<BillBomsetgrp>().NoLock().First(x => x.BomCode.Contains(taskInfo.MatCode) && x.IsStop == 0);
  229. if (toTruss == null)
  230. {
  231. World.Log($"未找到任务{taskInfo.ID}对应垛型信息,请确认垛型是否停用", LogLevelEnum.Mid);
  232. return;
  233. }
  234. if (toTruss.IsTruss)
  235. {
  236. nextAdd = obj.Entity.Code switch
  237. {
  238. "418" => 455,
  239. "618" => 655,
  240. "818" => 855,
  241. "1018" => 1055,
  242. "1218" => 1255,
  243. "1418" => 1455,
  244. _ => 9999
  245. };
  246. isTruss = true;
  247. }
  248. }
  249. taskInfo = db.Queryable<WCS_TaskInfo>().ReadPastUpdLock().First(v => v.ID == obj.Data2.TaskNumber && v.Status < TaskStatus.FinishOfShunt && !v.BarCode.Contains("Error"));
  250. if (taskInfo == null)
  251. {
  252. taskInfo = db.Queryable<WCS_TaskInfo>().ReadPastUpdLock().First(x => x.AddrFrom == obj.Entity.Code && x.BarCode.Contains("Error") && x.Status== TaskStatus.WaitingToExecute && x.AddrNext == null && x.GoodsType == goodsType);
  253. if (taskInfo==null)
  254. {
  255. World.Log($"未找到对应的WCS任务{obj.Data2.TaskNumber},2", LogLevelEnum.Mid);
  256. return;
  257. };
  258. }
  259. taskId = taskInfo.ID;
  260. if (goodsType != 9 && !isTruss) //09工字轮在环形库分流点只分配下一个地址,不作任何变更
  261. {
  262. taskInfo.AddrNext = nextAdd.ToString();
  263. taskInfo.Status = TaskStatus.FinishOfShunt;
  264. taskInfo.EditTime = DateTime.Now;
  265. taskInfo.WarehouseCode = taskInfo.WarehouseCode.Contains("R") ? taskInfo.WarehouseCode : taskInfo.WarehouseCode + "R";
  266. taskInfo.AddrTo = "Robot";
  267. taskInfo.GoodsType = goodsType;
  268. db.UpdateableRowLock(taskInfo).UpdateColumns(x => new { x.AddrNext, x.Status, x.EditTime, x.WarehouseCode, x.AddrTo, x.GoodsType }).ExecuteCommand();
  269. taskInfo.AddWCS_TASK_DTL(db, obj.Entity.Code, nextAdd.ToString(), "工字轮完成环形库分流");
  270. }
  271. else
  272. {
  273. taskInfo.AddrNext = nextAdd.ToString();
  274. taskInfo.EditTime = DateTime.Now;
  275. taskInfo.GoodsType = goodsType;
  276. db.UpdateableRowLock(taskInfo).UpdateColumns(x => new { x.AddrNext, x.EditTime, x.GoodsType }).ExecuteCommand();
  277. taskInfo.AddWCS_TASK_DTL(db, obj.Entity.Code, nextAdd.ToString(), "工字轮途径环形库分流点");
  278. }
  279. isPut = true;
  280. });
  281. if (!isPut) return;
  282. obj.Data.TaskNumber = taskId;
  283. obj.Data.TaskNumber = taskId;
  284. obj.Data.GoodsStart = obj.Entity.Code.ToShort();
  285. obj.Data.GoodsStart = obj.Entity.Code.ToShort();
  286. obj.Data.GoodsEnd = nextAdd;
  287. obj.Data.GoodsEnd = nextAdd;
  288. obj.Data.SetVoucherNo();
  289. World.Log($"执行记录:任务号[{taskId}]-[{obj.Data.VoucherNo}]");
  290. }
  291. private void WriteInfo(Device<IStation520, IStation521, IStation523, IStation91> device, WCS_TaskInfo task)
  292. {
  293. device.Data.TaskNumber = 2;
  294. device.Data.GoodsEnd = device.Entity.Code.ToShort();
  295. device.Data.VoucherNo++;
  296. World.Log($"南北流错执行记录:任务号[{task.ID}]-[{device.Data.TaskNumber}]-[{device.Data.VoucherNo}]");
  297. }
  298. /// <summary>
  299. /// 编辑流向
  300. /// </summary>
  301. public void EditFlow(WCS_TaskInfo task, string warehouseCode, SqlSugarScopeProvider db, string locCode)
  302. {
  303. WmsApi.EditorialFlow(new FjEditorialFlowRequest() { TaskId = task.ID, LocCode = locCode, WareCode = warehouseCode });
  304. task.WarehouseCode = warehouseCode;
  305. db.UpdateableRowLock(task).UpdateColumns(x=> new {x.WarehouseCode}).ExecuteCommand();
  306. task.AddWCS_TASK_DTL(db, locCode, $"变更流向至{warehouseCode}");
  307. }
  308. public override bool Select(Device dev)
  309. {
  310. return dev.Code is "418" or "618" or "818" or "1018" or "1218" or "1418";
  311. }
  312. /// <summary>
  313. /// 计算非零九的去向
  314. /// </summary>
  315. /// <param name="obj"></param>
  316. /// <returns></returns>
  317. public short GetNext(Device<IStation520, IStation521, IStation523, IStation91> obj)
  318. {
  319. return obj.Data4.Length switch
  320. {
  321. 9 => obj.Entity.Code switch
  322. {
  323. "418" => 455,
  324. "618" => 655,
  325. "818" => 855,
  326. "1018" => 1055,
  327. "1218" => 1255,
  328. "1418" => 1455,
  329. _ => 9999
  330. },
  331. 18 => obj.Entity.Code switch
  332. {
  333. "418" => 444,
  334. "618" => 644,
  335. "818" => 844,
  336. "1018" => 1044,
  337. "1218" => 1244,
  338. "1418" => 1444,
  339. _ => 9999
  340. },
  341. 50 => obj.Entity.Code switch
  342. {
  343. "418" => 426,
  344. "618" => 626,
  345. "818" => 826,
  346. "1018" => 1026,
  347. "1218" => 1226,
  348. "1418" => 1426,
  349. _ => 9999
  350. },
  351. 34 => obj.Entity.Code switch
  352. {
  353. "418" => 435,
  354. "618" => 635,
  355. "818" => 835,
  356. "1018" => 1035,
  357. "1218" => 1235,
  358. "1418" => 1435,
  359. _ => 9999
  360. },
  361. _ => 9999
  362. };
  363. }
  364. }
  365. }