1
wankeda
2025-02-12 8326222e65f0bb258c99d93f1954d7696c4ef00d
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
using AutoMapper;
using MailKit.Search;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Utilities;
using WIDESEA_DTO;
using WIDESEA_DTO.Inbound;
using WIDESEA_DTO.Outbound;
using WIDESEA_IBasicService;
using WIDESEA_IInboundRepository;
using WIDESEA_IInboundService;
using WIDESEA_IStockService;
using WIDESEA_ITaskInfoRepository;
using WIDESEA_Model.Models;
using static WIDESEA_Common.HouseInventoryIn;
 
namespace WIDESEA_InboundService
{
    public partial class InboundOrderService : ServiceBase<Dt_InboundOrder, IInboundOrderRepository>, IInboundOrderService
    {
        private readonly IMapper _mapper;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly ITaskRepository _taskRepository;
 
        private IBasicService _basicService;
        private IStockService _stockService;
        private IInboundOrderDetailService _inboundOrderDetailService;
        private IInboundOrder_HtyService _inboundOrderHtyService;
        private IInboundOrderDetail_HtyService _inboundOrderDetail_HtyService;
 
        public IInboundOrderRepository Repository => BaseDal;
 
        public InboundOrderService(IInboundOrderRepository BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, ITaskRepository taskRepository, IBasicService basicService, IStockService stockService, IInboundOrderDetailService inboundOrderDetailService, IInboundOrder_HtyService inboundOrderHtyService, IInboundOrderDetail_HtyService inboundOrderDetail_HtyService) : base(BaseDal)
        {
            _mapper = mapper;
            _unitOfWorkManage = unitOfWorkManage;
            _taskRepository = taskRepository;
            _basicService = basicService;
            _stockService = stockService;
            _inboundOrderDetailService = inboundOrderDetailService;
            _inboundOrderHtyService = inboundOrderHtyService;
            _inboundOrderDetail_HtyService = inboundOrderDetail_HtyService;
        }
 
        /// <summary>
        /// 添加单据
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public override WebResponseContent AddData(SaveModel saveModel)
        {
            InboundOrderAddDTO orderAddDTO = saveModel.MainData.DicToModel<InboundOrderAddDTO>();
            orderAddDTO.Details = saveModel.DetailData.DicToIEnumerable<InboundOrderDetailAddDTO>();
            return AddInboundOrder(orderAddDTO);
        }
 
