qinchulong
2024-10-12 b17d60113095491a95557f6f785cb1c0e744e8cc
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using Newtonsoft.Json;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_Core.Enums;
using WIDESEA_IBusinessesRepository;
using WIDESEA_IBusinessServices;
using WIDESEA_Model.Models;
 
namespace WIDESEA_BusinessServices
{
    public class Dt_BillGroupStockService : ServiceBase<Dt_BillGroupStock, IDt_BillGroupStockRepository>, IDt_BillGroupStockService
    {
        private readonly IUnitOfWorkManage _unitOfWorkManage;
        private readonly IDt_BillGroupStockDetailRepository _dt_BillGroupStockDetailRepository;
        private readonly IDt_InboundOrderDetailRepository _dt_InboundOrderDetailRepository;
        private readonly IDt_MaterielInfoRepository _dt_MaterielInfoRepository;
        private readonly IDt_InboundOrderRepository _dt_InboundOrderRepository;
        public Dt_BillGroupStockService(IDt_BillGroupStockRepository BaseDal, IUnitOfWorkManage unitOfWorkManage, IDt_BillGroupStockDetailRepository dt_BillGroupStockDetailRepository, IDt_InboundOrderDetailRepository dt_InboundOrderDetailRepository, IDt_MaterielInfoRepository dt_MaterielInfoRepository, IDt_InboundOrderRepository dt_InboundOrderRepository) : base(BaseDal)
        {
            _unitOfWorkManage = unitOfWorkManage;
            _dt_BillGroupStockDetailRepository = dt_BillGroupStockDetailRepository;
            _dt_InboundOrderDetailRepository = dt_InboundOrderDetailRepository;
            _dt_MaterielInfoRepository = dt_MaterielInfoRepository;
            _dt_InboundOrderRepository = dt_InboundOrderRepository;
        }
        /// <summary>
        /// PDA组盘前,选择单据信息时需要根据物料号确定组盘物料数量
        /// PDA组盘确认,记录组盘信息,保存到:主表 Dt_BillGroupStock,明细表 Dt_BillGroupStockDetail
        /// </summary>
        /// <param name="dt_BillGroupStock">组盘信息</param>
        /// <returns></returns>
        public WebResponseContent PDAGroupPlateConfirm(Dt_BillGroupStock dt_BillGroupStock)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                bool isFinishGroup = false;//当前单据是否完成组盘
                Dt_InboundOrder inboundOrder = new Dt_InboundOrder();
                string requeststr = JsonConvert.SerializeObject(dt_BillGroupStock);
                new LogLibrary.Log.LogFactory().GetLog("接口").InfoFormat(true, "PDA组盘-PDAGroupPlate", "请求", requeststr);
                //组盘物料是否与单据中的物料基础信息匹配
                foreach (var item in dt_BillGroupStock.Dt_BillGroupStockDetailList)
                {
                   int count = _dt_MaterielInfoRepository.QueryData(x => x.MaterielCode == item.MaterialNo).Count();//在物料信息表中查询物料信息是否存在
                    if(count==0)//物料信息不存在
                    {
                        new LogLibrary.Log.LogFactory().GetLog("接口").InfoFormat(true, "PDA组盘-PDAGroupPlate", "响应", $"该物料信息不存在:{item.MaterialName}+{item.MaterialNo}");
                        return content = WebResponseContent.Instance.Error($"该物料信息不存在:{item.MaterialName}+{item.MaterialNo},请维护物料基本信息!");
                    }
                }
 
