Admin
7 小时以前 1cd9280bbecf557f8978ad3839f14827ff9f4d34
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
/*
 *所有关于VV_ContainerInfo_EmptyPallet类的业务代码应在此处编写
*可使用repository.调用常用方法,获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*VV_ContainerInfo_EmptyPalletService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using WIDESEA.Core.BaseProvider;
using WIDESEA.Core.Extensions.AutofacManager;
using WIDESEA.Entity.DomainModels;
using System.Linq;
using WIDESEA.Core.Utilities;
using System.Linq.Expressions;
using WIDESEA.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using WIDESEA.Services.IRepositories;
using System;
using WIDESEA.Core.EFDbContext;
using WIDESEA.Services.Repositories;
using WIDESEA.Core.ManageUser;
using System.Collections.Generic;
using WIDESEA.Common;
using WIDESEA_Services;
using WIDESEA.Core.Services;
using WIDESEA_Services.Services.APIInvoke.RGV;
using WIDESEA_Entity.CustomModels.RGVModel;
using static WIDESEA_Entity.CustomModels.RGVModel.RgvDeviceStatusModel;
 
namespace WIDESEA.Services.Services
{
    public partial class VV_ContainerInfo_EmptyPalletService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IVV_ContainerInfo_EmptyPalletRepository _repository;//访问数据库
 
 
        [ActivatorUtilitiesConstructor]
        public VV_ContainerInfo_EmptyPalletService(
            IVV_ContainerInfo_EmptyPalletRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码,其他情况可以不用
            //base.Init(dbRepository);
        }
 
 
 
        /// <summary>
        /// 呼叫空托盘、给WCS下发任务
        /// </summary>
        /// <param name="number">呼叫数量</param>
        /// <returns></returns>
        public WebResponseContent CallEmptyPallet(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                int number = int.Parse(saveModel.MainData["callNumber"].ToString());
                //查找库内空托盘数量
                List<VV_ContainerInfo_EmptyPallet> emptyPalletList = VV_ContainerInfo_EmptyPalletRepository.Instance.Find(x => x.location_state
                == LocationState.LocationState_Stored.ToString() && !x.location_islocked).OrderBy(x => x.containerdtl_createtime).ToList();
 
                if (number > emptyPalletList.Count())
                    throw new Exception($"无法呼叫数量为:【{number}】个空托盘,当库内可用空托盘数量为:【{emptyPalletList.Count()}】");
 
                List<int> rgvLayer = new List<int>();
                WebResponseContent resultcontent = RGVAPIInvokeGetRgvInfo.GetRgvInfo();
                if (resultcontent.Status)
                {
                    RgvDeviceStatusModel rgvData = (RgvDeviceStatusModel)resultcontent.Data;
                    //0离线 1正常 2故障
                    for (int i = 0; i < rgvData.data.Count(); i++)
                    {
                        Info _info = rgvData.data[i];
                        if (_info.status == 1)
                            rgvLayer.Add(_info.layer);
                    }
                }
                if (rgvLayer.Count <= 0)
                    return content.Error("当前无可用的RGV小车出库.");
                //剩下的
                //int count = number % rgvLayer.Count();
                // 每台车的,均分
                // int rgvCount = (number - count) / rgvLayer.Count();
 
                List<int> newLayer = new List<int>();
                //避免有的车当前层没有空托盘
                foreach (var item in rgvLayer)
                {
                    VV_ContainerInfo_EmptyPallet container = emptyPalletList.Find(r => r.location_layer == item);
                    if (null == container)
                    {
                        container = emptyPalletList.Find(r => !rgvLayer.Contains(r.location_layer));
                        if (null != container)
                            newLayer.Add(container.location_layer);
                        //rgvLayer.Remove(item);
                    }
                    else
                        newLayer.Add(item);
                }
                rgvLayer = newLayer;
 
 
                List<VV_ContainerInfo_EmptyPallet> listContainer = emptyPalletList.FindAll(r => rgvLayer.Contains(r.location_layer));
                if (listContainer.Count >= number)
                {
                    emptyPalletList = new List<VV_ContainerInfo_EmptyPallet>();
                    int layerCount = rgvLayer.Count();
                    int index = 0;
                    for (int i = 0; i < number; i++)
                    {
                        for (int layer = 0; layer < layerCount; layer++)
                        {
                            if (index == layerCount)
                                index = 1;
                            else
                                index++;
                            int item = rgvLayer[index - 1];
                            VV_ContainerInfo_EmptyPallet container = listContainer.Find(r => r.location_layer == item);
                            if (null != container)
                            {
                                emptyPalletList.Add(container);
                                listContainer.Remove(container);
                                break;
                            }
                        }
                    }
                    if (emptyPalletList.Count < number)
                        return content.Error("空托盘出库分配出错.");
                }
                else
                {
                    List<VV_ContainerInfo_EmptyPallet> ortherContainer = emptyPalletList.FindAll(r => !rgvLayer.Contains(r.location_layer));
                    emptyPalletList = new List<VV_ContainerInfo_EmptyPallet>();
                    emptyPalletList.AddRange(listContainer);
                    emptyPalletList.AddRange(ortherContainer.OrderBy(r => r.location_layer));
                }
 
                Dt_general_info general_Info = Dt_general_infoRepository.Instance.FindFirst(x => true);
                if ("应急模式".Equals(general_Info.general_inline_current_model))
                {
                    Dt_taskinfo task = Dt_taskinfoRepository.Instance.FindFirst(r => LayerToStation.inboundStations.Contains(r.task_endstation)
                 || LayerToStation.outboundStations.Contains(r.task_beginstation));
                    if (null != task || number > 1)
                        return content.Error("应急模式只能出1个任务.");
                }
 
                //更改货位状态、添加WMS任务和WCS任务
                content = VV_ContainerInfo_EmptyPalletRepository.Instance.DbContextBeginTransaction(() =>
                {
                    List<Dt_taskinfo> listTask = new List<Dt_taskinfo>();
                    foreach (var item in emptyPalletList.Take(number))
                    {
                        Dt_locationinfo locationinfo = Dt_locationinfoRepository.Instance.FindFirst(x => x.location_id == item.location_id);
                        //修改货位状态
                        CommonFunction.ChangeLocationState(locationinfo, LocationState.LocationState_Empty_OutBound_Wait_Executing.ToString());
                        Dt_container_head head = Dt_container_headRepository.Instance.FindFirst(r => r.containerhead_barcode == item.containerhead_barcode);
                        Dt_taskinfo tmpTaskInfo = CommonFunction.AddWMSTask_EmptyPalletOut(locationinfo, item.containerhead_barcode, head.containerhead_palletweight, general_Info);
                        listTask.Add(tmpTaskInfo);
 
                    }
 
                    content = WCSApi.SendTaskToWCS(listTask);
                    if (content.Status)
                        content.OK($"呼叫空托盘出库成功,任务托盘码为 = {listTask.Select(r => r.task_barcode).Serialize()}");
                    else
                        content.Error($"呼叫空托盘出库任务出错,原因:{content.Message}");
 
                    return content;
                });
            }
            catch (Exception ex)
            {
                content.Error($"呼叫空托盘出库任务出错" + ex.Message);
            }
            Logger.AddLog(Core.Enums.LoggerType.Add, saveModel, content, content);
            return content;
 
        }
 
 
        public override WebResponseContent Update(SaveModel saveModel)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                IDt_container_headRepository detailRepository = Dt_container_headRepository.Instance;
                Guid detID = saveModel.MainData["containerhead_id"].ToGuid();
                string testResult = saveModel.MainData["containerdtl_text1"].ToString();
                Dt_container_head container_Detail = detailRepository.FindFirst(x => x.containerhead_id == detID);
                container_Detail.containerhead_palletweight = testResult;
                detailRepository.Update(container_Detail, true);
                return content.OK("修改成功");
            }
            catch (Exception ex)
            {
                return content.Error($"修改失败,原因:{ex.Message}");
                throw;
            }
        }
 
 
        public override WebResponseContent Del(object[] keys, bool delList = true)
        {
            WebResponseContent content = new WebResponseContent();
            try
            {
                if (keys.Length > 5)
                    return content.Error("一次最多删除5条库存记录");
                content = repository.DbContextBeginTransaction(() =>
                {
                    foreach (var item in keys)
                    {
                        Guid id = new Guid(item.ToString());
                        VV_ContainerInfo_EmptyPallet containerInfo = repository.FindFirst(x => x.containerhead_id == id);
                        if (null != containerInfo)
                        {
                            Dt_taskinfo task = Dt_taskinfoRepository.Instance.FindFirst(r => r.task_barcode == containerInfo.containerdtl_barcode);
                            if (null != task)
                                return content.Error($"托盘号{task.task_barcode}存在未完成的任务,请先确认。");
 
                            Dt_container_head container_Head = Dt_container_headRepository.Instance.FindFirst(x => x.containerhead_barcode == containerInfo.containerhead_barcode);
                            Dt_container_detail container_Detail = Dt_container_detailRepository.Instance.FindFirst(x => x.containerdtl_headid == id.ToString());
                            //将库存数据一到到历史表 head、detail
                            CommonFunction.AddContainerHeadToHistory(container_Head);
                            CommonFunction.AddContainerDetailToHistory(container_Detail);
 
                            //barcodeinfo数据移动到历史
                            Dt_barcodeinfo barcodeinfo = Dt_barcodeinfoRepository.Instance.FindFirst(x => x.barcode_value == containerInfo.containerhead_barcode);
                            CommonFunction.AddBarcodeInfoToHistory(barcodeinfo, UserContext.Current.UserTrueName);
 
                            //恢复空货位状态
                            Dt_locationinfo locationinfo = Dt_locationinfoRepository.Instance.FindFirst(x => x.location_id == containerInfo.location_id);
                            CommonFunction.ChangeLocationState(locationinfo, LocationState.LocationState_Empty.ToString());
                        }
                    }
                    return content.OK("删除空托盘信息成功.");
                });
            }
            catch (Exception ex)
            {
                return content.OK("删除空托盘信息失败." + ex.Message);
            }
            finally
            {
                Logger.AddLog(Core.Enums.LoggerType.Del, keys, content, content);
            }
            return content;
        }
 
    }
    public class emptyPalletOutInfo
    {
        public int layer { get; set; }
        public int count { get; set; }
    }
 
}