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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
using Newtonsoft.Json;
using Org.BouncyCastle.Crypto;
using SqlSugar;
//using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.Inbound;
using WIDESEA_DTO.Stock;
using WIDESEA_IBasicRepository;
using WIDESEA_IStockRepository;
using WIDESEA_Model;
using WIDESEA_Model.Models;
using WIDESEA_Model.Models.System.Reponse;
using WIDESEA_Model.Models.System.Request;
 
namespace WIDESEA_StockRepository
{
    public class StockInfoRepository : RepositoryBase<Dt_StockInfo>, IStockInfoRepository
    {
        private readonly ILabelMasterRepository _labelMasterRepository;
        public StockInfoRepository(IUnitOfWorkManage unitOfWorkManage, ILabelMasterRepository labelMasterRepository) : base(unitOfWorkManage)
        {
            _labelMasterRepository = labelMasterRepository;
        }
 
        public List<StockViewDTO> GetAllStockViews()
        {
            ISugarQueryable<Dt_LocationInfo> sugarQueryable = Db.Queryable<Dt_LocationInfo>();
            ISugarQueryable<Dt_StockInfo> sugarQueryable1 = Db.Queryable<Dt_StockInfo>();
 
            return sugarQueryable.InnerJoin(sugarQueryable1, (a, b) => a.LocationCode == b.LocationCode).Select((a, b) => new StockViewDTO
            {
                AreaId = a.AreaId,
                LocationCode = b.LocationCode,
                Column = a.Column,
                CreateDate = b.CreateDate,
                Creater = b.Creater,
                Depth = a.Depth,
                EnalbeStatus = a.EnableStatus,
                IsFull = b.IsFull,
                Layer = a.Layer,
                LocationName = a.LocationName,
                LocationStatus = a.LocationStatus,
                LocationType = a.LocationType,
                Modifier = b.Modifier,
                ModifyDate = b.ModifyDate,
                PalletCode = b.PalletCode,
                StockRemark = b.Remark,
                RoadwayNo = a.RoadwayNo,
                Row = a.Row,
                StockId = b.Id,
                StockStatus = b.StockStatus,
                Details = b.Details,
            }).Includes(x => x.Details).ToList();
        }
 
        public Dt_StockInfo GetStockInfo(string palletCode)
        {
            return Db.Queryable<Dt_StockInfo>().Where(x => x.PalletCode == palletCode).Includes(x => x.Details).First();
        }
 
        public List<Dt_StockInfo> GetStockInfos(string materielCode)
        {
            return Db.Queryable<Dt_StockInfo>().Includes(x => x.Details).Where(x => x.Details.Any(v => v.MaterielCode == materielCode)).ToList();
            //ISugarQueryable<Dt_LocationInfo> sugarQueryable = Db.Queryable<Dt_LocationInfo>();
            //ISugarQueryable<Dt_StockInfo> sugarQueryable1 = Db.Queryable<Dt_StockInfo>().Includes(x => x.Details).Where(x => x.Details.Any(v => v.MaterielCode == materielCode));
            //return sugarQueryable.InnerJoin(sugarQueryable1, (a, b) => a.LocationCode == b.LocationCode).Select((a, b) => b).Includes(x => x.Details).ToList();
        }
 
        /// <summary>
        /// 根据批次号和物料编码获取库存信息
        /// </summary>
        /// <param name="materielCode"></param>
        /// <returns></returns>
        public List<Dt_StockInfo> GetStockInfos(string materielCode, string batchNo)
        {
            return Db.Queryable<Dt_StockInfo>().Includes(x => x.Details).Where(x => x.Details.Any(v => v.MaterielCode == materielCode && v.BatchNo == batchNo)).ToList();
        }
 
        public List<Dt_StockInfo> GetStockInfos(string materielCode, List<string> locationCodes)
        {
            //修改只获取已上架状态的库存
            return Db.Queryable<Dt_StockInfo>().Where(x => locationCodes.Contains(x.LocationCode) && x.StockStatus == (int)StockStatusEmun.已上架).Includes(x => x.Details).Where(x => x.Details.Any(v => v.MaterielCode == materielCode)).ToList();
 
 
            //ISugarQueryable<Dt_LocationInfo> sugarQueryable = Db.Queryable<Dt_LocationInfo>().Where(x => locationCodes.Contains(x.LocationCode));
            //ISugarQueryable<Dt_StockInfo> sugarQueryable1 = Db.Queryable<Dt_StockInfo>().Includes(x => x.Details).Where(x => x.Details.Any(v => v.MaterielCode == materielCode));
            //return sugarQueryable.InnerJoin(sugarQueryable1, (a, b) => a.LocationCode == b.LocationCode).Select((a, b) => b).OrderBy(a => a.CreateDate).Includes(a => a.Details).ToList();
        }
 
        public List<Dt_StockInfo> GetStockInfosByIds(List<int> ids)
        {
            return Db.Queryable<Dt_StockInfo>().Where(x => ids.Contains(x.Id)).Includes(x => x.Details).ToList();
        }
 
