using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using WIDESEA_Comm;
using WIDESEA_Comm.LogInfo;
using WIDESEA_Core.BaseProvider;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Entity.ToAGV;
using WIDESEA_WCS.IRepositories;
using WIDESEA_WCS.Repositories;
using WIDESEA_WCS.WCSClient;
using WIDESEA_WMS.IRepositories;
using WIDESEA_WMS.IServices;
using WIDESEA_WMS.Repositories;
using static FreeSql.Internal.GlobalFilter;
using static System.Collections.Specialized.BitVector32;
namespace WIDESEA_WCS.JobsPart.Common
{
public class RestockHCJ
{
///
/// 更新队列任务
///
public static void HCJGetBarcode()
{
try
{
VOLContext Context = new VOLContext();
Idt_stationinfoRepository stationinfoRepository = new dt_stationinfoRepository(Context);
IVV_Mes_WorkinfoRepository workinfoRepository = new VV_Mes_WorkinfoRepository(Context);
Idt_agvtaskRepository agvtaskService = new dt_agvtaskRepository(Context);
var tasks = agvtaskService.Find(x => x.agv_taskstate == "Queue").OrderByDescending(x => x.agv_grade).ThenBy(x => x.agv_createtime).ToList();
foreach (var task in tasks)
{
if (task.agv_tasktype == "TaskType_EmptyPallet")//空托任务
{
if (task.agv_fromaddress == "" && DateTime.Now - task.agv_createtime >= TimeSpan.FromMinutes(2))
{
if (task.agv_tasknum.Contains("_"))
{
string mainTaskNum = task.agv_tasknum.Split('_')[0];
if (agvtaskService.Find(x => x.agv_tasknum == mainTaskNum && task.agv_taskstate != "Executing" && task.agv_taskstate != "Create").Any() || !agvtaskService.Find(x => x.agv_tasknum == mainTaskNum).Any())
GetStation.EmptyPalletStation(task);
}
else
GetStation.EmptyPalletStation(task);
#region
//var Pipeline_client = PLCClient.Clients.FirstOrDefault(t => t.PLCName == "链条机");
//if (Pipeline_client == null) throw new Exception("链条机调度服务未开启!");
//if (!Pipeline_client.IsConnected) throw new Exception("与链条机连接超时!");
//var area = task.agv_Traytype == "SmallTray" ? "11" : "10";
////找1库区的空托位
//var EmptyStation = stationinfoRepository.Find(x => x.area == area && x.quantity > 0 && x.stationCode.Contains("A") /*&& x.location_state == "Stroge"*//*LocationStateEnum.Stroge.ToString()*/ && x.enable).OrderBy(x => x.column).ThenBy(x => x.line).FirstOrDefault();
//if (EmptyStation != null)
// if (EmptyStation.location_state == LocationStateEnum.Busy.ToString()) EmptyStation = null;
//#region 一个区域只能有一个空托任务
////bool ok = false;
////if (EmptyStation != null)
//// ok = agvtaskService.Find(x => x.agv_fromaddress == EmptyStation.stationCode || x.agv_toaddress == EmptyStation.stationCode).Any();
////if (EmptyStation == null || ok)
//// EmptyStation = stationinfoRepository.Find(x => x.area == area && x.quantity > 0 && x.stationCode.Contains(area == "10" ? "D" : "C") && x.location_state == "Stroge" /*LocationStateEnum.Stroge.ToString()*/ && x.enable)
//// .OrderBy(x => x.line).OrderByDescending(x => x.column).FirstOrDefault();
//#endregion
//if (EmptyStation == null)
// EmptyStation = stationinfoRepository.Find(x => x.area == area && x.quantity > 0 && x.stationCode.Contains(area == "10" ? "D" : "C") && x.enable).OrderByDescending(x => x.column).FirstOrDefault();
//if (EmptyStation == null) continue;
//#region 库内存在任务占用
//if (EmptyStation.location_state == LocationStateEnum.Busy.ToString())
// continue;
//#endregion
//if (EmptyStation == null)
// EmptyStation = stationinfoRepository.Find(x => x.area == area && x.quantity > 0 && x.stationCode.Contains(area == "10" ? "D" : "C") && x.location_state == "Stroge" && x.enable).OrderByDescending(x => x.column).FirstOrDefault();
//if (EmptyStation != null)
//{
// #region 库内存在任务占用
// //if (EmptyStation.location_state == LocationStateEnum.Busy.ToString())
// // continue;
// #endregion
// //if (agvtaskService.Find(x => x.agv_fromaddress == EmptyStation.stationCode || x.agv_toaddress == EmptyStation.stationCode).Any())
// // return;
// #region 补库内空托盘,不需要光电判断
// var PalletSignal = Pipeline_client.ReadByOrder("R_PalletSignal", task.agv_toaddress);//读取托盘信号:1:有,2无
// var MaterialSignal = Pipeline_client.ReadByOrder("R_MaterialSignal", task.agv_toaddress);//读取货物信号:1:有,2无
// if (PalletSignal == 2 && MaterialSignal == 2)
// {
// task.agv_fromaddress = EmptyStation.stationCode;
// task.StarQuantity = EmptyStation.quantity - 1;
// task.agv_taskstate = "Create";
// agvtaskService.Update(task, true);
// EmptyStation.location_state = LocationStateEnum.Busy.ToString();
// stationinfoRepository.Update(EmptyStation, true);
// WriteDBLog.Success("更新空盘队列任务", $"任务编号:{task.agv_tasknum}", "PCS");
// }
// #endregion
//}
#endregion
}
}
else if (task.agv_tasktype == "TaskType_OutsourceInbound" || task.agv_tasktype == "TaskType_Inbound")//外协库入库/AB库入库
{
if (task.agv_toaddress == "")
{
var stationinfo = stationinfoRepository.Find(x => x.stationCode == task.agv_fromaddress).FirstOrDefault();
var work = workinfoRepository.Find(x => x.workOrder == stationinfo.Number && x.drawingNo == stationinfo.stationType && x.heatID == stationinfo.heatNumber && x.processCode == "17").FirstOrDefault();
var TargetLocation = task.agv_tasktype == "TaskType_Inbound" ? GetLocation.GetEmptyLocation(stationinfoRepository, work, stationinfo) : StationTask.GetEmptyLocation(stationinfoRepository);
if (TargetLocation != null)
{
if (agvtaskService.Find(x => x.agv_toaddress == TargetLocation.stationCode).Any()) continue;
task.agv_taskstate = "Create";
task.agv_toaddress = TargetLocation.stationCode;
TargetLocation.location_state = LocationStateEnum.InBusy.ToString();
TargetLocation.billetID = stationinfo.billetID;
TargetLocation.stationType = task.agv_materielid;
TargetLocation.heatNumber = stationinfo.heatNumber;
TargetLocation.Number = task.jobID;
stationinfoRepository.Update(TargetLocation, true);
agvtaskService.Update(task, true);
}
}
}
else if (task.agv_tasktype == "TaskType_OutsourceOutbound" || task.agv_tasktype == "TaskType_Outbound")//外协库出库/AB库出库
{
if (task.agv_toaddress == "")
{
var EmptyStations = stationinfoRepository.Find(x => x.stationCode.Contains("S01001") && x.tray_status == "EmptyTray" /*x.location_state == "Empty"*/ && x.enable).ToList();
foreach (var EmptyStation in EmptyStations)
{
if (agvtaskService.Find(x => x.agv_toaddress == EmptyStation.stationCode).Any()) continue;
task.agv_taskstate = "Create";
task.agv_toaddress = EmptyStation.stationCode;
EmptyStation.location_state = LocationStateEnum.Busy.ToString();
stationinfoRepository.Update(EmptyStation, true);
agvtaskService.Update(task, true);
WriteDBLog.Success("更新出库队列任务", $"任务编号:{task.agv_tasknum}", "PCS");
return;
}
}
}
else if (task.agv_tasktype == "TaskType_OutsourceCarry")//移库外协
{
if (task.agv_toaddress == "")
{
var TargetLocation = StationTask.GetEmptyLocation(stationinfoRepository);
if (TargetLocation != null)
{
if (agvtaskService.Find(x => x.agv_toaddress == TargetLocation.stationCode).Any()) continue;
task.agv_taskstate = "Create";
task.agv_toaddress = TargetLocation.stationCode;
TargetLocation.location_state = LocationStateEnum.OutBusy.ToString();
TargetLocation.stationType = task.agv_materielid;
stationinfoRepository.Update(TargetLocation, true);
agvtaskService.Update(task, true);
WriteDBLog.Success("更新出库队列任务", $"任务编号:{task.agv_tasknum}", "PCS");
return;
}
}
}
}
}
catch (Exception ex)
{
WriteDBLog.Error("更新队列任务", $"错误信息:{ex.Message}", "PCS");
}
}
}
}