schangxiang@126.com
2025-09-17 ff43ddf18764629ff875478e4e47a7281cbd230a
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
using Admin.NET.Core.BasicInformation.Enum;
using Admin.NET.Core.TaskModule.Enum;
using iWareCommon.Utils;
using iWareModel.Entity.MES;
using iWareModel.Entity.WCS;
using iWareModel.Struct;
using iWareSql.MyDbContext;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data.Entity;
using System.Linq;
 
 
namespace iWareCC.Common.Helper
{
 
    /// <summary>
    /// 增补创建的扩展帮助类 【EditBy shaocx,2022-02-05】
    /// </summary>
    public class MyExtendHelper
    {
 
 
 
 
 
        /// <summary>
        /// 判断一二巷道堆垛机是否故障
        /// </summary>
        /// <param name="mycontext"></param>
        /// <param name="ContainerCode"></param>
        /// <returns></returns>
        public static void GetDevicesInfo(MyDbContext mycontext, string WcsIp, ref List<int> useSrmList)
        {
            #region 检验堆垛机是否故障
            WCSDevicesOutput data1 = null;
            if (FormCC.IsSimulationPLC)
            {//如果是模拟
                data1 = MyExtendHelper.GetSimulationSrmDevicesOutput();
                Log4NetHelper.WriteInfoLog(LogType.OutPlanTask, "出库处理的线程(OutPlanTaskHandler)查询堆垛机是否空闲,模拟环境");
                SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "堆垛机状态查询模拟";
            }
            else
            {
                var utl = string.Format(WcsIp + @"GetDevicesInfo?Timestamp={0}&Creator={1}", DateTime.Now.ToString(), "cc");
                //var result1 = HttpHelper.Post(utl, "");
                IDictionary<string, string> parameter = new Dictionary<string, string>();
                var result1 = new HTTPService(WcsIp).postContentForString(utl, parameter, "");
                if (result1 == null)
                {
                    SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "堆垛机状态查询失败,WCS返回NULL";
                }
                data1 = JsonConvert.DeserializeObject<WCSDevicesOutput>(result1);
                if (data1 == null || !data1.Success || data1.Data == null)
                {
                    SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "堆垛机状态查询失败,WCS返回" + result1;
                }
            }
 
            bool SRM1 = false;
            bool SRM2 = false;
            var dev = data1.Data.SRM1;
            if (dev == null || dev.Alram != "0" || dev.isConnect != "是")
            {
                SRM1 = true;
            }
            else
            {
                useSrmList.Add(1);
            }
 
            dev = data1.Data.SRM2;
            if (dev == null || dev.Alram != "0" || dev.isConnect != "是")
            {
                SRM2 = true;
            }
            else
            {
                useSrmList.Add(2);
            }
 
            if (SRM2 == true && SRM1 == true)
            {
                SystemWarningMsg._lbl_Alert_OutPlanTaskHandler = "堆垛机1,堆垛机2故障,无法下发任务";
            }
 
            //这个逻辑是不对的 【Editby shaocx,2023-12-29】
            //int a = 0;
            //if (SRM1 == true)
            //{
            //    a = 2;
            //}
            //if (SRM2 == true)
            //{
            //    a = 1;
            //}
            //if (SRM1 == false && SRM2 == false)
            //{
            //    var count = mycontext.wms_task.Where(x => x.Id > 0).ToList();
            //    a = count.Count() % 2 + 1;//1,2巷道评均入库
            //}
            //return a;
            #endregion
        }
 
