wanshenmean
3 天以前 ce1292c9cf37195b6abd2699dfc5d6cb3e143c9b
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WIDESEA_Core;
using WIDESEA_Core.BaseController;
using WIDESEA_DTO.MES;
using WIDESEA_IStockService;
using WIDESEA_IBasicService;
using WIDESEA_IMesService;
using WIDESEA_ISystemService;
using WIDESEA_Model.Models;
using System.Diagnostics;
 
namespace WIDESEA_WMSServer.Controllers.Stock
{
    /// <summary>
    /// 库存明细
    /// </summary>
    [Route("api/StockInfoDetail")]
    [ApiController]
    public class StockInfoDetailController : ApiBaseController<IStockInfoDetailService, Dt_StockInfoDetail>
    {
        private readonly IMesLogService _mesLogService;
        private readonly IMesService _mesService;
        private readonly ISys_DictionaryService _sysDictionaryService;
 
        public StockInfoDetailController(
            IStockInfoDetailService service,
            IMesLogService mesLogService,
            IMesService mesService,
            ISys_DictionaryService sysDictionaryService) : base(service)
        {
            _mesLogService = mesLogService;
            _mesService = mesService;
            _sysDictionaryService = sysDictionaryService;
        }
 
        /// <summary>
        /// 托盘电芯绑定 - 调用MES接口
        /// </summary>
        /// <param name="dto">绑定请求参数</param>
        /// <returns>操作结果</returns>
        [HttpPost("bindContainer")]
        public async Task<WebResponseContent> BindContainer([FromBody] BindContainerRequestDto dto)
        {
            var response = new WebResponseContent();
            var stopwatch = Stopwatch.StartNew();
 
            try
            {
                // 1. 参数验证
                if (string.IsNullOrWhiteSpace(dto.PalletCode))
                {
                    return response.Error("托盘编号不能为空");
                }
 
                if (dto.SfcList == null || !dto.SfcList.Any())
                {
                    return response.Error("电芯码列表不能为空");
                }
 
                // 2. 验证电芯状态(非'已锁定'状态允许绑定)
                var stockDetails = await Service.Repository.QueryDataAsync(x => dto.SfcList.Contains(x.SerialNumber));
                if (stockDetails != null && stockDetails.Any(d => d.Status == 99))
                {
                    return response.Error("当前库存明细包含已锁定状态,不允许执行绑定操作");
                }
 
                // 3. 获取系统配置
                var configs = _sysDictionaryService.GetVueDictionary(new[] { "MES_EquipmentCode", "MES_ResourceCode" });
 
                string equipmentCode = GetConfigValue(configs, "MES_EquipmentCode", "WCS_001");
                string resourceCode = GetConfigValue(configs, "MES_ResourceCode", "RESOURCE_001");
 
                // 3. 构造MES请求 - 将电芯列表转换为ContainerSfcItem格式
                var mesRequest = new BindContainerRequest
                {
                    EquipmentCode = equipmentCode,
                    ResourceCode = resourceCode,
                    LocalTime = DateTime.Now,
                    ContainerCode = dto.PalletCode,
                    ContainerSfcList = dto.SfcList.Select(sfc => new ContainerSfcItem
                    {
                        Sfc = sfc,
                        Location = dto.Location ?? ""
                    }).ToList(),
                    OperationType = dto.OperationType
                };
 
                string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
 
                // 4. 调用MES接口(同步方法)
                var mesResult = _mesService.BindContainer(mesRequest);
                stopwatch.Stop();
 
                // 5. 记录日志
                await _mesLogService.LogAsync(new MesApiLogDto
                {
                    ApiType = "BindContainer",
                    RequestJson = requestJson,
                    ResponseJson = System.Text.Json.JsonSerializer.Serialize(mesResult),
                    IsSuccess = mesResult.IsSuccess,
                    ErrorMessage = mesResult.ErrorMessage,
                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
                    Creator = App.User.UserName
                });
 
                // 6. 返回结果
                if (mesResult.IsSuccess)
                {
                    return response.OK("托盘电芯绑定成功");
                }
                else
                {
                    return response.Error($"MES接口调用失败: {mesResult.ErrorMessage}");
                }
            }
            catch (System.Exception ex)
            {
                stopwatch.Stop();
 
                // 记录错误日志
                await _mesLogService.LogAsync(new MesApiLogDto
                {
                    ApiType = "BindContainer",
                    IsSuccess = false,
                    ErrorMessage = ex.Message,
                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
                    Creator = App.User.UserName
                });
 
                return response.Error($"托盘电芯绑定失败: {ex.Message}");
            }
        }
 