        public List<Dt_StockInfo> GetStockInfosByPalletCodes(List<string> palletCodes)
        {
 
            return Db.Queryable<Dt_StockInfo>().Where(x => palletCodes.Contains(x.PalletCode)).Includes(x => x.Details).ToList();
        }
 
        public List<Dt_StockInfo> GetStockInfosExclude(string materielCode, List<string> palletCodes)
        {
            return Db.Queryable<Dt_StockInfo>().Where(x => !palletCodes.Contains(x.PalletCode)).Includes(x => x.Details).Where(x => x.Details.Any(v => v.MaterielCode == materielCode)).ToList();
        }
 
        public bool UpdateDataWithDetail(Dt_StockInfo stockInfo)
        {
            return Db.UpdateNav(stockInfo).Include(x => x.Details).ExecuteCommand();
        }
        public Dt_StockInfo GetPalletStockInfo(string roadwayNo)
        {
            //ISugarQueryable<string> locationCodes = Db.Queryable<Dt_LocationInfo>().Where(x => x.RoadwayNo == roadwayNo && x.LocationStatus == LocationStatusEnum.Pallet.ObjToInt() && (x.EnalbeStatus == LocationEnalbeStatusEnum.OnlyOut.ObjToInt() || LocationEnalbeStatusEnum.Normal.ObjToInt() == x.EnalbeStatus)).Select(x => x.LocationCode);
            return Db.Queryable<Dt_StockInfo>().Where(x => x.StockStatus == StockStatusEmun.已入库.ObjToInt() && SqlFunc.Subqueryable<Dt_LocationInfo>().Where(v => v.LocationCode == x.LocationCode && v.RoadwayNo == roadwayNo && v.LocationStatus == LocationStatusEnum.Pallet.ObjToInt() && (EnableStatusEnum.Normal.ObjToInt() == v.EnableStatus)).Any()).OrderBy(x => x.ModifyDate).First();
 
        }
        public Dt_StockInfo GetPalletStockInfo(string roadwayNo, string strayType)
        {
            //ISugarQueryable<string> locationCodes = Db.Queryable<Dt_LocationInfo>().Where(x => x.RoadwayNo == roadwayNo && x.LocationStatus == LocationStatusEnum.Pallet.ObjToInt() && (x.EnalbeStatus == LocationEnalbeStatusEnum.OnlyOut.ObjToInt() || LocationEnalbeStatusEnum.Normal.ObjToInt() == x.EnalbeStatus)).Select(x => x.LocationCode);
            if (strayType == "1")
            {
                return Db.Queryable<Dt_StockInfo>().Where(x => x.StockStatus == StockStatusEmun.已入库.ObjToInt() && x.PalletCode.Substring(0, 1) == "T" && SqlFunc.Subqueryable<Dt_LocationInfo>().Where(v => v.LocationCode == x.LocationCode && v.RoadwayNo == roadwayNo && v.LocationStatus == LocationStatusEnum.Pallet.ObjToInt() && (EnableStatusEnum.Normal.ObjToInt() == v.EnableStatus)).Any()).OrderBy(x => x.ModifyDate).First();
            }
            else
            {
                return Db.Queryable<Dt_StockInfo>().Where(x => x.StockStatus == StockStatusEmun.已入库.ObjToInt() && x.PalletCode.Substring(0, 1) == "B" && SqlFunc.Subqueryable<Dt_LocationInfo>().Where(v => v.LocationCode == x.LocationCode && v.RoadwayNo == roadwayNo && v.LocationStatus == LocationStatusEnum.Pallet.ObjToInt() && (EnableStatusEnum.Normal.ObjToInt() == v.EnableStatus)).Any()).OrderBy(x => x.ModifyDate).First();
            }
        }
 
 
        /// <summary>
        /// 立库WMS库存信息接口,供上游系统调用
        /// </summary>
        /// <param name="inventoryQueryRequest"></param>
        /// <returns></returns>
        public InventoryQueryResponse InventoryQuery(string inventoryQueryRequeststr)
        {
            InventoryQueryResponse inventoryQueryResponse = new InventoryQueryResponse();
            try
            {
                InventoryQueryRequest inventoryQueryRequest = JsonConvert.DeserializeObject<InventoryQueryRequest>(inventoryQueryRequeststr);
                if (inventoryQueryRequest != null)
                {
                    List<StockItem> DATA = new List<StockItem>();
                    inventoryQueryResponse.DATA = DATA;
                    List<StockViewDTO> stockviews = GetAllStockViews();
                    foreach (var view in stockviews)
                    {
                        foreach (var detail in view.Details)
                        {
                            Dt_LabelMaster labelMaster = _labelMasterRepository.QueryFirst(x => x.LABEL_NO == detail.SerialNumber);
                            StockItem stockItem = new StockItem();
                            stockItem.BATCH = detail.BatchNo;
                            stockItem.LABEL_NO = detail.SerialNumber;
                            stockItem.LGORT = "0030";//库位
                            if (labelMaster != null)
                            {
                                if (labelMaster.WH_NUMBER == inventoryQueryRequest.DATA[0].WH_NUMBER && labelMaster.WERKS == inventoryQueryRequest.DATA[0].WERKS)
                                {
                                    stockItem.LIFNR = labelMaster.LIFNR;
                                    stockItem.LIKTX = labelMaster.LIKTX;
                                    stockItem.LK_BIN_CODE = view.LocationCode;
                                    stockItem.MAKTX = labelMaster.MAKTX;
                                    stockItem.MATNR = labelMaster.MATNR;
                                    stockItem.MEINS = labelMaster.UNIT;
                                    stockItem.QTY = labelMaster.BOX_QTY;
                                    var sobkz = "非限制";
                                    if (labelMaster.SOBKZ == "0")
                                    {
                                        sobkz = "非限制";
                                    }
                                    else if (labelMaster.SOBKZ == "1")
                                    {
                                        sobkz = "冻结";
                                    }
                                    else if (labelMaster.SOBKZ == "2")
                                    {
                                        sobkz = "待质检";
                                    }
                                    stockItem.SOBKZ = sobkz;
                                    stockItem.TPNUM = view.PalletCode;
                                    stockItem.WERKS = labelMaster.WERKS;//工厂
                                    stockItem.WH_NUMBER = labelMaster.WH_NUMBER;//仓库号
                                    DATA.Add(stockItem);
                                }
                            }
                        }
                    }
                    if (DATA.Count > 0)
                    {
                        inventoryQueryResponse.MSGTY = "S";
                        inventoryQueryResponse.MSGTX = "";
                    }
                    else
                    {
                        inventoryQueryResponse.MSGTY = "I";
                        inventoryQueryResponse.MSGTX = "";
                    }
                }
            }
            catch (Exception ex)
            {
                inventoryQueryResponse.MSGTY = "E";
                inventoryQueryResponse.MSGTX = ex.Message;
 
            }
            return inventoryQueryResponse;
 
        }
 