        /// <summary>
        /// 获取 托盘类型
        /// </summary>
        /// <param name="mycontext"></param>
        /// <param name="containerCode"></param>
        /// <param name="arr"></param>
        /// <param name="isLane3"></param>
        /// <returns></returns>
        public static int GetPalletType(MyDbContext mycontext, string containerCode, ref bool isLane3)
        {
            var containers2 = GetWareContainerType(mycontext, containerCode);//查询容器类型
            isLane3 = false;
            int PalletType = 0;
            //if (containers2.WareMaterialTypeCode == "292302417842245" || containers2.WareMaterialTypeCode == "293486279884869")
            if (containers2.WareMaterialTypeCode == WmsCarrierType.CarrierType_大料箱.ToString() || containers2.WareMaterialTypeCode == WmsCarrierType.CarrierType_塑料托盘.ToString())
            {
                PalletType = 1;//大料箱 或 塑料托盘
            }
            else
            {
                PalletType = 2;//小料箱 或 刚托盘
            }
 
            // if (containers2.WareMaterialTypeCode == "293486279884869" || containers2.WareMaterialTypeCode == "299120688255045")//判断是否为托盘
            if (containers2.WareMaterialTypeCode == WmsCarrierType.CarrierType_塑料托盘.ToString() || containers2.WareMaterialTypeCode == WmsCarrierType.CarrierType_钢制托盘.ToString())//判断是否为托盘
            {
                isLane3 = true;//托盘入3巷道
            }
 
            return PalletType;
        }
 
        /// <summary>
        /// 查找容器类型
        /// </summary>
        /// <param name="mycontext"></param>
        /// <param name="ContainerCode"></param>
        /// <returns></returns>
        public static WareContainerType GetWareContainerType(MyDbContext mycontext, string ContainerCode)
        {
            return mycontext.wms_base_container_type.Join(mycontext.wms_base_container,
                             a => a.Id,
                             b => b.ContainerTypeId, (a, b) => new WareContainerType
                             {
                                 WareMaterialTypeCode = a.Id.ToString(),
                                 ContainerCode = b.ContainerCode
                             }).Where(x => x.ContainerCode == ContainerCode).FirstOrDefault();//查询容器类型
        }
 
        /// <summary>
        /// 获取 字典数据的值
        /// </summary>
        /// <param name="_sysDictDataRep"></param>
        /// <param name="sysDictDataEnum"></param>
        /// <returns></returns>
        public static string GetSysDictData(MyDbContext dbContext, SysDictDataEnum sysDictDataEnum)
        {
            var sysDictData = dbContext.SysDictData.Where(x => x.Code == sysDictDataEnum.ToString()).FirstOrDefault();
            if (sysDictData != null)
                return sysDictData.Value;
            return string.Empty;
        }
 