        /// <summary>
        /// 托盘电芯解绑 - 调用MES接口
        /// </summary>
        /// <param name="dto">解绑请求参数</param>
        /// <returns>操作结果</returns>
        [HttpPost("unbindContainer")]
        public async Task<WebResponseContent> UnbindContainer([FromBody] UnbindContainerRequestDto dto)
        {
            var response = new WebResponseContent();
            var stopwatch = Stopwatch.StartNew();
 
            try
            {
                // 1. 参数验证
                if (string.IsNullOrWhiteSpace(dto.PalletCode))
                {
                    return response.Error("托盘编号不能为空");
                }
 
                if (dto.SfcList == null || !dto.SfcList.Any())
                {
                    return response.Error("电芯码列表不能为空");
                }
 
                // 2. 验证电芯状态(非'已锁定'状态允许解绑)
                var stockDetails = await Service.Repository.QueryDataAsync(x => dto.SfcList.Contains(x.SerialNumber));
                if (stockDetails != null && stockDetails.Any(d => d.Status == 99))
                {
                    return response.Error("当前库存明细包含已锁定状态,不允许执行解绑操作");
                }
 
                // 3. 获取系统配置
                var configs = _sysDictionaryService.GetVueDictionary(new[] { "MES_EquipmentCode", "MES_ResourceCode" });
 
                string equipmentCode = GetConfigValue(configs, "MES_EquipmentCode", "WCS_001");
                string resourceCode = GetConfigValue(configs, "MES_ResourceCode", "RESOURCE_001");
 
                // 3. 构造MES请求
                var mesRequest = new UnBindContainerRequest
                {
                    EquipmentCode = equipmentCode,
                    ResourceCode = resourceCode,
                    LocalTime = DateTime.Now,
                    ContainCode = dto.PalletCode,
                    SfcList = dto.SfcList
                };
 
                string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
 
                // 4. 调用MES接口(同步方法)
                var mesResult = _mesService.UnBindContainer(mesRequest);
                stopwatch.Stop();
 
                // 5. 记录日志
                await _mesLogService.LogAsync(new MesApiLogDto
                {
                    ApiType = "UnbindContainer",
                    RequestJson = requestJson,
                    ResponseJson = System.Text.Json.JsonSerializer.Serialize(mesResult),
                    IsSuccess = mesResult.IsSuccess,
                    ErrorMessage = mesResult.ErrorMessage,
                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
                    Creator = App.User.UserName
                });
 
                // 6. 返回结果
                if (mesResult.IsSuccess)
                {
                    return response.OK("托盘电芯解绑成功");
                }
                else
                {
                    return response.Error($"MES接口调用失败: {mesResult.ErrorMessage}");
                }
            }
            catch (System.Exception ex)
            {
                stopwatch.Stop();
 
                // 记录错误日志
                await _mesLogService.LogAsync(new MesApiLogDto
                {
                    ApiType = "UnbindContainer",
                    IsSuccess = false,
                    ErrorMessage = ex.Message,
                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
                    Creator = App.User.UserName
                });
 
                return response.Error($"托盘电芯解绑失败: {ex.Message}");
            }
        }
 