        /// <summary>
        /// 立库WMS冻结/解冻信息接口,供上游系统调用
        /// </summary>
        /// <param name="freezeByCustomerResponse"></param>
        /// <returns></returns>
        public freezeByCustomerResponse freezeByCustomer(string freezeRequeststr)
        {
 
            freezeByCustomerResponse freezeResponse = new freezeByCustomerResponse();
            try
            {
                freezeByCustomerRequest freezeRequest = JsonConvert.DeserializeObject<freezeByCustomerRequest>(freezeRequeststr);
                if (freezeRequest.WERKS == AppSettings.Configuration["WERKS"] && freezeRequest.WH_NUMBER == AppSettings.Configuration["WERKS"])
                {
                    //根据批次和物料号查找对应的托盘号,冻结库存,只有入库成功了,才能被冻结
                    List<Dt_StockInfo> stockInfos = GetStockInfos(freezeRequest.MATNR, freezeRequest.BATCH);
 
                    List<Dt_LabelMaster> labelInfos = _labelMasterRepository.QueryData(x => x.MATNR == freezeRequest.MATNR && x.BATCH == freezeRequest.BATCH);
                    if (freezeRequest.YWLX == "00")//00冻结 
                    {
                        foreach (var item in stockInfos)
                        {
                            if (item.StockStatus == (int)StockStatusEmun.已入库)
                            {
                                item.StockStatus = (int)StockStatusEmun.出库冻结;
                                UpdateData(item);
                            }
                        }
 
                        foreach (var li in labelInfos)
                        {
                            li.SOBKZ = "1";
                        }
                        _labelMasterRepository.UpdateData(labelInfos);
 
                        freezeResponse.MSGTY = "S";
                        freezeResponse.MSGTX = "";
                    }
                    else if (freezeRequest.YWLX == "01") //01解冻
                    {
                        foreach (var item in stockInfos)
                        {
                            if (item.StockStatus == (int)StockStatusEmun.出库冻结)
                            {
                                item.StockStatus = (int)StockStatusEmun.已入库;
                                UpdateData(item);
                            }
                        }
 
                        foreach (var li in labelInfos)
                        {
                            //if (li.LABEL_STATUS == "01")
                            //{
                            //    li.SOBKZ = "2";
                            //}
                            //else
                            //{
                            li.SOBKZ = "0";
                            //}
                        }
                        _labelMasterRepository.UpdateData(labelInfos);
 
                        freezeResponse.MSGTY = "S";
                        freezeResponse.MSGTX = "";
 
                    }
                    else
                    {
                        freezeResponse.MSGTY = "E";
                        freezeResponse.MSGTX = "不支持该业务类型";
                    }
                }
                else
                {
                    freezeResponse.MSGTY = "E";
                    freezeResponse.MSGTX = "请传入正确的工厂号和仓库号";
 
                }
 
            }
            catch (Exception ex)
            {
                freezeResponse.MSGTY = "E";
                freezeResponse.MSGTX = ex.Message;
 
            }
            return freezeResponse;
 
        }
    }
}