肖洋
2025-01-10 5d663325953692c634046194bedd39b89ace41f0
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
using LogLibrary.Log;
using Masuit.Tools;
using Newtonsoft.Json;
using WIDESEA_Common;
using WIDESEA_Core;
using WIDESEA_Core.Const;
using WIDESEA_Core.Helper;
using WIDESEA_DTO;
using WIDESEA_DTO.MOM;
using WIDESEA_IBusinessesRepository;
using WIDESEA_IServices;
using WIDESEA_IStorageBasicRepository;
using WIDESEA_IStorageTaskRepository;
using WIDESEA_IStoragIntegrationServices;
using WIDESEA_Model.Models;
 
namespace WIDESEA_StoragIntegrationServices;
 
public class ProcessApplyService : IProcessApplyService
{
    private readonly LogFactory LogFactory = new LogFactory();
    private readonly ISys_ConfigService _configService;
    private readonly IDt_AreaInfoRepository _areaInfoRepository;
    private readonly ICellStateService _cellStateService;
    private readonly IStockInfoRepository _stockInfoRepository;
    private readonly IAgingInOrOutInputService _gingInOrOutInputService;
 
    public ProcessApplyService(ISys_ConfigService configRepository, IDt_AreaInfoRepository areaInfoRepository, ICellStateService cellStateService, IDt_TaskRepository taskRepository, IStockInfoRepository stockInfoRepository, IAgingInOrOutInputService gingInOrOutInputService)
    {
        _configService = configRepository;
        _areaInfoRepository = areaInfoRepository;
        _cellStateService = cellStateService;
        _stockInfoRepository = stockInfoRepository;
        _gingInOrOutInputService = gingInOrOutInputService;
    }
 
