using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; using Serilog; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; using WPFWCSDemo.Data; using WPFWCSDemo.Models.Tables; using WPFWCSDemo.StateCode; namespace WPFWCSDemo.Utilities.Table { public class TaskManager { /// /// 获取【调度管理】表所有行数据集合 /// /// 返回所有行的数据 public static List ListAll() { using (DataTable? dt = SqlHelper.ExecuteTable("SELECT * FROM TASK ORDER BY CREATETIME DESC")) { if (dt == null || dt.Rows.Count == 0) return null; List taskRTs = new List(); foreach (DataRow dr in dt.Rows) { taskRTs.Add(ToModels(dr)); } #region 反射方法 /* foreach (DataRow dr in dt.Rows) { tasks.Add(dr.DataRowToModel()); } */ #endregion return taskRTs; } } /// /// 将【调度管理】表的单行转换为实体数据对象 /// /// 获取表的某行数据 /// 当前行所有字段的集合 public static TaskRT? ToModels(DataRow dr) { TaskRT taskRT = new TaskRT(); try { taskRT.JobId = dr["jobId"].ToString(); taskRT.TaskId = dr["TaskId"].ToString(); //taskRT.PalletId = dr["PalletId"].ToString(); //taskRT.Priority = (int)dr["Priority"]; taskRT.SourceLocation = dr["SourceLocation"].ToString(); taskRT.TargetLocation = dr["TargetLocation"].ToString(); taskRT.TaskType = (int)dr["TaskType"]; taskRT.TaskState = dr["TaskState"].ToString(); taskRT.ScheduleState = dr["ScheduleState"].ToString(); taskRT.CreateTime = Convert.ToDateTime(dr["CreateTime"]); taskRT.ScheduleTime = Convert.ToDateTime(dr["ScheduleTime"]); //taskRT.ReportState = (int)dr["ReportState"]; return taskRT; } catch (Exception e) { Log.Warning($"【TaskManager】实体类型转换失败:{e.Message}"); Log.Warning($"【TaskManager】{JsonConvert.SerializeObject(taskRT)}"); } return null; } /// /// 向【调度管理】表中插入数据 /// /// /// 返回受影响的行数 public static int Insert(TaskRT taskRT, int type) { switch (type) { case 1: return SqlHelper.ExecuteNonQuery("INSERT INTO TASK(TASKID,PalletId,SourceLocation,TARGET,TASKTYPE)" + "VALUES(@TaskId,@PalletId,@SourceLocation,@Target,@TaskType)", new SqlParameter("@TaskId", taskRT.TaskId), new SqlParameter("@PalletId", taskRT.PalletId), new SqlParameter("@SourceLocation", taskRT.SourceLocation), new SqlParameter("@TargetLocation", taskRT.TargetLocation), new SqlParameter("@TaskType", taskRT.TaskType) ); case 2: return SqlHelper.ExecuteNonQuery("INSERT INTO TASK(TASKID,SourceLocation,TARGETLOCATION,TASKTYPE)" + "VALUES(@TaskId,@Origin,@Target,@TaskType)", new SqlParameter("@TaskId", taskRT.TaskId), new SqlParameter("@SourceLocation", taskRT.SourceLocation), new SqlParameter("@TargetLocation", taskRT.TargetLocation), new SqlParameter("@TaskType", taskRT.TaskType) ); case 3: return SqlHelper.ExecuteNonQuery("INSERT INTO TASK(TASKID,SourceLocation,TARGETLOCATION,TASKTYPE)" + "VALUES(@TaskId,@Origin,@Target,@TaskType)", new SqlParameter("@TaskId", taskRT.TaskId), new SqlParameter("@SourceLocation", taskRT.SourceLocation), new SqlParameter("@TargetLocation", taskRT.TargetLocation), new SqlParameter("@TaskType", taskRT.TaskType) ); default: Log.Error("[Schedule]下发任务错误[Schedule]"); return 0; } } /// /// 从【调度管理】表中移除数据记入历史表 /// /// 当前已完成的任务 /// 执行后受影响的行数 public static int Delete(TaskRT taskRT) { try { using var context = new AppDbContext(); TaskHistory taskHistory = new TaskHistory() { JobId = taskRT.JobId, TaskId = taskRT.TaskId, //PalletId = taskRT.PalletId, TaskType = taskRT.TaskType.ToString(), TaskState = taskRT.TaskState, ScheduleState = taskRT.ScheduleState, SourceLocation = taskRT.SourceLocation, TargetLocation = taskRT.TargetLocation, CreateTime = taskRT.CreateTime, ScheduleTime = taskRT.ScheduleTime, FinishTime = DateTime.Now, Id = Guid.NewGuid(), }; //1--------------向【调度历史】表添加数据 context.TaskHistories.Add(taskHistory); //2--------------移除【调度管理】表数据 context.Remove(taskRT); context.SaveChanges(); return 1; } catch (DBConcurrencyException e) { Log.Error($"【WCS移除已完成任务<异常>】任务号:{taskRT.TaskId},{e.Message}"); Log.Error($"【WCS移除已完成任务<异常>】任务号:{taskRT.TaskId},{e.InnerException}"); } return 0; } } }