z8018
2025-12-17 20a71f4bafb8cda355948cc061827b8d4752966f
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
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;
 
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;
 
        public FeedbackMesService(IRepository<Dt_MesReturnRecord> BaseDal, IUnitOfWorkManage unitOfWorkManage, HttpClientHelper httpClientHelper, IRepository<Dt_OutboundOrder> outboundOrderRepository, IBasicService basicService) : base(BaseDal)
        {
            _unitOfWorkManage = unitOfWorkManage;
            _httpClientHelper = httpClientHelper;
            _outboundOrderRepository = outboundOrderRepository;
            _basicService = basicService;
        }
 
        public void MaterialOutboundFeedback(string orderNo)
        {
            try
            {
                Dt_OutboundOrder outboundOrder = _outboundOrderRepository.Db.Queryable<Dt_OutboundOrder>().Where(x => x.OrderNo == orderNo).Includes(x => x.Details).First();
                if (outboundOrder == null)
                {
                    // todo 记录日志:未找到对应的出库单
 
                    return;
                }
 
                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)
                {
                    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;
            }
        }
    }
}