        /// <summary>
        /// 托盘NG电芯上报 - 调用MES接口
        /// </summary>
        /// <param name="dto">NG上报请求参数</param>
        /// <returns>操作结果</returns>
        [HttpPost("containerNgReport")]
        public async Task<WebResponseContent> ContainerNgReport([FromBody] ContainerNgReportRequestDto dto)
        {
            var response = new WebResponseContent();
            var stopwatch = Stopwatch.StartNew();
 
            try
            {
                // 1. 参数验证
                if (string.IsNullOrWhiteSpace(dto.PalletCode))
                {
                    return response.Error("托盘编号不能为空");
                }
 
                if (dto.NgSfcList == null || !dto.NgSfcList.Any())
                {
                    return response.Error("NG电芯列表不能为空");
                }
 
                // 2. 验证电芯状态(非'已锁定'状态允许NG上报)
                var sfcList = dto.NgSfcList.Select(x => x.Sfc).ToList();
                var stockDetails = await Service.Repository.QueryDataAsync(x => sfcList.Contains(x.SerialNumber));
                if (stockDetails != null && stockDetails.Any(d => d.Status == 99))
                {
                    return response.Error("当前库存明细包含已锁定状态,不允许执行NG上报操作");
                }
 
                // 3. 获取系统配置
                var configs = _sysDictionaryService.GetVueDictionary(new[] { "MES_EquipmentCode", "MES_ResourceCode" });
 
                string equipmentCode = GetConfigValue(configs, "MES_EquipmentCode", "WCS_001");
                string resourceCode = GetConfigValue(configs, "MES_ResourceCode", "RESOURCE_001");
 
                // 3. 构造MES请求 - 将DTO格式转换为MES请求格式
                var mesRequest = new ContainerNgReportRequest
                {
                    EquipmentCode = equipmentCode,
                    ResourceCode = resourceCode,
                    LocalTime = DateTime.Now,
                    ContainerCode = dto.PalletCode,
                    NgSfcList = dto.NgSfcList.Select(ng => new NgSfcItem
                    {
                        Sfc = ng.Sfc,
                        NgCode = ng.NgCode,
                        NgEquipmentCode = ng.NgEquipmentCode,
                        NgResourceCode = ng.NgResourceCode
                    }).ToList()
                };
 
                string requestJson = System.Text.Json.JsonSerializer.Serialize(mesRequest);
 
                // 4. 调用MES接口(同步方法)
                var mesResult = _mesService.ContainerNgReport(mesRequest);
                stopwatch.Stop();
 
                // 5. 记录日志
                await _mesLogService.LogAsync(new MesApiLogDto
                {
                    ApiType = "ContainerNgReport",
                    RequestJson = requestJson,
                    ResponseJson = System.Text.Json.JsonSerializer.Serialize(mesResult),
                    IsSuccess = mesResult.IsSuccess,
                    ErrorMessage = mesResult.ErrorMessage,
                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
                    Creator = App.User.UserName
                });
 
                // 6. 返回结果
                if (mesResult.IsSuccess)
                {
                    return response.OK("NG电芯上报成功");
                }
                else
                {
                    return response.Error($"MES接口调用失败: {mesResult.ErrorMessage}");
                }
            }
            catch (System.Exception ex)
            {
                stopwatch.Stop();
 
                // 记录错误日志
                await _mesLogService.LogAsync(new MesApiLogDto
                {
                    ApiType = "ContainerNgReport",
                    IsSuccess = false,
                    ErrorMessage = ex.Message,
                    ElapsedMs = (int)stopwatch.ElapsedMilliseconds,
                    Creator = App.User.UserName
                });
 
                return response.Error($"NG电芯上报失败: {ex.Message}");
            }
        }
 
        /// <summary>
        /// 从配置字典中获取配置值
        /// </summary>
        /// <param name="configs">配置字典列表</param>
        /// <param name="key">配置键</param>
        /// <param name="defaultValue">默认值</param>
        /// <returns>配置值</returns>
        private string GetConfigValue(System.Collections.Generic.List<WIDESEA_DTO.System.VueDictionaryDTO> configs, string key, string defaultValue = "")
        {
            if (configs != null)
            {
                var config = configs.FirstOrDefault(c => c.DicNo == key);
                if (config != null && config.Data != null)
                {
                    // Data是dynamic类型,尝试获取第一个元素的value属性
                    try
                    {
                        // 使用dynamic来访问匿名类型的属性
                        dynamic data = config.Data;
                        if (data != null)
                        {
                            // data可能是IEnumerable或者单个对象
                            var enumerable = data as System.Collections.IEnumerable;
                            if (enumerable != null)
                            {
                                foreach (var item in enumerable)
                                {
                                    // 获取第一个元素
                                    dynamic firstItem = item;
                                    var value = firstItem.value;
                                    return value?.ToString() ?? defaultValue;
                                }
                            }
                        }
                    }
                    catch
                    {
                        // 如果无法获取,返回默认值
                        return defaultValue;
                    }
                }
            }
            return defaultValue;
        }
    }
}