Admin
2025-12-02 9e42f0dafa019f5ecf6b0ff425ecb966b002171e
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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using WIDESEA.Common;
using WIDESEA.Core.Extensions;
using WIDESEA.Core.ManageUser;
using WIDESEA.Core.Services;
using WIDESEA.Core.Utilities;
using WIDESEA.Entity.DomainModels;
using WIDESEA.Services.IRepositories;
using WIDESEA.Services.Repositories;
using WIDESEA_Common.LogEnum;
 
namespace WIDESEA.Services.Services
{
    public partial class CommonFunction
    {
        /// <summary>
        /// 轴承出库
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public static WebResponseContent BearingOutboundAction(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                //轮对条码
                string barcode = saveModel.MainData["barcode"].ToString();
                //车型
                string materiel = saveModel.MainData["materiel"].ToString();
                //轴承状态新旧
                string zcState = saveModel.MainData["zcState"].ToString();
                //轴承厂家
                string Manufacturer = saveModel.MainData["Manufacturer"].ToString();
                //右防尘板
                string yfcbAVG = saveModel.MainData["yfcbAVG"]?.ToString();
                //左防尘板
                string zfcbAVG = saveModel.MainData["zfcbAVG"]?.ToString();
                //左油档
                string zydAVG = saveModel.MainData["zydAVG"]?.ToString();
                //右油档
                string yydAVG = saveModel.MainData["yydAVG"]?.ToString();
                //左轴径
                string zzjAVG = saveModel.MainData["zzjAVG"]?.ToString();
                //右轴径
                string yzjAVG = saveModel.MainData["yzjAVG"]?.ToString();
                //呼叫工位
                string targetStation = saveModel.MainData["targetStation"].ToString();
                //轴承方向
                string ZCdirection = saveModel.MainData["ZCdirection"].ToString();
 
                //轮对号
                string ldCode = saveModel.MainData["ldCode"].ToString();
 
 
                //左出库轴承
                VV_ContainerInfo leftContainer = null;
                //右出库轴承
                VV_ContainerInfo rightContainer = null;
                List<Dt_materielinfo> materielinfo = Dt_materielinfoRepository.Instance.Find(r => r.materiel_suitMateriel.Contains(materiel));
 
 
                //获取选配标准
                List<Dt_selection_standards> listStandard = null;
                foreach (var item in materielinfo)
                {
                    listStandard = Dt_selection_standardsRepository.Instance.Find(r => r.selection_carId == item.materiel_id
                 && r.selection_zc_manufacturer == Manufacturer && r.selection_zc_type.Contains(zcState));
                    if (null != listStandard && listStandard.Count > 0)
                        break;
                }
 
                foreach (var item in materielinfo)
                {
                    List<VV_ContainerInfo> listContainer = VV_ContainerInfoRepository.Instance.Find(r => !r.location_islocked
             && r.location_state == LocationState.LocationState_Stored.ToString() && r.containerdtl_madeUnit == Manufacturer
            && r.containerdtl_standard.Contains(zcState) && r.materiel_id == item.materiel_id && r.containerdtl_standardResult == 1
             && (string.IsNullOrEmpty(r.csize_out_result) || r.csize_out_result == "合格") && r.containerdtl_qualityCheck == true
             && r.containerdtl_takeGoogsCheck == true);
 
                    //listContainer = listContainer.OrderBy(x => DateTime.ParseExact(x.containerdtl_ProductDate,"yyyy-MM-dd",CultureInfo.InvariantCulture)).ToList();
 
 
 
                    //&& Convert.ToDateTime(r.containerdtl_ProductDate) > DateTime.Now  
                    if (null != listContainer && listContainer.Count > 0)
                    {
                        if ("all".Equals(ZCdirection) || string.IsNullOrEmpty(ZCdirection))
                        {
                            if (null == leftContainer)
                            {
                                leftContainer = GetContainer(listStandard, listContainer, zzjAVG, zfcbAVG, zydAVG, "left");
                                if (null != leftContainer)
                                    listContainer.Remove(leftContainer);
                            }
                            if (null == rightContainer)
                            {
                                rightContainer = GetContainer(listStandard, listContainer, yzjAVG, yfcbAVG, yydAVG, "right");
                                if (null != rightContainer)
                                    listContainer.Remove(rightContainer);
                            }
                            if (null == leftContainer || null == rightContainer)
                                continue;
                            else
                                break;
                        }
                        else if ("left".Equals(ZCdirection))
                        {
                            leftContainer = GetContainer(listStandard, listContainer, zzjAVG, zfcbAVG, zydAVG, "left");
                            if (null == leftContainer)
                                continue;
                            else
                                break;
                        }
                        else if ("right".Equals(ZCdirection))
                        {
                            rightContainer = GetContainer(listStandard, listContainer, yzjAVG, yfcbAVG, yydAVG, "right");
                            if (null == rightContainer)
                                continue;
                            else
                                break;
                        }
                    }
                }
 
                if ("all".Equal(ZCdirection))
                {
                    if (null == leftContainer || null == rightContainer)
                        return content.Error("未找到可匹配的出库轴承,车型:" + materiel);
                }
 
                if (null != leftContainer || null != rightContainer)
                {
                    IDt_taskinfoRepository taskinfoRepository = Dt_taskinfoRepository.Instance;
                    content = taskinfoRepository.DbContextBeginTransaction(() =>
                    {
                        List<Dt_taskinfo> listTask = new List<Dt_taskinfo>();
                        if (null != leftContainer)
                        {
                            Dt_taskinfo leftTask = GetOutboundTask(leftContainer, targetStation, "left", CommonFunction.GetZCInfo(leftContainer, materiel, barcode, zfcbAVG, zydAVG, zzjAVG, ldCode, targetStation, "left"));
                            listTask.Add(leftTask);
                        }
                        if (null != rightContainer)
                        {
                            Dt_taskinfo rightTask = GetOutboundTask(rightContainer, targetStation, "right", CommonFunction.GetZCInfo(rightContainer, materiel, barcode, yfcbAVG, yydAVG, yzjAVG, ldCode, targetStation, "right"));
                            listTask.Add(rightTask);
                        }
                        taskinfoRepository.AddRange(listTask, true);
                        //下发出库任务给WCS
                        content = WIDESEA_Services.WCSApi.SendTaskToWCS(listTask);
                        if (content.Status)
                        {
                            //if (null != leftContainer)
                            //    CommonFunction.WheelsetMatch(barcode, leftContainer.containerdtl_number, "6");
                            //if (null != rightContainer)
                            //    CommonFunction.WheelsetMatch(barcode, rightContainer.containerdtl_number, "7");
                            content.OK($"轴承出库任务下发给WCS成功.");
                        }
                        else
                            content.Error($"轴承出库任务下发给WCS失败,原因 => {content.Message}");
 
                        return content;
                    });
                }
                else
                    return content.Error("未找到可匹配的出库轴承,车型:" + materiel);
            }
            catch (Exception ex)
            {
                content.Error("创建轴承出库任务失败:" + ex.Message);
            }
            finally
            {
                LogRecord.WriteLog((int)LogEnum.OutBound, $"{UserContext.Current.UserTrueName}轴承选配出库:参数:{saveModel.Serialize()},返回参数:{content.Serialize()}");
                //Logger.AddLog(Core.Enums.LoggerType.Add, saveModel, content, content);
            }
            return content;
        }
 
