1
hutongqing
2024-12-22 5be086f36d5fbcde9aaa6f775961f292aaae6ec1
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
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_Common.StockEnum;
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.ERP;
using WIDESEA_DTO.Inbound;
using WIDESEA_DTO.Outbound;
using WIDESEA_External.Model;
using WIDESEA_IBasicService;
using WIDESEA_IOutboundRepository;
using WIDESEA_IOutboundService;
using WIDESEA_IStockRepository;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
 
namespace WIDESEA_OutboundService
{
    public class OutboundOrderService : ServiceBase<Dt_OutboundOrder, IOutboundOrderRepository>, IOutboundOrderService
    {
        private readonly IMapper _mapper;
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IBasicService _basicService;
        private readonly IOutboundOrderDetailRepository _outboundOrderDetailRepository;
        private readonly IStockInfoService _stockInfoService;
        private readonly IStockInfoRepository _stockInfoRepository;
        public IOutboundOrderRepository Repository => BaseDal;
 
        public OutboundOrderService(IOutboundOrderRepository BaseDal, IMapper mapper,IUnitOfWorkManage unitOfWorkManage, IBasicService basicService, IOutboundOrderDetailRepository outboundOrderDetailRepository, IStockInfoService stockInfoService,
          IStockInfoRepository stockInfoRepository) : base(BaseDal)
        {
            _mapper = mapper;
            _unitOfWorkManage=unitOfWorkManage;
            _basicService = basicService;
            _outboundOrderDetailRepository = outboundOrderDetailRepository;
            _stockInfoService = stockInfoService;
            _stockInfoRepository= stockInfoRepository;
        }
 
