#region << 版 本 注 释 >>
|
/*----------------------------------------------------------------
|
* 命名空间:WIDESEAWCS_TaskInfoService
|
* 创建者:胡童庆
|
* 创建时间:2024/8/2 16:13:36
|
* 版本:V1.0.0
|
* 描述:
|
*
|
* ----------------------------------------------------------------
|
* 修改人:
|
* 修改时间:
|
* 版本:V1.0.1
|
* 修改说明:
|
*
|
*----------------------------------------------------------------*/
|
#endregion << 版 本 注 释 >>
|
|
using System.Text;
|
using System.Threading.Tasks;
|
using AutoMapper;
|
using HslCommunication;
|
using HslCommunication.WebSocket;
|
using SqlSugar;
|
using WIDESEAWCS_BasicInfoRepository;
|
using WIDESEAWCS_BasicInfoService;
|
using WIDESEAWCS_Common;
|
using WIDESEAWCS_Core;
|
using WIDESEAWCS_Core.BaseRepository;
|
using WIDESEAWCS_Core.BaseServices;
|
using WIDESEAWCS_Core.Enums;
|
using WIDESEAWCS_Core.Helper;
|
using WIDESEAWCS_DTO.BasicInfo;
|
using WIDESEAWCS_DTO.PlacedBlockDTO;
|
using WIDESEAWCS_DTO.TaskInfo;
|
using WIDESEAWCS_IBasicInfoRepository;
|
using WIDESEAWCS_IBasicInfoService;
|
using WIDESEAWCS_ITaskInfoRepository;
|
using WIDESEAWCS_ITaskInfoService;
|
using WIDESEAWCS_Model.Models;
|
using WIDESEAWCS_QuartzJob.Repository;
|
using static Dm.net.buffer.ByteArrayBuffer;
|
|
namespace WIDESEAWCS_TaskInfoService
|
{
|
public class TaskService : ServiceBase<Dt_Task, ITaskRepository>, ITaskService
|
{
|
private readonly IUnitOfWorkManage _unitOfWorkManage;
|
private readonly IMapper _mapper;
|
private readonly IContainerRepository _containerRepository;
|
private readonly IContainerItemRepository _containerItemRepository;
|
private readonly WebSocketServer _webSocketServer;
|
private readonly IOrderDetailsRepository _orderDetailsRepository;
|
private readonly IContainerService _containerService;
|
private readonly IOrderDetailsService _orderDetailsService;
|
private readonly IOrderrowsRepository _orderrowsRepository;
|
private readonly IOrderContainerRepository _orderContainerRepository;
|
|
public TaskService(ITaskRepository BaseDal, IUnitOfWorkManage unitOfWorkManage, IMapper mapper, IContainerRepository containerRepository, IContainerItemRepository containerItemRepository, WebSocketServer webSocketServer, IOrderDetailsRepository orderDetailsRepository, IContainerService containerService, IOrderDetailsService orderDetailsService, IOrderrowsRepository orderrowsRepository, IOrderContainerRepository orderContainerRepository) : base(BaseDal)
|
{
|
_unitOfWorkManage = unitOfWorkManage;
|
_mapper = mapper;
|
_containerRepository = containerRepository;
|
_containerItemRepository = containerItemRepository;
|
_webSocketServer = webSocketServer;
|
_orderDetailsRepository = orderDetailsRepository;
|
_containerService = containerService;
|
_orderDetailsService = orderDetailsService;
|
_orderrowsRepository = orderrowsRepository;
|
_orderContainerRepository = orderContainerRepository;
|
}
|
|
public Dt_Task? QueryAGantryUnExecuteTask(string gantryDeviceNo)
|
{
|
return BaseDal.QueryFirst(x => x.TaskState == (int)TaskStatusEnum.Gantry_New && x.DeviceCode == gantryDeviceNo);
|
}
|
|
public WebResponseContent PlaceBlockTest(int orderRowId)
|
{
|
try
|
{
|
List<OrderDetails> orderDetails = _orderDetailsRepository.QueryData(x => x.Orderrowsid == orderRowId);
|
if (orderDetails == null || orderDetails.Count == 0)
|
{
|
return WebResponseContent.Instance.Error("未找到订单明细信息");
|
}
|
|
List<PlacedBlock> placedBlocks = new List<PlacedBlock>();
|
|
string putPosition = "";
|
|
for (int i = 0; i < orderDetails.Count; i++)
|
{
|
try
|
{
|
lock (placedBlocks)
|
{
|
int length = Convert.ToInt32(orderDetails[i].Orderdetails_length);
|
int width = Convert.ToInt32(orderDetails[i].Orderdetails_width);
|
int height = Convert.ToInt32(orderDetails[i].Orderdetails_thickness);
|
|
if (length < 300 || width < 50)
|
{
|
continue;
|
}
|
|
OrderInfo orderInfo = _orderDetailsService.GetOrderInfoByBarcode(orderDetails[i].Orderdetails_outid);
|
|
var (flag, taskPosition, message) = _containerService.GetPosition(orderInfo.OrderHeadId, orderInfo.OrderNo, length, width, height);
|
if (flag && taskPosition != null)
|
{
|
if (string.IsNullOrEmpty(putPosition))
|
{
|
putPosition = taskPosition.PutPosition;
|
}
|
|
if (putPosition != taskPosition.PutPosition)
|
{
|
continue;
|
}
|
|
Dt_OrderContainer orderContainer = _orderContainerRepository.QueryFirst(x => x.OrderNo == orderInfo.OrderNo && x.ContainerCode == taskPosition.PutPosition);
|
|
Dt_Container putContainer = _containerRepository.QueryFirst(x => x.ContainerCode == taskPosition.PutPosition);
|
if (putContainer == null)
|
{
|
//todo
|
throw new Exception("未找到放货位置");
|
}
|
|
bool isAdd = false;
|
if (orderContainer == null)
|
{
|
orderContainer = new Dt_OrderContainer()
|
{
|
OrderId = orderInfo.OrderHeadId,
|
ContainerCode = putContainer.ContainerCode,
|
ContainerId = putContainer.Id,
|
OrderNo = orderInfo.OrderNo,
|
};
|
orderContainer.MaxLength = putContainer.ContainerLength;
|
isAdd = true;
|
}
|
|
if (taskPosition != null)
|
{
|
Dt_ContainerItem dt_ContainerItem = new Dt_ContainerItem()
|
{
|
ContainerId = putContainer.Id,
|
ItemCode = orderInfo.Barcode,
|
ItemLength = length,
|
ItemWidth = width,
|
ItemHeight = height,
|
ItemPositionX = taskPosition.PositionX,
|
ItemPositionY = taskPosition.PositionY,
|
ItemPositionZ = taskPosition.PositionZ,
|
ItemRelaPositionX = taskPosition.PutPositionX,
|
ItemRelaPositionY = taskPosition.PutPositionY,
|
ItemRelaPositionZ = taskPosition.PutPositionZ,
|
ItemStatus = (int)ItemStatusEnum.Assigned,
|
ItemName = orderInfo.Barcode
|
};
|
|
Dt_Task dt_Task = new Dt_Task()
|
{
|
PalletCode = orderInfo.Barcode,
|
DeviceCode = putContainer.DeviceCode,
|
TaskState = (int)TaskStatusEnum.Gantry_Wait,
|
TaskType = 0,
|
SourceAddress = "",
|
TargetAddress = $"F06",
|
CurrentAddress = $"F06*{taskPosition.TakePositionX}*{taskPosition.TakePositionY}*{taskPosition.TakePositionZ}*{taskPosition.PositionR}",
|
NextAddress = $"{putContainer.ContainerCode}*{taskPosition.PutPositionX}*{taskPosition.PutPositionY}*{taskPosition.PutPositionZ}*{taskPosition.PositionR}",
|
ItemInfo = $"{length}*{width}*{height}",
|
Grade = 0,
|
};
|
putContainer.ContainerStatus = ContainerStatusEnum.NonEmpty.ObjToInt();
|
_unitOfWorkManage.BeginTran();
|
BaseDal.AddData(dt_Task);
|
_containerRepository.UpdateData(putContainer);
|
_containerItemRepository.AddData(dt_ContainerItem);
|
if (isAdd)
|
{
|
_orderContainerRepository.AddData(orderContainer);
|
}
|
_unitOfWorkManage.CommitTran();
|
|
PlacedBlock placedBlock = new PlacedBlock(new Point3D(taskPosition.PositionX, taskPosition.PositionY, taskPosition.PositionZ), length > width ? length : width, width > length ? length : width, height);
|
placedBlocks.Add(placedBlock);
|
|
object obj = new
|
{
|
x = taskPosition.PutCenterPositionX - putContainer.ContainerLength / 2,
|
y = taskPosition.PutCenterPositionY - putContainer.ContainerWidth / 2,
|
z = taskPosition.PutCenterPositionZ,
|
length,
|
width,
|
height,
|
};
|
_webSocketServer.PublishAllClientPayload(obj.Serialize());
|
}
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
|
}
|
}
|
return WebResponseContent.Instance.OK(data: placedBlocks);
|
}
|
catch (Exception ex)
|
{
|
return WebResponseContent.Instance.Error(ex.Message);
|
}
|
}
|
|
public WebResponseContent CreateTask(string takePosition, string putPosition, string deviceCode, int length, int width, int height)
|
{
|
try
|
{
|
Dt_Container takeContainer = _containerRepository.QueryFirst(x => x.ContainerCode == takePosition && x.ContainerType == ContainerTypeEnum.TakeContainer.ObjToInt());
|
if (takeContainer == null)
|
{
|
return WebResponseContent.Instance.Error("取货位置不存在");
|
}
|
|
List<Dt_Container> containers = _containerRepository.QueryData(x => x.DeviceCode == deviceCode && x.ContainerType == ContainerTypeEnum.PutContainer.ObjToInt());
|
|
Dt_Container putContainer = _containerRepository.QueryFirst(x => x.ContainerCode == putPosition && x.ContainerType == ContainerTypeEnum.PutContainer.ObjToInt());
|
if (putContainer == null)
|
{
|
return WebResponseContent.Instance.Error("放货位置不存在");
|
}
|
|
int edge = 0;
|
if (putContainer.ContainerNo == containers.Min(x => x.ContainerNo))
|
{
|
edge = 1;
|
}
|
|
ContainerSize containerSize = new ContainerSize(putContainer.ContainerLength, putContainer.ContainerWidth, putContainer.ContainerHeight);
|
List<Dt_ContainerItem> containerItems = _containerItemRepository.QueryData(x => x.ContainerId == putContainer.Id);
|
|
List<PlacedBlock> placedBlocks = containerItems.Select(x => new PlacedBlock(new Point3D(x.ItemPositionX, x.ItemPositionY, x.ItemPositionZ), x.ItemLength > x.ItemLength ? x.ItemLength : x.ItemWidth, x.ItemLength > x.ItemLength ? x.ItemWidth : x.ItemLength, x.ItemHeight)).ToList();
|
|
|
TaskPosition? taskPosition = _containerService.GetTaskPosition(length, width, height, containerSize, placedBlocks, edge);
|
if (taskPosition == null)
|
{
|
return WebResponseContent.Instance.Error("未找到合适放置位置");
|
}
|
|
object obj = new
|
{
|
x = taskPosition.PutCenterPositionX - putContainer.ContainerLength / 2,
|
y = taskPosition.PutCenterPositionY - putContainer.ContainerWidth / 2,
|
z = taskPosition.PutCenterPositionZ,
|
length,
|
width,
|
height,
|
};
|
|
_webSocketServer.PublishAllClientPayload(obj.Serialize());
|
|
string code = DateTime.Now.ToString("yyyyMMddHHmmss");
|
|
Dt_ContainerItem dt_ContainerItem = new Dt_ContainerItem()
|
{
|
ContainerId = putContainer.Id,
|
ItemCode = code,
|
ItemLength = length,
|
ItemWidth = width,
|
ItemHeight = height,
|
ItemPositionX = taskPosition.PositionX,
|
ItemPositionY = taskPosition.PositionY,
|
ItemPositionZ = taskPosition.PositionZ,
|
ItemRelaPositionX = taskPosition.PutPositionX,
|
ItemRelaPositionY = taskPosition.PutPositionY,
|
ItemRelaPositionZ = taskPosition.PutPositionZ,
|
ItemStatus = (int)ItemStatusEnum.Assigned,
|
ItemName = code
|
};
|
|
Dt_Task dt_Task = new Dt_Task()
|
{
|
PalletCode = code,
|
DeviceCode = putContainer.DeviceCode,
|
TaskState = (int)TaskStatusEnum.Gantry_New,
|
TaskType = 0,
|
SourceAddress = "",
|
TargetAddress = $"{takeContainer.ContainerNo}",
|
CurrentAddress = $"{takeContainer.ContainerCode}*{taskPosition.TakePositionX}*{taskPosition.TakePositionY}*{taskPosition.TakePositionZ}*{taskPosition.PositionR}",
|
NextAddress = $"{putContainer.ContainerCode}*{taskPosition.PutPositionX}*{taskPosition.PutPositionY}*{taskPosition.PutPositionZ}*{taskPosition.PositionR}",
|
ItemInfo = $"{length}*{width}*{height}",
|
Grade = 0,
|
};
|
|
_containerItemRepository.AddData(dt_ContainerItem);
|
base.AddData(dt_Task);
|
|
return WebResponseContent.Instance.OK();
|
}
|
catch (Exception ex)
|
{
|
return WebResponseContent.Instance.Error(ex.Message);
|
}
|
}
|
|
public (bool, Dt_Task?, string) GenerateTask(OrderInfo orderInfo)
|
{
|
try
|
{
|
int length = Convert.ToInt32(orderInfo.Length);
|
int width = Convert.ToInt32(orderInfo.Width);
|
int height = Convert.ToInt32(orderInfo.Thickness);
|
|
var (flag, taskPosition, message) = _containerService.GetPosition(orderInfo.OrderHeadId, orderInfo.OrderNo, length, width, height);
|
if (flag && taskPosition != null)
|
{
|
Dt_Container putContainer = _containerRepository.QueryFirst(x => x.ContainerCode == taskPosition.PutPosition);
|
if (putContainer == null)
|
{
|
return (false, null, "未找到放货位置");
|
}
|
|
Dt_Container? takeContainer = GetTakePosition(putContainer.DeviceCode);
|
if (takeContainer == null)
|
{
|
return (false, null, "未找到取货位置");
|
}
|
|
Dt_OrderContainer orderContainer = _orderContainerRepository.QueryFirst(x => x.OrderNo == orderInfo.OrderNo && x.ContainerCode == putContainer.ContainerCode);
|
|
bool isAdd = false;
|
bool isUpdate = false;
|
if (orderContainer == null)
|
{
|
orderContainer = new Dt_OrderContainer()
|
{
|
OrderId = orderInfo.OrderHeadId,
|
ContainerCode = putContainer.ContainerCode,
|
ContainerId = putContainer.Id,
|
OrderNo = orderInfo.OrderNo,
|
};
|
orderContainer.MaxLength = putContainer.ContainerLength;
|
isAdd = true;
|
}
|
|
Dt_ContainerItem dt_ContainerItem = new Dt_ContainerItem()
|
{
|
ContainerId = putContainer.Id,
|
ItemCode = orderInfo.Barcode,
|
ItemLength = length,
|
ItemWidth = width,
|
ItemHeight = height,
|
ItemPositionX = taskPosition.PositionX,
|
ItemPositionY = taskPosition.PositionY,
|
ItemPositionZ = taskPosition.PositionZ,
|
ItemRelaPositionX = taskPosition.PutPositionX,
|
ItemRelaPositionY = taskPosition.PutPositionY,
|
ItemRelaPositionZ = taskPosition.PutPositionZ,
|
ItemStatus = (int)ItemStatusEnum.Assigned,
|
ItemName = orderInfo.Barcode,
|
Remark = $"{takeContainer.ContainerCode}*{taskPosition.TakePositionX}*{taskPosition.TakePositionY}*{taskPosition.TakePositionZ}*{taskPosition.PositionR}"
|
};
|
|
Dt_Task dt_Task = new Dt_Task()
|
{
|
PalletCode = orderInfo.Barcode,
|
DeviceCode = putContainer.DeviceCode,
|
TaskState = (int)TaskStatusEnum.Gantry_Wait,
|
TaskType = 0,
|
SourceAddress = "",
|
TargetAddress = $"{takeContainer.ContainerNo}",
|
CurrentAddress = $"{takeContainer.ContainerCode}*{taskPosition.TakePositionX}*{taskPosition.TakePositionY}*{taskPosition.TakePositionZ}*{taskPosition.PositionR}",
|
NextAddress = $"{putContainer.ContainerCode}*{taskPosition.PutPositionX}*{taskPosition.PutPositionY}*{taskPosition.PutPositionZ}*{taskPosition.PositionR}",
|
ItemInfo = $"{length}*{width}*{height}",
|
Grade = 0,
|
};
|
putContainer.ContainerStatus = ContainerStatusEnum.NonEmpty.ObjToInt();
|
_unitOfWorkManage.BeginTran();
|
BaseDal.AddData(dt_Task);
|
_containerRepository.UpdateData(putContainer);
|
_containerItemRepository.AddData(dt_ContainerItem);
|
if (isAdd)
|
{
|
_orderContainerRepository.AddData(orderContainer);
|
}
|
else if (isUpdate)
|
{
|
_orderContainerRepository.UpdateData(orderContainer);
|
}
|
_unitOfWorkManage.CommitTran();
|
|
return (true, dt_Task, "");
|
}
|
else
|
{
|
return (false, null, $"无可放置位置,{orderInfo.Serialize()}");
|
}
|
}
|
catch (Exception ex)
|
{
|
_unitOfWorkManage.RollbackTran();
|
return (false, null, $"错误,{ex.ExceptionToString()}");
|
}
|
}
|
|
public Dt_Container? GetTakePosition(string deviceCode)
|
{
|
Dictionary<string, OrderByType> orderby = new Dictionary<string, OrderByType>() { { nameof(Dt_Container.ContainerSort), OrderByType.Asc } };
|
List<Dt_Container> containers = _containerRepository.QueryData(x => x.ContainerType == ContainerTypeEnum.TakeContainer.ObjToInt() && x.DeviceCode == deviceCode && x.ContainerEnable, orderby);
|
|
try
|
{
|
if (containers == null || containers.Count == 0)
|
{
|
throw new Exception($"未找到对应的取货位置,设备编号:{deviceCode}");
|
}
|
|
Dictionary<string, OrderByType> taskOrderby = new Dictionary<string, OrderByType>() { { nameof(Dt_Task.TaskNum), OrderByType.Desc } };
|
Dt_Task task = BaseDal.QueryFirst(x => x.DeviceCode == deviceCode, taskOrderby);
|
if (task != null)
|
{
|
string? sourceCode = task.SourceAddress.Split("*").FirstOrDefault();
|
if (!string.IsNullOrEmpty(sourceCode))
|
{
|
Dt_Container? container = containers.FirstOrDefault(x => x.ContainerCode == sourceCode);
|
if (container != null)
|
{
|
int index = containers.IndexOf(container);
|
|
if (index + 1 < containers.Count)
|
{
|
return containers[index + 1];
|
}
|
else
|
{
|
return containers.FirstOrDefault();
|
}
|
}
|
}
|
}
|
}
|
catch (Exception ex)
|
{
|
|
}
|
return containers.FirstOrDefault();
|
}
|
|
public WebResponseContent TaskComplete(Dt_Task task)
|
{
|
try
|
{
|
task.TaskState = TaskStatusEnum.Gantry_Completed.ObjToInt();
|
_unitOfWorkManage.BeginTran();
|
OrderDetails orderDetails = _orderDetailsRepository.QueryFirst(x => x.Orderdetails_outid == task.PalletCode);
|
if (orderDetails != null)
|
{
|
orderDetails.Orderdetails_status = PalletingStatusEnmu.PalletingSuccess.ObjToInt();
|
|
Orderrows orderrows = _orderrowsRepository.QueryFirst(x => x.id == orderDetails.Orderrowsid);
|
if (orderrows != null)
|
{
|
orderrows.Orderrows_PalletNum = orderrows.Orderrows_PalletNum ?? 0 + 1;
|
|
Dt_Container container = _containerRepository.QueryFirst(x => task.NextAddress.Contains(x.ContainerCode));
|
|
_orderDetailsRepository.UpdateData(orderDetails);
|
_orderrowsRepository.UpdateData(orderrows);
|
|
try
|
{
|
List<string> containerItemCodes = _containerItemRepository.QueryData(x => x.ContainerId == container.Id).Select(x => x.ItemCode).ToList();
|
|
List<OrderDetails> totalDetails = _orderDetailsRepository.QueryData(x => x.Orderrowsid == orderrows.id);
|
List<OrderDetails> details = totalDetails.Where(x => x.Orderrowsid == orderrows.id && x.Orderdetails_status == PalletingStatusEnmu.PalletingSuccess.ObjToInt() && containerItemCodes.Contains(x.Orderdetails_outid)).ToList();
|
|
int sortedNum = totalDetails.Where(x => x.Orderrowsid == orderrows.id && x.Orderdetails_status == PalletingStatusEnmu.PalletingSuccess.ObjToInt()).Count();
|
object data = new object();
|
if (totalDetails.Count <= sortedNum)
|
{
|
data = new
|
{
|
stationCode = task.NextAddress.Split("*").FirstOrDefault(),
|
release = 1,
|
data = new
|
{
|
stationCode = task.NextAddress.Split("*").FirstOrDefault(),
|
orderTotalNum = totalDetails.Count,
|
sortedNum = sortedNum,
|
unsortedNum = totalDetails.Count - sortedNum,
|
stationSortedNum = details.Count,
|
orderId = orderrows.id,
|
orderData = new
|
{
|
orderCode = orderrows.Orderrows_orderid,
|
cusName = orderrows.Orderrows_customer,
|
orderName = orderrows.Orderrows_name,
|
batch = orderrows.Orderrows_batchid,
|
productName = "",
|
}
|
}
|
};
|
}
|
else
|
{
|
data = new
|
{
|
stationCode = task.NextAddress.Split("*").FirstOrDefault(),
|
release = 0,
|
data = new
|
{
|
stationCode = task.NextAddress.Split("*").FirstOrDefault(),
|
orderTotalNum = totalDetails.Count,
|
sortedNum = sortedNum,
|
unsortedNum = totalDetails.Count - sortedNum,
|
stationSortedNum = details.Count,
|
orderId = orderrows.id,
|
orderData = new
|
{
|
orderCode = orderrows.Orderrows_orderid,
|
cusName = orderrows.Orderrows_customer,
|
orderName = orderrows.Orderrows_name,
|
batch = orderrows.Orderrows_batchid,
|
productName = "",
|
}
|
}
|
};
|
}
|
List<WebSocketSession> webSocketSessions2 = _webSocketServer.OnlineSessions.ToList();
|
List<WebSocketSession> webSocketSessions = _webSocketServer.OnlineSessions.Where(x => x.Url.Contains(task.DeviceCode ?? "")).ToList();
|
foreach (var item in webSocketSessions)
|
{
|
_webSocketServer.SendClientPayload(item, data.Serialize());
|
}
|
}
|
catch (Exception ex)
|
{
|
|
}
|
}
|
}
|
|
BaseDal.DeleteAndMoveIntoHty(task, App.User?.UserId > 0 ? OperateTypeEnum.人工完成 : OperateTypeEnum.自动完成);
|
_unitOfWorkManage.CommitTran();
|
return WebResponseContent.Instance.OK();
|
}
|
catch (Exception ex)
|
{
|
_unitOfWorkManage.RollbackTran();
|
return WebResponseContent.Instance.Error(ex.Message);
|
}
|
}
|
}
|
}
|