        private static Dt_taskinfo GetOutboundTask(VV_ContainerInfo container, string target, string direction, object zcInfo)
        {
            Dt_locationinfo locationinfo = Dt_locationinfoRepository.Instance.FindFirst(x => x.location_id == container.location_id);
            CommonFunction.ChangeLocationState(locationinfo, LocationState.LocationState_Outbound_Wait_Executing.ToString());
            Dt_taskinfo taskinfo = new Dt_taskinfo();
            taskinfo.task_id = Guid.NewGuid();
            taskinfo.task_type = TaskType.TaskType_Box_Pallet_Outbound.ToString();
            taskinfo.task_state = TaskState.TaskState_Create.ToString();
            taskinfo.task_barcode = container.containerhead_barcode;
            taskinfo.task_materielid = container.materiel_id;
            //起始货位
            taskinfo.task_fromlocationid = container.location_id;
            //目的货位
            taskinfo.task_tolocationid = LayerToStation.OutAreaLayerToStation(locationinfo.location_layer);
            //起始站台
            taskinfo.task_beginstation = LayerToStation.OutAreaLayerToStation(locationinfo.location_layer);
            //目的站台
            taskinfo.task_endstation = LayerToStation.GetOutboundStation(target, direction);
            taskinfo.task_grade = 0;
            taskinfo.task_isunpacked = false;
            taskinfo.task_creator = UserContext.Current.UserTrueName;
            taskinfo.task_createtime = DateTime.Now;
            taskinfo.task_bak_1 = JsonConvert.SerializeObject(zcInfo);
            taskinfo.task_sn = container.containerdtl_goodsCode;
            taskinfo.task_materielType = container.containerdtl_type;
            taskinfo.task_weight = container.containerdtl_goodsWeight;
            return taskinfo;
        }
 
