hutongqing
2024-11-22 85458565e09bda1044d19b13d0b1ffb7ab576857
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
using AutoMapper;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.OrderEnum;
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_IBasicService;
using WIDESEA_IOutboundRepository;
using WIDESEA_IOutboundService;
using WIDESEA_IStockService;
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;
 
        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;
        }
 
        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),
                MaterielName= x.FirstOrDefault()?.MaterielName ?? "",
                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 = OutOrderStatusEnum.未开始.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;
        }
 
        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;
        }
 
        public WebResponseContent GetOutboundOrder(OutboundOrderGetDTO outboundOrderGetDTO)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Expression<Func<Dt_OutboundOrder, bool>> expressionOrder = x => true;
                if (!string.IsNullOrEmpty(outboundOrderGetDTO.OrderNo))
                {
                    expressionOrder = x => x.OrderNo.Contains(outboundOrderGetDTO.OrderNo);
                }
                int count = BaseDal.QueryData(x => x.OrderStatus == OutOrderStatusEnum.未开始.ObjToInt()).ToList().Count();
                int maxPage = Convert.ToInt32(Math.Ceiling(count / 10.0));
                if (outboundOrderGetDTO.pageNo <= maxPage)
                {
                    var outboundOrder = BaseDal.Db.Queryable<Dt_OutboundOrder>().Where(expressionOrder).OrderByDescending(x => x.CreateDate).Skip((outboundOrderGetDTO.pageNo - 1) * 10).Take(10).Select(x => new Dt_OutboundOrder { OrderNo = x.OrderNo, Id = x.Id, CreateDate = x.CreateDate, Creater = x.Creater }).ToList();
 
                    content = WebResponseContent.Instance.OK(data: outboundOrder);
                }
                else
                {
                    content = WebResponseContent.Instance.OK(data: null, message: "已到最后一页");
                }
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error($"查询出库单据错误,错误信息:{ex.Message}");
            }
 
            return content;
        }
 
        public WebResponseContent GetOutboundOrderDetail(string OrderNo)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                Dt_OutboundOrder outboundOrder = BaseDal.QueryFirst(x => x.OrderNo == OrderNo);
 
                var outboundOrderDetail = BaseDal.Db.Queryable<Dt_OutboundOrderDetail>().Where(x => x.OrderId == outboundOrder.Id).Take(10).Select(x => new Dt_OutboundOrderDetail {Id=x.Id, MaterielCode = x.MaterielCode, MaterielName = x.MaterielName, OrderQuantity = x.OrderQuantity, OverOutQuantity = x.OverOutQuantity, LockQuantity = x.LockQuantity }).ToList();
 
                content = WebResponseContent.Instance.OK(data: outboundOrderDetail);
            }
            catch (Exception ex)
            {
                content = WebResponseContent.Instance.Error($"查询出库单据明细错误,错误信息:{ex.Message}");
            }
            return content;
        }
    }
}