using SqlSugar;
using WIDESEA_Common.CommonEnum;
using WIDESEA_Common.LocationEnum;
using WIDESEA_Common.OtherEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Core;
using WIDESEA_DTO.ToMes;
using WIDESEA_Model.Models;
namespace WIDESEA_TaskInfoService
{
public partial class TaskService
{
///
/// MES下发库位调拨任务
///
/// 库位调拨任务信息
///
public ApiResponse sendAllocationTask(AllocationTaskReceived allocationTask)
{
try
{
// 参数验证
if (allocationTask == null)
return MESresponse("调拨任务信息不能为空", false);
if (string.IsNullOrEmpty(allocationTask.palletCode))
return MESresponse("托盘编码不能为空", false);
if (string.IsNullOrEmpty(allocationTask.sourceLocationCode))
return MESresponse("源货位编码不能为空", false);
if (string.IsNullOrEmpty(allocationTask.locationCode))
return MESresponse("目标货位编码不能为空", false);
_unitOfWorkManage.BeginTran();
// 验证托盘是否存在
var stockInfo = _stockRepository.QueryFirst(x => x.PalletCode == allocationTask.palletCode);
if (stockInfo == null)
{
_unitOfWorkManage.RollbackTran();
return MESresponse($"未找到托盘信息,托盘编码:{allocationTask.palletCode}", false);
}
// 验证源货位
var sourceLocation = _basicService.LocationInfoService.Repository
.QueryFirst(x => x.LocationCode == allocationTask.sourceLocationCode);
if (sourceLocation == null)
{
_unitOfWorkManage.RollbackTran();
return MESresponse($"未找到源货位信息,货位编码:{allocationTask.sourceLocationCode}", false);
}
// 验证目标货位
var targetLocation = _basicService.LocationInfoService.Repository
.QueryFirst(x => x.LocationCode == allocationTask.locationCode && x.LocationStatus == (int)LocationStatusEnum.Free);
if (targetLocation == null)
{
_unitOfWorkManage.RollbackTran();
return MESresponse($"未找到目标货位信息,货位编码:{allocationTask.locationCode}或货位为其他状态", false);
}
// 验证托盘当前位置是否与源货位一致
if (stockInfo.LocationCode != allocationTask.sourceLocationCode)
{
_unitOfWorkManage.RollbackTran();
return MESresponse($"托盘当前位置与源货位不一致,托盘编码:{allocationTask.palletCode},当前位置:{stockInfo.LocationCode},源货位:{allocationTask.sourceLocationCode}", false);
}
// 验证源货位状态
if (sourceLocation.LocationStatus != (int)LocationStatusEnum.InStock)
{
_unitOfWorkManage.RollbackTran();
return MESresponse($"源货位状态异常,货位编码:{allocationTask.sourceLocationCode},当前状态:{sourceLocation.LocationStatus}", false);
}
// 验证目标货位状态
if (targetLocation.LocationStatus != (int)LocationStatusEnum.Free)
{
_unitOfWorkManage.RollbackTran();
return MESresponse($"目标货位不为空闲状态,货位编码:{allocationTask.locationCode},当前状态:{targetLocation.LocationStatus}", false);
}
// 检查是否已存在相同的调拨任务
var existingTask = BaseDal.QueryFirst(x =>
x.PalletCode == allocationTask.palletCode &&
x.SourceAddress == allocationTask.sourceLocationCode &&
x.TargetAddress == allocationTask.locationCode &&
x.TaskType == (int)TaskRelocationTypeEnum.Relocation &&
x.TaskStatus < (int)TaskRelocationStatusEnum.RelocationFinish);
if (existingTask != null)
{
_unitOfWorkManage.RollbackTran();
return MESresponse($"已存在相同的调拨任务,托盘编码:{allocationTask.palletCode},任务号:{existingTask.TaskNum}", false);
}
// 锁定货位状态
sourceLocation.LocationStatus = (int)LocationStatusEnum.InStockLock;
targetLocation.LocationStatus = (int)LocationStatusEnum.InStockLock;
// 更新库存状态
stockInfo.StockStatus = (int)StockStatusEmun.调拨中;
List dt_Tasks = new List();
// 创建调拨任务
Dt_Task allocationTaskEntity = new Dt_Task
{
PalletCode = allocationTask.palletCode,
TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
Roadway = sourceLocation.RoadwayNo,
TaskType = (int)TaskRelocationTypeEnum.Relocation,
TaskStatus = (int)TaskRelocationStatusEnum.RelocationNew,
SourceAddress = allocationTask.sourceLocationCode,
TargetAddress = allocationTask.locationCode,
CurrentAddress = allocationTask.sourceLocationCode,
NextAddress = allocationTask.locationCode,
Grade = 2, // 默认优先级
Creater = "MES",
Depth = targetLocation.Depth,
CreateDate = DateTime.Now,
MEStaskId = allocationTask.taskId,
MESbusinessId = allocationTask.businessId,
MESsubPalletCode = "", // 新结构中没有此字段
Remark = "MES库位调拨任务"
};
dt_Tasks.Add(allocationTaskEntity);
// 批量更新数据
_basicService.LocationInfoService.Repository.UpdateData(sourceLocation);
_basicService.LocationInfoService.Repository.UpdateData(targetLocation);
_stockRepository.UpdateData(stockInfo);
BaseDal.AddData(allocationTaskEntity);
var respon = PushTasksToWCS(dt_Tasks, "");
if (respon.Status)
{
_unitOfWorkManage.CommitTran(); //提交事务
return MESresponse("", true);
}
else
{
_unitOfWorkManage.RollbackTran(); //回滚事务
return MESresponse($"下发库位调拨失败,原因:{respon.Message}!", false);
}
}
catch (Exception ex)
{
_unitOfWorkManage.RollbackTran();
// 记录错误日志
Console.WriteLine($"MES库位调拨任务创建失败: {ex.Message}");
return MESresponse($"调拨任务创建失败:{ex.Message}", false);
}
}
///
/// MES响应格式化
///
/// 消息
/// 是否成功
/// 数据
///
private ApiResponse MESresponse(string message, bool success, object data = null)
{
if (success)
{
return ApiResponse.SuccessResponse(message, data);
}
else
{
return ApiResponse.ErrorResponse(message);
}
}
}
}