using MailKit.Search;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common;
using WIDESEA_Core;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.WCSInfo;
using WIDESEA_Model.Models;
using WIDESEAWCS_DTO.WCSInfo;
namespace WIDESEA_TaskInfoService
{
public partial class TaskService
{
///
/// 立库出库AGV搬运
///
///
///
public WebResponseContent ShelfOutboundCarry(Dt_Task outTask)
{
Dt_CachePoint cachePoint = GetCachePointByStartPoint(outTask.TargetAddress);
Dt_Task task = new()
{
CurrentAddress = outTask.TargetAddress,
Grade = 0,
NextAddress = cachePoint.PointCode,
OrderNo = outTask.OrderNo,
PalletCode = outTask.PalletCode + "-AGV",
Roadway = "",
SourceAddress = outTask.TargetAddress,
TargetAddress = cachePoint.PointCode,
//SourceKey = outTask.TaskId,
TaskNum = outTask.TaskNum,
TaskState = AGVTaskStatusEnum.AGV_New.ObjToInt(),
TaskType = TaskTypeEnum.AGVCarry.ObjToInt(),
};
cachePoint.PointStatus = LocationStatusEnum.OutLock.ObjToInt();
_basicService.CachePointService.Repository.UpdateData(cachePoint);
Repository.AddData(task);
return WebResponseContent.Instance.OK();
}
///
/// 叫料
///
///
///
public WebResponseContent CallMateriel(string endPoint)
{
Dt_CachePoint cachePoint = GetCachePointByEndPoint(endPoint);
Dt_Task task = new Dt_Task()
{
CurrentAddress = cachePoint.PointCode,
Grade = 0,
NextAddress = endPoint,
OrderNo = "",
PalletCode = cachePoint.Remark,
Roadway = "",
SourceAddress = cachePoint.PointCode,
TargetAddress = endPoint,
//SourceKey = 0,
TaskNum = BaseDal.GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
TaskState = AGVTaskStatusEnum.AGV_New.ObjToInt(),
TaskType = TaskTypeEnum.AGVCarry.ObjToInt(),
};
cachePoint.PointStatus = LocationStatusEnum.OutLock.ObjToInt();
_basicService.CachePointService.Repository.UpdateData(cachePoint);
Repository.AddData(task);
return WebResponseContent.Instance.OK();
}
///
/// 物料搬运
///
///
///
public WebResponseContent MaterielCarry(string startPoint)
{
Dt_CachePoint cachePoint = GetCachePointByStartPoint(startPoint);
Dt_Task task = new()
{
CurrentAddress = startPoint,
Grade = 0,
NextAddress = cachePoint.PointCode,
OrderNo = "",
PalletCode = startPoint + "-AGV",
Roadway = "",
SourceAddress = startPoint,
TargetAddress = cachePoint.PointCode,
//SourceKey = 0,
TaskNum = BaseDal.GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
TaskState = AGVTaskStatusEnum.AGV_New.ObjToInt(),
TaskType = TaskTypeEnum.AGVCarry.ObjToInt(),
};
cachePoint.PointStatus = LocationStatusEnum.OutLock.ObjToInt();
_basicService.CachePointService.Repository.UpdateData(cachePoint);
Repository.AddData(task);
return WebResponseContent.Instance.OK();
}
///
/// 人工组盘入库
///
///
///
public WebResponseContent InMateriel(SaveModel saveModel)
{
WebResponseContent responseContent = new WebResponseContent();
try
{
var palletCode = saveModel.MainData["palletCode"].ToString();
if (string.IsNullOrEmpty(palletCode)) throw new Exception("托盘号不可为空");
//var orderNo = saveModel.MainData["orderNo"].ToString();
//if (string.IsNullOrEmpty(orderNo)) throw new Exception("批号不可为空");
var qty = saveModel.MainData["qty"].ToString();
if (string.IsNullOrEmpty(qty)) throw new Exception("袋数不可为空");
var batchNo = saveModel.MainData["batchNo"].ToString();
if (string.IsNullOrEmpty(batchNo)) throw new Exception("柜号不可为空");
#region 查询库存、入库单
Dt_InventoryInfo inventoryInfo = _inventoryInfoRepository.QueryFirst(x => x.PalletCode == palletCode);
if (inventoryInfo != null) throw new Exception($"托盘【{palletCode}】已存在库存信息");
Dt_InboundOrderDetail? inboundOrderDetail = _inboundService.InboundOrderDetailService.Repository.QueryFirst(x => x.BatchNo == batchNo);
if (inboundOrderDetail == null) throw new Exception($"未找到批号【{batchNo}】的入库单");
if (inboundOrderDetail.OrderQuantity - inboundOrderDetail.ReceiptQuantity < qty.ObjToInt()) throw new Exception($"批号【{batchNo}】可组盘数量不足");
Dt_InboundOrder? inboundOrder = _inboundService.InbounOrderService.Repository.QueryFirst(x => x.Id == inboundOrderDetail.OrderId);
if (inboundOrder == null) throw new Exception($"未找到批号为【{batchNo}】的入库单");
Dt_Inventory_Batch inventory_Batch = _inventoryBatchRepository.QueryFirst(x => x.BatchNo == batchNo);
if (inventory_Batch == null) throw new Exception($"未找到批号为【{batchNo}】的批次信息");
#endregion
#region 创建任务
Dt_Task dt_Task = new Dt_Task()
{
CurrentAddress = "1001",
NextAddress = "1002",
SourceAddress = "RGRK",
TargetAddress = "SC01",
Creater = "System",
PalletCode = palletCode,
Roadway = "SC01",
OrderNo = batchNo,
TaskNum = BaseDal.GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
TaskState = InTaskStatusEnum.AGV_InFinish.ObjToInt(),
TaskType = inboundOrder.OrderType == InOrderTypeEnum.Quality.ObjToInt() ? TaskTypeEnum.InQuality.ObjToInt() : TaskTypeEnum.Inbound.ObjToInt(),
CreateDate = DateTime.Now,
Dispatchertime = DateTime.Now,
};
#endregion
#region 添加库存信息、处理入库单
inventoryInfo = new Dt_InventoryInfo()
{
BatchNo = inventory_Batch.BatchNo,
MaterielCode = inventory_Batch.MaterielCode,
MaterielName = inventory_Batch.MaterielName,
PalletCode = palletCode,
ProductionDate = inventory_Batch.ProductionDate,
StockStatus = StockStatusEmun.组盘暂存.ObjToInt(),
ProductStatus = dt_Task.TaskType == TaskTypeEnum.InQuality.ObjToInt() ? ProductStatusEmun.待检2.ObjToInt() : ProductStatusEmun.待检1.ObjToInt(),
ShelfLife = inventory_Batch.ShelfLife,
ValidityPeriod = inventory_Batch.ValidityPeriod,
StockQuantity = qty.ObjToInt(),
BoxQuantity = (qty.ObjToInt() % 8 > 0) ? (qty.ObjToInt() / 8) + 1 : qty.ObjToInt() / 8,
Creater = "System"
};
inboundOrder.OrderStatus = InboundStatusEnum.入库中.ObjToInt();
inboundOrderDetail.ReceiptQuantity += inventoryInfo.StockQuantity;
inboundOrderDetail.OrderDetailStatus = OrderDetailStatusEnum.GroupAndInbound.ObjToInt();
inventory_Batch.ProductStatus = inventoryInfo.ProductStatus;
#endregion
Db.Ado.BeginTran();
_inboundService.InbounOrderService.UpdateData(inboundOrder);
_inboundService.InboundOrderDetailService.UpdateData(inboundOrderDetail);
_inventoryInfoRepository.AddData(inventoryInfo);
_inventoryBatchRepository.UpdateData(inventory_Batch);
AddData(dt_Task);
List wMSTaskDTOs = _mapper.Map>(new List { dt_Task });
var ResultData = HttpHelper.PostAsync(WCSInterfaceAddress.ReceiveTask, wMSTaskDTOs.ToJson(), headers: new Dictionary());
if (ResultData.Result == null) throw new Exception($"向WCS下发入库任务超时");
responseContent = JsonConvert.DeserializeObject(ResultData.Result);
if (responseContent == null) throw new Exception($"下发入库任务WCS无响应");
if (!responseContent.Status) throw new Exception(responseContent.Message);
Db.Ado.CommitTran();
}
catch (Exception ex)
{
Db.Ado.RollbackTran();
responseContent.Error(ex.Message);
}
return responseContent;
}
private Dt_CachePoint GetCachePointByStartPoint(string startPoint)
{
Dt_AreaRouter areaRouter = _basicService.AreaRouterService.Repository.QueryFirst(x => x.StartArea == startPoint);
if (areaRouter == null)
{
throw new Exception("未找到路由信息");
}
Dt_AreaInfo areaInfo = _basicService.AreaInfoService.Repository.QueryFirst(x => x.AreaCode == areaRouter.NextArea);
if (areaInfo == null)
{
throw new Exception("未找到下一区域信息");
}
Dt_CachePoint? cachePoint = _basicService.CachePointService.AssignCachePoint(areaInfo.Id);
if (cachePoint == null)
{
throw new Exception("未找到空闲缓存点");
}
return cachePoint;
}
private Dt_CachePoint GetCachePointByEndPoint(string endPoint)
{
Dt_AreaRouter areaRouter = _basicService.AreaRouterService.Repository.QueryFirst(x => x.NextArea == endPoint);
if (areaRouter == null)
{
throw new Exception("未找到路由信息");
}
Dt_AreaInfo areaInfo = _basicService.AreaInfoService.Repository.QueryFirst(x => x.AreaCode == areaRouter.StartArea);
if (areaInfo == null)
{
throw new Exception("未找到起点缓存区域信息");
}
Dt_CachePoint? cachePoint = _basicService.CachePointService.GetIbStockCachePoint(areaInfo.Id);
if (cachePoint == null)
{
throw new Exception("未找到有货缓存点");
}
return cachePoint;
}
public object AGVTaskFeedBack(AGVTaskFeedBackModel model)
{
Dt_Task task = BaseDal.QueryFirst(x => x.TaskId == Convert.ToInt32(model.Task_id));
if (task == null)
{
return new { code = 404, message = "未找到该任务" };
}
Dt_CachePoint startCachePoint = _basicService.CachePointService.Repository.QueryFirst(x => x.PointCode == task.SourceAddress);
if (startCachePoint != null)
{
startCachePoint.PointStatus = LocationStatusEnum.Free.ObjToInt();
_basicService.CachePointService.Repository.UpdateData(startCachePoint);
}
Dt_CachePoint endCachePoint = _basicService.CachePointService.Repository.QueryFirst(x => x.PointCode == task.TargetAddress);
if (endCachePoint != null)
{
endCachePoint.PointStatus = LocationStatusEnum.InStock.ObjToInt();
_basicService.CachePointService.Repository.UpdateData(endCachePoint);
}
BaseDal.DeleteData(task);
return new { code = 200, message = "成功" };
}
}
}