|
|
using iWareCommon.Common.Globle;
|
using iWareCommon.Utils;
|
using iWareModel;
|
using iWareSql;
|
using iWareSql.DBModel;
|
using iWareSql.Entity.ViewMode;
|
using iWareSql.Orm;
|
using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Net;
|
using System.Text;
|
using System.Threading;
|
using System.Threading.Tasks;
|
using XiGang.Core.Model;
|
|
namespace iWareSql.DataAccess
|
{
|
/// <summary>
|
/// 设备任务处理类
|
/// </summary>
|
public class PartTaskHandler
|
{
|
|
/// <summary>
|
/// 获取设备任务
|
/// </summary>
|
/// <returns></returns>
|
public static Task_Part GetPartTask(DbModel edm, string taskId)
|
{
|
return edm.Task_Part.Where(x => x.Id == taskId).FirstOrDefault();
|
}
|
|
/// <summary>
|
/// 获取当前正在执行的设备任务
|
/// </summary>
|
/// <returns></returns>
|
public static Task_Part GetCurrentDoingTask(int deviceId)
|
{
|
using (DbModel edm = new DbModel())
|
{
|
return edm.Task_Part.Where(x => x.TaskState == (int)DeviceTaskStatusEnum.已下发 && x.DeviceId == deviceId).FirstOrDefault();
|
}
|
}
|
|
/// <summary>
|
/// 是否存在 托盘 有任务占用
|
/// </summary>
|
/// <param name="edm"></param>
|
/// <param name="salverCode">托盘号</param>
|
/// <returns></returns>
|
public static bool IsExistOtherTaskDoingForSalverCode(string salverCode)
|
{
|
using (DbModel edm = new DbModel())
|
{
|
int num = edm.Task_Part.Where(x =>
|
(x.TaskState == (int)DeviceTaskStatusEnum.未开始 || x.TaskState == (int)DeviceTaskStatusEnum.已下发)
|
&& (x.SalverCode == salverCode )
|
).Count();
|
return num > 0 ? true : false;
|
}
|
}
|
|
/// <summary>
|
/// 是否存在其他设备任务在占用该站点
|
/// </summary>
|
/// <param name="edm"></param>
|
/// <param name="stationCode">站点编号</param>
|
/// <returns></returns>
|
public static bool IsExistOtherTaskDoing(string stationCode)
|
{
|
using (DbModel edm = new DbModel())
|
{
|
int num = edm.Task_Part.Where(x =>
|
(x.TaskState == (int)DeviceTaskStatusEnum.未开始 || x.TaskState == (int)DeviceTaskStatusEnum.已下发)
|
&& (x.RealToPlace == stationCode || x.RealSourcePlace == stationCode)
|
).Count();
|
return num > 0 ? true : false;
|
}
|
}
|
|
/// <summary>
|
/// 是否存在其他设备任务在占用该站点
|
/// </summary>
|
/// <param name="edm"></param>
|
/// <param name="stationCode">站点编号</param>
|
/// <returns></returns>
|
public static bool IsExistOtherTaskDoing(int stationId)
|
{
|
using (DbModel edm = new DbModel())
|
{
|
int num = edm.Task_Part.Where(x =>
|
(x.TaskState == (int)DeviceTaskStatusEnum.未开始 || x.TaskState == (int)DeviceTaskStatusEnum.已下发)
|
&& (x.SourcePlace == stationId || x.ToPlace == stationId)
|
).Count();
|
return num > 0 ? true : false;
|
}
|
}
|
|
/// <summary>
|
/// 是否存在其他设备任务在占用该站点
|
/// </summary>
|
/// <param name="edm"></param>
|
/// <param name="placeId"></param>
|
/// <returns></returns>
|
public static bool IsExistOtherTaskDoing(DbModel edm, int placeId)
|
{
|
int num = edm.Task_Part.Where(x =>
|
(x.TaskState == (int)DeviceTaskStatusEnum.未开始 || x.TaskState == (int)DeviceTaskStatusEnum.已下发)
|
&& (x.SourcePlace == placeId || x.ToPlace == placeId)
|
).Count();
|
return num > 0 ? true : false;
|
}
|
|
/// <summary>
|
/// 是否存在其他设备任务(根据任务类型)
|
/// </summary>
|
/// <param name="edm"></param>
|
/// <param name="_deviceTaskTypeEnum"></param>
|
/// <returns>true:有任务占用,false:没有任务占用</returns>
|
public static bool IsExistOtherTaskDoingForDeviceTaskTypeEnum(DeviceTaskTypeEnum _deviceTaskTypeEnum)
|
{
|
using (DbModel edm = new DbModel())
|
{
|
var queryTaskType = (int)_deviceTaskTypeEnum;
|
int num = edm.Task_Part.Where(x =>
|
(x.TaskState == (int)DeviceTaskStatusEnum.未开始 || x.TaskState == (int)DeviceTaskStatusEnum.已下发)
|
&& (x.TaskType == queryTaskType)
|
).Count();
|
return num > 0 ? true : false;
|
}
|
}
|
|
|
|
|
|
/// <summary>
|
/// 创建一个设备任务
|
/// </summary>
|
/// <param name="edm"></param>
|
/// <param name="deviceType"></param>
|
/// <param name="userName"></param>
|
/// <param name="taskSequence"></param>
|
/// <param name="_DeviceTaskTypeEnum"></param>
|
/// <param name="mainTask"></param>
|
/// <param name="salver"></param>
|
/// <param name="sourcePlace"></param>
|
/// <param name="toPlace"></param>
|
/// <param name="description"></param>
|
/// <returns></returns>
|
public static FunRetEntity CreatePartTask(DateTime createTime, string plcTaskNo, DbModel edm,
|
EDeviceType deviceType,
|
string userName,
|
int taskSequence,
|
DeviceTaskTypeEnum _DeviceTaskTypeEnum,
|
Task_Main mainTask,
|
Base_Salver salver, Base_Station sourcePlace, Base_Station toPlace, string description)
|
{
|
var validate_deviceByPlace = "";
|
switch (_DeviceTaskTypeEnum)
|
{
|
case DeviceTaskTypeEnum.出库:
|
case DeviceTaskTypeEnum.立库空托到拆盘机入口:
|
validate_deviceByPlace = sourcePlace.SrmStationCode;
|
break;
|
case DeviceTaskTypeEnum.组盘入库:
|
case DeviceTaskTypeEnum.空托转运到立体库:
|
validate_deviceByPlace = toPlace.SrmStationCode;
|
break;
|
}
|
EDevice device = BusinessHelper.GetDeviceByPlaceNo(deviceType, validate_deviceByPlace);
|
var sourcePlaceNo = StationHandler.GetStationCodeByDeviceType(sourcePlace, deviceType);
|
var toPlaceNo = StationHandler.GetStationCodeByDeviceType(toPlace, deviceType);
|
|
FunRetEntity fre = new FunRetEntity();
|
fre.result = true;
|
var isEquipmentBreakdown1 = Wms_EquipmentSituationHandler.IsEquipmentBreakdown((int)device);
|
if (isEquipmentBreakdown1.result)
|
{//如果设备已经设定为 故障状态,不允许下发任务!!! [EditBy shaocx,2020-12-07]
|
fre.result = false;
|
fre.resMsg = isEquipmentBreakdown1.resMsg;
|
return fre;
|
}
|
|
StationHandler.SetPlaceLockStatus(true, userName, ref sourcePlace, description);
|
StationHandler.SetPlaceLockStatus(true, userName, ref toPlace, description);
|
StationHandler.SetPlaceTaskDoingStatus(true, userName, ref sourcePlace, description);
|
StationHandler.SetPlaceTaskDoingStatus(true, userName, ref toPlace, description);
|
Task_Part tsk = new Task_Part()
|
{
|
Id = Guid.NewGuid().ToString(),
|
|
InOutFlag = mainTask.InOutFlag,
|
InOutFlagName = mainTask.InOutFlagName,
|
|
DeviceType = (int)deviceType,
|
DeviceTypeName = deviceType.ToString(),
|
DeviceId = Convert.ToInt16(device),
|
DeviceName = Convert.ToString(device),
|
|
TaskSequence = taskSequence,
|
MainTaskSequence = mainTask.TaskSequence,//增加主任务优先级
|
TaskDescribe = description,
|
MainTaskId = mainTask.Id,
|
MainTaskNo = mainTask.TaskNo,
|
MaterialId = mainTask.MaterialId,
|
Qty = mainTask.Qty,//增加数量 【EditBy shaocx,2022-05-01】
|
|
//增加 订货号和序列号的赋值 [Editby shaocx,2022-04-21]
|
SerialNumber = mainTask.SerialNumber,
|
OrderNo = mainTask.OrderNo,
|
|
//ToNumber = toNumber,
|
//TaskGuid = taskGuid,
|
//MatID = MatID,
|
//MatName = MatName,
|
//Remark = "手动入库创建设备任务" + description,
|
//Creator = "手动入库创建设备任务" + "," + Dns.GetHostName(),
|
//UpdateUser = "手动入库创建设备任务" + "," + Dns.GetHostName(),
|
CreateTime = createTime,
|
ModifyTime = createTime,
|
|
PlcTaskId = plcTaskNo,
|
//ContainerId = ctn.ContainerId,
|
SalverId = salver.Id,
|
SalverCode = salver.SalverCode,
|
|
SourcePlace = sourcePlace.Id,
|
ToPlace = toPlace.Id,
|
RealSourcePlace = sourcePlaceNo,
|
RealToPlace = toPlaceNo,
|
|
TaskName = string.Format("(来源:id-{0} no-{1},目标:id-{2} no-{3})", sourcePlace.Id, sourcePlaceNo,
|
toPlace.Id, toPlaceNo),
|
|
IsReleased = false,
|
// IsCurrentTask = 0,
|
IsFinished = false,
|
//IsLastTask = 0,
|
//Void = 0,
|
TaskState = (int)DeviceTaskStatusEnum.未开始,
|
TaskStateName = DeviceTaskStatusEnum.未开始.ToString(),
|
TaskType = (int)_DeviceTaskTypeEnum,
|
TaskTypeName = _DeviceTaskTypeEnum.ToString(),
|
|
//增加物料类型 【EditBy shaocx,2022-05-04】
|
MaterialType = mainTask.MaterialType,
|
MaterialTypeName = mainTask.MaterialTypeName,
|
};
|
|
DoCreateSrmTask(edm, tsk);
|
return fre;
|
}
|
|
|
private static bool DoCreateSrmTask(DbModel edm, Task_Part tsk)
|
{
|
edm.Task_Part.Add(tsk);
|
return true;
|
}
|
|
|
/// <summary>
|
/// 移除30天前的任务
|
/// </summary>
|
/// <param name="edm"></param>
|
public static void DeletePartTask(DbModel edm)
|
{
|
var queryTime = DateTime.Now.AddDays(-30);
|
var queryTaskStaus1 = (int)DeviceTaskStatusEnum.已完成;
|
var queryTaskStaus2 = (int)DeviceTaskStatusEnum.已取消;
|
var removeList = edm.Task_Part.Where(x => x.CreateTime <= queryTime && (x.TaskState == queryTaskStaus1 || x.TaskState == queryTaskStaus2)).ToList();
|
if (removeList != null && removeList.Count > 0)
|
{
|
edm.Task_Part.RemoveRange(removeList);
|
}
|
var removeRecordList = edm.Task_RequestLog.Where(x => x.CreateTime <= queryTime).ToList();
|
if (removeRecordList != null && removeRecordList.Count > 0)
|
{
|
edm.Task_RequestLog.RemoveRange(removeRecordList);
|
}
|
}
|
|
#region 更新 OperationRemark
|
|
|
/// <summary>
|
/// 表PartTask写入OperationRemark
|
/// </summary>
|
/// <param name="partTaskId"></param>
|
/// <param name="errMsg"></param>
|
public static void WriteOperationRemarkForPartTask(string partTaskId, string errMsg)
|
{
|
try
|
{
|
using (DbModel edm = new DbModel())
|
{
|
var item = edm.Task_Part.Where(x => x.Id == partTaskId).FirstOrDefault();
|
if (item != null)
|
{
|
item.OperationRemark = errMsg;
|
item.ModifyTime = DateTime.Now;
|
item.ModifyBy = SysGloble.WCSSystem;
|
|
edm.SaveChanges();
|
}
|
}
|
}
|
catch (Exception)
|
{
|
|
throw;
|
}
|
}
|
|
///// <summary>
|
///// 表PartTask写入OperationRemark
|
///// </summary>
|
///// <param name="partTaskId"></param>
|
///// <param name="errMsg"></param>
|
//private void WriteOperationRemarkForPartTaskList(string errMsg)
|
//{
|
// try
|
// {
|
// using (DbModel edm = new DbModel())
|
// {
|
// var queryLesTaskStatus = Convert.ToInt16(LesTaskStatusEnum.新建任务);
|
// var pisList = edm.Task_Part.Where(x =>
|
// x.LesTaskStatus == queryLesTaskStatus).ToList();
|
// if (pisList != null && pisList.Count > 0)
|
// {
|
// foreach (var item in pisList)
|
// {
|
// item.OperationRemark = errMsg;
|
// item.ModifyTime = DateTime.Now;
|
// item.ModifyBy = SysGloble.WCSSystem;
|
// }
|
// edm.SaveChanges();
|
// }
|
// }
|
// }
|
// catch (Exception)
|
// {
|
|
// throw;
|
// }
|
//}
|
|
#endregion
|
|
}
|
}
|