huanghongfeng
2025-01-16 77864cf547c716d4dca35e735636013dbc4dc033
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
using AutoMapper;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common;
using WIDESEA_Core;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_Core.Utilities;
using WIDESEA_DTO.Inbound;
using WIDESEA_DTO.Outbound;
using WIDESEA_DTO.Stock;
using WIDESEA_IBasicService;
using WIDESEA_IOutboundRepository;
using WIDESEA_IOutboundService;
using WIDESEA_IStockService;
//using WIDESEA_ITaskInfoRepository;
//using WIDESEA_ITaskInfoService;
using WIDESEA_Model.Models;
 
namespace WIDESEA_OutboundService
{
    public partial class OutboundOrderService : ServiceBase<Dt_OutboundOrder, IOutboundOrderRepository>, IOutboundOrderService
    {
        private readonly IMapper _mapper;
        private readonly IMaterielInfoService _materielInfoService;
        private readonly IStockInfoService _stockService;
        private readonly IStockInfoDetailService _stockDetailService;
        //private readonly ITaskService _taskService;
 
        public IOutboundOrderRepository Repository => BaseDal;
 
        public OutboundOrderService(IOutboundOrderRepository BaseDal, IMapper mapper, IMaterielInfoService materielInfoService, IStockInfoDetailService stockDetailService, IStockInfoService stockInfoService) : base(BaseDal)
        {
            _mapper = mapper;
            _materielInfoService = materielInfoService;
            _stockDetailService = stockDetailService;
            _stockService = stockInfoService;
            //_taskService = taskService;
        }
 
        public override WebResponseContent AddData(SaveModel saveModel)
        {
            OutboundOrderAddDTO outboundOrder = saveModel.MainData.DicToModel<OutboundOrderAddDTO>();
            List<OutboundOrderDetailAddDTO> orderDetailAddDTOs = saveModel.DetailData.DicToIEnumerable<OutboundOrderDetailAddDTO>();
            outboundOrder.Details = orderDetailAddDTOs.GroupBy(x => x.MaterielCode).Select(x => new OutboundOrderDetailAddDTO
            {
                BatchNo = x.FirstOrDefault()?.BatchNo ?? "",
                MaterielCode = x.Key,
                OrderQuantity = x.Sum(x => x.OrderQuantity),
                Remark = x.FirstOrDefault(v => !string.IsNullOrEmpty(v.Remark))?.Remark ?? ""
            }).ToList();
            return AddOutboundOrder(outboundOrder);
        }
 
        public override WebResponseContent UpdateData(SaveModel saveModel)
        {
            List<Dt_OutboundOrderDetail> outboundOrderDetails = saveModel.DetailData.DicToIEnumerable<Dt_OutboundOrderDetail>();
            if (outboundOrderDetails.GroupBy(x => x.MaterielCode).Select(x => x.Count()).Any(x => x > 1))
            {
                return WebResponseContent.Instance.Error("物料重复");
            }
            outboundOrderDetails = outboundOrderDetails.Where(x => (x.Id > 0 && x.OrderDetailStatus == OrderDetailStatusEnum.New.ObjToInt()) || x.Id == 0).ToList();
 
            List<Dictionary<string, object>> dics = new List<Dictionary<string, object>>();
            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            foreach (var item in outboundOrderDetails)
            {
                string str = JsonConvert.SerializeObject(item, settings);
                Dictionary<string, object>? dic = JsonConvert.DeserializeObject<Dictionary<string, object>>(str);
                if (dic != null)
                    dics.Add(dic);
            }
            saveModel.DetailData = dics;
            return base.UpdateData(saveModel);
        }
 
        public WebResponseContent AddOutboundOrder(OutboundOrderAddDTO orderAddDTO)
        {
            WebResponseContent content = new();
            try
            {
                #region 验证数据
                (bool, string, object?) result = CheckOutboundOrderAddData(orderAddDTO);
                if (!result.Item1) return content = WebResponseContent.Instance.Error(result.Item2);
                #endregion
 
                Dt_OutboundOrder outboundOrder = _mapper.Map<Dt_OutboundOrder>(orderAddDTO);
                outboundOrder.OrderStatus = InboundStatusEnum.未开始.ObjToInt();
                bool a = BaseDal.Db.InsertNav(outboundOrder).Include(x => x.Details).ExecuteCommand();
                content = WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            finally
            {
 
            }
            return content;
        }
 
        public WebResponseContent AddOutboundOrders(Houseounbound orderAddDTO)
        {
            WebResponseContent content = new();
            try
            {
                OutboundOrderAddDTO orderAddDTO1 = new OutboundOrderAddDTO();
                orderAddDTO1.OrderNo = orderAddDTO.No;
                orderAddDTO1.UpperOrderNo = orderAddDTO.No;
                orderAddDTO1.OutWareHouse = orderAddDTO.OutWareHouse;
                orderAddDTO1.TransactionCode = orderAddDTO.TransactionCode;
                orderAddDTO1.OrderType = orderAddDTO.InoutType.ObjToInt();
                orderAddDTO1.Details = orderAddDTO.DetailList.DicToIEnumerable<OutboundOrderDetailAddDTO>();
                #region 验证数据
                (bool, string, object?) result = CheckOutboundOrderAddData(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.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 CancelOut(HouseCancelOut houseCancelOut)
        {
            WebResponseContent content = new();
            try
            {
                OutboundOrderAddDTO orderAddDTO1 = new OutboundOrderAddDTO();
                orderAddDTO1.OrderNo = houseCancelOut.No;
                orderAddDTO1.Details = houseCancelOut.DetailList.DicToIEnumerable<OutboundOrderDetailAddDTO>();
                #region 验证数据
                (bool, string, object?) result = CheckOutboundOrderAddData(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;
        }
 
        private (bool, string, object?) CheckOutboundOrderAddData(OutboundOrderAddDTO outboundOrderAddDTO)
        {
            (bool, string, object?) result1 = ModelValidate.ValidateModelData(outboundOrderAddDTO);
            if (!result1.Item1) return result1;
 
            (bool, string, object?) result2 = ModelValidate.ValidateModelData(outboundOrderAddDTO.Details);
            if (!result2.Item1) return result2;
 
            IEnumerable<int> inOrderTypes = Enum.GetValues<OutOrderTypeEnum>().Cast<int>();
            if (!inOrderTypes.Contains(outboundOrderAddDTO.OrderType))
            {
                return (false, "未找到该单据类型", outboundOrderAddDTO);
            }
 
            List<string> materielCodes = outboundOrderAddDTO.Details.Select(x => x.MaterielCode).ToList();
            if (!_materielInfoService.ExsitMateriels(materielCodes))
            {
                return (false, "有物料信息未录入,请录入物料信息", outboundOrderAddDTO);
            }
 
            if (BaseDal.QueryFirst(x => x.UpperOrderNo == outboundOrderAddDTO.UpperOrderNo && !string.IsNullOrEmpty(x.UpperOrderNo)) != null)
            {
                return (false, "单据已存在", outboundOrderAddDTO);
            }
            return (true, "成功", outboundOrderAddDTO);
        }
 
 
        public WebResponseContent ReleaseOutOrder(int orderId)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
 
            }
            catch (Exception ex)
            {
 
            }
            return content;
        }
    }
}