using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using WIDESEA_Common;
using WIDESEA_Common.Tools;
using WIDESEA_Core.EFDbContext;
using WIDESEA_Core.Utilities;
using WIDESEA_Entity.DomainModels;
using WIDESEA_Services;
using WIDESEA_Services.IRepositories;
using WIDESEA_Services.Repositories;
using WIDESEA_Services.Services;
using WIDESEA_WCS.WCSClient;
namespace WIDESEA_WCS.Jobs
{
public partial class EquipmentExecutor : SchedulerExecuteBase
{
private static int _readXJJKSignalso = 0;
///
/// 正极和负极小极卷库入中转料架逻辑
///
///
public static void ZF_XJJK_DownTask()
{
if (Interlocked.Exchange(ref _readXJJKSignalso, 1) == 0)
{
try
{
VOLContext Context = new VOLContext();
WebResponseContent responseContent = new WebResponseContent();
Idt_task_numberRepository tasknumberRep = new dt_task_numberRepository(Context);
dt_task_numberService tasknumber = new dt_task_numberService(tasknumberRep);
Idt_agvtaskRepository agvRepository = new dt_agvtaskRepository(Context);
Ibase_routing_tableRepository routingRepository = new base_routing_tableRepository(Context);
Ibase_ware_locationRepository locRepository = new base_ware_locationRepository(Context);
List ZZLJS = new List { "ZJXL-ZZLJ001", "ZJXL-ZZLJ004","ZJXL-ZZLJ002", "ZJXL-ZZLJ003", "FJXL-ZZLJ001",
"FJXL-ZZLJ002", "FJXL-ZZLJ003", "FJXL-ZZLJ004" };
// List ZZLJS = new List { "FJXL-ZZLJ002", "FJXL-ZZLJ003","FJXL-ZZLJ001", "FJXL-ZZLJ004" };
PLCClient plc = null;
foreach (var ZZLJ in ZZLJS)
{
var location = locRepository.FindFirst(v => v.down_code == ZZLJ);
if (ZZLJ.Contains("ZJXL"))
{
plc = WCSService.Clients.Find(v => v.PLCName == "正极1号AGV");
if (plc == null)
continue;
string isZWork = plc.ReadValue(ConveyorLineInfoDBName.R_Location_iswork.ToString(), ZZLJ).ToString();
if (isZWork == "2")
continue;
var routes = routingRepository.Find(v => v.route_end == ZZLJ).ToList();
foreach (var route in routes)
{
var task = agvRepository.FindFirst(v => v.agv_fromaddress == ZZLJ || v.agv_toaddress == location.upper_code || v.agv_toaddress == ZZLJ);
if (task != null)
continue;
if (location.location_state == LocationStateEnum.LocationState_Empty.ToString())
{
responseContent = locRepository.DbContextBeginTransaction(() =>
{
dt_agvtask agvtask = new dt_agvtask
{
agv_materbarcode = "",
agv_barcode = "",
agv_code = "正极1号AGV",
agv_createtime = DateTime.Now,
agv_fromaddress = "nou",
agv_grade = 1,
agv_materielid = "空托盘",
agv_qty = 1,
agv_tasknum = "KH-" + tasknumber.GetTaskNumber(tasknumberRep),
agv_taskstate = "WaitStockOut",
agv_tasktype = "TaskType_Outbound",
agv_toaddress = ZZLJ,
agv_userid = "WCS",
agv_worktype = 2
};
List StockList = new List { "ZXD0201", "ZXC0201", "ZXB0201", "ZXA0201" };
MESback WMSbackresult = MESAPIInvoke.OutNeedStosk(agvtask.agv_tasknum, StockList, 3, "空托盘", "", "");
if (WMSbackresult == null) { return WebResponseContent.Instance.Error("正极小极卷库不通过要料申请"); }
location.location_state = LocationStateEnum.LocationState_Lock.ToString();
locRepository.Update(location, true);
agvRepository.Add(agvtask, true);
WriteLog.Info("ZF_XJJK_DownTask").Write("ZF_XJJK_DownTask" + agvtask.agv_tasknum + DateTime.Now, "ZF_XJJK_DownTask");
return WebResponseContent.Instance.OK();
});
break;
}
}
}
else if (ZZLJ.Contains("FJXL"))
{
plc = WCSService.Clients.Find(v => v.PLCName == "负极1号AGV");
if (plc == null)
continue;
string isZWork = plc.ReadValue(ConveyorLineInfoDBName.R_Location_iswork.ToString(), ZZLJ).ToString();
if (isZWork == "2")
continue;
var routes = routingRepository.Find(v => v.route_end == ZZLJ).ToList();
foreach (var route in routes)
{
var task = agvRepository.FindFirst(v => v.agv_fromaddress == route.route_end || v.agv_toaddress == route.route_end || v.agv_toaddress == location.upper_code || v.agv_toaddress == ZZLJ);
if (task != null)
continue;
if (location.location_state == LocationStateEnum.LocationState_Empty.ToString())
{
responseContent = locRepository.DbContextBeginTransaction(() =>
{
dt_agvtask agvtask = new dt_agvtask
{
agv_materbarcode = "",
agv_barcode = "",
agv_code = "负极1号AGV",
agv_createtime = DateTime.Now,
agv_fromaddress = "nou",
agv_grade = 1,
agv_materielid = "空托盘",
agv_qty = 1,
agv_tasknum = "KH-" + tasknumber.GetTaskNumber(tasknumberRep),
agv_taskstate = "WaitStockOut",
agv_tasktype = "TaskType_Outbound",
agv_toaddress = ZZLJ,
agv_userid = "WCS",
agv_worktype = 2
};
List StockList = new List { "FXD0201", "FXC0201", "FXB0201", "FXA0201" };
MESback WMSbackresult = MESAPIInvoke.OutNeedStosk(agvtask.agv_tasknum, StockList, 3, "空托盘", "", "");
if (WMSbackresult == null) { return WebResponseContent.Instance.Error("负极小极卷库不通过要料申请"); }
location.location_state = LocationStateEnum.LocationState_Lock.ToString();
locRepository.Update(location, true);
agvRepository.Add(agvtask, true);
WriteLog.Info("ZF_XJJK_DownTask").Write("ZF_XJJK_DownTask" + agvtask.agv_tasknum + DateTime.Now, "ZF_XJJK_DownTask");
return WebResponseContent.Instance.OK();
});
break;
}
}
}
}
}
catch (Exception ex)
{
WriteLog.Info("ZF_XJJK_DownTask").Write("ZF_XJJK_DownTask" + ex.Message + DateTime.Now, "ZF_XJJK_DownTask");
}
finally
{
Interlocked.Exchange(ref _readXJJKSignalso, 0);
}
}
}
}
}