杨前锦
2025-07-07 c8f338feee0b6003d8f069b1d37fd9b90dd1b7f4
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
using HH.WCS.Mobox3.Template.device;
using HH.WCS.Mobox3.Template.process;
using HH.WCS.Mobox3.Template.util;
using HH.WCS.Mobox3.Template.wms;
using Newtonsoft.Json;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using static HH.WCS.Mobox3.Template.api.ApiModel;
using static HH.WCS.Mobox3.Template.api.OtherModel;
 
namespace HH.WCS.Mobox3.Template.api {
    /// <summary>
    /// api接口辅助类
    /// </summary>
    public class ApiHelper {
        static ApiHelper() {
 
        }
 
        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, "");
                }
 
            }
        }
 
        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;
        }
 
        internal static SimpleResult PalletSorting(PalletSorting model) {
            var result = new SimpleResult();
            //校验入库单数量,不可以超,成功后插入托盘物料表,更新入库单累计数量
            //也可以直接lua查询入库单数量,做校验
            var info = WMSHelper.GetPutawayOrderDetail(model.arrival_no, model.item_code);
            if (info != null) {
                if (info.F_QTY - info.F_ACC_B_QTY >= model.qty) {
                    // 插入到托盘明细表
                    var cntr = ContainerHelper.GetCntr(model.cntr_code, true);
                    if (cntr != null) {
                        ContainerHelper.BindCntrItem(cntr, model.item_code, info.S_BATCH_NO, model.qty, model.arrival_no);
                        //更新入库单累计绑定数量
                        info.F_ACC_B_QTY += model.qty;
                        WMSHelper.UpdatePutawayOrderDetailQty(info);
                    }
                    else {
                        result.resultCode = 2;
                        result.resultMsg = "获取托盘信息失败";
                    }
 
 
                }
                else {
                    result.resultCode = 1;
                    result.resultMsg = "累计码盘数量超出入库单数量";
                }
            }
            else {
                result.resultCode = 3;
                result.resultMsg = $"未找到入库单{model.arrival_no}";
            }
            
            return result;
        }
 
        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;
        }
 
        internal static SimpleResult ShippingOrderExecute(ShippingOrderCheck model) {
            var result = new SimpleResult();
            //检查库存,更新发货单,生成分拣单,自动合并波次
            //这个后台做比较麻烦,mobox3库存在内存中,任务完成的时候无法增加,还是用c#直接管内存,计算比较方便
            WMSHelper.CreateSortingOrder(model.out_nos.Split(',').ToList());
            return result;
        }
 
        /// <summary>
        /// 后面要把方法放到wmsHelper中
        /// </summary>
        /// <param name="models"></param>
        /// <returns></returns>
        internal static SimpleResult SortingResultCheck(List<SortingResultCheck> models) {
            //生成分拣结果,更新分拣明细状态
            var result = new SimpleResult();
            WMSHelper.SortingConfrim(models);
            return result;
        }
 
 
 
        /// <summary>
        /// 后面要把方法放到wmsHelper中
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        internal static SimpleResult Instock(InstockInfo model) {
            var result = new SimpleResult();
            //pda入库,目前人工放货在巷道口,扫托盘,后台只能计算当前巷道的终点,单深位立库
            //1,判断托盘信息,一种是码盘后的,默认enable是N,另一种是分拣回的
            var cntr = ContainerHelper.GetCntr(model.cntr);
            if (cntr != null) {
                var sortingInfo = WMSHelper.GetSortingDetailByCntr(model.cntr);
                if (cntr.C_ENABLE == "Y" && sortingInfo != null && sortingInfo.Count > 0) {
                    result.resultCode = 2;
                    result.resultMsg = $"托盘{model.cntr}未分拣完成";
                }
            }
            else {
                result.resultCode = 1;
                result.resultMsg = $"托盘{model.cntr}不存在";
            }
            if (result.resultCode == 0) {
                //获取接驳位所在的巷道, 查找功能区,入库接驳位找逻辑库区(每个巷道一个逻辑库区)
                var fa = LocationHelper.GetFunctionAreaByCode(model.start, 2, 10);
                if (fa != null) {
                    //创建入库作业,简单判断一下是否有可入货位,如果不判断,人工货放上去一直不能入,也不知道原因
                    var end = LocationHelper.GetZoneLoc(fa.S_MASTER_CODE);
                    if (end != null) {
                        //判断托盘是否已经生成任务,如果没有则生成
                        var wmsTask = WMSHelper.GetWmsTaskByCntr(model.cntr);
                        if (wmsTask != null) {
                            result.resultCode = 3;
                            result.resultMsg = $"起点{model.start} 托盘{model.cntr}已经创建任务,请勿重复申请";
                        }
                        else {
                            wmsTask = new WMSTask
                            {
                                S_CNTR_CODE = model.cntr,
                                S_CODE = WMSHelper.GenerateTaskNo(),
                                S_START_LOC = model.start,
                                S_END_LOC = end.S_LOC_CODE,
                                N_TYPE = 1,
                                S_TYPE = WMSTask.GetTypeStr(1),
                                S_OP_DEF_CODE = "",
                                S_OP_DEF_NAME = "pda入立库"
                            };
                            if (WMSHelper.CreateWmsTask(wmsTask)) {
                                LocationHelper.LockLoc(end.S_LOC_CODE, 1);
                                result.resultMsg = $"创建作业成功,作业号{wmsTask.S_CODE}";
                            }
                        }
 
                    }
                    else {
                        result.resultCode = 3;
                        result.resultMsg = $"起点{model.start}对应的逻辑库区{fa.S_MASTER_CODE}没有可用货位,请更换巷道";
                    }
                }
                else {
                    result.resultCode = 3;
                    result.resultMsg = $"起点{model.start}没有对应的逻辑库区,请在逻辑库区设置功能区-入库接驳位";
                }
            }
            return result;
        }
 
        internal static SimpleResult SortingOrderExecute(SortingOrderCheck model) {
            var result = new SimpleResult();
            //分拣单配货执行
            WMSHelper.CreateSortingOrderDetail(model.s_no);
            return result;
        }
 
 
        internal static SimpleResult CheckSortingWholeCntr(CheckSortingWholeCntr model) {
            var result = new SimpleResult();
            if (WMSHelper.CheckSortingWholeCntr(model.cntr, model.autoSort == 1)) {
                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;
        }
 
        internal static SimpleResult PalletSorting1(PalletSorting1 model) {
            var result = new SimpleResult();
            //校验入库单数量,不可以超,成功后插入托盘物料表,更新入库单累计数量
            //也可以直接lua查询入库单数量,做校验
            //先用bar_code读存储过程获取信息
            var codeInfo = GetCodeInfo(model.bar_code, model.org);
            var info = WMSHelper.GetPutawayOrderDetail(model.bar_code);
            if (info != null) {
                if (info.F_QTY - info.F_ACC_B_QTY >= model.qty) {
                    // 插入到托盘明细表
 
                    var cntr = ContainerHelper.GetCntr(model.cntr_code, true);
                    if (cntr != null) {
                        ContainerHelper.BindCntrItem(cntr, model.bar_code, info.S_BATCH_NO, model.qty, info.S_BATCH_NO);
                        //更新入库单累计绑定数量
                        info.F_ACC_B_QTY += model.qty;
                        WMSHelper.UpdatePutawayOrderDetailQty(info);
                    }
                    else {
                        result.resultCode = 2;
                        result.resultMsg = "获取托盘信息失败";
                    }
 
 
                }
                else {
                    result.resultCode = 1;
                    result.resultMsg = "累计码盘数量超出入库单数量";
                }
            }
            else {
                result.resultCode = 3;
                result.resultMsg = $"未获取到该物料{model.bar_code}的入库单明细信息";
            }
 
            return result;
        }
        
 
        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; }
        }
    }
}