1
czw
2025-07-01 d4fd2574b94a355de638c0a3ce5436aec8d9edea
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
using HH.WCS.QingXigongchang.dispatch;
using HH.WCS.QingXigongchang.process;
using HH.WCS.QingXigongchang.util;
using Newtonsoft.Json;
using NLog.Fluent;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
 
namespace HH.WCS.QingXigongchang.wms
{
    internal class TaskHelper
    {
        internal static string GenerateTaskNo()
        {
            var id = SYSHelper.GetSerialNumber("任务号", "TN");
            var date = DateTime.Now.ToString("yyMMdd");
            return $"TN{date}{id.ToString().PadLeft(4, '0')}";
        }
        internal static bool UpdateStatus(WMSTask task, string status)
        {
            var res = false;
            var db = new SqlHelper<WMSTask>().GetInstance();
            task.S_B_STATE = status;
            db.Updateable(task).UpdateColumns(it => new { it.S_B_STATE }).ExecuteCommand();
            return res;
        }
        internal static bool UpdateInfo(WMSTask task, string sourceNo, string endBit, string status)
        {
            var res = false;
            var db = new SqlHelper<WMSTask>().GetInstance();
            task.S_B_STATE = status;
            task.S_SRC_NO = sourceNo;
            task.S_END_LOC = endBit;
            db.Updateable(task).UpdateColumns(it => new { it.S_B_STATE, it.S_SRC_NO, it.S_END_LOC }).ExecuteCommand();
            return res;
        }
        internal static WMSTask GetTask(string no)
        {
            var db = new SqlHelper<WMSTask>().GetInstance();
            var task = db.Queryable<WMSTask>().Where(a => a.S_TASK_NO.Trim() == no).ToList().FirstOrDefault();
            if (task != null)
            {
                task.S_TASK_NO = task.S_TASK_NO.Trim();
                task.S_START_LOC = task.S_START_LOC.Trim();
                task.S_END_LOC = task.S_END_LOC.Trim();
            }
            return task;
        }
        internal static WMSTask GetTask(Expression<Func<WMSTask, bool>> expression, Expression<Func<WMSTask, object>> expression2, OrderByType type = OrderByType.Asc)
        {
            var db = new SqlHelper<WMSTask>().GetInstance();
            var task = db.Queryable<WMSTask>().Where(expression).OrderBy(expression2, type).ToList().FirstOrDefault();
            if (task != null)
            {
                task.S_TASK_NO = task.S_TASK_NO.Trim();
                task.S_START_LOC = task.S_START_LOC.Trim();
                task.S_END_LOC = task.S_END_LOC.Trim();
            }
            return task;
        }
        internal static WMSTask GetTaskBySrcNo(string no)
        {
            var db = new SqlHelper<WMSTask>().GetInstance();
            var task = db.Queryable<WMSTask>().Where(a => a.S_SRC_NO.Trim() == no).First();
            return task;
        }
        internal static List<WMSTask> GetTaskByStart(string bit)
        {
            var db = new SqlHelper<WMSTask>().GetInstance();
            var task = db.Queryable<WMSTask>().Where(a => a.S_START_LOC.Trim() == bit.Trim()).ToList();
            return task;
        }
        internal static List<WMSTask> GetTaskByEnd(string bit)
        {
            var db = new SqlHelper<WMSTask>().GetInstance();
            var task = db.Queryable<WMSTask>().Where(a => a.S_END_LOC.Trim() == bit.Trim()).ToList();
            return task;
        }
        internal static List<WMSTask> GetTaskByType(string taskType)
        {
            var db = new SqlHelper<WMSTask>().GetInstance();
            return db.Queryable<WMSTask>().Where(a => a.S_TYPE.Trim() == taskType).ToList();
        }
        internal static bool CreateTask(string no, string from, string to, string taskType, int pri, string cntrInfo, int cntrCount = 1, int startLayer = 1, int endLayer = 1, string note = "", bool lockLoc = false, string TwoEndLoc = "")
        {
            var fromLoc = LocationHelper.GetLoc(from);
            var endLoc = LocationHelper.GetLoc(to);
 
            //NDCHelper.ChangeParam(taskNo, agvsite, 2);
            //NDCHelper.ChangeParam(taskNo, 2, 6);
 
            if (fromLoc.S_AREA_CODE == "YWLRGDD")
            {
                //查询任务表  要求是只能生成两个任务 起点库区为YWLRGDD  hh_plg  2025年6月9日 
                List<string> TaskState = new List<string>() { "完成", "取消" };
                var TaskListNum = LocationHelper.GetList<WMSTask>(x => x.S_START_LAREA == "YWLRGDD" && !TaskState.Contains(x.S_B_STATE));
                if (TaskListNum.Count() >= 2)
                {
                    LogHelper.Info($"CreateTransport-  CreateTask Error" + $"起点库区为YWLRGDD 则最多只能生成两个任务");
                    return false;
                }
            }
            else if (endLoc.S_AREA_CODE == "YWLWJJB")
            {
                //查询任务表  要求是只能生成两个任务 起点库区为YWLWJJB  hh_plg  2025年6月9日 
                List<string> TaskState = new List<string>() { "完成", "取消" };
                var TaskListNum = LocationHelper.GetList<WMSTask>(x => x.S_END_LAREA == "YWLWJJB" && !TaskState.Contains(x.S_B_STATE));
                if (TaskListNum.Count() >= 2)
                {
                    LogHelper.Info($"CreateTransport-  CreateTask Error" + $"终点库区为YWLWJJB 则最多只能生成两个任务");
                    return false;
                }
            }
            if (!taskType.Contains("清溪纸箱"))
            {
                if (fromLoc == null || endLoc == null)
                {
                    LogHelper.Info($"CreateTransport-  CreateTask Error" + $"起点或终点货位数据null");
                    return false;
                }
                if (fromLoc.S_AREA_CODE != "YWLWJJB")
                {
                    if (fromLoc.S_LOCK_STATE.Trim() != "无" || endLoc.S_LOCK_STATE.Trim() != "无")
                    {
                        LogHelper.Info($"CreateTransport-  CreateTask Error" + $"起点 {fromLoc.S_LOCK_STATE}或终点{endLoc.S_LOCK_STATE}状态不为 无 ");
                        return false;
                    }
                }
                if (!taskType.Contains("成品满框-入库"))
                    if (endLoc.N_CURRENT_NUM >= endLayer || fromLoc.N_CURRENT_NUM < startLayer)
                    {
                        LogHelper.Info($"CreateTransport-  CreateTask Error" + $"起点数量{fromLoc.N_CURRENT_NUM}<{startLayer}或者{endLoc.N_CURRENT_NUM}>={endLayer} ");
                        return false;
                    }
            }
            WMSTask wmsTask = new WMSTask()
            {
                S_TASK_NO = GenerateTaskNo(),
                S_START_LAREA = fromLoc.S_AREA_CODE,
                S_END_LAREA = endLoc.S_AREA_CODE,
                S_DEPART_NAME = endLoc.S_WH_CODE,//taskType.Contains("原物料") ? "原物料" : (endLoc.S_WH_CODE == "CK002" ? "瓶盖" : GetDeptName(taskType)),
                S_START_LOC = from,
                S_END_LOC = to,
                S_TYPE = taskType,
                S_SRC_NO = no,
                N_PRIORITY = pri,
                S_NOTE = note,
                S_TWO_END_LOC = TwoEndLoc,
                S_WORK_MODE = "agv",
                S_B_STATE = "未执行",
                S_CNTRS = cntrInfo,
                N_START_LAYER = startLayer,
                N_END_LAYER = endLayer,
                N_CNTR_COUNT = cntrCount
            };
 
            LogHelper.Info($"CreateTransport-  CreateTask" + $"{JsonConvert.SerializeObject(wmsTask)}");
            LogHelper.Info($"CreateTransport-  CreateTask {wmsTask.S_TASK_NO} 起点:{JsonConvert.SerializeObject(from)}");
            LogHelper.Info($"CreateTransport-  CreateTask {wmsTask.S_TASK_NO} 终点:{JsonConvert.SerializeObject(to)}");
            return CreateTask(wmsTask, lockLoc);
        }
        internal static string GetDeptName(string taskType)
        {
            if (taskType.Contains("成品")) return "成品";
 
            else return "瓶坯";
        }
        internal static bool CheckExist(string no)
        {
            return GetTask(no.Trim()) != null;
        }
        internal static bool UpdateStatus(string no, string status)
        {
            var res = false;
            var db = new SqlHelper<WMSTask>().GetInstance();
            var task = db.Queryable<WMSTask>().Where(a => a.S_TASK_NO.Trim() == no).First();
            if (task != null)
            {
                task.S_B_STATE = status;
                //需要判断任务是否失败或者已完成,不允许再修改
                res = db.Updateable(task).UpdateColumns(it => new { it.S_B_STATE }).ExecuteCommand() > 0;
            }
            return res;
        }
        internal static bool AddActionRecord(string no, int state, string forkliftNo, string extData)
        {
            var db = new SqlHelper<WmsTaskAction>().GetInstance();
            var action = new WmsTaskAction()
            {
                S_ACTION_CODE = state.ToString(),
                S_TASK_NO = no,
                S_EQ_NO = forkliftNo,
                S_EQ_TYPE = "agv",
                S_DATA = extData
            };
            return db.Insertable(action).ExecuteCommand() > 0;
        }
 
