using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Core;
using WIDESEA_Core.Enums;
using WIDESEA_DTO;
using WIDESEA_Model.Models;
using WIDESEA_Repository;
using WIDESEA_StorageBasicRepository;
using WIDESEAWCS_BasicInfoRepository;
using WIDESEAWCS_Model.Models;
namespace WIDESEA_StoragIntegrationServices
{
public partial class WCSService
{
///
/// 请求入库巷道号
///
///
///
public WebResponseContent RequestEmptyInboundRoadWayNo(RequestTaskDto json)
{
WebResponseContent response = new WebResponseContent();
try
{
//如任务已存在则 返回此任务
var checkTask = _taskRepository.QueryFirst(x => x.PalletCode == json.PalletCode && x.PVI == json.PVI);
if (checkTask != null)
{
return response.OK("申请入库成功", data: checkTask);
}
var carInfo = _carBodyInfoRepository.QueryFirst(x => x.RFID == json.PVI && x.PalletCode == json.PalletCode);
if (carInfo == null)
{
Dt_CarBodyInfo dt_CarBodyInfo = new Dt_CarBodyInfo
{
PalletCode = json.PalletCode,
CarType = 3,
PVI = json.PalletCode,
RFID = json.PalletCode,
BodyStatus = 0
};
_carBodyInfoRepository.AddData(dt_CarBodyInfo);
carInfo = _carBodyInfoRepository.QueryFirst(x => x.RFID == json.PVI && x.PalletCode == json.PalletCode);
}
var maxGroup = _locationRepository.QueryData(x => x.LocationType == 3 && x.LocationStatus == (int)LocationEnum.Free && x.EnalbeStatus == (int)EnableEnum.Enable)
.GroupBy(x => x.RoadwayNo)
.OrderByDescending(g => g.Count()) // 根据每个组的元素数量排序
.ToList(); // 取出数量最多的组
Dictionary result = new Dictionary();
foreach (var item in maxGroup)
{
var number = _taskRepository.QueryData(x => x.TargetAddress == item.Key).Count();
if (item.Count() - number <= 0)
{
continue;
}
result.Add(item.Key, item.Count() - number);
}
string maxRoadwayNo = result.OrderByDescending(x => x.Value).FirstOrDefault().Key; // 数量最多的组的Key
var station = _stationManagerRepository.QueryFirst(x => x.RoadwayNo == maxRoadwayNo && x.stationArea == json.area && x.stationType == 6);
var newtask = new Dt_Task
{
CurrentAddress = json.Position,
Grade = 1,
Roadway = station.Roadway,
TargetAddress = station.Roadway,
//Dispatchertime = DateTime.Now,
NextAddress = station.stationChildCode,
OrderNo = null,
PalletCode = json.PalletCode,
SourceAddress = json.Position,
TaskState = (int)TaskInStatusEnum.InNew,
TaskType = (int)TaskInboundTypeEnum.InTray,
TaskNum = _taskRepository.GetTaskNo().Result,
Creater = "Systeam",
PVI = json.PVI,
CarType = carInfo.CarType
};
_unitOfWorkManage.BeginTran();
_taskRepository.AddData(newtask);
_unitOfWorkManage.CommitTran();
response.OK("空撬申请入库成功", data: newtask);
//response.OK(data: maxRoadwayNo);
}
catch (Exception ex)
{
response.Error($"空撬请求巷道号失败:{ex.Message}");
}
return response;
}
}
}