1
huangxiaoqiang
2025-12-18 9753fb2756f6b4e30ff79d901a7bb86145517c8b
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Helper;
using WIDESEA_Core.HttpContextUser;
using WIDESEA_Core.Util;
using WIDESEA_DTO.Base;
using WIDESEA_DTO.ReturnMES;
using WIDESEA_IBasicService;
using WIDESEA_Model.Models;
using static HslCommunication.Profinet.Knx.KnxCode;
 
namespace WIDESEA_BasicService.MESOperation
{
    public class FeedbackMesService : ServiceBase<Dt_MesReturnRecord, IRepository<Dt_MesReturnRecord>>, IFeedbackMesService
    {
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly HttpClientHelper _httpClientHelper;
        private readonly IRepository<Dt_OutboundOrder> _outboundOrderRepository;
        private readonly IBasicService _basicService;
        private readonly IRepository<Dt_AllocateOrder> _allocateRepository;
 
        public FeedbackMesService(IRepository<Dt_MesReturnRecord> BaseDal, IUnitOfWorkManage unitOfWorkManage, HttpClientHelper httpClientHelper, IRepository<Dt_OutboundOrder> outboundOrderRepository, IBasicService basicService, IRepository<Dt_AllocateOrder> allocateRepository) : base(BaseDal)
        {
            _unitOfWorkManage = unitOfWorkManage;
            _httpClientHelper = httpClientHelper;
            _outboundOrderRepository = outboundOrderRepository;
            _basicService = basicService;
            _allocateRepository = allocateRepository;
        }
 