    /// <summary>
    /// 工艺路线申请
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    public async Task<WebResponseContent> GetProcessApplyAsync(ProcessApplyDto input)
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            input.SessionId = Guid.NewGuid().ToString();
            input.EmployeeNo = "MITest";
            input.RequestTime = TimeZoneInfo.ConvertTimeToUtc(DateTime.Now).ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
            var inputJson = input.ToDictionary();
            var configs = _configService.GetConfigsByCategory(CateGoryConst.SYS_MOMIPAddress);
            var wmsBase = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.MOMBaseIP)?.ConfigValue;
            var ipAddress = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.ProcessApply)?.ConfigValue;
            if (wmsBase == null || ipAddress == null)
            {
                throw new InvalidOperationException("WMS IP 未配置");
            }
            var wmsIpAddress = wmsBase + ipAddress;
 
            var result = HttpsClient.PostAsync(wmsIpAddress, inputJson).Result;
            //var x = await HttpsClient.PostAsync("http://c24-cellmi3:12020/api/MachineIntegration/ProcessApply", inputJson);
            if (result != null)
            {
                //var ResultProcess = JsonConvert.DeserializeObject<ResultProcessApply>(x);
                content.OK(data: result);
            }
            LogFactory.GetLog("工艺路线申请").Info(true, $"\r\r--------------------------------------");
            LogFactory.GetLog("工艺路线申请").Info(true, result);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            LogFactory.GetLog("工艺路线申请").Error(true, $"\r\r--------------------------------------");
            LogFactory.GetLog("工艺路线申请").Error(true, ex.StackTrace);
        }
        return content;
    }
 
    /// <summary>
    /// 补录出库数据
    /// </summary>
    /// <param name="palletCode"></param>
    /// <returns></returns>
    public async Task<WebResponseContent> StockOutDataBackfillInterfaceAsync(string palletCode, int areaID)
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            var area = _areaInfoRepository.QueryFirst(x => x.AreaID == areaID);
            var trayCells = new TrayCellsStatusDto()
            {
                Software = area.Spare3,
                TrayBarcode = palletCode,
                EquipmentCode = area.Spare2,
                SceneType = area.Spare4
            };
            content = await _cellStateService.GetTrayCellStatusAsync(trayCells);
            if (!content.Status) return content;
 
            var result = JsonConvert.DeserializeObject<ResultTrayCellsStatus>(content.Data.ToString());
            if (result.SerialNos.Count > 0)
            {
                var stock = await SqlSugarHelper.DbWMS.Queryable<DtStockInfo>().FirstAsync(x => x.PalletCode == palletCode);
                if (stock != null)
                {
                    var parameterInfo = JsonConvert.DeserializeObject<List<ParameterInfo>>(stock.ParameterInfos).FirstOrDefault(y => y.Description.Contains("时间"));
                    if (parameterInfo == null) throw new Exception("");
 
                    var outHours = (DateTime.Now - (stock.LinedProcessFeedbackTime == null ? stock.ModifyDate.Value : stock.LinedProcessFeedbackTime.ToDateTime())).TotalHours;
                    var isNG = outHours > parameterInfo.LowerSpecificationsLimit.ToDouble() && outHours < parameterInfo.UpperSpecificationsLimit.ToDouble();
 
                    var defectCode = string.Empty;
                    if (!isNG) defectCode = "TQCK";
                    var outputDto = new AgingOutputDto
                    {
                        OpFlag = 1,
                        Software = area.Spare3,
                        EquipmentCode = area.Spare2,
                        TrayBarcode = palletCode,
                        SerialNos = result.SerialNos.Select(x => new SerialNoOutDto
                        {
                            SlotNo = x.PositionNo,
                            SerialNo = x.SerialNo,
                            SerialNoResult = true, //isNG,
                            ParameterInfo = new List<ParameterInfoOutput> {
                                new ParameterInfoOutput() {
                                    Value = outHours.ToString(),
                                    ParameterCode =parameterInfo.ParameterCode,
                                    ParameterDesc = parameterInfo.Description,
                                    ParameterResult  = "OK", //isNG.ToString(),
                                    TargetValue = parameterInfo.TargetValue,
                                    LowerLimit = parameterInfo.LowerSpecificationsLimit,
                                    UpperLimit = parameterInfo.UpperSpecificationsLimit,
                                    DefectCode = defectCode,
                                    UOMCode = parameterInfo.UOMCode,
                                }
                            }
                        }).ToList()
                    };
                    content = await _gingInOrOutInputService.GetOCVOutputAsync(outputDto);
                }
                else
                {
                    var stockHty = await SqlSugarHelper.DbWMS.Queryable<DtStockInfo_Hty>().Where(x => x.PalletCode == palletCode).OrderByDescending(x => x.CreateDate).FirstAsync();
                    if (stockHty != null)
                    {
                        var parameterInfo = JsonConvert.DeserializeObject<List<ParameterInfo>>(stockHty.ParameterInfos).FirstOrDefault(y => y.Description.Contains("时间"));
                        if (parameterInfo == null) throw new Exception("");
 
                        var outHours = (DateTime.Now - (stockHty.LinedProcessFeedbackTime == null ? "2025-01-08 15:59:34.310".ToDateTime() : stockHty.LinedProcessFeedbackTime.ToDateTime())).TotalHours;
                        var isNG = outHours > parameterInfo.LowerSpecificationsLimit.ToDouble() && outHours < parameterInfo.UpperSpecificationsLimit.ToDouble();
 
                        var defectCode = string.Empty;
                        if (!isNG) defectCode = "TQCK";
                        var outputDto = new AgingOutputDto
                        {
                            OpFlag = 1,
                            Software = area.Spare3,
                            EquipmentCode = area.Spare2,
                            TrayBarcode = palletCode,
                            SerialNos = result.SerialNos.Select(x => new SerialNoOutDto
                            {
                                SlotNo = x.PositionNo,
                                SerialNo = x.SerialNo,
                                SerialNoResult = true, //isNG,
                                ParameterInfo = new List<ParameterInfoOutput> {
                                new ParameterInfoOutput() {
                                    Value = outHours.ToString(),
                                    ParameterCode =parameterInfo.ParameterCode,
                                    ParameterDesc = parameterInfo.Description,
                                    ParameterResult  = "OK", //isNG.ToString(),
                                    TargetValue = parameterInfo.TargetValue,
                                    LowerLimit = parameterInfo.LowerSpecificationsLimit,
                                    UpperLimit = parameterInfo.UpperSpecificationsLimit,
                                    DefectCode = defectCode,
                                    UOMCode = parameterInfo.UOMCode,
                                }
                            }
                            }).ToList()
                        };
                        content = await _gingInOrOutInputService.GetOCVOutputAsync(outputDto);
                    }
                    else
                    {
                        content.Error("杀杀杀");
                    }
                }
            }
            else
            {
                content.Error("电芯数据为空");
            }
        }
        catch (Exception ex)
        {
        }
        return content;
    }
 
    /// <summary>
    /// 补录入库数据
    /// </summary>
    /// <param name="palletCode"></param>
    /// <param name="areaID"></param>
    /// <returns></returns>
    public async Task<WebResponseContent> StockInDataBackfillInterfaceAsync(string palletCode, int areaID)
    {
        WebResponseContent content = new WebResponseContent();
        try
        {
            var area = _areaInfoRepository.QueryFirst(x => x.AreaID == areaID);
            var trayCells = new TrayCellsStatusDto()
            {
                Software = area.Spare3,
                TrayBarcode = palletCode,
                EquipmentCode = area.Spare2,
                SceneType = area.Spare4
            };
            content = await _cellStateService.GetTrayCellStatusAsync(trayCells);
            if (!content.Status) return content;
 
            var result = JsonConvert.DeserializeObject<ResultTrayCellsStatus>(content.Data.ToString());
            if (result.SerialNos.Count > 0 && result.Success)
            {
                var stockInfo = await _stockInfoRepository.QueryFirstNavAsync(x => x.PalletCode == palletCode);
                if (stockInfo != null)
                {
                    stockInfo.IsFull = true;
                    stockInfo.StockInfoDetails = result.SerialNos.Select(serialNoObj => new DtStockInfoDetail
                    {
                        SerialNumber = serialNoObj.SerialNo,
                        OrderNo = serialNoObj.PositionNo.ToString(),
                        Status = serialNoObj.SerialNoStatus,
                        MaterielCode = result.BindCode,
                        Remark = result.TrayBarcodePropertys.ToJsonString(),
                    }).ToList();
                    stockInfo.Remark = stockInfo.StockInfoDetails.Count().ToString();
 
                    // 处理请求参数
                    AgingInputDto agingInputDto = new AgingInputDto()
                    {
                        SerialNos = stockInfo.StockInfoDetails
                            .Select(item => new SerialNoInDto { SerialNo = item.SerialNumber, PositionNo = item.OrderNo })
                            .ToList(),
                        TrayBarcode = palletCode,
                        OpFlag = 1,
                        EquipmentCode = area.Spare2,
                        Software = area.Spare3
                    };
 
                    content = _gingInOrOutInputService.GetOCVInputAsync(agingInputDto).Result;
                    var respone = JsonConvert.DeserializeObject<ResponeAgingInputDto>(content.Data.ToString());
 
                    stockInfo.LinedProcessFeedbackTime = respone.LinedProcessFeedbackTime;
                    stockInfo.SpecialParameterDuration = respone.SpecialParameterDuration;
                    //2024年11月16日:新增字段计算应出库时间
                    stockInfo.OutboundTime = Convert.ToDateTime(respone.LinedProcessFeedbackTime == null ? DateTime.Now : respone.LinedProcessFeedbackTime).AddHours(Convert.ToDouble(respone.SpecialParameterDuration));
                    stockInfo.ProductionLine = respone.ProductionLine;
                    stockInfo.ParameterInfos = respone.ParameterInfos.ToJsonString();
                    stockInfo.StockStatus = 1;
 
                    var isResult = await _stockInfoRepository.UpdateDataNavAsync(stockInfo);
                    if (isResult)
                        content.OK("更新数据成功");
                    else
                        content.Error("更新数据失败");
                }
                else
                {
                    content.Error("未找到库存");
                }
            }
            else
            {
                return content;
            }
        }
        catch (Exception ex)
        {
            content.Error(ex.Message);
        }
        return content;
    }
}