1
dengjunjie
2025-05-08 092971a8ba7848f024427694c642959d0fbc8599
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
using AutoMapper;
using Microsoft.IdentityModel.Tokens;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
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_IBasicService;
using WIDESEA_IInboundRepository;
using WIDESEA_IInboundService;
using WIDESEA_IStockService;
using WIDESEA_ITaskInfoRepository;
using WIDESEA_Model.Models;
 
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;
 
        public IInboundOrderRepository Repository => BaseDal;
 
        public InboundOrderService(IInboundOrderRepository BaseDal, IMapper mapper, IUnitOfWorkManage unitOfWorkManage, ITaskRepository taskRepository, IBasicService basicService, IStockService stockService, IInboundOrderDetailService inboundOrderDetailService) : base(BaseDal)
        {
            _mapper = mapper;
            _unitOfWorkManage = unitOfWorkManage;
            _taskRepository = taskRepository;
            _basicService = basicService;
            _stockService = stockService;
            _inboundOrderDetailService = inboundOrderDetailService;
        }
 
        /// <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
 
                List<string> LocationCodes = orderAddDTO.Details
                    .Where(x => !string.IsNullOrEmpty(x.LocationCode))
                    .Select(x => x.LocationCode).ToList();
 
                Dt_InboundOrder inboundOrder = _mapper.Map<Dt_InboundOrder>(orderAddDTO);
                //inboundOrder.OrderNo = DateTime.Now.ToString("yyMMddHHmmss");
                inboundOrder.OrderStatus = InboundStatusEnum.未开始.ObjToInt();
                Db.Ado.BeginTran();
                if (LocationCodes.Any())
                {
                    content = _basicService.LocationInfoService.UpdateStatus(LocationCodes, LocationStatusEnum.PalletLock.ObjToInt());
                    if (!content.Status)
                        throw new Exception(content.Message);
                }
 
                BaseDal.Db.InsertNav(inboundOrder).Include(x => x.Details).ExecuteCommand();
                Db.Ado.CommitTran();
                content = WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                content = WebResponseContent.Instance.Error(ex.Message);
            }
            finally
            {
 
            }
            return content;
        }
 
        public override WebResponseContent UpdateData(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                if (saveModel.DetailData == null || saveModel.DetailData.Count == 0) throw new Exception($"入库单明细不能为空");
                List<InboundOrderDetailAddDTO> inboundOrderDetail = saveModel.DetailData.DicToIEnumerable<InboundOrderDetailAddDTO>();
                //if (inboundOrderDetail.FirstOrDefault(x => x.OrderDetailStatus > 0) != null) throw new Exception($"订单已开始组盘入库");
                //var inboundOrderDetails = _inboundOrderDetailService.Db.Queryable<Dt_InboundOrderDetail>().Where(x => saveModel.DelKeys.Contains(x.Id)).ToList();
                //if (inboundOrderDetails.Count > 0)
                //{
                //    if (inboundOrderDetails.FirstOrDefault(x => x.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt()) != null)
                //        throw new Exception($"存在已组盘工单,已先解盘");
                //}
                content = base.UpdateData(saveModel);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            return content;
        }
 
        public override WebResponseContent DeleteData(object[] keys)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                //var inboundOrders = BaseDal.QueryData(x => keys.Contains(x.Id));
                var inboundOrders = BaseDal.Db.Queryable<Dt_InboundOrder>().Includes(x => x.Details).Where(x => keys.Contains(x.Id)).ToList();
                if (inboundOrders.Count < 1) throw new Exception("未找到入库单");
                List<Dt_InboundOrderDetail> orderDetails = new List<Dt_InboundOrderDetail>();
                foreach (var item in inboundOrders)
                {
                    if (item.Details.Where(x => x.ReceiptQuantity != x.OverInQuantity).Any())
                        throw new Exception("存在未入库完成托盘");
                    orderDetails.AddRange(item.Details);
                    item.Details = null;
                }
                BaseDal.DeleteAndMoveIntoHty(inboundOrders, OperateType.人工删除);
                content.Status = _inboundOrderDetailService.Repository.DeleteAndMoveIntoHty(orderDetails, OperateType.人工删除);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
            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<InOrderTypeEnum>().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.OrderNo == inboundOrderAddDTO.orderNo && !string.IsNullOrEmpty(x.OrderNo)) != null)
            {
                return (false, "单据已存在", inboundOrderAddDTO);
            }
            return (true, "成功", inboundOrderAddDTO);
        }
    }
}