1
zxx
2 天以前 d66d362ee6d76f02331e56df1fdec1c2114c9f3e
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
using HH.WCS.XiaoMi.util;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace HH.WCS.XiaoMi.wms
{
    internal class WCSHelper
    {
        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(WCSTask task, string status)
        {
            var res = false;
            var db = new SqlHelper<WCSTask>().GetInstance();
            task.S_B_STATE = status;
            db.Updateable(task).UpdateColumns(it => new { it.S_B_STATE }).ExecuteCommand();
            return res;
        }
        internal static bool UpdateStatus(WCSTask task)
        {
            var res = false;
            var db = new SqlHelper<WCSTask>().GetInstance();
            task.S_B_STATE = WCSTask.GetStateStr(task.N_B_STATE);
            task.T_MODIFY = DateTime.Now;
            db.Updateable(task).UpdateColumns(it => new { it.N_B_STATE, it.S_B_STATE, it.T_MODIFY }).ExecuteCommand();
            return res;
        }
        internal static bool UpdateEQNo(WCSTask task)
        {
            var res = false;
            var db = new SqlHelper<WCSTask>().GetInstance();
            task.T_MODIFY = DateTime.Now;
            db.Updateable(task).UpdateColumns(it => new { it.S_EQ_TASK_CODE, it.T_MODIFY }).ExecuteCommand();
            return res;
        }
        internal static bool UpdateInfo(WCSTask task, string sourceNo, string endBit, string status)
        {
            var res = false;
            var db = new SqlHelper<WCSTask>().GetInstance();
            task.S_B_STATE = status;
            task.S_OP_CODE = sourceNo;
            task.S_END_LOC = endBit;
            db.Updateable(task).UpdateColumns(it => new { it.S_B_STATE, it.S_OP_CODE, it.S_END_LOC }).ExecuteCommand();
            return res;
        }
        internal static WCSTask GetTask(string no)
        {
            var db = new SqlHelper<WCSTask>().GetInstance();
            var task = db.Queryable<WCSTask>().Where(a => a.S_CODE.Trim() == no).First();
            return task;
        }
        internal static WCSTask GetTaskName(string no)
        {
            var db = new SqlHelper<WCSTask>().GetInstance();
            var task = db.Queryable<WCSTask>().Where(a => a.S_WORKSHOP_NO.Trim() == no).First();
            return task;
        }
 
        internal static List<WCSTask> GetTaskBycntrcode(string no)
        {
            var db = new SqlHelper<WCSTask>().GetInstance();
            var task = db.Queryable<WCSTask>().Where(a => a.S_CNTR_CODE.Trim() == no && a.S_B_STATE.Trim() == "完成").ToList();
            return task;
        }
        internal static WCSTask GetTaskBySrcNo(string no)
        {
            var db = new SqlHelper<WCSTask>().GetInstance();
            var task = db.Queryable<WCSTask>().Where(a => a.S_OP_CODE == no).First();
            return task;
        }
        internal static List<WCSTask> GetTaskByStart(string bit)
        {
            var db = new SqlHelper<WCSTask>().GetInstance();
            var task = db.Queryable<WCSTask>().Where(a => a.S_START_LOC == bit).ToList();
            return task;
        }
        internal static List<WCSTask> GetTaskByEnd(string bit)
        {
            var db = new SqlHelper<WCSTask>().GetInstance();
            var task = db.Queryable<WCSTask>().Where(a => a.S_END_LOC == bit).ToList();
            return task;
        }
        internal static List<WCSTask> GetTaskByType(string taskType)
        {
            var db = new SqlHelper<WCSTask>().GetInstance();
            return db.Queryable<WCSTask>().Where(a => a.S_TYPE == 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)
        {
            var fromLoc = LocationHelper.GetLocation(from);
            var endLoc = LocationHelper.GetLocation(to);
            WCSTask TN_Task = new WCSTask()
            {
                S_CODE = GenerateTaskNo(),
                S_START_AREA = fromLoc.S_AREA_CODE,
                S_END_AREA = endLoc.S_AREA_CODE,
                S_START_LOC = from,
                S_END_LOC = to,
                S_TYPE = taskType,
                S_OP_CODE = no,
                N_PRIORITY = pri,
                N_SCHEDULE_TYPE = 1,
                N_B_STATE = 0,
                S_B_STATE = WCSTask.GetStateStr(0),
                S_CNTR_CODE = cntrInfo
                //N_START_LAYER = startLayer,
                //N_END_LAYER = endLayer,
                //N_CNTR_COUNT = cntrCount
 
            };
 
            return CreateTask(TN_Task);
        }
        //小米大件
        internal static bool CreateTaskDj(string no, string from, string to, string taskType, int pri, string cntrInfo,string orderName, int cntrCount = 1, int startLayer = 1, int endLayer = 1)
        {
            var fromLoc = LocationHelper.GetLocation(from);
            //因为有点到未知的情况
            string endLocAreaStr = null;
            if (to!=null && to != "")
            {
                endLocAreaStr = LocationHelper.GetLocation(to).S_AREA_CODE;
            }
            WCSTask TN_Task = new WCSTask()
            {
                //S_CODE = GenerateTaskNo(),
                S_CODE = orderName,
                S_START_AREA = fromLoc.S_AREA_CODE,
                S_END_AREA = endLocAreaStr,
                S_START_LOC = from,
                S_END_LOC = to,
                S_TYPE = taskType,
                S_OP_CODE = no,
                N_PRIORITY = pri,
                N_SCHEDULE_TYPE = 1,
                N_B_STATE = 0,
                S_B_STATE = WCSTask.GetStateStr(0),
                S_CNTR_CODE = cntrInfo,
                ////存小米任务号
                //S_WORKSHOP_NO = orderName
                //N_START_LAYER = startLayer,
                //N_END_LAYER = endLayer,
                //N_CNTR_COUNT = cntrCount
 
            };
            LogHelper.Info($"WCSTask参数:======{JsonConvert.SerializeObject(TN_Task)}");
            return CreateTask(TN_Task);
        }
        //小米货架
        internal static bool CreateTaskHj(string no, string from, string to, string taskType, int pri, string cntrInfo, int cntrCount = 1, int startLayer = 1, int endLayer = 1)
        {
            var fromLoc = LocationHelper.GetLocation(from);
            var endLoc = LocationHelper.GetLocation(to);
            WCSTask TN_Task = new WCSTask()
            {
                S_CODE = GenerateTaskNo(),
                S_START_AREA = fromLoc.S_AREA_CODE,
                S_END_AREA = endLoc.S_AREA_CODE,
                S_START_LOC = from,
                S_END_LOC = to,
                S_TYPE = taskType,
                S_OP_CODE = no,
                N_PRIORITY = pri,
                N_SCHEDULE_TYPE = 1,
                N_B_STATE = 0,
                S_B_STATE = WCSTask.GetStateStr(0),
                S_FRAME_CODE = cntrInfo,
                //N_START_LAYER = startLayer,
                //N_END_LAYER = endLayer,
                //N_CNTR_COUNT = cntrCount
 
            };
 
            return CreateTask(TN_Task);
        }
        internal static bool CreateTask(string no, Location from, Location to, string taskType, int scheduleType, int pri, string cntrInfo, int cntrCount = 1, int startLayer = 1, int endLayer = 1)
        {
            WCSTask TN_Task = new WCSTask()
            {
                S_CODE = GenerateTaskNo(),
                S_START_AREA = from.S_AREA_CODE,
                S_END_AREA = to.S_AREA_CODE,
                S_START_LOC = from.S_CODE,
                S_END_LOC = to.S_CODE,
                S_TYPE = taskType,
                S_OP_CODE = no,
                N_PRIORITY = pri,
                N_SCHEDULE_TYPE = scheduleType,
                S_B_STATE = WCSTask.GetStateStr(0),
                S_SCHEDULE_TYPE = WCSTask.GetSchedule(scheduleType),
                S_CNTR_CODE = cntrInfo,
                //N_START_LAYER = startLayer,
                //N_END_LAYER = endLayer,
                //N_CNTR_COUNT = cntrCount
 
            };
            return CreateTask(TN_Task);
        }
        internal static bool CheckExist(string no)
        {
            return GetTask(no) != null;
        }
        internal static bool UpdateStatus(string no, int state)
        {
            var res = false;
            var db = new SqlHelper<WCSTask>().GetInstance();
            var task = db.Queryable<WCSTask>().Where(a => a.S_CODE == no).First();
            if (task != null)
            {
                task.N_B_STATE = state;
                task.S_B_STATE = WCSTask.GetStateStr(state);
                //需要判断任务是否失败或者已完成,不允许再修改
                res = db.Updateable(task).UpdateColumns(it => new { it.N_B_STATE, it.S_B_STATE }).ExecuteCommand() > 0;
            }
            return res;
        }
        internal static bool AddActionRecord(string no, int state, string forkliftNo, string extData)
        {
            var db = new SqlHelper<TaskAction>().GetInstance();
            var action = new TaskAction()
            {
                N_ACTION_CODE = state,
                S_TASK_CODE = no,
                S_EQ_CODE = forkliftNo,
                S_EQ_TYPE = "agv",
                S_DATA = extData
            };
            return db.Insertable(action).ExecuteCommand() > 0;
        }
        internal static bool CheckActionRecordExist(string no, int code)
        {
            var db = new SqlHelper<TaskAction>().GetInstance();
            return db.Queryable<TaskAction>().Count(a => a.S_TASK_CODE == no && a.N_ACTION_CODE == code) > 0;
        }
        internal static void Begin(WCSTask task)
        {
            var db = new SqlHelper<WCSTask>().GetInstance();
            if (task != null)
            {
                if (task.N_B_STATE == 1)
                {
                    task.N_B_STATE = 2;
                    task.S_B_STATE = WCSTask.GetStateStr(task.N_B_STATE);
                    task.T_START_TIME = DateTime.Now;
                    db.Updateable(task).UpdateColumns(it => new { it.N_B_STATE, it.S_B_STATE, it.T_START_TIME }).ExecuteCommand();
                }
 
            }
        }
        internal static void End(WCSTask task)
        {
            var db = new SqlHelper<WCSTask>().GetInstance();
            if (task != null)
            {
                task.N_B_STATE = 3;
                task.S_B_STATE = WCSTask.GetStateStr(task.N_B_STATE);
                task.T_END_TIME = DateTime.Now;
                db.Updateable(task).UpdateColumns(it => new { it.N_B_STATE, it.S_B_STATE, it.T_END_TIME }).ExecuteCommand();
            }
        }
        internal static void Fail(WCSTask task)
        {
            var db = new SqlHelper<WCSTask>().GetInstance();
            if (task != null)
            {
                //判断有没有取货完成,没有就变成失败。有取货完成默认完成了(跟据项目而定,有些项目人工拉走了也没有放到终点)。
                task.N_B_STATE = 4;
                task.S_B_STATE = WCSTask.GetStateStr(task.N_B_STATE);
                db.Updateable(task).UpdateColumns(it => new { it.N_B_STATE, it.S_B_STATE }).ExecuteCommand();
            }
        }
        internal static bool CreateTask(WCSTask TN_Task)
        {
            try
            {
                var db = new SqlHelper<WCSTask>().GetInstance();
                return db.Insertable(TN_Task).ExecuteCommand() > 0;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                throw;
            }
 
        }
 
        /// <summary>
        /// 根据任务状态获取任务
        /// </summary>
        /// <param name="state"></param>
        /// <returns></returns>
        internal static List<WCSTask> GetTaskListByState(string status)
        {
            var db = new SqlHelper<object>().GetInstance();
            return db.Queryable<WCSTask>().Where(a => a.S_B_STATE.Trim() == status).OrderBy(s=>s.T_CREATE).ToList();
 
        }
        internal static List<WCSTask> GetTaskListByState(int state)
        {
            var db = new SqlHelper<object>().GetInstance();
            return db.Queryable<WCSTask>().Where(a => a.N_B_STATE == state).ToList();
        }
        internal static List<WCSTask> GetWaitingTaskList()
        {
            var db = new SqlHelper<object>().GetInstance();
            return db.Queryable<WCSTask>().Where(a => a.N_B_STATE == 0).ToList();
        }
    }
}