using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core.Enums;
using WIDESEA_Core;
using WIDESEA_Model.Models;
using WIDESEA_Core.Helper;
using Microsoft.Extensions.Logging;
using MailKit.Search;
using System.Reflection.Metadata;
using static WIDESEA_ITaskInfoService.ITaskService;
using WIDESEA_Common;
using WIDESEA_Core.LogHelper;
using WIDESEA_DTO.Task;
using WIDESEA_Common.TaskEnum;
using WIDESEA_Common.StockEnum;
using WIDESEA_Common.LocationEnum;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using WIDESEA_Common.OtherEnum;
using WIDESEA_Core.Const;
using WIDESEA_DTO.ToMes;
using System.DirectoryServices.Protocols;
using System.Net;
namespace WIDESEA_TaskInfoService
{
public partial class TaskService
{
///
/// 下发出库接口
///
///
///
public ApiResponse sendExTask(InOutboundTaskReceived outbound)
{
List Adddtstockt = new List();
List Addlocations = new List();
List Addtaskdt = new List();
if ( outbound != null )
{
if (outbound.PalletCodeList.Count > 0)
{
List StockData = _stockRepository.QueryData();
List LocationData=_locationInfoRepository.QueryData(x=>x.LocationStatus== (int)LocationStatusEnum.InStock);
List taskData = BaseDal.QueryData();
foreach (string Pallet in outbound.PalletCodeList)
{
//查找库存信息
Dt_StockInfo dt_StockInfo = StockData.Find(x => x.PalletCode == Pallet);
if(dt_StockInfo==null) return MESresponse($"接收到的托盘条码:{Pallet},未在系统中找到库存信息!", false);
//查找库位信息
Dt_LocationInfo locationInfo = LocationData.Find(x => x.LocationCode == dt_StockInfo.LocationCode);
if (locationInfo == null) return MESresponse($"接收到的托盘条码:{Pallet},未在系统中找到库位信息!", false);
//判断当前货位是否有任务
Dt_Task _Task = taskData.Find(x => x.SourceAddress == locationInfo.LocationCode || x.TargetAddress == locationInfo.LocationCode);
if (_Task != null) return MESresponse($"接收到的托盘条码:{Pallet},当前托盘对应的库位信息已有任务,不可出库!", false);
dt_StockInfo.StockStatus = (int)StockStatusEmun.出库锁定;
locationInfo.LocationStatus = (int)LocationStatusEnum.InStockLock;
string TaregtAddres = "";
if (outbound.NodeCode != "2011" && outbound.NodeCode != "2009")
{
int nextTaregt = int.Parse(outbound.NodeCode);
//确认目标站台(固定)
TaregtAddres = (nextTaregt + 1).ToString();
}
else
{
TaregtAddres = locationInfo.RoadwayNo == "SC01" ? "2021" : "2020";
}
//生成移动任务
Dt_Task dt_Task = new()
{
PalletCode = dt_StockInfo.PalletCode,
TaskNum = GetTaskNum(nameof(SequenceEnum.SeqTaskNum)),
Roadway = locationInfo.RoadwayNo,
TaskType = TaskOutboundTypeEnum.Outbound.ObjToInt(),
TaskStatus = TaskOutStatusEnum.OutNew.ObjToInt(),
SourceAddress = locationInfo.LocationCode,
TargetAddress = TaregtAddres,
CurrentAddress = locationInfo.LocationCode,
NextAddress = TaregtAddres,
Grade = 1,
Creater = "MES",
Depth = locationInfo.Depth,
CreateDate = DateTime.Now,
MEStaskId = outbound.TaskId,
MESbusinessId = outbound.BusinessId,
MESsubPalletCode = dt_StockInfo.MESsubPalletCode,
OutboundPlatform = outbound.NodeCode
};
Adddtstockt.Add(dt_StockInfo);
Addlocations.Add(locationInfo);
Addtaskdt.Add(dt_Task);
}
if (Adddtstockt.Count > 0 && Addlocations.Count > 0 && Addtaskdt.Count > 0)
{
_unitOfWorkManage.BeginTran();
_stockRepository.UpdateData(Adddtstockt);
_locationInfoRepository.UpdateData(Addlocations);
BaseDal.AddData(Addtaskdt);
var respon = PushTasksToWCS(Addtaskdt,"");
if (respon.Status)
{
_unitOfWorkManage.CommitTran(); //提交事务
return MESresponse("", true);
}
else
{
_unitOfWorkManage.RollbackTran(); //回滚事务
return MESresponse($"下发出库失败,原因:{respon.Message}!", false);
}
}
else
{
return MESresponse("任务生成失败!", false);
}
}
else
{
return MESresponse("接收到MES托盘编码列表无数据!", false);
}
}
else
{
return MESresponse("接收到MES下发的任务为空!", false);
}
}
}
}