using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
|
using SqlSugar;
|
using System;
|
using System.Collections;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Text.RegularExpressions;
|
using System.Threading.Tasks;
|
using WIDESEA_Common.CommonEnum;
|
using WIDESEA_Common.LocationEnum;
|
using WIDESEA_Common.OrderEnum;
|
using WIDESEA_Common.StockEnum;
|
using WIDESEA_Common.TaskEnum;
|
using WIDESEA_Common.WareHouseEnum;
|
using WIDESEA_Core;
|
using WIDESEA_Core.Helper;
|
using WIDESEA_DTO;
|
using WIDESEA_DTO.Task;
|
using WIDESEA_Model.Models;
|
|
namespace WIDESEA_TaskInfoService
|
{
|
public partial class TaskService
|
{
|
/// <summary>
|
/// 仅申请任务,让WCS根据路由确定下一地址
|
/// </summary>
|
/// <param name="stationCode"></param>
|
/// <param name="palletCode"></param>
|
/// <returns></returns>
|
public WebResponseContent DeviceRequestInboundTaskSimple(string stationCode, string palletCode)
|
{
|
try
|
{
|
Dt_Task task = Repository.QueryFirst(x => x.PalletCode == palletCode);
|
if (task != null)
|
{
|
PushTasksToWCS(new List<Dt_Task> { task });
|
return WebResponseContent.Instance.OK($"该托盘已生成任务", _mapper.Map<WMSTaskDTO>(task));
|
}
|
|
if (Repository.QueryFirst(x => x.SourceAddress == stationCode && x.TaskStatus == TaskStatusEnum.New.ObjToInt()) != null)
|
{
|
return WebResponseContent.Instance.Error($"该站点已有未执行的任务");
|
}
|
|
Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == palletCode);
|
if (stockInfo == null)
|
{
|
return WebResponseContent.Instance.Error($"未找到组盘信息");
|
}
|
if (stockInfo.StockStatus != StockStatusEmun.退库.ObjToInt() && stockInfo.StockStatus != StockStatusEmun.MES退库.ObjToInt() && stockInfo.StockStatus != StockStatusEmun.MES空托退库.ObjToInt())
|
{
|
return WebResponseContent.Instance.Error($"该托盘状态不正确,不可申请入库");
|
}
|
if (!string.IsNullOrEmpty(stockInfo.LocationCode))
|
{
|
return WebResponseContent.Instance.Error($"该托盘已绑定货位");
|
}
|
|
Dt_Task newTask = new Dt_Task()
|
{
|
CurrentAddress = stationCode,
|
Grade = 0,
|
NextAddress = "",
|
PalletCode = palletCode,
|
Roadway = "",
|
SourceAddress = stationCode,
|
TargetAddress = "",
|
TaskType = TaskTypeEnum.Inbound.ObjToInt(),
|
TaskStatus = TaskStatusEnum.New.ObjToInt(),
|
WarehouseId = stockInfo.WarehouseId,
|
PalletType = stockInfo.PalletType,
|
};
|
|
|
if (stockInfo.StockStatus == StockStatusEmun.手动组盘暂存.ObjToInt())
|
{
|
stockInfo.StockStatus = StockStatusEmun.手动组盘入库确认.ObjToInt();
|
}
|
else if (stockInfo.StockStatus == StockStatusEmun.MES退库.ObjToInt())
|
{
|
newTask.TaskType = TaskTypeEnum.MesMatReturn.ObjToInt();
|
}
|
else if (stockInfo.StockStatus == StockStatusEmun.MES空托退库.ObjToInt())
|
{
|
//todo 小托盘暂时未启用
|
if (stockInfo.PalletType == PalletTypeEnum.MediumPallet.ObjToInt())
|
{
|
newTask.TaskType = TaskTypeEnum.MesPalletLargeReturn.ObjToInt();
|
}
|
else
|
{
|
newTask.TaskType = TaskTypeEnum.MesPalletSmallReturn.ObjToInt();
|
}
|
}
|
else
|
{
|
stockInfo.StockStatus = StockStatusEmun.入库确认.ObjToInt();
|
}
|
|
_unitOfWorkManage.BeginTran();
|
int taskId = BaseDal.AddData(newTask);
|
newTask.TaskId = taskId;
|
_stockRepository.StockInfoRepository.UpdateData(stockInfo);
|
_unitOfWorkManage.CommitTran();
|
WMSTaskDTO wMSTaskDTO = _mapper.Map<WMSTaskDTO>(newTask);
|
|
PushTasksToWCS(new List<Dt_Task> { newTask });
|
if (newTask.WarehouseId == 5) PutFinish(stationCode, newTask.PalletCode, newTask.TaskNum.ToString());
|
return WebResponseContent.Instance.OK(data: wMSTaskDTO);
|
}
|
catch (Exception ex)
|
{
|
_unitOfWorkManage.RollbackTran();
|
return WebResponseContent.Instance.Error(ex.Message);
|
}
|
}
|
/// <summary>
|
/// 原料申请入库
|
/// </summary>
|
/// <param name="stationCode">起始站点</param>
|
/// <param name="palletCode">托盘</param>
|
/// <returns></returns>
|
public WebResponseContent RequestYLWMSTaskSimple(string stationCode, string palletCode)
|
{
|
Dt_Task task = Repository.QueryFirst(x => x.PalletCode == palletCode);
|
if (task != null)
|
{
|
PushTasksToWCS(new List<Dt_Task> { task });
|
return WebResponseContent.Instance.OK($"该托盘已生成任务", _mapper.Map<WMSTaskDTO>(task));
|
}
|
|
if (Repository.QueryFirst(x => x.SourceAddress == stationCode && x.TaskStatus == TaskStatusEnum.New.ObjToInt()) != null)
|
{
|
return WebResponseContent.Instance.Error($"该站点已有未执行的任务");
|
}
|
Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == palletCode);
|
if (stockInfo == null)
|
{
|
return WebResponseContent.Instance.Error($"未找到组盘信息");
|
}
|
if (stockInfo.StockStatus != StockStatusEmun.手动组盘暂存.ObjToInt())
|
{
|
return WebResponseContent.Instance.Error($"该托盘状态不正确,不可申请入库");
|
}
|
string rowWay = AssignYLRoadwayNo(stockInfo.PalletCode);
|
Dt_Task newTask = new Dt_Task()
|
{
|
CurrentAddress = stationCode,
|
Grade = 0,
|
NextAddress = "",
|
PalletCode = palletCode,
|
Roadway = rowWay,
|
SourceAddress = stationCode,
|
TargetAddress = "",
|
TaskType = TaskTypeEnum.Inbound.ObjToInt(),
|
TaskStatus = TaskStatusEnum.New.ObjToInt(),
|
WarehouseId = stockInfo.WarehouseId,
|
PalletType = stockInfo.PalletType,
|
};
|
|
if (stockInfo.StockStatus == StockStatusEmun.手动组盘暂存.ObjToInt())
|
{
|
stockInfo.StockStatus = StockStatusEmun.手动组盘入库确认.ObjToInt();
|
}
|
_unitOfWorkManage.BeginTran();
|
int taskId = BaseDal.AddData(newTask);
|
newTask.TaskId = taskId;
|
_stockRepository.StockInfoRepository.UpdateData(stockInfo);
|
_unitOfWorkManage.CommitTran();
|
PushTasksToWCS(new List<Dt_Task> { newTask });
|
WMSTaskDTO wMSTaskDTO = _mapper.Map<WMSTaskDTO>(newTask);
|
return WebResponseContent.Instance.OK(data: wMSTaskDTO);
|
}
|
public string AssignYLRoadwayNo(string palletCode)
|
{
|
try
|
{
|
Dt_StockInfo stockInfo = _stockRepository.StockInfoRepository.QueryFirst(x => x.PalletCode == palletCode);
|
|
Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseId == stockInfo.WarehouseId);
|
|
if (warehouse == null)
|
{
|
throw new Exception($"未找到巷道对应仓库信息");
|
}
|
|
string roadwayNo = "";
|
if (stockInfo.MaterielWide < 2200 && stockInfo.MaterielThickness > 800)
|
{
|
List<LocationCount> locationCounts = Db.Queryable<Dt_LocationInfo>().Where(x => x.WarehouseId == warehouse.WarehouseId && x.LocationType == LocationTypeEnum.Undefined.ObjToInt() && x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && (x.EnableStatus == EnableStatusEnum.OnlyIn.ObjToInt() || x.EnableStatus == EnableStatusEnum.Normal.ObjToInt()) && x.RoadwayNo.Contains("SC01_YL")).GroupBy(x => x.RoadwayNo).Select(x => new LocationCount { RoadwayNo = x.RoadwayNo, Count = SqlFunc.AggregateCount(x) }).ToList();
|
roadwayNo = locationCounts.OrderBy(x => x.Count).FirstOrDefault()?.RoadwayNo ?? "";
|
}
|
else if (stockInfo.MaterielWide>2200 && stockInfo.MaterielThickness>800)
|
{
|
List<LocationCount> locationCounts = Db.Queryable<Dt_LocationInfo>().Where(x => x.WarehouseId == warehouse.WarehouseId && x.LocationType == LocationTypeEnum.Undefined.ObjToInt() && x.LocationStatus == LocationStatusEnum.Free.ObjToInt() && (x.EnableStatus == EnableStatusEnum.OnlyIn.ObjToInt() || x.EnableStatus == EnableStatusEnum.Normal.ObjToInt()) && !x.RoadwayNo.Contains("SC01_YL")).GroupBy(x => x.RoadwayNo).Select(x => new LocationCount { RoadwayNo = x.RoadwayNo, Count = SqlFunc.AggregateCount(x) }).ToList();
|
roadwayNo = locationCounts.OrderBy(x => x.Count).FirstOrDefault()?.RoadwayNo ?? "";
|
}
|
return !string.IsNullOrEmpty(roadwayNo) ? (roadwayNo) : throw new Exception("未找到可分配巷道");
|
}
|
catch (Exception ex)
|
{
|
_unitOfWorkManage.RollbackTran();
|
throw new Exception(ex.Message);
|
}
|
}
|
/// <summary>
|
/// 入库任务申请分配货位
|
/// </summary>
|
/// <param name="taskNum">任务号</param>
|
/// <param name="roadwayNo">巷道号</param>
|
/// <returns></returns>
|
public WebResponseContent AssignInboundTaskLocation(int taskNum, string roadwayNo)
|
{
|
try
|
{
|
Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
|
if (task == null)
|
{
|
return WebResponseContent.Instance.Error($"未找到该入库任务");
|
}
|
|
if (_basicRepository.LocationInfoRepository.QueryFirst(x => x.LocationCode == task.TargetAddress) != null)
|
{
|
return WebResponseContent.Instance.OK(data: task.TargetAddress);
|
}
|
|
Dt_LocationInfo? locationInfo = _basicService.LocationInfoService.AssignLocation(roadwayNo, task.PalletType, task.WarehouseId);
|
if (locationInfo == null)
|
{
|
return WebResponseContent.Instance.Error($"货位分配失败,未找到可分配货位");
|
}
|
|
task.Roadway = roadwayNo;
|
task.TargetAddress = locationInfo.LocationCode;
|
if (task.Roadway.Contains("AGV"))
|
{
|
task.TaskStatus = TaskStatusEnum.AGV_Execute.ObjToInt();
|
}
|
else
|
{
|
task.TaskStatus = TaskStatusEnum.SC_Execute.ObjToInt();
|
}
|
|
|
LocationStatusEnum lastStatus = (LocationStatusEnum)locationInfo.LocationStatus;
|
|
_unitOfWorkManage.BeginTran();
|
_recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, lastStatus, LocationStatusEnum.Lock, LocationChangeType.InboundAssignLocation);
|
_basicService.LocationInfoService.UpdateLocationStatus(locationInfo, task.PalletType, LocationStatusEnum.Lock, task.WarehouseId);
|
BaseDal.UpdateData(task);
|
_unitOfWorkManage.CommitTran();
|
return WebResponseContent.Instance.OK(data: locationInfo.LocationCode);
|
}
|
catch (Exception ex)
|
{
|
_unitOfWorkManage.RollbackTran();
|
return WebResponseContent.Instance.Error(ex.Message);
|
}
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
/// <param name="taskNum"></param>
|
/// <param name="roadwayNo"></param>
|
/// <param name="heightType"></param>
|
/// <returns></returns>
|
public WebResponseContent AssignInboundTaskLocationByHeight(int taskNum, string roadwayNo, int heightType)
|
{
|
try
|
{
|
Dt_Task task = BaseDal.QueryFirst(x => x.TaskNum == taskNum);
|
if (task == null)
|
{
|
return WebResponseContent.Instance.Error($"未找到该入库任务");
|
}
|
|
if (_basicRepository.LocationInfoRepository.QueryFirst(x => x.LocationCode == task.TargetAddress) != null)
|
{
|
return WebResponseContent.Instance.OK(data: task.TargetAddress);
|
}
|
|
Dt_LocationInfo? locationInfo = _basicService.LocationInfoService.AssignLocation(roadwayNo, task.PalletType, task.WarehouseId, heightType: heightType);
|
if (locationInfo == null)
|
{
|
return WebResponseContent.Instance.Error($"货位分配失败,未找到可分配货位");
|
}
|
|
task.Roadway = roadwayNo;
|
task.TargetAddress = locationInfo.LocationCode;
|
task.TaskStatus = TaskStatusEnum.SC_Execute.ObjToInt();
|
|
LocationStatusEnum lastStatus = (LocationStatusEnum)locationInfo.LocationStatus;
|
|
_unitOfWorkManage.BeginTran();
|
_recordService.LocationStatusChangeRecordSetvice.AddLocationStatusChangeRecord(locationInfo, lastStatus, LocationStatusEnum.Lock, LocationChangeType.InboundAssignLocation);
|
_basicService.LocationInfoService.UpdateLocationStatus(locationInfo, task.PalletType, LocationStatusEnum.Lock, task.WarehouseId);
|
BaseDal.UpdateData(task);
|
_unitOfWorkManage.CommitTran();
|
return WebResponseContent.Instance.OK(data: locationInfo.LocationCode);
|
}
|
catch (Exception ex)
|
{
|
_unitOfWorkManage.RollbackTran();
|
return WebResponseContent.Instance.Error(ex.Message);
|
}
|
}
|
public readonly string[] OutStartPonits = { "5230", "5237", "5244" };
|
/// <summary>
|
/// 成品余料退库入仓
|
/// </summary>
|
/// <param name="barCode"></param>
|
/// <param name="startPoint"></param>
|
/// <returns></returns>
|
public WebResponseContent BackProductTask(string barCode, string startPoint)
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
string palletCode = @"^C\d{5}$"; // 正则表达式
|
bool isValid = Regex.IsMatch(barCode, palletCode);
|
if (!isValid)
|
{
|
return content.Error($"框码格式错误{barCode}");
|
}
|
if (!OutStartPonits.Contains(startPoint))
|
{
|
return content.Error($"起点错误{startPoint}");
|
}
|
//判断当前点位是否重复
|
Dt_Task taskOldPoint = BaseDal.QueryFirst(x=>x.SourceAddress==startPoint && (x.TaskStatus == TaskStatusEnum.New.ObjToInt()||x.TaskStatus==TaskStatusEnum.Line_Execute.ObjToInt()|| x.TaskStatus == TaskStatusEnum.Line_Executing.ObjToInt() || x.TaskStatus == TaskStatusEnum.AGV_Takeing.ObjToInt()));
|
if (taskOldPoint != null)
|
{
|
return content.Error($"站点{startPoint}已存在任务");
|
}
|
Dt_Task taskOld = BaseDal.QueryFirst(x => x.PalletCode == barCode);
|
if (taskOld != null)
|
{
|
return content.Error($"胶框{barCode}任务已存在");
|
}
|
//获取胶框余料
|
Dt_ProStockInfo proStockInfo = _stockRepository.ProStockInfoRepository.Db.Queryable<Dt_ProStockInfo>().Where(x=>x.PalletCode== barCode).Includes(x=>x.proStockInfoDetails).First();
|
if (proStockInfo==null)
|
{
|
return content.Error($"未找到{barCode}胶框信息");
|
}
|
if (proStockInfo.proStockInfoDetails==null || proStockInfo.proStockInfoDetails.Count<=0)
|
{
|
return content.Error($"{barCode}胶框信息为空");
|
}
|
if (proStockInfo.StockStatus!=StockStatusEmun.出库完成.ObjToInt())
|
{
|
return content.Error($"{barCode}胶框信息状态异常");
|
}
|
//获取成品平库
|
Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == WarehouseEnum.HA101.ToString());
|
if (proStockInfo.WarehouseId != warehouse.WarehouseId && proStockInfo.LocationCode!= "成品包装拣货区")
|
{
|
return content.Error($"{barCode}胶框信息不在拣货区");
|
}
|
//获取当前的库存属性
|
Dt_Warehouse? warePoint = null;
|
switch (proStockInfo.ProStockAttribute)
|
{
|
case (int)ProStockAttributeEnum.成品:
|
warePoint = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == WarehouseEnum.HA71.ToString());
|
break;
|
case (int)ProStockAttributeEnum.尾数:
|
warePoint = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == WarehouseEnum.HA72.ToString());
|
break;
|
case (int)ProStockAttributeEnum.研发:
|
warePoint = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == WarehouseEnum.HA73.ToString());
|
break;
|
default:
|
throw new Exception($"未找到{barCode}胶框信息库存属性");
|
}
|
Dt_Warehouse warehouseLocation = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == WarehouseEnum.HA71.ToString());
|
Dt_LocationInfo locationInfo = _basicRepository.LocationInfoRepository.QueryFirst(x => x.WarehouseId == warehouseLocation.WarehouseId);
|
if (locationInfo == null)
|
{
|
return content.Error($"未找到{warehouse.WarehouseCode}货位信息");
|
}
|
proStockInfo.StockStatus = StockStatusEmun.成品余料回库.ObjToInt();
|
proStockInfo.proStockInfoDetails.ForEach(x =>
|
{
|
x.ProOutDetailStatus = StockStatusEmun.成品余料回库.ObjToInt();
|
});
|
Dt_Task newTask = new Dt_Task()
|
{
|
CurrentAddress = startPoint,
|
Grade = 0,
|
NextAddress = "",
|
PalletCode = proStockInfo.PalletCode,
|
Roadway = locationInfo.RoadwayNo,
|
SourceAddress = startPoint,
|
TargetAddress = "",
|
TaskType = TaskTypeEnum.InProductBack.ObjToInt(),
|
TaskStatus = TaskStatusEnum.New.ObjToInt(),
|
WarehouseId = warePoint.WarehouseId,
|
PalletType = proStockInfo.PalletType,
|
MaterielCode = proStockInfo.proStockInfoDetails.Where(x => x.ProStockId == proStockInfo.Id).FirstOrDefault()?.ProductCode,
|
Quantity = (float)proStockInfo.proStockInfoDetails.Where(x => x.ProStockId == proStockInfo.Id).Sum(x => x.StockPcsQty)
|
};
|
_unitOfWorkManage.BeginTran();
|
int taskId = BaseDal.AddData(newTask);
|
newTask.TaskId = taskId;
|
_stockRepository.ProStockInfoRepository.UpdateData(proStockInfo);
|
_stockRepository.ProStockInfoDetailRepository.UpdateData(proStockInfo.proStockInfoDetails);
|
_unitOfWorkManage.CommitTran();
|
//推送任务
|
PushTasksToWCS(new List<Dt_Task> { newTask });
|
content.OK("发送成功");
|
}
|
catch (Exception ex)
|
{
|
_unitOfWorkManage.RollbackTran();
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
/// <summary>
|
/// 空框回库
|
/// </summary>
|
public WebResponseContent EmptyBackTask(string barCode, string startPoint)
|
{
|
WebResponseContent content = new WebResponseContent();
|
try
|
{
|
string palletCode = @"^C\d{5}$"; // 正则表达式
|
bool isValid = Regex.IsMatch(barCode, palletCode);
|
if (!isValid)
|
{
|
return content.Error($"框码格式错误{barCode}");
|
}
|
if (!OutStartPonits.Contains(startPoint))
|
{
|
return content.Error($"起点错误{startPoint}");
|
}
|
//判断当前点位是否重复
|
Dt_Task taskOldPoint = BaseDal.QueryFirst(x => x.SourceAddress == startPoint && (x.TaskStatus == TaskStatusEnum.New.ObjToInt() || x.TaskStatus == TaskStatusEnum.Line_Execute.ObjToInt() || x.TaskStatus == TaskStatusEnum.Line_Executing.ObjToInt()));
|
//if (taskOldPoint != null)
|
//{
|
// return content.Error($"站点{startPoint}已存在任务");
|
//}
|
Dt_Task taskOld = BaseDal.QueryFirst(x => x.PalletCode == barCode);
|
if (taskOld != null)
|
{
|
return content.Error($"胶框{barCode}任务已存在");
|
}
|
//if (_stockRepository.ProStockInfoRepository.QueryFirst(x => x.PalletCode == barCode) != null)
|
//{
|
// return content.Error($"胶框{barCode}库存信息已存在");
|
//}
|
//todo:临时解绑
|
Dt_ProStockInfo proStockInfo = _stockRepository.ProStockInfoRepository.QueryFirst(x => x.PalletCode == barCode);
|
if (_stockRepository.ProStockInfoRepository.QueryFirst(x => x.PalletCode == barCode) != null)
|
{
|
proStockInfo.PalletCode = proStockInfo.PalletCode + ":" + DateTime.Now.ToString("MM/dd");
|
}
|
//获取成品平库
|
Dt_Warehouse warehouse = _basicRepository.WarehouseRepository.QueryFirst(x => x.WarehouseCode == WarehouseEnum.HA101.ToString());
|
Dt_Task newTask = new Dt_Task()
|
{
|
CurrentAddress = startPoint,
|
Grade = 0,
|
NextAddress = "",
|
PalletCode = barCode,
|
Roadway = "CL01_CP",
|
SourceAddress = startPoint,
|
TargetAddress = "",
|
TaskType = TaskTypeEnum.EmptyProductBack.ObjToInt(),
|
TaskStatus = TaskStatusEnum.New.ObjToInt(),
|
WarehouseId = warehouse.WarehouseId,
|
PalletType = 1
|
};
|
_unitOfWorkManage.BeginTran();
|
int taskId = BaseDal.AddData(newTask);
|
if (proStockInfo!=null)
|
{
|
_stockRepository.ProStockInfoRepository.UpdateData(proStockInfo);
|
}
|
newTask.TaskId = taskId;
|
_unitOfWorkManage.CommitTran();
|
//推送任务
|
PushTasksToWCS(new List<Dt_Task> { newTask });
|
content.OK("发送成功");
|
}
|
catch (Exception ex)
|
{
|
content.Error(ex.Message);
|
}
|
return content;
|
}
|
}
|
}
|