        /// <summary>
        /// 获取库存
        /// </summary>
        /// <param name="listStand"></param>
        /// <param name="listContainer"></param>
        /// <param name="fcbAVG"></param>
        /// <param name="ydAVG"></param>
        /// <param name="zzAVG"></param>
        /// <param name="direction"></param>
        public static VV_ContainerInfo GetContainer(List<Dt_selection_standards> listStand, List<VV_ContainerInfo> listContainer,
          string zjAVG, string fcbAVG, string ydAVG, string direction)
        {
            IQueryable<VV_ContainerInfo> queryble = listContainer.AsQueryable();
 
            //轴承内径
 
            Dt_selection_standards zjStand = listStand.Find(r => r.selection_standard_type == "1");
            if (null != zjStand)
            {
                if (!string.IsNullOrEmpty(zjAVG))
                {
                    //上限
                    decimal zjStandQty_positive = zjStand.selection_positive_value.GetValueOrDefault();
                    //下限
                    decimal zjStandQty_negative = zjStand.selection_negative_value.GetValueOrDefault();
                    decimal mesStand = decimal.Parse(zjAVG);
                    queryble = queryble.Where(r => r.csize_in_result == "合格" && mesStand - (decimal.Parse(r.csize_in_value)) >= zjStandQty_negative
                    && mesStand - (decimal.Parse(r.csize_in_value)) <= zjStandQty_positive);
                }
                else
                    queryble = queryble.Where(r => r.csize_in_result != "不合格");
            }
            else
                queryble = queryble.Where(r => r.csize_in_result != "不合格");
 
            ////防尘板
            Dt_selection_standards fcbStand = listStand.Find(r => r.selection_standard_type == "2");
            if (null != fcbStand)
            {
 
                if (!string.IsNullOrEmpty(fcbAVG))
                {
                    //上限
                    decimal fcbStandQty_positive = fcbStand.selection_positive_value.GetValueOrDefault();
                    //下限
                    decimal fcbStandQty_negative = fcbStand.selection_negative_value.GetValueOrDefault();
                    decimal mesStand = decimal.Parse(fcbAVG);
                    queryble = queryble.Where(r => r.csize_four_result == "合格" && mesStand - (decimal.Parse(r.csize_four_3)) >= fcbStandQty_negative
                    && mesStand - (decimal.Parse(r.csize_four_3)) <= fcbStandQty_positive);
                }
                else
                    queryble = queryble.Where(r => r.csize_four_result != "不合格");
            }
            else
                queryble = queryble.Where(r => r.csize_four_result != "不合格");
 
            ////油档
            Dt_selection_standards ydStand = listStand.Find(r => r.selection_standard_type == "3");
            if (null != ydStand)
            {
                if (!string.IsNullOrEmpty(ydAVG))
                {
                    //上限
                    decimal ydStandQty_positive = ydStand.selection_positive_value.GetValueOrDefault();
                    //下限
                    decimal ydStandQty_negative = ydStand.selection_negative_value.GetValueOrDefault();
                    decimal mesStand = decimal.Parse(ydAVG);
                    queryble = queryble.Where(r => r.csize_three_result == "合格" && (mesStand - decimal.Parse(r.csize_three_3)) >= ydStandQty_negative
                    && mesStand - (decimal.Parse(r.csize_three_3)) <= ydStandQty_positive);
                }
                else
                    queryble = queryble.Where(r => r.csize_three_result != "不合格");
            }
            else
                queryble = queryble.Where(r => r.csize_three_result != "不合格");
 
 
 
            //轴承内径
            //if (!string.IsNullOrEmpty(zjAVG))
            //{
            //    Dt_selection_standards zjStand = listStand.Find(r => r.selection_standard_type == "1");
            //    if (null == zjStand)
            //        throw new Exception($"请先维护可用的轴承内径过盈量选配标准。");
            //    //上限
            //    decimal zjStandQty_positive = zjStand.selection_positive_value.GetValueOrDefault();
            //    //下限
            //    decimal zjStandQty_negative = zjStand.selection_negative_value.GetValueOrDefault();
            //    decimal mesStand = decimal.Parse(zjAVG);
            //    queryble = queryble.Where(r => r.csize_in_result == "合格" && (decimal.Parse(r.csize_in_value) - mesStand) >= zjStandQty_negative
            //    && (decimal.Parse(r.csize_in_value) - mesStand) <= zjStandQty_positive);
            //}
            //else
            //    queryble = queryble.Where(r => r.csize_in_result != "不合格");
 
            ////防尘板
            //if (!string.IsNullOrEmpty(fcbAVG))
            //{
            //    Dt_selection_standards fcbStand = listStand.Find(r => r.selection_standard_type == "2");
            //    if (null == fcbStand)
            //        throw new Exception($"请先维护可用的防尘板过盈量选配标准。");
            //    //上限
            //    decimal fcbStandQty_positive = fcbStand.selection_positive_value.GetValueOrDefault();
            //    //下限
            //    decimal fcbStandQty_negative = fcbStand.selection_negative_value.GetValueOrDefault();
            //    decimal mesStand = decimal.Parse(fcbAVG);
            //    queryble = queryble.Where(r => r.csize_four_result == "合格" && (decimal.Parse(r.csize_four_3) - mesStand) >= fcbStandQty_negative
            //    && (decimal.Parse(r.csize_four_3) - mesStand) <= fcbStandQty_positive);
            //}
            //else
            //    queryble = queryble.Where(r => r.csize_four_result != "不合格");
 
 
            ////油档
            //if (!string.IsNullOrEmpty(ydAVG))
            //{
            //    Dt_selection_standards ydStand = listStand.Find(r => r.selection_standard_type == "3");
            //    if (null == ydStand)
            //        throw new Exception($"请先维护可用的油档过盈量选配标准。");
            //    //上限
            //    decimal ydStandQty_positive = ydStand.selection_positive_value.GetValueOrDefault();
            //    //下限
            //    decimal ydStandQty_negative = ydStand.selection_negative_value.GetValueOrDefault();
            //    decimal mesStand = decimal.Parse(ydAVG);
            //    queryble = queryble.Where(r => r.csize_three_result == "合格" && (decimal.Parse(r.csize_three_3) - mesStand) >= ydStandQty_negative
            //    && (decimal.Parse(r.csize_three_3) - mesStand) <= ydStandQty_positive);
            //}
            //else
            //    queryble = queryble.Where(r => r.csize_three_result != "不合格");containerdtl_createtime
 
 
 
            return queryble.OrderBy(r => r.containerdtl_ProductDate).ThenBy(r => r.containerdtl_createtime).FirstOrDefault();
        }
 
 
    }
}