刘磊
2025-06-11 013d7277380d535118d83291d937b6cb3b6a5adb
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
using AngleSharp.Dom;
using log4net.Core;
using Masuit.Tools;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common;
using WIDESEA_Core;
using WIDESEA_Core.Const;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_DTO;
using WIDESEA_DTO.WMS;
using WIDESEA_Model.Models;
 
namespace WIDESEA_StoragIntegrationServices
{
    public partial class MCSService
    {
        /// <summary>
        /// 请求移库
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public WebResponseContent RequestChangeLocation(object json)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                if (string.IsNullOrEmpty(json.ToString())) throw new Exception("请求参数为空");
 
                var result = JsonConvert.DeserializeObject<RequestReMove>(json.ToString());
 
                var location = _locationRepository.QueryFirst(x => x.AreaId == result.LocationArea && x.LocationCode == result.LocationID);
 
                if (location == null) throw new Exception("未知库位");
 
                if (_taskRepository.QueryFirst(x => x.SourceAddress == result.LocationID && x.Roadway == location.RoadwayNo) != null)
                {
                    throw new Exception("当前库位已存在任务");
                }
 
                switch (result.MoveType)
                {
                    case 1:
                        CreateMoveTask(location);
                        break;
                    case 5:
                        CreateFireTask(location);
                        break;
                    default:
                        throw new Exception("无效的移库申请类型");
                }
 
                #region 火警出库
                //if (result.MoveType == 5)
                //{
                //    Console.WriteLine($"分容检测柜火警触发:库位{result.LocationID}");
 
                //    //查找消防站台
                //    var station = _stationManagerRepository.QueryFirst(t => t.Roadway == location.RoadwayNo
                //         && t.stationType == (int)StationManager.FireStation
                //         /*&& t. == "Enable"*/);
                //    if (station == null)
                //    {
                //        throw new Exception("消防站台未配置!");
                //    }
                //    //查找库存信息
                //    var barcodeData = _stockInfoRepository.QueryFirst(t => t.LocationCode == location.LocationCode);
                //    //托盘码
                //    string barcode = string.Empty;
                //    if (barcodeData != null)
                //    {
                //        barcode = barcodeData.PalletCode;
                //    }
                //    else
                //    {
                //        //无库存信息,生成随机托盘码
                //        barcode = "M" + DateTime.Now.ToString("MMddHHmmss") + "-" + new Random().Next(100, 1000);
                //    }
                //    int taskNum = _taskRepository.GetTaskNo().Result;
                //    Dt_Task task = new Dt_Task
                //    {
                //        CreateDate = DateTime.Now,
                //        Creater = "HK",
                //        CurrentAddress = result.LocationID,
                //        Grade = 1,
                //        Dispatchertime = DateTime.Now,
                //        PalletCode = barcode,
                //        Roadway = location.RoadwayNo,
                //        SourceAddress = result.LocationID,
                //        TaskState = (int)TaskOutStatusEnum.OutNew,
                //        TaskType = 500,
                //        TargetAddress = station.stationLocation,
                //        NextAddress = station.stationChildCode,
                //        TaskNum = taskNum, //_taskRepository.GetTaskNo().Result,
                //        TaskId = 0,
                //    };
 
                //    // 尝试添加新任务
                //    WMSTaskDTO taskDTO = new WMSTaskDTO()
                //    {
                //        TaskNum = task.TaskNum.Value,
                //        Grade = 1,
                //        PalletCode = task.PalletCode,
                //        RoadWay = task.Roadway,
                //        SourceAddress = task.SourceAddress,
                //        TargetAddress = task.TargetAddress,
                //        TaskState = task.TaskState.Value,
                //        Id = 0,
                //        TaskType = 500,
                //    };
                //} 
                #endregion
 