        internal static bool lOCReSetValue(Expression<Func<WMSTask, bool>> exQ, Action<WMSTask> exu, Action<List<WMSTask>, SqlSugarClient> action = null)
        {
            bool res = false;
            SqlSugarClient db = new SqlHelper<object>().GetInstance();
            List<WMSTask> models = db.Queryable<WMSTask>().Where(exQ).ToList();
            if (models != null && models.Count > 0)
            {
                if (exu != null)
                {
                    res = (db.Updateable(models).ReSetValue(exu).ExecuteCommand() > 0);
                    LogHelper.Info("lOCReSetValue:结果" + res.ToString());
                }
                action?.Invoke(models, db);
            }
            else
            {
                LogHelper.Info("lOCReSetValue 失败,没有找到数据 ");
            }
            return res;
        }
 
        internal static bool CheckActionRecordExist(string no, string code)
        {
            var db = new SqlHelper<WmsTaskAction>().GetInstance();
            return db.Queryable<WmsTaskAction>().Count(a => a.S_TASK_NO.Trim() == no.Trim() && a.S_ACTION_CODE == code) > 0;
        }
        internal static void Begin(WMSTask task)
        {
            var db = new SqlHelper<WMSTask>().GetInstance();
            if (task != null)
            {
                if (task.S_B_STATE.Trim() == "已推送")
                {
                    task.S_B_STATE = "执行中";
                    task.T_START_TIME = DateTime.Now;
                    db.Updateable(task).UpdateColumns(it => new { it.S_B_STATE, it.T_START_TIME }).ExecuteCommand();
                }
            }
        }
        internal static void End(WMSTask task)
        {
            var db = new SqlHelper<WMSTask>().GetInstance();
 
 
            //plg 2025年6月13日 10:42:24
            if (task.S_END_LAREA == "YWLWJJB" && task.S_START_LAREA != "YWLRGDD" && !string.IsNullOrEmpty(task.S_TWO_END_LOC))
            {
                var model = db.Queryable<Location>().Where(a => a.S_LOC_CODE == task.S_TWO_END_LOC).First();
                if (model != null)
                {
                    //先解锁终点 在锁定
                    db.Ado.BeginTran();
                    try
                    {
                        model.S_LOCK_STATE = "无";
                        var res = db.Updateable(model).UpdateColumns(it => new { it.S_LOCK_STATE }).ExecuteCommand() > 0;
                        LogHelper.Info(task.S_TWO_END_LOC + "LockLoc:锁结果" + res);
                        db.Ado.CommitTran();
                    }
                    catch (Exception ex)
                    {
                        db.Ado.RollbackTran();
                        LogHelper.Info("CreateTask 失败 " + ex.Message);
                    }
 
                    LogHelper.Info(task.S_TWO_END_LOC + "二段任务 开始。 ");
                    var b = TaskProcess.CreateTransport(task.S_SRC_NO, task.S_END_LOC, task.S_TWO_END_LOC, "原物料搬运-纸箱", task.S_CNTRS.Split(',').ToList(), task.N_START_LAYER, 1, task.N_CNTR_COUNT, task.N_PRIORITY);
                    //if (b)
                    //{
                    //    if (task != null)
                    //    {
                    //        task.S_B_STATE = "完成";
                    //        task.T_END_TIME = DateTime.Now;
                    //        db.Updateable(task).UpdateColumns(it => new { it.S_B_STATE, it.T_END_TIME }).ExecuteCommand();
                    //    }
                    //}
                }
            }
            if (task != null)
            {
                task.S_B_STATE = "完成";
                task.T_END_TIME = DateTime.Now;
                db.Updateable(task).UpdateColumns(it => new { it.S_B_STATE, it.T_END_TIME }).ExecuteCommand();
            }
        }
        internal static void Fail(WMSTask task)
        {
            var db = new SqlHelper<WMSTask>().GetInstance();
            if (task != null)
            {
                //判断有没有取货完成,没有就变成失败。有取货完成默认完成了(跟据项目而定,有些项目人工拉走了也没有放到终点)。
                task.S_B_STATE = "失败";
                db.Updateable(task).UpdateColumns(it => new { it.S_B_STATE }).ExecuteCommand();
            }
        }
 
