wangxinhui
2025-03-19 d8db1698c125618c1b5f62b009204ddc5d4eed5a
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Helper;
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 MesRworkOutboundOrderService : ServiceBase<Dt_MesRworkOutboundOrder, IMesRworkOutboundOrderRepository>, IMesRworkOutboundOrderService
    {
        private readonly IBasicRepository _basicRepository;
        private readonly IStockService _stockService;
        private readonly IOutStockLockInfoService _outStockLockInfoService;
        private readonly IBasicService _basicService;
        private readonly IRecordService _recordService;
        private readonly IOutProStockInfoService _outProStockInfoService;
 
        public IMesRworkOutboundOrderRepository Repository => BaseDal;
 
        public MesRworkOutboundOrderService(IMesRworkOutboundOrderRepository BaseDal, IBasicRepository basicRepository, IStockService stockService, IOutStockLockInfoService outStockLockInfoService, IBasicService basicService, IRecordService recordService, IOutProStockInfoService outProStockInfoService) : base(BaseDal)
        {
            _basicRepository = basicRepository;
            _stockService = stockService;
            _outStockLockInfoService = outStockLockInfoService;
            _basicService = basicService;
            _recordService = recordService;
            _outProStockInfoService = outProStockInfoService;
        }
        /// <summary>
        /// 提库任务分配库存
        /// </summary>
        public (List<Dt_ProStockInfo>?,Dt_MesRworkOutboundOrder?,List<Dt_OutProStockInfo>?,List<Dt_LocationInfo>) AssignMesStocks(Dt_MesRworkOutboundOrder mesRworkOutboundOrder)
        {
            List<Dt_ProStockInfo> proStockInfos = new List<Dt_ProStockInfo>();
            Dt_MesRworkOutboundOrder assignOutOrder= new Dt_MesRworkOutboundOrder();
            List<Dt_OutProStockInfo> outProStockInfos=new List<Dt_OutProStockInfo>();
            List<Dt_LocationInfo> locationInfos=new List<Dt_LocationInfo>();
            float originalNeedQuantity = mesRworkOutboundOrder.RequiredQuantity;
 
            float needQuantity = originalNeedQuantity;
 
            //查找可用库存
            List<Dt_ProStockInfo> stockInfoss = _stockService.ProStockInfoService.GetUseableStocks(mesRworkOutboundOrder);
            if (!stockInfoss.Any())
            {
                throw new Exception("未找到可分配库存");
            }
            List<Dt_ProStockInfo> autoAssignStocks = _stockService.ProStockInfoService.GetOutboundStocks(stockInfoss,mesRworkOutboundOrder, needQuantity,out float residueQuantity);
            mesRworkOutboundOrder.LockQuantity += needQuantity - residueQuantity;
            autoAssignStocks.OrderBy(x => x.proStockInfoDetails.FirstOrDefault()?.StockPcsQty).ToList();
            proStockInfos.AddRange(autoAssignStocks);
            float assignQuantity = needQuantity - residueQuantity;
 
            float orderQuantity = mesRworkOutboundOrder.RequiredQuantity;
            for (int j = 0; j < autoAssignStocks.Count; j++)
            {
                //出库订单明细已分配数量
                float detailAssignQuantity = outProStockInfos
                    .Where(x => x.SaleOrder == mesRworkOutboundOrder.SaleOrder 
                    && x.PCode == mesRworkOutboundOrder.ProductCode 
                    && x.PVer == mesRworkOutboundOrder.ProductVersion)
                    .Sum(x => x.AssignQuantity);
 
                //出库详情已分配数量
                float palletAssignQuantity = outProStockInfos
                    .Where(x => x.SaleOrder == mesRworkOutboundOrder.SaleOrder 
                    && x.PCode == mesRworkOutboundOrder.ProductCode 
                    && x.PVer == mesRworkOutboundOrder.ProductVersion 
                    && x.PalletCode == autoAssignStocks[j].PalletCode)
                    .Sum(x => x.AssignQuantity);
 
                float palletOutboundQuantity = autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity);
                if (palletAssignQuantity < palletOutboundQuantity)//如果出库详情已分配数量小于托盘已分配数量,则可以继续添加该托盘出库信息
                {
                    float orderDetailNeedQuantity = mesRworkOutboundOrder.RequiredQuantity - detailAssignQuantity;
                    if (orderDetailNeedQuantity > autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity) - palletAssignQuantity)
                    {
                        mesRworkOutboundOrder.LockQuantity += autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity) - palletAssignQuantity;
                        Dt_OutProStockInfo outStockLockInfo = _outProStockInfoService.GetOutStockLockInfo(mesRworkOutboundOrder, autoAssignStocks[j], autoAssignStocks[j].proStockInfoDetails.Sum(x => x.OutboundQuantity) - palletAssignQuantity);
                        outProStockInfos.Add(outStockLockInfo);
                    }
                    else
                    {
                        Dt_OutProStockInfo outStockLockInfo = _outProStockInfoService.GetOutStockLockInfo(mesRworkOutboundOrder, autoAssignStocks[j], mesRworkOutboundOrder.RequiredQuantity-mesRworkOutboundOrder.LockQuantity);
                        outProStockInfos.Add(outStockLockInfo);
                        mesRworkOutboundOrder.LockQuantity = mesRworkOutboundOrder.RequiredQuantity;
                        break;
                    }
                }
            }
            locationInfos.AddRange(_basicService.LocationInfoService.Repository.GetLocationInfos(proStockInfos.Select(x => x.LocationCode).ToList()));
 
            return (proStockInfos, assignOutOrder, outProStockInfos, locationInfos);
        }
        public WebResponseContent LockOutboundStockDataUpdate(List<Dt_ProStockInfo> stockInfos, List<Dt_OutProStockInfo> outStockLockInfos, List<Dt_LocationInfo> locationInfos, LocationStatusEnum locationStatus = LocationStatusEnum.Lock, List<Dt_Task>? tasks = null)
        {
            try
            {
                stockInfos.ForEach(x => {
                    x.StockStatus = StockStatusEmun.出库锁定.ObjToInt();
                });
                _stockService.ProStockInfoService.Repository.UpdateData(stockInfos);
                List<Dt_ProStockInfoDetail> stockInfoDetails = new List<Dt_ProStockInfoDetail>();
                foreach (var item in stockInfos)
                {
                    stockInfoDetails.AddRange(item.proStockInfoDetails);
                }
                _stockService.ProStockInfoDetailService.Repository.UpdateData(stockInfoDetails);
 
                List<Dt_OutProStockInfo> 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;
                        });
                    }
 
                    _outProStockInfoService.Repository.AddData(addOutStockLockInfos);
                }
                List<Dt_OutProStockInfo> updateOutStockLockInfos = outStockLockInfos.Where(x => x.Id > 0).ToList();
                if (updateOutStockLockInfos != null && updateOutStockLockInfos.Any())
                {
                    _outProStockInfoService.Repository.UpdateData(updateOutStockLockInfos);
                }
 
                _recordService.LocationStatusChangeRecordSetvice.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);
            }
        }
    }
}