/* *所有关于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); } /// /// 呼叫空托盘、给WCS下发任务 /// /// 呼叫数量 /// public WebResponseContent CallEmptyPallet(SaveModel saveModel) { WebResponseContent content = new WebResponseContent(); try { int number = int.Parse(saveModel.MainData["callNumber"].ToString()); //查找库内空托盘数量 List 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 rgvLayer = new List(); 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 newLayer = new List(); //避免有的车当前层没有空托盘 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 listContainer = emptyPalletList.FindAll(r => rgvLayer.Contains(r.location_layer)); if (listContainer.Count >= number) { emptyPalletList = new List(); 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 ortherContainer = emptyPalletList.FindAll(r => !rgvLayer.Contains(r.location_layer)); emptyPalletList = new List(); 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 listTask = new List(); 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; } } }