        internal static void SetTaskState(WMSTask task, string state)
        {
            var db = new SqlHelper<WMSTask>().GetInstance();
            if (task != null)
            {
                //判断有没有取货完成,没有就变成失败。有取货完成默认完成了(跟据项目而定,有些项目人工拉走了也没有放到终点)。
                task.S_B_STATE = state;
                task.T_MODIFY = DateTime.Now;
                db.Updateable(task).UpdateColumns(it => new { it.S_B_STATE, it.T_MODIFY }).ExecuteCommand();
            }
        }
        internal static void BeEnd(WMSTask task)
        {
            var db = new SqlHelper<WMSTask>().GetInstance();
            if (task != null)
            {
                task.S_B_STATE = "强制完成";
                db.Updateable(task).UpdateColumns(it => new { it.S_B_STATE }).ExecuteCommand();
            }
        }
        internal static bool CreateTask(WMSTask wmsTask, bool lockLoc = false)
        {
            var db = new SqlHelper<WMSTask>().GetInstance();
            db.Ado.BeginTran();
            try
            {
                if (lockLoc)
                {
                    var model = db.Queryable<Location>().Where(a => a.S_LOC_CODE == wmsTask.S_START_LOC).First();
 
                    model.S_LOCK_STATE = "出库锁";
                    var res = db.Updateable(model).UpdateColumns(it => new { it.S_LOCK_STATE }).ExecuteCommand() > 0;
                    LogHelper.Info(wmsTask.S_START_LOC + "LockLoc:锁结果" + res);
 
                    model = db.Queryable<Location>().Where(a => a.S_LOC_CODE == wmsTask.S_END_LOC).First();
                    model.S_LOCK_STATE = "入库锁";
                    res = db.Updateable(model).UpdateColumns(it => new { it.S_LOCK_STATE }).ExecuteCommand() > 0;
                    LogHelper.Info(wmsTask.S_END_LOC + "LockLoc:锁结果" + res);
 
                }
                db.Insertable(wmsTask).ExecuteCommand();
                db.Ado.CommitTran();
            }
            catch (Exception ex)
            {
                db.Ado.RollbackTran();
                LogHelper.Info("CreateTask 失败 " + ex.Message);
                return false;
            }
            return true;
        }
 
