ke_junjie
2025-06-04 101c57ec4c28bc3c36e49c50a926e9e7c0dd0247
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
using IWareDataAccess.EF;
using IWareDataAccess.Entity.Base;
using IWareDataAccess.Entity.Summary;
using IWareDataAccess.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IWareDataAccess.Base.PLACEVSCONTAINER
{
    public static class PlaceVsContainerSqlFunc
    {
        /// <summary>
        /// 搜索表
        /// </summary>
        /// <param name="keyValue"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static List<BASE_PLACE_VS_CONTAINER> Search(PlaceVsContainerWebEntity webEntity, int page, int onePageNum, out string msg)
        {
            msg = "";
            using (Model edm = new Model())
            {
                var f = PredicateBuilder.True<BASE_PLACE_VS_CONTAINER>();
                //便利所有属性
                Type type = webEntity.GetType();
                foreach (var i in type.GetProperties())
                {
                    object v = Helper.Helper.GetFieldValueByName(webEntity, i.Name);
                    if (i.PropertyType == typeof(String))
                    {
                        string value;
                        if (v != null)
                        {
                            value = v.ToString();
                            f = f.And(x => Helper.Helper.GetFieldValueByName(x, i.Name) != null ? Helper.Helper.GetFieldValueByName(x, i.Name).ToString().Contains(value) : false);
                        }
                    }
                    else if (i.PropertyType == typeof(DateTime?))
                    {
                        if (v != null)
                        {
                            DateTime value = DateTime.Parse(v.ToString());
                            if (i.Name == "updateTimeStart")
                            {
                                f = f.And(x => x.UPDATETIME > value);
                            }
                            if (i.Name == "updateTimeEnd")
                            {
                                f = f.And(x => x.UPDATETIME < value);
                            }
                        }
                    }
                    else
                    {
                        if (v != null)
                        {
                            f = f.And(x => Object.Equals(Helper.Helper.GetFieldValueByName(x, i.Name), v));
                        }
                    }
                }
                List<BASE_PLACE_VS_CONTAINER> list = edm.BASE_PLACE_VS_CONTAINER.Where(f.Compile()).Skip((page - 1) * onePageNum).Take(onePageNum).ToList();
 
                return list;
            }
        }
 
        /// <summary>
        /// 搜索视图
        /// </summary>
        /// <param name="keyValue"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static List<View_BASE_PLACE_VS_CONTAINER> SearchView(PlaceVsContainerWebEntity webEntity, int page, int onePageNum, out int totalnum, out string msg)
        {
            msg = "";
            using (Model edm = new Model())
            {
                if (!string.IsNullOrEmpty(webEntity.isEmptyContainer))
                {
                    webEntity.enable2 = null;
                }
                Dictionary<string, object> parm = new Dictionary<string, object>();
                var f = PredicateBuilder.True<View_BASE_PLACE_VS_CONTAINER>();
                //便利所有属性
                Type type = webEntity.GetType();
                foreach (var i in type.GetProperties())
                {
                    object v = Helper.Helper.GetFieldValueByName(webEntity, i.Name);
                    if (i.PropertyType == typeof(String))
                    {
                        string value;
                        if (v != null)
                        {
                            value = v.ToString();
                            f = f.And(x => Helper.Helper.GetFieldValueByName(x, i.Name) != null ? Helper.Helper.GetFieldValueByName(x, i.Name).ToString().Contains(value) : false);
                            parm.Add(i.Name, value);
                        }
                    }
                    else if (i.PropertyType == typeof(DateTime?))
                    {
                        if (v != null)
                        {
                            DateTime value = DateTime.Parse(v.ToString());
                            if (i.Name == "updateTimeStart")
                            {
                                f = f.And(x => x.updateTime > value);
                                parm.Add("updateTime", value);
                            }
                            if (i.Name == "updateTimeEnd")
                            {
                                f = f.And(x => x.updateTime < value);
                                parm.Add("updateTime", value);
                            }
 
                        }
                    }
                    else
                    {
                        if (v != null)
                        {
                            f = f.And(x => Object.Equals(Helper.Helper.GetFieldValueByName(x, i.Name), v));
                            parm.Add(i.Name, v);
                        }
                    }
                }
                List<View_BASE_PLACE_VS_CONTAINER> list = new List<View_BASE_PLACE_VS_CONTAINER>();
                View_BASE_PLACE_VS_CONTAINER pvi = new View_BASE_PLACE_VS_CONTAINER();
                var data = SearchHelper.GetSearchData(edm, page, onePageNum, out totalnum, parm, pvi);
                list = data;
                return list;
            }
        }
 
 
        /// <summary>
        /// 搜索视图数量
        /// </summary>
        /// <param name="keyValue"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static int SearchViewNum(PlaceVsContainerWebEntity webEntity, out string msg)
        {
            msg = "";
            using (Model edm = new Model())
            {
                var f = PredicateBuilder.True<View_BASE_PLACE_VS_CONTAINER>();
                //便利所有属性
                Type type = webEntity.GetType();
                foreach (var i in type.GetProperties())
                {
                    object v = Helper.Helper.GetFieldValueByName(webEntity, i.Name);
                    if (i.PropertyType == typeof(String))
                    {
                        string value;
                        if (v != null)
                        {
                            value = v.ToString();
                            f = f.And(x => Helper.Helper.GetFieldValueByName(x, i.Name) != null ? Helper.Helper.GetFieldValueByName(x, i.Name).ToString().Contains(value) : false);
                        }
                    }
                    else if (i.PropertyType == typeof(DateTime?))
                    {
                        if (v != null)
                        {
                            DateTime value = DateTime.Parse(v.ToString());
                            if (i.Name == "updateTimeStart")
                            {
                                f = f.And(x => x.updateTime > value);
                            }
                            if (i.Name == "updateTimeEnd")
                            {
                                f = f.And(x => x.updateTime < value);
                            }
                        }
                    }
                    else
                    {
                        if (v != null)
                        {
                            f = f.And(x => Object.Equals(Helper.Helper.GetFieldValueByName(x, i.Name), v));
                        }
                    }
                }
 
                int num = 0;
                //补丁
                if (string.IsNullOrEmpty(webEntity.itemName))
                {
                    num = edm.View_BASE_PLACE_VS_CONTAINER.Where(f.Compile()).Count();
                }
                else
                {
                    num = edm.View_BASE_PLACE_VS_CONTAINER.Where(x => x.itemName.Contains(webEntity.itemName)).Where(f.Compile()).Count();
                }
 
                return num;
            }
        }
 
        /// <summary>
        /// 搜索总库存数量
        /// </summary>
        /// <param name="keyValue"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static int SearchItemAllNum(PlaceVsContainerWebEntity webEntity, out string msg)
        {
            msg = "";
            using (Model edm = new Model())
            {
                var f = PredicateBuilder.True<View_BASE_PLACE_VS_CONTAINER>();
                //便利所有属性
                Type type = webEntity.GetType();
                foreach (var i in type.GetProperties())
                {
                    object v = Helper.Helper.GetFieldValueByName(webEntity, i.Name);
                    if (i.PropertyType == typeof(String))
                    {
                        string value;
                        if (v != null)
                        {
                            value = v.ToString();
                            f = f.And(x => Helper.Helper.GetFieldValueByName(x, i.Name) != null ? Helper.Helper.GetFieldValueByName(x, i.Name).ToString().Contains(value) : false);
                        }
                    }
                    else if (i.PropertyType == typeof(DateTime?))
                    {
                        if (v != null)
                        {
                            DateTime value = DateTime.Parse(v.ToString());
                            if (i.Name == "updateTimeStart")
                            {
                                f = f.And(x => x.updateTime > value);
                            }
                            if (i.Name == "updateTimeEnd")
                            {
                                f = f.And(x => x.updateTime < value);
                            }
                        }
                    }
                    else
                    {
                        if (v != null)
                        {
                            f = f.And(x => Object.Equals(Helper.Helper.GetFieldValueByName(x, i.Name), v));
                        }
                    }
                }
                int num = edm.View_BASE_PLACE_VS_CONTAINER.Where(x => x.itemName.Contains(webEntity.itemName)).Where(f.Compile()).Sum(x => x.itemNum) ?? 0;
 
                return num;
            }
        }
 
        /// <summary>
        /// 搜索有效库存数量
        /// </summary>
        /// <param name="keyValue"></param>
        /// <param name="page"></param>
        /// <returns></returns>
        public static int SearchItemCanUseNum(PlaceVsContainerWebEntity webEntity, out string msg)
        {
            msg = "";
            using (Model edm = new Model())
            {
 
                //int numCanUse = edm.BASE_CONTAINER_VS_ITEM.Where(x => (x.OUTPLANID ?? 0) == 0 && x.BASE_ITEM.ITEMNAME==webEntity.itemName && x.ENABLE==1).Sum(x => x.ITEMNUM) ?? 0;
                int numCanUse = edm.BASE_CONTAINER_VS_ITEM.Where(x => x.BASE_ITEM.ITEMNAME == webEntity.itemName && x.ENABLE == 1).Sum(x => x.ITEMNUM) ?? 0;
                List<ORDER_OUTORDER> orderList = edm.ORDER_OUTORDER.Where(x => x.BASE_ITEM.ITEMNAME == webEntity.itemName && x.ORDERSTATUS == "执行中" && x.ENABLE == 1).ToList();
                foreach (var i in orderList)
                {
                    numCanUse = numCanUse - ((i.TOTALOUTNUM ?? 0) - (i.CHECKOUTNUM ?? 0));
                }
                return numCanUse;
            }
        }
 
        /// <summary>
        /// 添加修改
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public static bool Add(PlaceVsContainerWebEntity webEntity, out string msg)
        {
            msg = "";
            using (Model edm = new Model())
            {
 
                BASE_PLACE_VS_CONTAINER pvc = new BASE_PLACE_VS_CONTAINER();
                pvc = edm.BASE_PLACE_VS_CONTAINER.FirstOrDefault(x => x.PVCCODE == webEntity.pvcCode);
                if (pvc != null)
                {
                    //msg = "此库存编号已存在";
                    //return false;
                }
                else
                {
                    pvc = new BASE_PLACE_VS_CONTAINER();
                    pvc.STATUS = "IN";
                    pvc.BASE_PLACE.ISFULL = 1;
                }
                BASE_CONTAINER container = edm.BASE_CONTAINER.FirstOrDefault(x => x.CONTAINERNAME == webEntity.containerName);
                if (container == null)
                {
                    msg = "无此托盘";
                    return false;
                }
                pvc.BASE_CONTAINER = container;
                BASE_PLACE place = edm.BASE_PLACE.FirstOrDefault(x => x.PLACE == webEntity.place);
                if (place == null)
                {
                    msg = "无此库位";
                    return false;
                }
                if (place.ISFULL == 1)
                {
                    msg = "此库位已有东西";
                    return false;
                }
                pvc.BASE_PLACE = place;
                pvc.UPDATETIME = DateTime.Now;
 
                pvc.ENABLE = webEntity.enable ?? pvc.ENABLE;
                pvc.PVCCODE = webEntity.pvcCode ?? pvc.PVCCODE;
 
                edm.BASE_PLACE_VS_CONTAINER.AddOrUpdateExtension(pvc);
                if (edm.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    msg = "保存失败";
                    return false;
                }
            }
        }
 
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="webDevice"></param>
        /// <returns></returns>
        public static bool Delete(PlaceVsContainerWebEntity webEntity, out string msg)
        {
            msg = "";
            using (Model edm = new Model())
            {
 
                BASE_PLACE_VS_CONTAINER pvc = edm.BASE_PLACE_VS_CONTAINER.FirstOrDefault(x => x.BASE_PLACE.PLACE == webEntity.place);
                if (pvc == null)
                {
                    msg = "无此库存";
                    return false;
                }
                else
                {
                    ///更改库位状态
                    var place = edm.BASE_PLACE.FirstOrDefault(x => x.ID == pvc.PLACEID);
                    if (place != null)
                    {
                        place.ISFULL = 0;
                    }
                    ///删除托盘物料关系
                    var cvilst = edm.BASE_CONTAINER_VS_ITEM.Where(x => x.CONTAINERID == pvc.CONTAINERID).ToList();
                    if (cvilst != null && cvilst.Count > 0)
                    {
                        edm.BASE_CONTAINER_VS_ITEM.RemoveRange(cvilst);
                    }
                    ///删除托盘库位关系
                    edm.BASE_PLACE_VS_CONTAINER.Remove(pvc);
 
                    if (edm.SaveChanges() > 0)
                    {
                        //记录删除托盘和物料绑定关系日志 【Editby shaoc,2023-03-07】
                        IWareDataAccess.Helper.Helper.LogRemoveBASE_CONTAINER_VS_ITEM(cvilst, "PlaceVsContainerSqlFunc.Delete", "删除");
                        WZ.Useful.Commons.LogTextHelper.WriteLine("PlaceVsContainerSqlFunc", "Delete", "成功删除" + pvc.BASE_PLACE.PLACE + ",器具" + pvc.BASE_CONTAINER.CONTAINERNAME);
                        return true;
                    }
                    else
                    {
                        msg = "保存失败";
                        return false;
                    }
                }
            }
        }
 
        /// <summary>
        /// 获取组盘号
        /// </summary>
        /// <returns></returns>
        public static string GetCode()
        {
            using (Model edm = new Model())
            {
                string month = DateTime.Now.Month.ToString("00");
                string day = DateTime.Now.Day.ToString("00");
                string hour = DateTime.Now.Hour.ToString("00");
                string minute = DateTime.Now.Minute.ToString("00");
                string second = DateTime.Now.Second.ToString("00");
                string millSecond = DateTime.Now.Millisecond.ToString("0000");
                string time = DateTime.Now.Year.ToString() + month + day + hour + minute + second + millSecond;
 
                return "PC" + time;
            }
        }
 
        public static List<HomeNumDataEntity> SearchEmptyContainerPlace(PlaceVsContainerWebEntity model, out string msg)
        {
            msg = "";
            using (Model edm = new Model())
            {
                var item = edm.BASE_ITEM.FirstOrDefault(x => x.ITEMNAME == model.itemName);
                int allcout = 0;
                int cout = 0;
                if (item != null && !string.IsNullOrEmpty(item.USECONTAINERTYPE))
                {
                    List<string> typeList = item.USECONTAINERTYPE.Split(',').ToList();
                    foreach (var i in typeList)
                    {
                        var place = edm.BASE_PLACE_VS_CONTAINER.Where(x => x.BASE_CONTAINER.BASE_CONTAINER_VS_ITEM.Count == 0 && i.Contains(x.BASE_CONTAINER.CONTAINERTYPE)).ToList();
                        allcout = (place == null ? 0 : place.Select(x => x.CONTAINERID).ToList().Distinct().ToList().Count) + allcout;
                        cout = (place == null ? 0 : place.Where(x => x.STATUS == "IN").Select(x => x.CONTAINERID).ToList().Distinct().ToList().Count) + cout;
                    }
                }
                List<HomeNumDataEntity> hndlst = new List<HomeNumDataEntity>();
                hndlst.Add(new HomeNumDataEntity() { no = 1, titel = "总空器具数", value = allcout });
                hndlst.Add(new HomeNumDataEntity() { no = 2, titel = "有效空器具数", value = cout });
                return hndlst;
            }
        }
    }
}