杨前锦
2025-05-15 0de17c9615a32b1d83319121179e3f6b725b85c7
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
using HH.WCS.Mobox3.YNJT_BZP_GT.device;
using HH.WCS.Mobox3.YNJT_BZP_GT.process;
using HH.WCS.Mobox3.YNJT_BZP_GT.util;
using HH.WCS.Mobox3.YNJT_BZP_GT.wms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading;
using static HH.WCS.Mobox3.YNJT_BZP_GT.api.ApiModel;
using static HH.WCS.Mobox3.YNJT_BZP_GT.api.OtherModel;
 
namespace HH.WCS.Mobox3.YNJT_BZP_GT.api {
    /// <summary>
    /// api接口辅助类
    /// </summary>
    public class ApiHelper {
        static ApiHelper() {
 
        }
        /// <summary>
        /// 创建任务
        /// </summary>
        /// <param name="model"></param>
        internal static void AddTask(AddTaskModel model) {
            if (!WCSHelper.CheckExist(model.No)) {
                if (LocationHelper.CheckExist(model.From) && LocationHelper.CheckExist(model.To)) {
                    WCSHelper.CreateTask(model.No, model.From, model.To, "搬运", 99, "");
                }
 
            }
        }
        /// <summary>
        /// 创建入库单主子表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult Putaway_Order_In(Putaway_Order_In model) {
            var result = new SimpleResult();
            //创建入库单主子表
            var po = WMSHelper.GetPutawayOrder(model.Data.arrival_no);
            if (po == null) {
                po = new PutawayOrder { S_NO = model.Data.arrival_no, S_BS_TYPE = model.Data.op_type };
                po.Details = new List<PutawayDetail>();
                if (model.Data.items.Count > 0) {
                    model.Data.items.ForEach(a => {
                        po.Details.Add(new PutawayDetail
                        {
                            S_PUTAWAY_NO = model.Data.arrival_no,
                            N_ROW_NO = po.Details.Count + 1,
                            S_ITEM_CODE = a.item_code,
                            F_QTY = a.qty,
                            S_BATCH_NO = a.batch_no,
                        });
                    });
                    WMSHelper.CreatePutawayOrder(po);
                }
 
            }
            return result;
        }
 