                //查询已组盘物料数量和入库单物料总数量,并与当前组盘物料数量对比判断是否可以组盘
                decimal PreInboundQuantity = _dt_InboundOrderDetailRepository.QueryData(x=>x.InboundNo==dt_BillGroupStock.InboundNo&&x.MaterialNo==dt_BillGroupStock.MaterialNo).Sum(x=>Convert.ToDecimal(x.PreInboundQuantity));
                decimal PalletQuantity =Convert.ToDecimal(Db.Queryable<Dt_BillGroupStock>().InnerJoin<Dt_BillGroupStockDetail>((s, sd) => s.InboundNo == sd.InboundNo && s.State != StockStateEmun.组盘撤销.ToString()).Select((a, b) => b).ToList().Sum(x => x.PalletQuantity));
                decimal currentPalletQuantity = Convert.ToDecimal(dt_BillGroupStock.Dt_BillGroupStockDetailList.Sum(x=>x.PalletQuantity));
                if(currentPalletQuantity > PreInboundQuantity - PalletQuantity)//如果已组盘物料数量大于单据物料数量
                {
                    new LogLibrary.Log.LogFactory().GetLog("接口").InfoFormat(true, "PDA组盘-PDAGroupPlate", "响应", "如果已组盘物料数量大于单据物料数量,需重新调整物料数量!");
                    return content = WebResponseContent.Instance.Error("如果已组盘物料数量大于单据物料数量,需重新调整物料数量!");
                }
                else if (currentPalletQuantity == PreInboundQuantity - PalletQuantity)
                {
                    isFinishGroup = true;
                }
                 inboundOrder = _dt_InboundOrderRepository.QueryData(x => x.InboundNo == dt_BillGroupStock.InboundNo).FirstOrDefault();
                //先判断组盘类型,如果是二次组盘,托盘号可以重复
                if (dt_BillGroupStock.GroupType == GroupTypeEmun.二次组盘.ToString())
                {
                    //查询当前托盘号已组盘的信息,库存状态为“入库确认”状态不能再组盘
                    List<Dt_BillGroupStock> billGroupList = BaseDal.QueryData(x => x.PalletCode == dt_BillGroupStock.PalletCode && string.IsNullOrEmpty(x.LocationCode) && x.State == StockStateEmun.组盘暂存.ToString());
                    if (billGroupList != null && billGroupList.Count == 1)//组盘明细中增加二次组盘明细
                    {
                        Dt_BillGroupStock billGroupStock = billGroupList[0];
                        if (Convert.ToBoolean(billGroupStock.IsFull))//如果已满盘,不能二次组盘
                        {
                            new LogLibrary.Log.LogFactory().GetLog("接口").InfoFormat(true, "PDA组盘-PDAGroupPlate", "响应", "已满盘不能进行二次组盘!");
                            return content = WebResponseContent.Instance.Error("已满盘不能进行二次组盘!");       
                        }
                        else
                        {
                            try
                            {
                                //开启事物
                                _unitOfWorkManage.BeginTran();
                                //修改组盘类型为"二次组盘"
                                dt_BillGroupStock.GroupType = "2";
                                BaseDal.UpdateData(dt_BillGroupStock);
                                //添加组盘明细
                                _dt_BillGroupStockDetailRepository.AddData(dt_BillGroupStock.Dt_BillGroupStockDetailList);
 
                                if (inboundOrder != null)//如果当前单据完成组盘,更新入库表Dt_InboundOrder 单据状态为:完成组盘
                                {
                                    if (isFinishGroup)
                                    {
                                        inboundOrder.InboundState = InboundStateEmun.完成组盘.ToString();
                                        _dt_InboundOrderRepository.UpdateData(inboundOrder);
                                    }
                                    else
                                    {
                                        inboundOrder.InboundState = InboundStateEmun.组盘中.ToString();
                                        _dt_InboundOrderRepository.UpdateData(inboundOrder);
                                    }
                                }
                                //提交事务
                                _unitOfWorkManage.CommitTran();
                            }
                            catch
                            {
                                _unitOfWorkManage.RollbackTran();
                                throw;
                            }
 
                        }
                    }
                    else if (billGroupList != null && billGroupList.Count > 1)
                    {
                        new LogLibrary.Log.LogFactory().GetLog("接口").InfoFormat(true, "PDA组盘-PDAGroupPlate", "响应", "当前托盘号存在多条暂存组盘记录!");
                        return content = WebResponseContent.Instance.Error("当前托盘号存在多条暂存组盘记录!");
                    }
                    else
                    {
                        new LogLibrary.Log.LogFactory().GetLog("接口").InfoFormat(true, "PDA组盘-PDAGroupPlate", "响应", "当前托盘之前未组过盘,不属于二次组盘,请重新选择组盘类型!");
                        return content = WebResponseContent.Instance.Error("当前托盘之前未组过盘,不属于二次组盘,请重新选择组盘类型!");
                    }
                }
                else //初次组盘
                {
                    //查询当前托盘号已组盘的信息,如果库存状态为已组盘暂存或已入库,则提示不允许重复组盘
                    List<Dt_BillGroupStock> billGroupList = BaseDal.QueryData(x => x.PalletCode == dt_BillGroupStock.PalletCode && (x.State == StockStateEmun.组盘暂存.ToString() || x.State == StockStateEmun.已入库.ToString()));
                    if (billGroupList != null && billGroupList.Count == 1)
                    {
                        dt_BillGroupStock = billGroupList[0];
                        if (Convert.ToBoolean(dt_BillGroupStock.IsFull))
                        {
                            new LogLibrary.Log.LogFactory().GetLog("接口").InfoFormat(true, "PDA组盘-PDAGroupPlate", "响应", "当前托盘已组盘,不允计重复组盘!");
                            return content = WebResponseContent.Instance.Error("当前托盘已组盘,不允计重复组盘!");
                        }
                        else
                        {
                            new LogLibrary.Log.LogFactory().GetLog("接口").InfoFormat(true, "PDA组盘-PDAGroupPlate", "响应", "当前托盘已组盘,可以选择二次组盘重新组盘!");
                            return content = WebResponseContent.Instance.Error("当前托盘已组盘,可以选择二次组盘重新组盘!");
                        }
                    }
                    try
                    {
                        //开启事物
                        _unitOfWorkManage.BeginTran();
 
                        BaseDal.AddData(dt_BillGroupStock);
                        //添加组盘明细
                        _dt_BillGroupStockDetailRepository.AddData(dt_BillGroupStock.Dt_BillGroupStockDetailList);
                     
                        if (inboundOrder != null)//如果当前单据完成组盘,更新入库表Dt_InboundOrder 单据状态为:完成组盘
                        {
                            if (isFinishGroup)
                            {
                                inboundOrder.InboundState = InboundStateEmun.完成组盘.ToString();
                                _dt_InboundOrderRepository.UpdateData(inboundOrder);
                            }
                            else
                            {
                                inboundOrder.InboundState = InboundStateEmun.组盘中.ToString();
                                _dt_InboundOrderRepository.UpdateData(inboundOrder);
                            }
                        }
 
                        //提交事务
                        _unitOfWorkManage.CommitTran();
                        new LogLibrary.Log.LogFactory().GetLog("接口").InfoFormat(true, "PDA组盘-PDAGroupPlate", "响应", $"组盘成功");
                        content = WebResponseContent.Instance.OK("组盘成功");
                       
                    }
                    catch
                    {
                        _unitOfWorkManage.RollbackTran();
                        throw;
                    }
 
                }
      
            }
            catch (Exception ex)
            {
                new LogLibrary.Log.LogFactory().GetLog("接口").ErrorFormat(true, "PDA组盘-PDAGroupPlate", "响应", $"{dt_BillGroupStock.PalletCode}组盘错误:{ex.Message}");
                content = WebResponseContent.Instance.Error($"组盘错误:{ex.Message}");
            }
            return content;
 
        }
 
        /// <summary>
        /// PDA组盘撤销确认,解绑托盘码和物料码
        /// 1,根据托盘码查询组盘信息 2,当库存状态为“组盘暂存”时可以撤销 3,撤销后,修改库存状态为"组盘撤销",
        /// </summary>
        /// <param name="plateCode">托盘号</param>
        /// <returns></returns>
        public WebResponseContent PDAGroupPlateRevoke(string plateCode)
        {
            WebResponseContent content = new WebResponseContent();
            new LogLibrary.Log.LogFactory().GetLog("接口").InfoFormat(true, "PDA组盘-PDAGroupPlateRevoke", "请求", plateCode);
            List<Dt_BillGroupStock> billGroupStockList = BaseDal.QueryData(x => x.PalletCode == plateCode&&x.State== StockStateEmun.组盘暂存.ToString());
            if(billGroupStockList != null &&billGroupStockList.Count == 1)
            {
                Dt_BillGroupStock billGroupStock = billGroupStockList[0];
                billGroupStock.State = StockStateEmun.组盘撤销.ToString();
                BaseDal.UpdateData(billGroupStock);
                new LogLibrary.Log.LogFactory().GetLog("接口").InfoFormat(true, "PDA组盘-PDAGroupPlateRevoke", "响应", $"撤销组盘成功");
                content = WebResponseContent.Instance.OK("撤销组盘成功");
            }
            else
            {
                new LogLibrary.Log.LogFactory().GetLog("接口").InfoFormat(true, "PDA组盘-PDAGroupPlate", "响应", $"撤销组务失败,未查询到可以撤销的组盘");
                content = WebResponseContent.Instance.OK("撤销组盘失败");
            }
 
            return content;
 
        }
    }
}