using iWareCc.Conveyor.Entity;
using iWareCc.Conveyor.EnumType;
using iWareCc.DecomposeTask.Entity;
using iWareCcTest.Properties;
using iWareCommon.Common.Entity;
using iWareCommon.Common.EnumType;
using iWareCommon.Utils;
using iWareDataCore.ORM;
using iWareDataCore.TASK.Entity;
using iWareDataCore.TASK.EnumType;
using iWareLog.ORM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace iWareCc.DoConveyorTaskAuto.Chain
{
public class 选择一个未执行的输送机的任务 : IHandler
{
///
/// 该节点的下一个节点
///
private IHandler nextHandler = null;
public IHandler NextHandler
{
set { nextHandler = value; }
}
///
/// WIP任务容器
///
private PartTaskContainer DecompositionTaskContainer;
private ConveyorEntity Conveyor;
///
/// 构造函数
///
/// 结果容器
/// WIP任务容器
public 选择一个未执行的输送机的任务(ConveyorEntity c, PartTaskContainer decompositionTaskContainer)
{
this.Conveyor = c;
this.DecompositionTaskContainer = decompositionTaskContainer;
}
public void Handle()
{
using (var dbModel = new DbModelCore())
{
try
{
var task = dbModel.TASKPartTasks.OrderBy(x=>x.handletimes).FirstOrDefault(x => x.type == (int)EPartTaskType.输送机任务 && x.isreleased == (int)EYesOrNo.否 && x.isfinished == (int)EYesOrNo.否 && x.equipid == Conveyor.Equipment.Id);
var gate=Conveyor.Gates.FirstOrDefault(x=>x.Place.PlaceTypeName=="gate11");
var alarm=Conveyor.Gates.FirstOrDefault(x=>x.Place.PlaceTypeName=="alarm");
var sc = Conveyor.Gates.FirstOrDefault(x => x.Place.PlaceTypeName == "sc");
if (task == null)
{
LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行输送机任务流程/" + Conveyor.Equipment.EquipName, "选择一个未执行的输送机的任务:{0}", "未找到相应的输送机任务");
return;
}
task.handletimes += 1;
dbModel.SaveChanges();
if (gate != null && gate.Place.Id == task.sourceplaceid && !string.IsNullOrEmpty(sc.RCheckMaterialCode) && !string.IsNullOrWhiteSpace(sc.RCheckMaterialCode) && !sc.RCheckMaterialCode.Contains("ERROR"))
{
var material = dbModel.BASEMaterialViews.FirstOrDefault(x => x.code == sc.RCheckMaterialCode.Trim());
//判断物料是否入库,如果入库直接发送入库任务,不入库生成退库记录
if (material != null)
{
LogTextHelper.WriteLine(Resources.LogDir + @"/物料码信息/", "接收的物料号:{0}", sc.RCheckMaterialCode);
var tasknew = dbModel.TASKPartTasks.FirstOrDefault(x => x.type == (int)EPartTaskType.输送机任务 && x.isreleased == (int)EYesOrNo.否 && x.isfinished == (int)EYesOrNo.否 && x.equipid == Conveyor.Equipment.Id && x.materialcode == material.code && x.sourceplaceid == gate.Place.Id);
if (tasknew != null)
{
LogTextHelper.WriteLine(Resources.LogDir + @"/物料码信息/", "发送的物料:{0};任务号;{1}", sc.RCheckMaterialCode, tasknew.id);
if (double.Parse(material.wide) >= 250)//退库
{
if (gate.RIsEmpty && !gate.ROccupied)//端口有货才发送退库任务
{
LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行输送机任务流程/" + Conveyor.Equipment.EquipName, "获取需要执行任务的输送机:输送机端口{0}上没有检测到物料", gate.Place.PlaceTypeName);
return;
}
int i = 0;
using (var dbModelLog = new DbModelLog())
{
InOutStorageDetail iosd = new InOutStorageDetail()
{
fromplacecode = tasknew.sourceplace,
formplaceid = tasknew.sourceplaceid,
toplacecode = tasknew.toplace,
toplaceid = tasknew.toplaceid,
equipid = tasknew.equipid,
equipname = tasknew.equipname,
updatetime = DateTime.Now,
createtime = tasknew.createtime,
materialcode = tasknew.materialcode,
materialid = material.id,
type = (int)EMainTaskType.退库任务
};
dbModelLog.InOutStorageDetails.Add(iosd);
i = dbModelLog.SaveChanges();
}
if (i > 0)
{
var flag = gate.SendNotIngGateTask();
if (!flag)
{
gate.WPutFinishSymbol = false;
LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行输送机任务流程/" + Conveyor.Equipment.EquipName, "发送输送机退库任务:{0}失败!", tasknew.id, flag);
gate.SendNotIngGateTask();//发送失败,再发一次
}
dbModel.TASKPartTasks.Remove(tasknew);
dbModel.SaveChanges();
sc.RCheckMaterialCode = "";
}
return;
}
else//入库
{
task = tasknew;
}
gate.RCheckMaterialCode = "";
}
else
{
alarm.SendGateAlarmTask((int)ESendCode.物料不在入库清单中);
LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行输送机任务流程/" + Conveyor.Equipment.EquipName, "选择一个未执行的输送机的任务:{0}", "扫描的物料号在入库任务清单中不存在!");
return;
}
}
else
{
alarm.SendGateAlarmTask((int)ESendCode.物料不存在);
LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行输送机任务流程/" + Conveyor.Equipment.EquipName, "选择一个未执行的输送机的任务:{0}", "没有收到扫描的物料号");
return;
}
}
var fromgate = Conveyor.Gates.FirstOrDefault(x => x.Place.Id == task.sourceplaceid);
var togate = Conveyor.Gates.FirstOrDefault(x => x.Place.Id == task.toplaceid);
if (fromgate == null || togate==null)
{
LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行输送机任务流程/" + Conveyor.Equipment.EquipName, "选择一个未执行的输送机的任务:未找到名称为{0}的输送机端口", task.sourceplaceid);
return;
}
//确定当前到位的端口上有物料
if (fromgate.RIsEmpty && !fromgate.ROccupied)
{
LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行输送机任务流程/" + Conveyor.Equipment.EquipName, "获取需要执行任务的输送机:输送机端口{0}上没有检测到物料", fromgate.Place.PlaceTypeName);
return;
}
//确定下一个到位的端口上没有物料
if (togate.Place.PlaceTypeName!="gate22")
{
if (!togate.RIsEmpty)
{
LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行输送机任务流程/" + Conveyor.Equipment.EquipName, "获取需要执行任务的输送机:输送机目标端口{0}上有物料", togate.Place.PlaceTypeName);
return;
}
}
DecompositionTaskContainer.PartTask = new PartTaskEntity(task);
LogTextHelper.WriteLine(Resources.LogDir + @"/自动执行输送机任务流程/" + Conveyor.Equipment.EquipName, "选择一个未执行的输送机的任务:{0}", DecompositionTaskContainer.PartTask.Id);
if (nextHandler != null)
{
fromgate.RIsEmpty = true;
fromgate.ROccupied = false;
nextHandler.Handle();
}
}
catch (Exception ex)
{
LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Handle", ex.Message);
}
}
}
}
}