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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.WareHouseEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseServices;
using WIDESEA_IBasicRepository;
using WIDESEA_IBasicService;
using WIDESEA_IOutboundRepository;
using WIDESEA_IOutboundService;
using WIDESEA_IRecordService;
using WIDESEA_IStockService;
using WIDESEA_Model.Models;
using WIDESEA_OutboundRepository;
 
namespace WIDESEA_OutboundService
{
    public class OutBSTOrderDetailService : ServiceBase<Dt_OutBSTOrderDetail, IOutBSTOrderDetailRepository>, IOutBSTOrderDetailService
    {
        public IOutBSTOrderDetailRepository Repository => BaseDal;
        private IOutBSTOrderRepository _outBSTOrderRepository;
        private IBasicRepository _basicRepository;
        private IStockService _stockService;
        private IOutStockLockInfoService _outStockLockInfoService;
        private IBasicService _basicService;
        private ILocationStatusChangeRecordService _locationStatusChangeRecordService;
 
        public OutBSTOrderDetailService(IOutBSTOrderDetailRepository BaseDal, IOutBSTOrderRepository outBSTOrderRepository,IBasicRepository basicRepository, IStockService stockService, IOutStockLockInfoService outStockLockInfoService,IBasicService basicService, ILocationStatusChangeRecordService locationStatusChangeRecordService) : base(BaseDal)
        {
            _outBSTOrderRepository = outBSTOrderRepository;
            _basicRepository = basicRepository;
            _stockService = stockService;
            _outStockLockInfoService = outStockLockInfoService;
            _basicService = basicService;
            _locationStatusChangeRecordService=locationStatusChangeRecordService;
        }
        /// <summary>
        /// 分配库存
        /// </summary>
        public (List<Dt_StockInfo>, List<Dt_OutBSTOrderDetail>, List<Dt_OutStockLockInfo>, List<Dt_LocationInfo>) AssignStockOutbound(List<Dt_OutBSTOrderDetail> outboundOrderDetails)
        {
            if (!outboundOrderDetails.Any())
            {
                throw new Exception($"未找到出库单明细信息");
            }
 
            if (outboundOrderDetails.GroupBy(x => x.OutBSTOrderId).Count() > 1)
            {
                throw new Exception($"请勿同时操作多个单据明细");
            }
            //获取排程主表
            Dt_OutBSTOrder outBSTOrder = _outBSTOrderRepository.QueryFirst(x => x.Id == outboundOrderDetails.FirstOrDefault().OutBSTOrderId);
            List<Dt_StockInfo> outStocks = new List<Dt_StockInfo>();
            //对出库物料进行分组
            List<Dt_OutBSTOrderDetail> groupDetails = outboundOrderDetails.GroupBy(x => new { x.MaterialId,x.Width }).Select(x => 
            new Dt_OutBSTOrderDetail { 
                XqLen = x.Sum(v => v.XqLen),
                MaterialId = x.Key.MaterialId,
                Width=x.Key.Width
            }
            ).ToList();
            Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == outBSTOrder.WarehouseId);
            //出库详情
            List<Dt_OutStockLockInfo> outStockLockInfos = new List<Dt_OutStockLockInfo>();
            //货位存储
            List<Dt_LocationInfo> locationInfos = new List<Dt_LocationInfo>();
 
