using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using WIDESEA_Common;
using WIDESEA_Common.LoctionEnum;
using WIDESEA_Common.Tools;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Core.Utilities;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Services.IRepositories;
using WIDESEA_Services.Repositories;
using WIDESEA_WCS.WCSClient;
namespace WIDESEA_WCS
{
public partial class StackerExecutor
{
public static object requestFJTBDown = new object();//requestZJJK
private static int _readFTBSignalsoZ_Down = 0;//_readZTBUPSignalso
///
/// 负极涂布下料工作逻辑
///
///
public static void F_TBSB_DownTask()
{
if (Interlocked.Exchange(ref _readFTBSignalsoZ_Down, 1) == 0)
{
Task.Run(() =>
{
try
{
VOLContext Context = new VOLContext();
Ibase_ware_locationRepository locRepository = new base_ware_locationRepository(Context);
Ibase_routing_tableRepository routingRepository = new base_routing_tableRepository(Context);
Idt_agvtaskRepository agvRepository = new dt_agvtaskRepository(Context);
List TBSBS = new List { "涂布机负极1", "涂布机负极2", "涂布机负极3", "涂布机负极4" };
//循环处理涂布设备下料请求
foreach (var TBSB in TBSBS)
{
if (WCSService.Clients == null)
{
return;
}
PLCClient plc = WCSService.Clients.Find(v => v.PLCName == TBSB);
if (plc == null)
{
continue;
}
string SB_Up_Location = "FJXL-TBSB" + TBSB.Replace("涂布机负极", "");//拼接涂布下料口
//查看当前涂布机设备是否有AGV任务
var SBtask = agvRepository.FindFirst(f => f.agv_fromaddress == SB_Up_Location);
//有结束本次循环
if (SBtask != null) { continue; }
//当前设备有没有上料请求
bool GYR_RFX = plc.OmronPLCClient.OmronFinsNetClient.ReadBool("D11293.00").Content;
//Convert.ToBoolean(plc.ReadValue("R_Feedingrequest".ToString(), SB_Up_Location));
if (GYR_RFX)
{
#region 记录下料请求信号
WriteLog.GetLog().Write($"{plc.PLCName}下料请求:{GYR_RFX}---{DateTime.Now}", $"负极涂布下料请求");
#endregion
//找到路由规则(以设备为起点的路由规则)
List Routing_Table = routingRepository.Find(d => d.route_began == SB_Up_Location);
base_ware_location location_TB = new base_ware_location();
if (Routing_Table.Count > 0)
{
foreach (var item in Routing_Table)
{
//找先找涂布区域缓存架
location_TB = locRepository.FindFirst(t => t.upper_code == item.route_end && t.location_state == LoctionStateEnum.LocationState_Empty.ToString() && t.area == "FJ-TBHCJ" && t.status == 1 && t.task == 2);
if (location_TB != null)
{
// location = location_TB.FirstOrDefault();//如果找到,赋值后退出循环
break;
}
}
//以上循环未找到合适货位,再找箔材库三号位缓存架
if (location_TB == null)
{
//定位铂材库路由,找到铂材库货位号
string Loc_BCK = Routing_Table.FirstOrDefault(t => t.route_end.Contains("FJ-BCHCJ")).route_end.ToString();
if (Loc_BCK != null)
{
//根据根据货位号找到铂材库位置并确认是否有料
location_TB = locRepository.FindFirst(t => t.upper_code == Loc_BCK && t.location_state == LoctionStateEnum.LocationState_Empty.ToString() && t.status == 1 && t.task == 2);
if (location_TB == null)
{
return;
}
}
}
}
if (location_TB != null)
{
WebResponseContent content = new WebResponseContent();
locRepository.DbContextBeginTransaction(() =>
{
//生成涂布机下料任务
dt_agvtask agvtask = new dt_agvtask()
{
agv_id = Guid.NewGuid(),
agv_tasknum = "KH-" + IdenxManager.GetTaskNo(),
agv_fromaddress = SB_Up_Location,//以设备未起点
agv_toaddress = location_TB.upper_code,
agv_code = "负极AGV调度",
agv_remark = "涂布下料任务",
agv_taskstate = AGVTaskStateEnum.Create.ToString(),
agv_tasktype = AGVTaskTypeEnum.TaskType_Outbound.ToString(),
agv_worktype = 1,//工作类型
agv_materielid = "",
agv_qty = 1,
agv_createtime = DateTime.Now,
agv_grade = 0,//任务优先级
agv_userid = "WCS",
agv_barcode = "",
};
location_TB.update_time = DateTime.Now;
location_TB.task = 1;
var entry = locRepository.DbContext.ChangeTracker.Entries().FirstOrDefault();
if (entry != null)
{
entry.State = EntityState.Detached;
}
//location_TB.logic_col = 1;
int x = locRepository.Update(location_TB, d => new { d.task, d.logic_col, d.update_time }, true);
if (x == 1)
{
agvRepository.Add(agvtask, true);
return content.OK();
}
else
{
return content.Error();
}
});
}
}
}
}
catch (Exception ex)
{
WriteLog.GetLog().Write($"负极涂布下料:{DateTime.Now} --------------{ex}", $"负极涂布下料调度");
}
finally
{
Interlocked.Exchange(ref _readFTBSignalsoZ_Down, 0);
}
});
}
}
}
}