lss
2025-05-30 38eff4fc0100131b180ffa872009b502629743f5
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
using HH.WCS.JingyuNongfu.device;
using HH.WCS.JingyuNongfu.wms;
using HH.WCS.JingyuNongfu.process;
using HH.WCS.JingyuNongfu.util;
using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using static HH.WCS.JingyuNongfu.api.ApiModel;
using static HH.WCS.JingyuNongfu.util.Settings;
using System;
 
namespace HH.WCS.JingyuNongfu.api
{
    /// <summary>
    /// api接口辅助类
    /// </summary>
    public class ApiHelper
    {
        static ApiHelper()
        {
 
        }
 
        /// <summary>
        /// 绑定托盘物料
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static ReturnResult BindArea(BindArea model)
        {
            ReturnResult result = new ReturnResult { ResultCode = 0, ResultMsg = "绑定成功" };
            var db = new SqlHelper<object>().GetInstance();
 
            try
            {
 
                var locList = db.Queryable<Location>().Where(a => a.S_AREA_CODE == model.AreaCode).ToList();
                if (locList.Count > 0)
                {
                    var IsLoct = locList.Find(a => a.S_LOCK_STATE.Trim() != "无");
                    if (IsLoct != null)
                    {
                        result.ResultCode = -1;
                        result.ResultMsg = "库区存在货位有锁,不允许绑定托盘物料";
                        return result;
                    }
                    locList.ForEach(a =>
                    {
                        if (a.N_CURRENT_NUM == 0)
                        {
                            LogHelper.Info($"货位{a.S_LOC_CODE}库区绑定,绑定物料{model.ItemCode}");
                            var traylist = ContainerHelper.GenerateCntrNo();
                            ContainerHelper.CreateCntrItem(a.S_LOC_CODE, traylist, model.ItemCode);
                        }
 
                    });
 
                }
                else
                {
                    result.ResultCode = -1;
                    result.ResultMsg = "根据库区找不到货位";
                    return result;
                }
                return result;
            }
            catch (Exception ex)
            {
                result.ResultCode = -1;
                result.ResultMsg = ex.ToString();
                return result;
            }
 
        }
 