        /// <summary>
        /// 1和2 巷道的,预留出 移库的空闲数
        /// </summary>
        /// <param name="_sysDictDataRep"></param>
        /// <param name="criterionContainerOutputs"></param>
        /// <returns></returns>
        public static List<CriterionContainerOutput> RemoveEmptyLocatonForReservedMoveLocationNumByLane12(MyDbContext dbContext, List<CriterionContainerOutput> criterionContainerOutputs)
        {
            if (criterionContainerOutputs == null || criterionContainerOutputs.Count == 0)
            {
                return criterionContainerOutputs;
            }
            var reservedLocationNum = GetSysDictData(dbContext, SysDictDataEnum.ReservedLocationNumForLan12);
            if (string.IsNullOrEmpty(reservedLocationNum))
            {//说明没有配置预留移库数
                return criterionContainerOutputs;
            }
            int i_reservedLocationNum = Convert.ToInt32(reservedLocationNum);
            //1、2巷道的空闲数判断
            var lan1List = criterionContainerOutputs.Where(x => x.Lane == 1).ToList();
            var lan2List = criterionContainerOutputs.Where(x => x.Lane == 2).ToList();
            if (lan1List.Count <= i_reservedLocationNum)
            {
                //抛弃1巷道
                criterionContainerOutputs.RemoveAll(x => x.Lane == 1);
            }
            if (lan2List.Count <= i_reservedLocationNum)
            {
                //抛弃2巷道
                criterionContainerOutputs.RemoveAll(x => x.Lane == 2);
            }
            return criterionContainerOutputs;
        }
 
 
 
 
        /// <summary>
        /// 锁定和解锁库位
        /// </summary>
        /// <param name="isLock">是否是锁定</param>
        /// <param name="dbContext"></param>
        /// <param name="location"></param>
        /// <param name="lockRemark"></param>
        public static void LockLocation(bool isLock, MyDbContext dbContext, string placeCode, string lockRemark)
        {
            //var _Location = dbContext.wms_base_place.Where(x => x.PlaceCode == placeCode ).FirstOrDefault();
            //if (isLock)
            //{
            //    //锁定库位
            //    _Location.PlaceStatus = 1;
            //    _Location.LockRemark = lockRemark;
            //    _Location.UpdatedTime = DateTime.Now;
            //}
            //else
            //{
            //    //解锁库位
            //    _Location.IsLocked = 0;
            //    _Location.LockRemark = lockRemark;
            //    _Location.UpdatedTime = DateTime.Now;
            //}
        }
 
 
        /// <summary>
        /// 生成虚拟的堆垛机列表
        /// </summary>
        /// <returns></returns>
        public static WCSDevicesOutput GetSimulationSrmDevicesOutput()
        {
            WCSDevicesOutput wCSDevicesOutput = new WCSDevicesOutput()
            {
                Success = true,
                Data = new WDO_Dev()
                {
                    SRM1 = new WDO_Data()
                    {
                        isfree = "1",
                        SrmNo = "1",
                        isConnect = "是",
                        Alram = "0"
                    },
                    SRM2 = new WDO_Data()
                    {
                        isfree = "1",
                        SrmNo = "2",
                        isConnect = "是",
                        Alram = "0"
                    },
                    SRM3 = new WDO_Data()
                    {
                        isfree = "1",
                        SrmNo = "3",
                        isConnect = "是",
                        Alram = "0"
                    },
                }
            };
            return wCSDevicesOutput;
        }
 
        /// <summary>
        /// 生成模拟的WCS返回结果
        /// </summary>
        /// <param name="model"></param>
        public static List<TaskDatailsOutPut> CreateSimulationWcsData(List<wms_task> model)
        {
            List<string> str = new List<string>();
            List<TaskDatailsOutPut> taskDatailsOutPutsList = new List<TaskDatailsOutPut>();
            foreach (var item in model)
            {
                taskDatailsOutPutsList.Add(new TaskDatailsOutPut()
                {
                    WmsTaskNo = item.TaskNo,
                    FinishTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    StartTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    TaskState = 3,
                    ToPlace = item.ToPlaceCode,
                });
            }
            return taskDatailsOutPutsList;
        }
 
 
        /// <summary>
        /// 获取库位对象
        /// </summary>
        /// <param name="_wareContainerRep"></param>
        /// <param name="containerCode"></param>
        /// <param name="noFoundThrowException"></param>
        /// <returns></returns>
        public static wms_base_place GetWareLocationObject(MyDbContext dbContext, string placeCode, bool noFoundThrowException = true)
        {
            var wareLocations = dbContext.wms_base_place.Where(x => x.PlaceCode == placeCode).ToList();
            if (wareLocations == null || wareLocations.Count == 0)
            {
                if (noFoundThrowException)
                {
                    throw new Exception($"根据库位编号{placeCode}没有找到库位的基础数据!");
                }
                return null;
            }
            if (wareLocations.Count > 1)
            {
                throw new Exception($"根据库位编号{placeCode}找到库位的基础数据{wareLocations.Count}条!");
            }
            return wareLocations.First();
        }
 
        /// <summary>
        /// 判断库位是否是平库
        /// </summary>
        /// <param name="dbContext"></param>
        /// <param name="locationCode"></param>
        /// <returns></returns>
        public static bool IsPingku(MyDbContext dbContext, string locationCode)
        {
            //var vFromLocation = MyExtendHelper.GetWareLocationObject(dbContext, locationCode, false);
            //if (vFromLocation != null)
            //{
            //    if (vFromLocation.isPingKu == true) return true;
            //}
            return false;
        }
    }
}