            foreach (var item in groupDetails)
            {
                decimal needQuantity = item.XqLen;
                //获取可用库存
                List<Dt_StockInfo> stockInfos = _stockService.StockInfoService.GetUseableStocks(item.MaterialId,item.Width, outBSTOrder.WarehouseId);
                if (!stockInfos.Any())
                {
                    throw new Exception($"未找到可分配库存");
                }
                //分配实际库存
                List<Dt_StockInfo> autoAssignStocks = _stockService.StockInfoService.GetOutboundStocks(stockInfos,  needQuantity).OrderBy(x => x.StockLength - x.StockOutLength).ToList();
                //添加库存分配
                outStocks.AddRange(autoAssignStocks);
                //获取所有该物料单据
                List<Dt_OutBSTOrderDetail> details = outboundOrderDetails.Where(x => x.MaterialId == item.MaterialId).ToList();
 
                for (int i = 0; i < details.Count; i++)
                {
                    //订单数量
                    decimal orderQuantity = details[i].XqLen;
 
                    for (int j = 0; j < autoAssignStocks.Count; j++)
                    {
                        //出库订单明细已分配数量
                        decimal detailAssignQuantity = outStockLockInfos.Where(x => x.OrderDetailId == details[i].Id).Sum(x => x.AssignQuantity);
 
                        decimal orderDetailNeedQuantity = details[i].XqLen - detailAssignQuantity;
 
                        decimal useStockLength = autoAssignStocks[j].StockLength;
 
                        if (orderDetailNeedQuantity > useStockLength)
                        {
                            //生成详情
                            Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(outBSTOrder, details[i], autoAssignStocks[j], useStockLength);
                            outStockLockInfos.Add(outStockLockInfo);
                            details[i].AssignTotalUsage += useStockLength;
 
                        }
                        else
                        {
                            //生成详情
                            Dt_OutStockLockInfo outStockLockInfo = _outStockLockInfoService.GetOutStockLockInfo(outBSTOrder, details[i], autoAssignStocks[j], orderDetailNeedQuantity);
                            outStockLockInfos.Add(outStockLockInfo);
                            details[i].AssignTotalUsage = orderQuantity;
                            break;
                        }
                    }
                }
 
                locationInfos.AddRange(_basicRepository.LocationInfoRepository.GetLocationInfos(outStocks.Select(x => x.LocationCode).ToList()));
            }
 
            return (outStocks, outboundOrderDetails, outStockLockInfos, locationInfos);
        }
        /// <summary>
        /// 出库库存分配后,更新数据库数据
        /// </summary>
        public WebResponseContent LockOutboundStockDataUpdate(List<Dt_StockInfo> stockInfos, List<Dt_OutBSTOrderDetail> outboundOrderDetails, List<Dt_OutStockLockInfo> outStockLockInfos, List<Dt_LocationInfo> locationInfos, LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List<Dt_Task>? tasks = null)
        {
            try
            {
                _stockService.StockInfoService.Repository.UpdateData(stockInfos);
                BaseDal.UpdateData(outboundOrderDetails);
 
                List<Dt_OutStockLockInfo> addOutStockLockInfos = outStockLockInfos.Where(x => x.Id == 0).ToList();
                if (addOutStockLockInfos != null && addOutStockLockInfos.Any())
                {
                    if (tasks != null)
                    {
                        addOutStockLockInfos.ForEach(x =>
                        {
                            x.TaskNum = tasks.FirstOrDefault(v => v.PalletCode == x.PalletCode)?.TaskNum;
                        });
                    }
 
                    _outStockLockInfoService.Repository.AddData(addOutStockLockInfos);
                }
                List<Dt_OutStockLockInfo> updateOutStockLockInfos = outStockLockInfos.Where(x => x.Id > 0).ToList();
                if (updateOutStockLockInfos != null && updateOutStockLockInfos.Any())
                {
                    _outStockLockInfoService.Repository.UpdateData(updateOutStockLockInfos);
                }
                //添加货位状态
                _locationStatusChangeRecordService.AddLocationStatusChangeRecord(locationInfos, locationStatus, LocationChangeType.OutboundAssignLocation, "", tasks?.Select(x => x.TaskNum).ToList());
                //批量更新货位状态
                _basicService.LocationInfoService.Repository.UpdateLocationStatus(locationInfos, locationStatus);
                return WebResponseContent.Instance.OK();
            }
            catch (Exception ex)
            {
                return WebResponseContent.Instance.Error(ex.Message);
            }
        }
 
    }
}