        /// <summary>
        /// 创建发货单主子表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult OutboundOrder(OutboundOrder model) {
            var result = new SimpleResult();
            //创建发货单,人工点确认后生成分拣单,没有缺货的自动更新为失败,有货的更新为执行中
            //创建出库单主子表
            var po = WMSHelper.GetShippingOrder(model.Data.out_no);
            if (po == null) {
                po = new ShippingOrder { S_NO = model.Data.out_no, S_BS_TYPE = model.Data.op_type };
                po.Details = new List<ShippingDetail>();
                if (model.Data.items.Count > 0) {
                    model.Data.items.ForEach(a => {
                        po.Details.Add(new ShippingDetail
                        {
                            S_SHIPPING_NO = model.Data.out_no,
                            N_ROW_NO = po.Details.Count + 1,
                            S_ITEM_CODE = a.item_code,
                            F_QTY = a.qty,
                            S_BATCH_NO = a.batch_no,
                        });
                    });
                    WMSHelper.CreateShippingOrder(po);
                }
 
            }
            return result;
        }
        
        public static List<KeyValuePair<string, InstockInfo>> cacheInstockInfos = new List<KeyValuePair<string, InstockInfo>>();
 
        public static void addKeyValuePair(string ip , InstockInfo instockInfo) {
            KeyValuePair<string, InstockInfo> pair = new KeyValuePair<string, InstockInfo>(ip,instockInfo);
            LogHelper.Info("添加缓存信号,缓存信息:" + JsonConvert.SerializeObject(pair), "TSSG");
 
            // 查找并替换键值对
            bool replaced = false;
            for (int i = 0; i < cacheInstockInfos.Count; i++)
            {
                if (cacheInstockInfos[i].Key == pair.Key)
                {
                    cacheInstockInfos[i] = pair;
                    replaced = true;
                    LogHelper.Info("存在相同IP的缓存信号,替换缓存信息:" + JsonConvert.SerializeObject(pair), "TSSG");
                    break; // 如果只需要替换第一个匹配的项,就跳出循环
                }
            }
            // 如果没有找到要替换的项,可以选择添加新的键值对或执行其他操作
            if (!replaced)
            {
                cacheInstockInfos.Add(pair);
            }
        }
        
        
        internal static object in_lock = new object();
 
        /// <summary>
        /// pad入库
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult Instock(InstockInfo model) {
            var result = new SimpleResult();
            //pda入库
            //1 判断起点有没有任务,有没有入库锁
            lock (in_lock) {
                var loc = LocationHelper.GetLoc(model.start);
                if (loc != null && loc.N_LOCK_STATE == 0)
                {
                    //2 根据终点库区计算终点
                    var end = WMSHelper.GetInstockEnd(model.item, model.endArea);
                    LogHelper.Info("终点货位:" + JsonConvert.SerializeObject(end), "TSSG");
                    if (end != null)
                    {
                        var cntrCode = ContainerHelper.GenerateCntrNo();
                        var wcsTask = new WCSTask
                        {
                            S_OP_NAME = "入库",
                            S_CODE = WCSHelper.GenerateTaskNo(),
                            S_TYPE = "下线入库",
                            S_START_LOC = model.start,
                            S_END_LOC = end.S_CODE,
                            S_SCHEDULE_TYPE = "NDC",
                            N_CNTR_COUNT = 1,
                            S_CNTR_CODE = cntrCode,
                            N_START_LAYER = 1,
                            N_END_LAYER = end.N_CURRENT_NUM + 1
                        };
                        if (ContainerHelper.BindNewCntrItem(model.start, cntrCode, model.item) && WCSHelper.CreateTask(wcsTask))
                        {
                            LocationHelper.LockLoc(model.start, 2);
                            LocationHelper.LockLoc(end.S_CODE, 1);
                            result.resultCode = 0;
                            result.resultMsg = $"任务创建成功,任务号为{wcsTask.S_CODE},终点为{end.S_CODE}";
                        }
                    }
                    else
                    {
                        addKeyValuePair(model.ip, model);
                        LogHelper.Info("缓存入库信息:" + JsonConvert.SerializeObject(model), "TSSG");
                        result.resultCode = 2;
                        result.resultMsg = "未获取到入库终点";
                    }
                }
                else
                {
                    result.resultCode = 1;
                    result.resultMsg = "起点有任务未完成";
                }
            }
            return result;
        }
 
        /// <summary>
        /// 移库
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult shiftStock(ShiftStockInfo model)
        {
            var result = new SimpleResult();
            //1 根据起点库区、排 确定起始货位
            var startLoc = WMSHelper.GetShiftStockStart(model.startArea, model.startRow);
            if (startLoc != null && startLoc.N_LOCK_STATE == 0)
            {
                //2 根据终点库区计算终点
                var end = WMSHelper.GetShiftStockEnd( model.endArea, model.endRow);
                if (end != null && end.N_LOCK_STATE == 0)
                {
                    var wcsTask = new WCSTask
                    {
                        S_OP_NAME = "移库",
                        S_CODE = WCSHelper.GenerateTaskNo(),
                        S_TYPE = "移库",
                        S_START_LOC = startLoc.S_CODE,
                        S_END_LOC = end.S_CODE,
                        S_SCHEDULE_TYPE = "NDC",
                        N_CNTR_COUNT = 1,
                        S_CNTR_CODE = startLoc.LocCntrRel.S_CNTR_CODE,
                        N_START_LAYER = startLoc.N_CURRENT_NUM,
                        N_END_LAYER = end.N_CURRENT_NUM + 1
                    };
                    if (WCSHelper.CreateTask(wcsTask))
                    {
                        LocationHelper.LockLoc(startLoc.S_CODE, 2);
                        LocationHelper.LockLoc(end.S_CODE, 1);
                        result.resultCode = 0;
                        result.resultMsg = $"任务创建成功,任务号为{wcsTask.S_CODE},终点为{end.S_CODE}";
                    }
                }
                else
                {
                    result.resultCode = 2;
                    result.resultMsg = "终点库区没有可入的货位";
                }
            }
            else
            {
                result.resultCode = 1;
                result.resultMsg = "未找到指定物料";
            }
            return result;
        }
 
            internal static CodeInfo GetCodeInfo(string code, string org) {
            //return new CodeInfo {  Fitemid_XK=code, FSourceNo="123456"};
            CodeInfo result = null;
            try {
                var db = new SqlHelper<object>().GetInstance(Settings.SqlServer1);
                var nameP = new SugarParameter("@FBarCode", code);
                var orgP = new SugarParameter("@Forg", org);
                //var ageP = new SugarParameter("@age", null, true);//设置为output
                //var dt = db.Ado.UseStoredProcedure().GetDataTable("sp_school", nameP, ageP);//返回dt
                result = db.Ado.UseStoredProcedure().SqlQuery<CodeInfo>("WMS_FBarCode", nameP, orgP).First();//返回List
                Console.WriteLine($"读存储过程成功,result={result}");
            }
            catch (Exception ex) {
                Console.WriteLine(ex.Message);
            }
            return result;
        }
 
        /// <summary>
        /// agv 车辆报警
        /// </summary>
        /// <param name="forkliftNo"></param>
        /// <param name="errCode"></param>
        /// <param name="errCode2"></param>
        /// <param name="failCode"></param>
        public static void agvCarAlarm(string  forkliftNo, string errCode , string errCode2 , string failCode) {
            bool turnLight = false;
 
            var errCodeList = Settings.agvAlarmNoList.Where(a => a.type == 1).Select(a => a.dex).ToList();
            var errCode2List = Settings.agvAlarmNoList.Where(a => a.type == 2).Select(a => a.dex).ToList();
            var failCodeList = Settings.agvAlarmNoList.Where(a => a.type == 3).Select(a => a.dex).ToList();
 
            if (errCodeList.Contains(int.Parse(errCode)))
            {
                turnLight = true;
                LogHelper.Info("StopWord 错误码:" + errCode, "HosttoagvCar");
            }
            if (errCode2List.Contains(int.Parse(errCode2)))
            {
                turnLight = true;
                LogHelper.Info("StopWord2 错误码:" + errCode2, "HosttoagvCar");
            }
            if (failCodeList.Contains(int.Parse(failCode)))
            {
                turnLight = true;
                LogHelper.Info("failCode 错误码:" + failCode, "HosttoagvCar");
            }
 
            var alertorDevices = Settings.alertorLightInfos.Where(a => a.deviceNo == 5 || a.deviceNo == 6).ToList();
            foreach (var alertorDevice in alertorDevices)
            {
                if (turnLight)
                {
                    var result = HH.WCS.Mobox3.YNJT_BZP_GT.device.PlcHelper.SendHex(alertorDevice.address, alertorDevice.turnLight);
                    LogHelper.Info("开灯,modbus 返回信号:" + result, "HosttoagvCar");
                }
                /*else
                {
                    var result = HH.WCS.Mobox3.YNJT_BZP_GT.device.PlcHelper.SendHex(alertorDevice.address, alertorDevice.offLight);
                    LogHelper.Info("关灯,modbus 返回信号:" + result, "HosttoagvCar");
                }*/
            }
        }
 
 
        public class AddTaskModel {
            public string From { get; set; }
            public string To { get; set; }
            public string No { get; set; }
        }
        public class TN_LocationModel {
            public string TN_Location { get; set; }
        }
        public class CodeInfo {
            /// <summary>
            /// 生产订单内码
            /// </summary>
            public string FInterID { get; set; }
            /// <summary>
            /// 生产订单编号
            /// </summary>
            public string FSourceNo { get; set; }
            /// <summary>
            /// 批号
            /// </summary>
            public string FGMPBatchNo { get; set; }
            public string FState { get; set; }
            /// <summary>
            /// 物料编码(内码就是编码)
            /// </summary>
            public string Fitemid_XK { get; set; }
            /// <summary>
            /// 分录id
            /// </summary>
            public string Fentryid { get; set; }
        }
        public class NoteInfo : CodeInfo {
            public string WmsBillNo { get; set; }
        }
    }
}