                LogFactory.GetLog("分容移库申请").Info(true, $"\r\r--------------------------------------");
                LogFactory.GetLog("分容移库申请").Info(true, result.ToJsonString());
                return content.OK();
            }
            catch (Exception ex)
            {
                LogFactory.GetLog("分容移库申请").Info(true, $"\r\r--------------------------------------");
                LogFactory.GetLog("分容移库申请").Info(true, ex.Message);
                return content.Error(ex.Message);
            }
        }
 
        private void CreateMoveTask(DtLocationInfo location)
        {
            Console.WriteLine($"分容申请移库:库位{location.LocationCode}");
 
            //查找可用库位
            DtLocationInfo CanRelocation = _locationRepository.QueryFirst(x => x.LocationStatus == (int)LocationEnum.Free && x.RoadwayNo == location.RoadwayNo && x.EnalbeStatus == 1 && x.LocationType == 2 && x.Remark == "1");
 
            if (CanRelocation == null) throw new Exception("申请移库失败:无可用库位");
 
            //查找库存信息
            var stockInfo = _stockInfoRepository.QueryFirst(x => x.LocationCode == location.LocationCode && x.LocationInfo.RoadwayNo == location.RoadwayNo);
 
            if (stockInfo == null) throw new Exception("申请移库失败:无库存记录");
 
            //修改移库目标库位状态
            CanRelocation.LocationStatus = (int)LocationEnum.FreeDisable;
 
            int taskNum = _taskRepository.GetTaskNo().Result;
            Dt_Task task = new Dt_Task
            {
                CreateDate = DateTime.Now,
                Creater = "HK",
                CurrentAddress = location.LocationCode,
                Grade = 1,
                Dispatchertime = DateTime.Now,
                PalletCode = stockInfo.PalletCode,
                Roadway = location.RoadwayNo,
                SourceAddress = location.LocationCode,
                TaskState = (int)TaskStatus.Created,
                TaskType = (int)TaskRelocationTypeEnum.Relocation,
                TargetAddress = CanRelocation.LocationCode,
                NextAddress = CanRelocation.LocationCode,
                TaskNum = taskNum, //_taskRepository.GetTaskNo().Result,
                TaskId = 0,
            };
 
            // 尝试添加新任务
            WMSTaskDTO taskDTO = new WMSTaskDTO()
            {
                TaskNum = task.TaskNum.Value,
                Grade = 1,
                PalletCode = task.PalletCode,
                RoadWay = task.Roadway,
                SourceAddress = task.SourceAddress,
                TargetAddress = task.TargetAddress,
                TaskState = task.TaskState.Value,
                Id = 0,
                TaskType = task.TaskType,
            };
 
            var configs = _configService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
            var ipAddress = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.WCSIPAddress)?.ConfigValue;
            var ReceiveByWMSTask = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.ReceiveByWMSTask)?.ConfigValue;
            if (ReceiveByWMSTask == null || ipAddress == null)
            {
                throw new Exception("WMS IP 未配置");
            }
            var wmsIpAddrss = ipAddress + ReceiveByWMSTask;
 
            var respon = HttpHelper.Post(wmsIpAddrss, JsonConvert.SerializeObject(taskDTO));   //http://localhost:9291/api/Task/ReceiveTask,
            if (respon != null)
            {
                WebResponseContent respone = JsonConvert.DeserializeObject<WebResponseContent>(respon.ToString());
                if (respone.Status)
                {
                    //添加WMS任务 并修改库位状态
                    var taskId = _taskRepository.AddData(task);
                    _locationRepository.UpdateData(CanRelocation);
                }
                else
                {
                    throw new Exception("WCS处理失败:" + respone.Message);
                }
            }
            else
            {
                throw new Exception("WCS处理失败");
            }
        }
 
 
        private void CreateFireTask(DtLocationInfo location)
        {
            Console.WriteLine($"分容检测柜火警触发:库位{location.LocationCode}");
 
            //查找消防站台
            var station = _stationManagerRepository.QueryFirst(t => t.Roadway == location.RoadwayNo
                 && t.stationType == (int)StationManager.FireStation
                 /*&& t. == "Enable"*/);
            if (station == null)
            {
                throw new Exception("消防站台未配置!");
            }
            //查找库存信息
            var barcodeData = _stockInfoRepository.QueryFirst(t => t.LocationCode == location.LocationCode && t.LocationInfo.RoadwayNo == location.RoadwayNo);
            //托盘码
            string barcode = string.Empty;
            if (barcodeData != null)
            {
                barcode = barcodeData.PalletCode;
            }
            else
            {
                //无库存信息,生成随机托盘码
                barcode = "M" + DateTime.Now.ToString("MMddHHmmss") + "-" + new Random().Next(100, 1000);
            }
            int taskNum = _taskRepository.GetTaskNo().Result;
            Dt_Task task = new Dt_Task
            {
                CreateDate = DateTime.Now,
                Creater = "HK",
                CurrentAddress = location.LocationCode,
                Grade = 1,
                Dispatchertime = DateTime.Now,
                PalletCode = barcode,
                Roadway = location.RoadwayNo,
                SourceAddress = location.LocationCode,
                TaskState = (int)TaskOutStatusEnum.OutNew,
                TaskType = 500,
                TargetAddress = station.stationLocation,
                NextAddress = station.stationChildCode,
                TaskNum = taskNum, //_taskRepository.GetTaskNo().Result,
                TaskId = 0,
            };
 
            // 尝试添加新任务
            WMSTaskDTO taskDTO = new WMSTaskDTO()
            {
                TaskNum = task.TaskNum.Value,
                Grade = 1,
                PalletCode = task.PalletCode,
                RoadWay = task.Roadway,
                SourceAddress = task.SourceAddress,
                TargetAddress = task.TargetAddress,
                TaskState = task.TaskState.Value,
                Id = 0,
                TaskType = 500,
            };
 
 
 
            var configs = _configService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
            var ipAddress = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.WCSIPAddress)?.ConfigValue;
            var ReceiveByWMSTask = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.ReceiveTask)?.ConfigValue;
            if (ReceiveByWMSTask == null || ipAddress == null)
            {
                throw new Exception("WMS IP 未配置");
            }
            var wmsIpAddrss = ipAddress + ReceiveByWMSTask;
 
            List<WMSTaskDTO> fireTaskDTO = new List<WMSTaskDTO>();
            fireTaskDTO.Add(taskDTO);
 
            var respon = HttpHelper.Post(wmsIpAddrss, JsonConvert.SerializeObject(fireTaskDTO));   //http://localhost:9291/api/Task/ReceiveTask,
            if (respon != null)
            {
                WebResponseContent respone = JsonConvert.DeserializeObject<WebResponseContent>(respon.ToString());
                if (respone.Status)
                {
                    var taskId = _taskRepository.AddData(task);
                }
 
                else
                {
                    throw new Exception("WCS处理失败:" + respone.Message);
                }
            }
            else
            {
                throw new Exception("WCS处理失败");
            }
        }
    }
}