using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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_Core.Helper;
using WIDESEA_DTO.Task;
using WIDESEA_Model.Models;
namespace WIDESEA_TaskInfoService
{
public partial class TaskService
{
///
/// 判断是否需要移库
///
///
///
///
public WebResponseContent IsRelocation(int taskNum, string locationCode)
{
try
{
Dt_LocationInfo locationInfo = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == locationCode);
if (locationInfo == null)
{
return WebResponseContent.Instance.Error($"未找到对应的货位信息");
}
Dt_Task task = Repository.QueryFirst(x => x.TaskNum == taskNum);
if (task == null)
{
return WebResponseContent.Instance.Error($"未找到任务信息");
}
List littleDepthLocations = _basicService.LocationInfoService.GetGroupLocations(locationInfo);
Dt_LocationInfo? needRelocationItem = littleDepthLocations.Where(x => x.LocationStatus != LocationStatusEnum.Free.ObjToInt() && x.LocationStatus != LocationStatusEnum.PalletLock.ObjToInt() && x.LocationStatus != LocationStatusEnum.FreeLock.ObjToInt() && x.Depth < locationInfo.Depth).OrderBy(x => x.Depth).FirstOrDefault();
if (needRelocationItem == null)
{
return WebResponseContent.Instance.OK(data: _mapper.Map(task));
}
else
{
Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.QueryFirst(x => x.LocationCode == needRelocationItem.LocationCode);
if (stockInfo == null)
{
return WebResponseContent.Instance.Error($"未找到对应货位的库存信息");
}
Dt_Task taskTemp = Repository.QueryFirst(x => x.PalletCode == stockInfo.PalletCode);
if (taskTemp != null)
{
return WebResponseContent.Instance.OK(data: _mapper.Map(taskTemp));
}
int heightType = 0;
if(needRelocationItem.Layer == 5)
{
heightType = 3;
}
else if(needRelocationItem.Layer == 4)
{
heightType = 2;
}
else if(needRelocationItem.Layer <= 3 && needRelocationItem.Layer >= 1)
{
heightType = 1;
}
Dt_LocationInfo? newLocation = _basicService.LocationInfoService.AssignLocation(needRelocationItem.RoadwayNo, task.PalletType, task.WarehouseId, needRelocationItem.LocationCode, heightType);
if (newLocation != null)
{
Dt_Task newTask = new Dt_Task()
{
Roadway = task.Roadway,
WarehouseId = task.WarehouseId,
CurrentAddress = locationCode,
Grade = 99,
NextAddress = newLocation.LocationCode,
OrderNo = "",
PalletCode = stockInfo.PalletCode,
PalletType = stockInfo.PalletType,
SourceAddress = needRelocationItem.LocationCode,
TargetAddress = newLocation.LocationCode,
TaskNum = Repository.GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
TaskStatus = 0,
TaskType = TaskTypeEnum.Relocation.ObjToInt()
};
_unitOfWorkManage.BeginTran();
Repository.AddData(newTask);
{
LocationStatusEnum lastStatus = (LocationStatusEnum)newLocation.LocationStatus;
_basicService.LocationInfoService.UpdateLocationStatus(newLocation, task.PalletType, LocationStatusEnum.Lock, stockInfo.WarehouseId);
_recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(newLocation, lastStatus, LocationStatusEnum.Lock, LocationChangeType.RelocationAssignLocation, taskNum: newTask.TaskNum);
}
{
LocationStatusEnum lastStatus = (LocationStatusEnum)needRelocationItem.LocationStatus;
_basicService.LocationInfoService.UpdateLocationStatus(needRelocationItem, task.PalletType, LocationStatusEnum.Lock, stockInfo.WarehouseId);
_recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(needRelocationItem, lastStatus, LocationStatusEnum.Lock, LocationChangeType.RelocationAssignLocation, taskNum: newTask.TaskNum);
}
stockInfo.StockStatus = StockStatusEmun.移库锁定.ObjToInt();
_stockRepository.StockInfoRepository.UpdateData(stockInfo);
_unitOfWorkManage.CommitTran();
return WebResponseContent.Instance.OK(data: _mapper.Map(newTask));
}
else
{
return WebResponseContent.Instance.Error($"未找到可分配货位,请查看该巷道【{task.Roadway}】是否还有空闲可使用货位");
}
}
}
catch (Exception ex)
{
_unitOfWorkManage.RollbackTran();
return WebResponseContent.Instance.Error(ex.Message);
}
}
public WebResponseContent RelocationTaskCompleted(Dt_Task task)
{
try
{
Dt_StockInfo stockInfo = _stockService.StockInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress);
if (stockInfo == null)
{
return WebResponseContent.Instance.Error($"未找到对应货位的库存信息");
}
Dt_LocationInfo locationInfoStart = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.SourceAddress);
if (locationInfoStart == null)
{
return WebResponseContent.Instance.Error($"未找到对应的起点货位信息");
}
Dt_LocationInfo locationInfoEnd = _basicService.LocationInfoService.Repository.QueryFirst(x => x.LocationCode == task.TargetAddress);
if (locationInfoEnd == null)
{
return WebResponseContent.Instance.Error($"未找到对应的终点货位信息");
}
task.TaskStatus = TaskStatusEnum.Finish.ObjToInt();
FeedBackWCSTaskCompleted(task.TaskNum);
_unitOfWorkManage.BeginTran();
stockInfo.LocationCode = locationInfoEnd.LocationCode;
stockInfo.StockStatus = StockStatusEmun.入库完成.ObjToInt();
_stockService.StockInfoService.Repository.UpdateData(stockInfo);
_basicService.LocationInfoService.UpdateLocationStatus(locationInfoStart, stockInfo.PalletType, LocationStatusEnum.Free, stockInfo.WarehouseId);
_basicService.LocationInfoService.UpdateLocationStatus(locationInfoEnd, stockInfo.PalletType, LocationStatusEnum.InStock, stockInfo.WarehouseId);
BaseDal.DeleteAndMoveIntoHty(task, App.User.UserId > 0 ? WIDESEA_Core.Enums.OperateTypeEnum.人工完成 : WIDESEA_Core.Enums.OperateTypeEnum.自动完成);
_unitOfWorkManage.CommitTran();
return WebResponseContent.Instance.OK();
}
catch (Exception ex)
{
_unitOfWorkManage.RollbackTran();
return WebResponseContent.Instance.Error(ex.Message);
}
}
}
}