        public WebResponseContent ReceiveOutOrder(ErpOutOrderDTO model)
        {
            try
            {
                Dt_MaterielInfo materielInfo = _basicService.MaterielInfoService.Repository.QueryFirst(x => x.MaterielCode == model.MCode);
                if (materielInfo == null)
                {
                    return WebResponseContent.Instance.Error($"未找到该物料信息");
                }
 
                Dt_Warehouse warehouse = _basicService.WarehouseService.Repository.QueryFirst(x => x.WarehouseCode == model.WaId);
                if (warehouse == null)
                {
                    return WebResponseContent.Instance.Error($"未找到该仓库信息");
                }
 
                Dt_OutboundOrder oldOutboundOrder = BaseDal.Db.Queryable<Dt_OutboundOrder>().Where(x => x.UpperOrderNo == model.OrderNo).Includes(x => x.Details).First();
 
                if (model.Way == 1)
                {
                    if (oldOutboundOrder != null)
                    {
                        if (oldOutboundOrder.Details.FirstOrDefault(x => x.RowNo == Convert.ToInt32(model.RowNo)) != null)
                        {
                            return WebResponseContent.Instance.Error($"该明细行号已存在");
                        }
                        if (oldOutboundOrder.WarehouseId != warehouse.WarehouseId)
                        {
                            return WebResponseContent.Instance.Error($"仓库不一致");
                        }
                        else
                        {
                            Dt_StockInfo? stockInfo = null;
                            Dt_OutboundOrderDetail outboundOrderDetail = new Dt_OutboundOrderDetail()
                            {
                                RowNo = Convert.ToInt32(model.RowNo),
                                BatchNo = model.MLot,
                                MaterielCode = model.MCode,
                                MaterielName = materielInfo.MaterielName,
                                OrderDetailStatus = OrderDetailStatusEnum.New.ObjToInt(),
                                OrderQuantity = model.Qty,
                                OrderId = oldOutboundOrder.Id
                            };
                            //ERP上传测试仓领料单更新对应库存状态
                            if (warehouse.WarehouseId == 1)
                            {
                                //获取出库单库存
                                stockInfo = _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.WarehouseId == warehouse.WarehouseId).Includes(x => x.Details).Where(x => x.Details.Any(x => x.MaterielCode == model.MCode && x.BatchNo == model.MLot && x.InboundOrderRowNo == Convert.ToInt32(model.RowNo))).First();
                                stockInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
                            }
                            _unitOfWorkManage.BeginTran();
                            if (stockInfo != null)
                            {
                                _stockInfoRepository.UpdateData(stockInfo);
                            }
                            _outboundOrderDetailRepository.AddData(outboundOrderDetail);
                            _unitOfWorkManage.CommitTran();
                        }
                    }
                    else
                    {
                        Dt_OutboundOrderDetail outboundOrderDetail = new Dt_OutboundOrderDetail()
                        {
                            RowNo = Convert.ToInt32(model.RowNo),
                            BatchNo = model.MLot,
                            MaterielCode = model.MCode,
                            MaterielName = materielInfo.MaterielName,
                            OrderDetailStatus = OrderDetailStatusEnum.New.ObjToInt(),
                            OrderQuantity = model.Qty,
                        };
 
                        Dt_OutboundOrder outboundOrder = new Dt_OutboundOrder()
                        {
                            UpperOrderNo = model.OrderNo,
                            OrderStatus = OutOrderStatusEnum.未开始.ObjToInt(),
                            OrderType = OutOrderTypeEnum.Issue.ObjToInt(),
                            CreateType = OrderCreateTypeEnum.UpperSystemPush.ObjToInt(),
                            WarehouseId = warehouse.WarehouseId,
                            Details = new List<Dt_OutboundOrderDetail> { outboundOrderDetail }
                        };
                        Dt_StockInfo? stockInfo = null;
                        //ERP上传测试仓领料单更新对应库存状态 
                        if (warehouse.WarehouseId==1)
                        {
                            //获取出库单库存
                            stockInfo = _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.WarehouseId == warehouse.WarehouseId).Includes(x => x.Details).Where(x => x.Details.Any(x => x.MaterielCode == model.MCode && x.BatchNo == model.MLot && x.InboundOrderRowNo == Convert.ToInt32(model.RowNo))).First();
                            stockInfo.StockStatus=StockStatusEmun.入库完成.ObjToInt();
                        }
                        _unitOfWorkManage.BeginTran();
                        if (stockInfo!=null)
                        {
                            _stockInfoRepository.UpdateData(stockInfo);
                        }
                        Db.InsertNav(outboundOrder).Include(x => x.Details).ExecuteCommand();
                        _unitOfWorkManage.CommitTran();
                    }
                }
                else if (model.Way == 2)
                {
                    if (oldOutboundOrder == null)
                    {
                        return WebResponseContent.Instance.Error($"未找到该出库单");
                    }
                    Dt_OutboundOrderDetail? outboundOrderDetail = oldOutboundOrder.Details.FirstOrDefault(x => x.RowNo == Convert.ToInt32(model.RowNo));
                    if (outboundOrderDetail == null)
                    {
                        return WebResponseContent.Instance.Error($"未找到该明细行号信息");
                    }
                    if (outboundOrderDetail.OrderDetailStatus != OrderDetailStatusEnum.New.ObjToInt())
                    {
                        return WebResponseContent.Instance.Error($"该明细不可修改");
                    }
                    outboundOrderDetail = new Dt_OutboundOrderDetail()
                    {
                        RowNo = Convert.ToInt32(model.RowNo),
                        BatchNo = model.MLot,
                        MaterielCode = model.MCode,
                        MaterielName = materielInfo.MaterielName,
                        OrderDetailStatus = OrderDetailStatusEnum.New.ObjToInt(),
                        OrderQuantity = model.Qty,
                    };
 
                    _outboundOrderDetailRepository.UpdateData(outboundOrderDetail);
                }
                else if (model.Way == 3)
                {
                    if (oldOutboundOrder == null)
                    {
                        return WebResponseContent.Instance.Error($"未找到该出库单");
                    }
                }
 
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                _unitOfWorkManage.RollbackTran();
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
 
        //public WebResponseContent FeedbackOutbondIssue(List<Dt_OutStockLockInfo> outStockLockInfos)
        //{
        //    ERPPickItemModel
        //}
    }
}