        public WebResponseContent OutboundFeedback(string orderNo)
        {
            try
            {
                Dt_OutboundOrder outboundOrder = _outboundOrderRepository.Db.Queryable<Dt_OutboundOrder>().Where(x => x.OrderNo == orderNo).Includes(x => x.Details).First();
                if (outboundOrder == null)
                {
                    return WebResponseContent.Instance.Error($"未找到对应的出库单信息");
                }
                HttpResponseResult<MesResponseDTO> httpResponseResult = new HttpResponseResult<MesResponseDTO>();
                string reqCode = Guid.NewGuid().ToString();
                string reqTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                string requestData = string.Empty;
                List<string> lineNos = new List<string>();
                if (outboundOrder.OrderType == 0)
                {
                    MaterialOutboundReturnDTO? returnDTO = BuildOutboundFeedbackData(outboundOrder);
                    if (returnDTO == null)
                    {
                        return WebResponseContent.Instance.Error($"构建回调对象失败");
                    }
                    string apiUrl = "";
                    returnDTO.ReqCode = reqCode;
                    returnDTO.ReqTime = reqTime;
                    requestData = returnDTO.Serialize();
                    lineNos = returnDTO.Details.Select(x => x.LineNo).ToList();
                    httpResponseResult = _httpClientHelper.Post<MesResponseDTO>(apiUrl, requestData);
                    httpResponseResult.ApiUrl = apiUrl;
                }
                else
                {
                    Dt_AllocateOrder allocateOrder = _allocateRepository.QueryFirst(x => x.OrderNo == outboundOrder.OrderNo);
                    if (allocateOrder == null)
                    {
                        return WebResponseContent.Instance.Error($"未找到对应的调拨单");
                    }
                    AllocationReturnDTO? returnDTO = BuildAllocationFeedbackData(outboundOrder, allocateOrder.FromWarehouse, allocateOrder.ToWarehouse);
                    if (returnDTO == null)
                    {
                        return WebResponseContent.Instance.Error($"构建回调对象失败");
                    }
                    string apiUrl = "";
                    returnDTO.ReqCode = reqCode;
                    returnDTO.ReqTime = reqTime;
                    requestData = returnDTO.Serialize();
                    lineNos = returnDTO.Details.Select(x => x.LineNo).ToList();
                    httpResponseResult = _httpClientHelper.Post<MesResponseDTO>(apiUrl, requestData);
                    httpResponseResult.ApiUrl = apiUrl;
                }
 
                bool isSuccess = httpResponseResult.IsSuccess && httpResponseResult.Data != null && httpResponseResult.Data.Code == "200";
                string message = "成功";
                if (!isSuccess)
                {
                    if (!httpResponseResult.IsSuccess)
                    {
                        message = $"MES接口返回错误,HTTP代码:{httpResponseResult.StatusCode},信息:{httpResponseResult.ErrorMessage}";
                    }
                    else if (httpResponseResult.Data.Code != "200")
                    {
                        message = $"调用MES接口失败,代码:{httpResponseResult.Data.Code},信息:{httpResponseResult.Data.Message}";
                    }
                }
 
                Dt_MesReturnRecord mesReturnRecord = new Dt_MesReturnRecord()
                {
                    ApiUrl = httpResponseResult.ApiUrl,
                    InterfaceType = 1,
                    OrderId = outboundOrder.Id,
                    OrderNo = outboundOrder.OrderNo,
                    OrderType = outboundOrder.OrderType,
                    RequestCode = reqCode,
                    RequestData = requestData,
                    FailureReason = message,
                    LastReturnTime = DateTime.Now,
                    HttpStatusCode = httpResponseResult.StatusCode.ObjToInt(),
                    ResponseData = httpResponseResult.Content,
                    ReturnType = 0,
                    ReturnCount = 1,
                    ReturnStatus = httpResponseResult.IsSuccess ? 1 : 2,
                    SuccessTime = httpResponseResult.IsSuccess ? DateTime.Now : null
                };
 
                _unitOfWorkManage.BeginTran();
                _unitOfWorkManage.Db.Insertable(mesReturnRecord).ExecuteCommand();
 
                if (isSuccess)
                {
                    List<Dt_OutboundOrderDetail> outboundOrderDetails = outboundOrder.Details.Where(x => lineNos.Contains(x.lineNo)).ToList();
                    outboundOrderDetails.ForEach(x =>
                    {
                        if (x.OverOutQuantity == x.OrderQuantity - x.MoveQty)
                        {
                            x.ReturnToMESStatus = isSuccess ? 1 : 2;
                        }
                        else
                        {
                            x.ReturnToMESStatus = isSuccess ? 3 : 4;
                        }
                        x.CurrentDeliveryQty = 0;
                        x.ReturnJsonData = "";
                    });
 
                    _outboundOrderRepository.Db.Updateable(outboundOrderDetails).ExecuteCommand();
                }
                
                _unitOfWorkManage.CommitTran();
 
                WebResponseContent responseContent = new WebResponseContent();
                responseContent.Status = isSuccess;
                responseContent.Message = message;
                return responseContent;
            }
            catch(Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
        public AllocationReturnDTO? BuildAllocationFeedbackData(Dt_OutboundOrder outboundOrder, string fromWarehouse, string toWarehouse)
        {
            try
            {
                List<Dt_OutboundOrderDetail> details = outboundOrder.Details;
 
                List<AllocationDetail> returnDetails = new List<AllocationDetail>();
 
                foreach (var detail in details)
                {
                    List<Barcodes>? barcodes = JsonConvert.DeserializeObject<List<Barcodes>>(detail.ReturnJsonData);
                    if (barcodes != null && barcodes.Any())
                    {
                        UnitConvertResultDTO currentResult = _basicService.UnitQuantityConvert(detail.MaterielCode, detail.Unit, detail.BarcodeUnit, detail.CurrentDeliveryQty);
                        UnitConvertResultDTO totalResult = _basicService.UnitQuantityConvert(detail.MaterielCode, detail.Unit, detail.BarcodeUnit, detail.OrderQuantity);
 
                        returnDetails.Add(new AllocationDetail
                        {
                            Barcodes = barcodes,
                            BatchNo = detail.BatchNo,
                            LineNo = detail.lineNo,
                            MaterialCode = detail.MaterielCode,
                            Qty = totalResult.ToQuantity,
                            WarehouseCode = detail.WarehouseCode,
                            Unit = detail.BarcodeUnit
                        });
                    }
                }
 
                AllocationReturnDTO outboundReturnDTO = new AllocationReturnDTO()
                {
                    Business_type = outboundOrder.BusinessType,
                    Details = returnDetails,
                    FactoryArea = outboundOrder.FactoryArea,
                    OperationType = 1,
                    OrderNo = outboundOrder.OrderNo,
                    FromWarehouse = fromWarehouse,
                    ToWarehouse = toWarehouse
                };
 
                return outboundReturnDTO;
 
            }
            catch (Exception ex)
            {
                return null;
            }
        }
 
        public void MaterialOutboundFeedback(Dt_OutboundOrder outboundOrder)
        {
            try
            {
                MaterialOutboundReturnDTO? returnDTO = BuildOutboundFeedbackData(outboundOrder);
                if (returnDTO != null)
                {
                    string apiUrl = "";
 
                    HttpResponseResult<MesResponseDTO> httpResponseResult = _httpClientHelper.Post<MesResponseDTO>(apiUrl, returnDTO.Serialize());
 
                    bool isSuccess = httpResponseResult.IsSuccess && httpResponseResult.Data != null && httpResponseResult.Data.Code == "200";
                    string message = "";
                    if (!isSuccess)
                    {
                        if (httpResponseResult.IsSuccess)
                        {
                            message = $"MES接口返回错误,HTTP代码:{httpResponseResult.StatusCode},信息:{httpResponseResult.ErrorMessage}";
                        }
                        else if (httpResponseResult.Data.Code != "200")
                        {
                            message = $"调用MES接口失败,代码:{httpResponseResult.Data.Code},信息:{httpResponseResult.Data.Message}";
                        }
                    }
 
                    Dt_MesReturnRecord mesReturnRecord = new Dt_MesReturnRecord()
                    {
                        ApiUrl = apiUrl,
                        InterfaceType = 1,
                        OrderId = outboundOrder.Id,
                        OrderNo = outboundOrder.OrderNo,
                        OrderType = outboundOrder.OrderType,
                        RequestCode = returnDTO.ReqCode,
                        RequestData = returnDTO.Serialize(),
                        FailureReason = message,
                        LastReturnTime = DateTime.Now,
                        HttpStatusCode = httpResponseResult.StatusCode.ObjToInt(),
                        ResponseData = httpResponseResult.Content,
                        ReturnType = 0,
                        ReturnCount = 1,
                        ReturnStatus = httpResponseResult.IsSuccess ? 1 : 2,
                        SuccessTime = httpResponseResult.IsSuccess ? DateTime.Now : null
                    };
 
                    _unitOfWorkManage.BeginTran();
                    _unitOfWorkManage.Db.Insertable(mesReturnRecord).ExecuteCommand();
                    List<string> lineNos = returnDTO.Details.Select(x => x.LineNo).ToList();
 
                    List<Dt_OutboundOrderDetail> outboundOrderDetails = outboundOrder.Details.Where(x => lineNos.Contains(x.lineNo)).ToList();
                    outboundOrderDetails.ForEach(x =>
                    {
                        if (x.OverOutQuantity == x.OrderQuantity - x.MoveQty)
                        {
                            x.ReturnToMESStatus = isSuccess ? 1 : 2;
                        }
                        else
                        {
                            x.ReturnToMESStatus = isSuccess ? 3 : 4;
                        }
                        x.CurrentDeliveryQty = 0;
                        x.ReturnJsonData = "";
                    });
 
                    _outboundOrderRepository.Db.Updateable(outboundOrderDetails).ExecuteCommand();
 
                    _unitOfWorkManage.CommitTran();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
 
        public MaterialOutboundReturnDTO? BuildOutboundFeedbackData(Dt_OutboundOrder outboundOrder)
        {
            try
            {
                List<Dt_OutboundOrderDetail> details = outboundOrder.Details;
 
                List<MaterialOutboundDetail> returnDetails = new List<MaterialOutboundDetail>();
 
                foreach (var detail in details)
                {
                    if (!string.IsNullOrWhiteSpace(detail.ReturnJsonData))
                    {
                        List<Barcodes>? barcodes = JsonConvert.DeserializeObject<List<Barcodes>>(detail.ReturnJsonData);
                        if (barcodes != null && barcodes.Any())
                        {
                            UnitConvertResultDTO currentResult = _basicService.UnitQuantityConvert(detail.MaterielCode, detail.Unit, detail.BarcodeUnit, detail.CurrentDeliveryQty);
                            UnitConvertResultDTO totalResult = _basicService.UnitQuantityConvert(detail.MaterielCode, detail.Unit, detail.BarcodeUnit, detail.OrderQuantity);
 
                            returnDetails.Add(new MaterialOutboundDetail
                            {
                                Barcodes = barcodes,
                                CurrentDeliveryQty = currentResult.ToQuantity,
                                LineNo = detail.lineNo,
                                MaterialCode = detail.MaterielCode,
                                Qty = totalResult.ToQuantity,
                                WarehouseCode = detail.WarehouseCode,
                                Unit = detail.BarcodeUnit
                            });
                        }
                    }
                }
 
                MaterialOutboundReturnDTO outboundReturnDTO = new MaterialOutboundReturnDTO()
                {
                    Business_type = outboundOrder.BusinessType,
                    Details = returnDetails,
                    DocumentsNO = "",
                    FactoryArea = outboundOrder.FactoryArea,
                    OperationType = 1,
                    Operator = App.User.UserName,
                    OrderNo = outboundOrder.OrderNo,
                    Status = 1
                };
 
                return outboundReturnDTO;
 
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}