        /// <summary>
        /// 添加单据
        /// </summary>
        /// <param name="orderAddDTO">单据添加DTO</param>
        /// <returns></returns>
        public WebResponseContent AddInboundOrder(InboundOrderAddDTO orderAddDTO)
        {
            WebResponseContent content = new();
            try
            {
                #region 验证数据
                (bool, string, object?) result = CheckInboundOrderAddData(orderAddDTO);
                if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2);
                #endregion
 
                Dt_InboundOrder inboundOrder = _mapper.Map<Dt_InboundOrder>(orderAddDTO);
                inboundOrder.OrderStatus = InboundStatusEnum.未开始.ObjToInt();
                bool a = BaseDal.Db.InsertNav(inboundOrder).Include(x => x.Details).ExecuteCommand();
                content = WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            finally
            {
 
            }
            return content;
        }
        /// <summary>
        /// 入库单据下发
        /// </summary>
        /// <param name="orderAddDTO"></param>
        /// <returns></returns>
        public WebResponseContent AddInboundOrders(HouseInbound orderAddDTO)
        {
            WebResponseContent content = new();
            try
            {
                InboundOrderAddDTO orderAddDTO1 = new InboundOrderAddDTO();
                orderAddDTO1.OrderNo = orderAddDTO.AsnNo;
                orderAddDTO1.UpperOrderNo = orderAddDTO.AsnNo;
                orderAddDTO1.OutWareHouse = orderAddDTO.InWarehouse;
                orderAddDTO1.TransactionCode = orderAddDTO.TransactionCode;
                orderAddDTO1.InoutType = orderAddDTO.OrderType;
                orderAddDTO1.OrderType = orderAddDTO.InoutType.ObjToInt();
                orderAddDTO1.Details = orderAddDTO.DetailList.DicToIEnumerable<InboundOrderDetailAddDTO>();
                #region 验证数据
                (bool, string, object?) result = CheckInboundOrderAddData(orderAddDTO1);
                if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2);
                #endregion
 
                Dt_InboundOrder inboundOrder = _mapper.Map<Dt_InboundOrder>(orderAddDTO1);
                inboundOrder.OrderStatus = InboundStatusEnum.未开始.ObjToInt();
                inboundOrder.Creater = "WMS";
                inboundOrder.CreateDate = DateTime.Now;
                bool a = BaseDal.Db.InsertNav(inboundOrder).Include(x => x.Details).ExecuteCommand();
                content = WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            finally
            {
 
            }
            return content;
        }
        /// <summary>
        /// 盘点入库
        /// </summary>
        /// <returns></returns>
        public WebResponseContent InventoryIn(string name)
        {
            WebResponseContent content = new();
            try
            {
                //Dt_StockInfoDetail stockInfoDetail = _stockService.StockInfoDetailService.Repository.QueryFirst(x => x.OrderNo == name);
                ////Dt_OutboundOrderDetail outboundOrderDetail= 
                //HouseInventoryIn houseInventoryIn1 = new HouseInventoryIn();
                //houseInventoryIn1.No = name;
                //InventoryIn inventoryIn = new InventoryIn();
                //inventoryIn.LinId = stockInfoDetail.LinId;
                ////inventoryIn.LPN_No =stockInfoDetail.
                //inventoryIn.MaterielCode = stockInfoDetail.MaterielCode;
                //inventoryIn.OrderQuantity = stockInfoDetail.StockQuantity;
                //inventoryIn.BatchNo = stockInfoDetail.BatchNo;
                //inventoryIn.FinishQty = stockInfoDetail.FinishQty;
                //inventoryIn.WarehouseCode =
                //  inventoryIn.StorageAreaCode =
                //  inventoryIn.StorageLocationCode =
                //          #region 验证数据
                //(bool, string, object ?) result = CheckInboundOrderAddData(orderAddDTO1);
                //          if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2);
                //          #endregion
 
                //          Dt_OutboundOrder inboundOrder = _mapper.Map<Dt_OutboundOrder>(orderAddDTO1);
                //          inboundOrder.OrderStatus = InboundStatusEnum.未开始.ObjToInt();
                //          inboundOrder.OrderType = OutOrderTypeEnum.OutInventory.ObjToInt();
                //          inboundOrder.Creater = "WMS";
                //          inboundOrder.CreateDate = DateTime.Now;
                //          bool a = BaseDal.Db.InsertNav(inboundOrder).Include(x => x.Details).ExecuteCommand();
                content = WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            finally
            {
 
            }
            return content;
        }
        /// <summary>
        /// 入库取消
        /// </summary>
        /// <param name="houseInventoryIn"></param>
        /// <returns></returns>
        public WebResponseContent CancelIn(HouseCancelIn houseCancelIn)
        {
            WebResponseContent content = new();
            try
            {
                InboundOrderAddDTO orderAddDTO1 = new InboundOrderAddDTO();
                orderAddDTO1.OrderNo = houseCancelIn.AsnNo;
                orderAddDTO1.Details = houseCancelIn.DetailList.DicToIEnumerable<InboundOrderDetailAddDTO>();
                Dt_InboundOrder oldOutboundOrder = BaseDal.Db.Queryable<Dt_InboundOrder>().Where(x => x.OrderNo == orderAddDTO1.OrderNo).Includes(x => x.Details).First();
                Dt_InboundOrderDetail dt_InboundOrderDetail = BaseDal.Db.Queryable<Dt_InboundOrderDetail>().Where(x => x.OrderId == oldOutboundOrder.Id).First();
                if (oldOutboundOrder.OrderStatus != InboundStatusEnum.未开始.ObjToInt())
                {
                    return WebResponseContent.Instance.Error("该入库单任务已开始执行,不可取消");
                }
                oldOutboundOrder.OrderStatus = InboundStatusEnum.取消.ObjToInt();
                BaseDal.UpdateData(oldOutboundOrder);
 
                Dt_InboundOrder_Hty inboundOrder_Hty = new Dt_InboundOrder_Hty
                {
                    OrderStatus = oldOutboundOrder.OrderStatus,
                    CreateType = oldOutboundOrder.CreateType,
                    //SourceId = oldOutboundOrder.SourceId,
                    UpperOrderNo = oldOutboundOrder.UpperOrderNo,
                    OrderNo = oldOutboundOrder.OrderNo,
                    OutWareHouse = oldOutboundOrder.OutWareHouse,
                    TransactionCode = oldOutboundOrder.TransactionCode,
                    InoutType = oldOutboundOrder.InoutType,
                    OrderType = oldOutboundOrder.OrderType,
                    Creater = "WMS",
                    CreateDate = DateTime.Now,
                };
                _inboundOrderHtyService.AddData(inboundOrder_Hty);
                foreach (var item in oldOutboundOrder.Details)
                {
                    Dt_InboundOrderDetail_Hty dt_InboundOrderDetail_Hty = new Dt_InboundOrderDetail_Hty
                    {
                        OrderId = dt_InboundOrderDetail.OrderId,
                        MaterielCode = dt_InboundOrderDetail.MaterielCode,
                        MaterielName = dt_InboundOrderDetail.MaterielName,
                        BatchNo = dt_InboundOrderDetail.BatchNo,
                        OrderQuantity = dt_InboundOrderDetail.OrderQuantity,
                        ReceiptQuantity = dt_InboundOrderDetail.ReceiptQuantity,
                        OverInQuantity = dt_InboundOrderDetail.OverInQuantity,
                        OrderDetailStatus = dt_InboundOrderDetail.OrderDetailStatus,
                        Creater = "WMS",
                        CreateDate = DateTime.Now,
                    };
                    _inboundOrderDetail_HtyService.AddData(dt_InboundOrderDetail_Hty);
                    _inboundOrderDetailService.DeleteData(item);
                }
                BaseDal.DeleteData(oldOutboundOrder);
 
                //inboundOrder_Hty.Details = oldOutboundOrder.Details;
 
                //#region 验证数据
                //(bool, string, object?) result = CheckInboundOrderAddData(orderAddDTO1);
                //#endregion
 
                //Dt_InboundOrder inboundOrder = _mapper.Map<Dt_InboundOrder>(orderAddDTO1);
                //inboundOrder.OrderStatus = InboundStatusEnum.未开始.ObjToInt();
                //inboundOrder.OrderType = OutOrderTypeEnum.OutInventory.ObjToInt();
                //inboundOrder.Creater = "WMS";
                //inboundOrder.CreateDate = DateTime.Now;
                //bool a = BaseDal.Db.InsertNav(inboundOrder).Include(x => x.Details).ExecuteCommand();
                content = WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            finally
            {
 
            }
            return content;
        }
 
        /// <summary>
        /// 验证单据添加DTO对象
        /// </summary>
        /// <param name="inboundOrderAddDTO">单据添加DTO</param>
        /// <returns></returns>
        private (bool, string, object?) CheckInboundOrderAddData(InboundOrderAddDTO inboundOrderAddDTO)
        {
            (bool, string, object?) result1 = ModelValidate.ValidateModelData(inboundOrderAddDTO);
            if (!result1.Item1) return result1;
 
            (bool, string, object?) result2 = ModelValidate.ValidateModelData(inboundOrderAddDTO.Details);
            if (!result2.Item1) return result2;
 
            IEnumerable<int> inOrderTypes = Enum.GetValues<OrderTypeEmun>().Cast<int>();
            if (!inOrderTypes.Contains(inboundOrderAddDTO.OrderType))
            {
                return (false, "未找到该单据类型", inboundOrderAddDTO);
            }
 
            List<string> materielCodes = inboundOrderAddDTO.Details.Select(x => x.MaterielCode).ToList();
            if (!_basicService.MaterielInfoService.ExsitMateriels(materielCodes))
            {
                return (false, "有物料信息未录入,请录入物料信息", inboundOrderAddDTO);
            }
 
            if (BaseDal.QueryFirst(x => x.OrderName == inboundOrderAddDTO.orderName && !string.IsNullOrEmpty(x.OrderName)) != null)
            {
                return (false, "单据已存在", inboundOrderAddDTO);
            }
            return (true, "成功", inboundOrderAddDTO);
        }
 
 
    }
}