        static DateTime ShuiGaiTask = DateTime.Now;
 
        /// <summary>
        /// 根据任务状态获取任务
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        internal static List<WMSTask> GetTaskListByState(string state)
        {
            var db = new SqlHelper<object>().GetInstance();
            //LogHelper.Info(DateTime.Now.ToLongTimeString() + "=====" + ShuiGaiTask.ToString());
            return db.Queryable<WMSTask>().OrderBy(" N_PRIORITY DESC ").Where(a => a.S_B_STATE.Trim() == state).ToList();
 
        }
 
        internal static List<WMSTask> GetTaskListByState2(string state)
        {
            var db = new SqlHelper<object>().GetInstance();
            //LogHelper.Info(DateTime.Now.ToLongTimeString() + "=====" + ShuiGaiTask.ToString());
 
            var list2 = db.Queryable<WMSTask>().OrderBy(" N_PRIORITY DESC ").Where(a => a.S_B_STATE.Trim() == state).Take(4).ToList();
 
            return list2;
        }
 
        #region 任务切面处理
 
        internal static T GetTSingle<T>(Expression<Func<T, bool>> expression) where T : class, new()
        {
            var db = new SqlHelper<object>().GetInstance();
            var t = db.Queryable<T>().Where(expression).ToList().FirstOrDefault();
            return t;
        }
        internal static List<T> GetTList<T>(Expression<Func<T, bool>> expression) where T : class, new()
        {
            var db = new SqlHelper<object>().GetInstance();
            var t = db.Queryable<T>().Where(expression).ToList();
            return t;
        }
 
        internal static bool InsertT<T>(T t) where T : class, new()
        {
            var db = new SqlHelper<object>().GetInstance();
            return db.Insertable(t).ExecuteCommand() > 0;
        }
 
        internal static void DoUpdate<T>(T t, Expression<Func<T, object>> columns) where T : class, new()
        {
            var db = new SqlHelper<WMSTask>().GetInstance();
            if (t != null)
            {
                db.Updateable(t).UpdateColumns(columns).ExecuteCommand();
            }
        }
 
        #endregion
 
 
        internal static AgvTask GetAgvTask(WMSTask wmsTask)
        {
            var db = new SqlHelper<AgvTask>().GetInstance(true);
            var task = db.Queryable<AgvTask>().Where(a => a.CN_S_TASK_NO.Trim() == wmsTask.S_TASK_NO).ToList().FirstOrDefault();
            return task;
        }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
    }
    [SugarTable("dbo.TN_SYS_TASK")]
    public class AgvTask
    {
        public string CN_GUID { get; set; }
        public int CN_N_STATE { get; set; }
        public int CN_N_INDEX { get; set; }
        public int CN_N_FLAG { get; set; }
        public string CN_S_ERR { get; set; }
        public int CN_N_TSNO { get; set; }
        public int CN_N_PRI { get; set; }
        public int CN_N_IKEY { get; set; }
        public string CN_S_TASK_NO { get; set; }
        public DateTime CN_DT_CREATE { get; set; }
    }
}