        /// <summary>
        /// 解绑库区
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static ReturnResult UntieArea(UntieArea model)
        {
            ReturnResult result = new ReturnResult { ResultCode = 0, ResultMsg = "解绑成功" };
            var db = new SqlHelper<object>().GetInstance();
            try
            {
 
                var locList = db.Queryable<Location>().Where(a => a.S_AREA_CODE == model.AreaCode).ToList();
                if (locList.Count > 0)
                {
                    var IsLoct = locList.Find(a => a.S_LOCK_STATE.Trim() != "无");
                    if (IsLoct != null)
                    {
                        result.ResultCode = -1;
                        result.ResultMsg = "库区存在货位有锁,不允许解绑托盘";
                        return result;
                    }
                    locList.ForEach(a =>
                    {
                        if (a.N_CURRENT_NUM > 0)
                        {
                            var cntr = db.Queryable<LocCntrRel>().Where(b => b.S_LOC_CODE == a.S_LOC_CODE).First();
                            if (cntr != null)
                            {
                                //起点终点解绑 删除托盘表托盘
                                LogHelper.Info($"货位{a.S_LOC_CODE}库区解绑,删除容器{cntr.S_CNTR_CODE}");
                                LocationHelper.UnBindingLoc(a.S_LOC_CODE, cntr.S_CNTR_CODE.Split(',').ToList());
                                var tpid = cntr.S_CNTR_CODE.Split(',');
                                foreach (var item in tpid)
                                {
                                    ContainerHelper.delCntrAndItem(item);
                                }
                            }
 
                        }
 
                    });
 
                }
                else
                {
                    result.ResultCode = -1;
                    result.ResultMsg = "根据库区找不到货位";
                    return result;
                }
                return result;
            }
            catch (Exception ex)
            {
                result.ResultCode = -1;
                result.ResultMsg = ex.ToString();
                return result;
            }
 
 
        }
        internal static Location FindEndcolByLocList(List<Location> locations)
        {
            try
            {
                Location end = null;
                var db = new SqlHelper<object>().GetInstance();
                //根据终点货位找空闲货位
                var rows = locations.Select(a => a.N_ROW).Distinct().ToList();
                for (int i = 0; i < rows.Count; i++)
                {
                    var rowList = locations.Where(r => r.N_ROW == rows[i]).ToList();
                    if (rowList.Count(a => a.S_LOCK_STATE != "无") == 0 && rowList.Count(a => a.N_CURRENT_NUM == 0) > 0)
                    {
                        Location other = null;
                        //当前排没有锁并且有空位置
                        //先找满位,然后后面一位要么是空,要么不存在
                        var full = rowList.OrderByDescending(a => a.N_COL).Where(a => a.N_CURRENT_NUM == 1).FirstOrDefault();
                        if (full == null)
                        {
 
                            //没有满位,那就找最小的空位
                            other = rowList.OrderBy(a => a.N_COL).FirstOrDefault();
                        }
                        else
                        {
                            other = rowList.OrderBy(a => a.N_COL).Where(a => a.N_COL > full.N_COL).FirstOrDefault();
                        }
                        if (other != null && (!string.IsNullOrEmpty(other.C_ENABLE) && other.C_ENABLE == "禁用"))
                        {
                            //禁用了选择后面一个货位
                            other = db.Queryable<Location>().OrderBy(a => a.N_COL).Where(a => (string.IsNullOrEmpty(a.C_ENABLE) || a.C_ENABLE.Trim() != "禁用") && a.S_AREA_CODE == other.S_AREA_CODE && a.N_ROW == other.N_ROW && a.N_COL > other.N_COL).First();
 
                            //LogHelper.Info($"禁用选择后一个货位{result}", "成品");
                        }
                        if (other != null)
                        {
                            end = other;
                            break;
 
                        }
 
                    }
                }
                return end;
            }
            catch (Exception)
            {
 
                throw;
            }
 
        }
        /// <summary>
        /// 清洁空框位
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult CleanReport(LocationModel model)
        {
            var result = new SimpleResult() { resultMsg = $"货位{model.location}清洁报工成功" };
            //1\3\4 可清洁
            var cntr = LocationHelper.GetLocCntr(model.location);
            if (cntr.Count > 0)
            {
                ContainerHelper.UpdateCntrState(new List<string> { cntr[0].S_CNTR_CODE.Trim() }, "已清洁");
            }
            else
            {
                result.resultCode = 1;
                result.resultMsg = $"货位{model.location}不存在,或者没有容器";
            }
            JingyuNongfu.LogHelper.Debug("CleanReport Response:" + JsonConvert.SerializeObject(result));
            return result;
        }
 
 
        /// <summary>
        /// 重置信号
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult ResetDevice(DeviceModel model)
        {
            var result = new SimpleResult();
            deviceInfo plc = null;
            if (string.IsNullOrEmpty(model.type))
            {
                plc = Settings.deviceInfos.Where(a => a.deviceName == model.deviceName).FirstOrDefault();
            }
            else
            {
                if (model.type == "下满复位")
                {
                    plc = Settings.deviceInfos.Where(a => a.deviceName == model.deviceName && a.deviceNo[0] == "1").FirstOrDefault();
                }
                else
                {
                    plc = Settings.deviceInfos.Where(a => a.deviceName == model.deviceName && a.deviceNo[0] == "2").FirstOrDefault();
                }
            }
 
            if (plc != null)
            {
                if (model.deviceName.Contains("无菌"))
                {
                    LogHelper.Info($"调用接口发送信号", "发送信号");
                    if (PlcHelper.SendHex(plc.address, "3F00100D0A"))
                    {
                        Thread.Sleep(300);
                        PlcHelper.SendHex(plc.address, "3F00200D0A");
 
                    }
                    else
                    {
                        result.resultCode = 2;
                        result.resultMsg = "满托重置信号发送失败";
                    }
 
                }
                else if (model.deviceName.Contains("翻斗机"))
                {
                    if (PlcHelper.SendHex(plc.address, "3F0010200D0A"))
                    {
 
                    }
                    else
                    {
                        result.resultCode = 2;
                        result.resultMsg = "重置信号发送失败";
                    }
                }
                else if (model.deviceName.Contains("成品"))
                {
                    if (model.type == "下满复位")
                    {
                        if (model.deviceName.Contains("靖宇成品仓E12输送线"))
                        {
                            PlcHelper.SendHex(plc.address, "3F00200D0A");
                        }
                        else
                        {
                            PlcHelper.SendHex(plc.address, "3F00100D0A");
                        }
 
                    }
                    else
                    {
                        PlcHelper.SendHex(plc.address, "3F00200D0A");
                    }
                }
                else
                {
                    if (PlcHelper.SendHex(plc.address, "3F00100D0A"))
                    {
 
                    }
                    else
                    {
                        result.resultCode = 2;
                        result.resultMsg = "重置信号发送失败";
                    }
 
                }
 
            }
            else
            {
                result.resultCode = 1;
                result.resultMsg = "设备不存在,请检查设备名称";
            }
            return result;
        }
 
 
        internal static void AddTask(AddTaskModel model)
        {
            if (!TaskHelper.CheckExist(model.No))
            {
                if (LocationHelper.CheckExist(model.From) && LocationHelper.CheckExist(model.To))
                {
                    TaskHelper.CreateTask(model.No, model.From, model.To, "搬运", 99, "");
                }
 
            }
        }
 
        public class AddTaskModel
        {
            public string From { get; set; }
            public string To { get; set; }
            public string No { get; set; }
        }
        public class LocationModel
        {
            public string location { get; set; }
        }
        public class DeviceModel
        {
            /// <summary>
            /// 瓶盖机名称
            /// </summary>
            public string deviceName { get; set; }
            /// <summary>
            /// 1 、2
            /// </summary>
            //public int order { get; set; }
            public string type { get; set; }
        }
        public class RowInfo
        {
            public string area { get; set; }
            public int row { get; set; }
        }
 
    }
}