266 lines
9.2 KiB
C#
266 lines
9.2 KiB
C#
using Cowain.Bake.Common.Core;
|
||
using Cowain.Bake.Common.Enums;
|
||
using Cowain.Bake.Model;
|
||
using Cowain.Bake.Model.Entity;
|
||
using MySql.Data.MySqlClient;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Data;
|
||
using System.Data.Entity;
|
||
using System.Linq;
|
||
using Unity;
|
||
|
||
namespace Cowain.Bake.BLL
|
||
{
|
||
public class TaskRecordService : ServiceBase
|
||
{
|
||
public TaskRecordService(IUnityContainer unityContainer) : base(unityContainer)
|
||
{
|
||
}
|
||
|
||
public List<TTaskRecord> GetAllTaskRun()
|
||
{
|
||
using (var Context = new BakingEntities())
|
||
{
|
||
var taskRunList = Context.Set<TTaskRecord>().OrderBy(x => x.Status).ThenByDescending(x => x.BuildTime).Take(1000).ToList();
|
||
if (taskRunList == null)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
return taskRunList;
|
||
}
|
||
}
|
||
|
||
public List<TTaskRecord> Query(DateTime startTime, DateTime endTime)
|
||
{
|
||
using (var Context = new BakingEntities())
|
||
{
|
||
return Context.Set<TTaskRecord>().Where(x => x.BuildTime >= startTime && x.BuildTime <= endTime)
|
||
.OrderByDescending(x=>x.Id).ToList();
|
||
}
|
||
}
|
||
|
||
public int UpdateFinishStepId(int taskId)
|
||
{
|
||
using (var Context = new BakingEntities())
|
||
{
|
||
var task = Context.Set<TTaskRecord>().Where(x => x.Id == taskId).ToList().FirstOrDefault();
|
||
task.StepId = (int)ETaskStep.Finish;
|
||
return Context.SaveChanges();
|
||
}
|
||
}
|
||
|
||
public int DeleteNotFinishTask()
|
||
{
|
||
using (var Context = new BakingEntities())
|
||
{
|
||
var trq = (from a in Context.Set<TTaskRecord>()
|
||
where a.Status != (int)ETaskStatus.ExecutionCompleted
|
||
select a);
|
||
if (trq == null)
|
||
{
|
||
return 0;
|
||
}
|
||
foreach (var item in trq)
|
||
{
|
||
Context.Set<TTaskRecord>().Remove(item);
|
||
}
|
||
return Context.SaveChanges();
|
||
}
|
||
}
|
||
public int DeleteTask(long id)
|
||
{
|
||
using (var Context = new BakingEntities())
|
||
{
|
||
var trq = (from a in Context.Set<TTaskRecord>()
|
||
where a.Id == id
|
||
select a).FirstOrDefault();
|
||
if (trq == null)
|
||
{
|
||
return 0;
|
||
}
|
||
Context.Set<TTaskRecord>().Remove(trq);
|
||
return Context.SaveChanges();
|
||
}
|
||
}
|
||
|
||
public int DeleteUnexecuteTask()
|
||
{
|
||
using (var Context = new BakingEntities())
|
||
{
|
||
var trq = (from a in Context.Set<TTaskRecord>()
|
||
where a.Status != (int)ETaskStatus.ExecutionCompleted
|
||
select a).ToList();
|
||
if (trq == null)
|
||
{
|
||
return 0;
|
||
}
|
||
Context.Set<TTaskRecord>().RemoveRange(trq);
|
||
return Context.SaveChanges();
|
||
}
|
||
}
|
||
|
||
//是否有未执行或执行中的任务(PLC有任务要执行)
|
||
public TTaskRecord UnexecuteTask()
|
||
{
|
||
using (var Context = new BakingEntities())
|
||
{
|
||
return Context.Set<TTaskRecord>().Where(x => x.Status != (int)ETaskStatus.ExecutionCompleted).FirstOrDefault();
|
||
}
|
||
}
|
||
|
||
//public TaskEntity GetManualTask()
|
||
//{
|
||
// using (var Context = new BakingEntities())
|
||
// {
|
||
// var trq = (from a in Context.Set<TTaskRecord>()
|
||
// where a.Status != (int)ETaskStep.ExecutionCompleted
|
||
// orderby a.Id descending
|
||
// select a).FirstOrDefault();
|
||
|
||
// return null;
|
||
// }
|
||
//}
|
||
|
||
public int UpdateTask()
|
||
{
|
||
using (var Context = new BakingEntities())
|
||
{
|
||
LogHelper.Instance.Debug("修改任务开始");
|
||
return Context.Database.ExecuteSqlCommand("call ProcUpdateTask()");
|
||
}
|
||
}
|
||
|
||
public int BindPalletToRobot()
|
||
{
|
||
using (var Context = new BakingEntities())
|
||
{
|
||
return Context.Database.ExecuteSqlCommand("call ProcBindPalletToRobot()");
|
||
}
|
||
}
|
||
|
||
public (bool, int) IsPullInBaker()
|
||
{
|
||
using (var Context = new BakingEntities())
|
||
{
|
||
var record = (from taskRecord in Context.Set<TTaskRecord>()
|
||
join taskType in Context.Set<TTaskType>() on taskRecord.TaskTypeId equals taskType.Id
|
||
orderby taskRecord.Id descending
|
||
//where taskType.Id == 1 //上料满夹具->烤箱
|
||
select new
|
||
{
|
||
TypeId = taskType.Id,
|
||
PalletId = taskRecord.PalletId,
|
||
}).FirstOrDefault();
|
||
|
||
if (null == record)
|
||
{
|
||
return (false, 0);
|
||
}
|
||
|
||
if (1 != record.TypeId) //where taskType.Id == 1 //上料满夹具->烤箱
|
||
{
|
||
return (false, 0);
|
||
}
|
||
|
||
return (true, record.PalletId);
|
||
}
|
||
}
|
||
|
||
public TaskEntity GetTask()
|
||
{
|
||
try
|
||
{
|
||
using (var Context = new BakingEntities())
|
||
{
|
||
TaskEntity result = Context.Database.SqlQuery<TaskEntity>("CALL ProcGetTask(1);").FirstOrDefault(); //显示FirstOrDefault崩溃,
|
||
|
||
if (null == result)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
result.Status = (int)ETaskStatus.UnExecute;
|
||
result.StepId = (int)ETaskStep.Unexecuted;
|
||
result.BuildTime = DateTime.Now;
|
||
TTaskRecord taskRecord = new TTaskRecord()
|
||
{
|
||
PalletId = result.PalletId,
|
||
TaskTypeId = result.TaskTypeId,
|
||
Source = result.Source,
|
||
Target = result.Target,
|
||
Status = result.Status,
|
||
BuildTime = result.BuildTime,
|
||
StepId = result.StepId,
|
||
};
|
||
|
||
Context.Set<TTaskRecord>().Add(taskRecord);
|
||
if (0 >= Context.SaveChanges())
|
||
{
|
||
LogHelper.Instance.Error("任务插入数据库失败,{num}!");
|
||
return null;
|
||
}
|
||
result.Id = taskRecord.Id;
|
||
return result;
|
||
}
|
||
}
|
||
catch(Exception ex)
|
||
{
|
||
LogHelper.Instance.Fatal($"获取任务信息时崩溃,{ex.Message},{ex.StackTrace}");
|
||
}
|
||
|
||
return null;
|
||
}
|
||
|
||
public bool ModifyTaskStatus(ETaskStatus status)
|
||
{
|
||
using (var Context = new BakingEntities())
|
||
{
|
||
var task = (from a in Context.Set<TTaskRecord>()
|
||
where a.Status != (int)ETaskStatus.ExecutionCompleted
|
||
orderby a.Id descending //只修改ID最大的
|
||
select a).FirstOrDefault();
|
||
|
||
if (task == null)
|
||
{
|
||
LogHelper.Instance.Error($"没有找到任务,所以修改任务状态失败!状态为:{status}");
|
||
return false;
|
||
}
|
||
|
||
//修改状态时间
|
||
if (ETaskStatus.ExecutionCompleted == status)
|
||
{
|
||
task.EndTime = DateTime.Now;
|
||
}
|
||
else if (ETaskStatus.Executing == status)
|
||
{
|
||
task.StartTime = DateTime.Now;
|
||
}
|
||
|
||
task.Status = (int)status;
|
||
return Context.SaveChanges() > 0 ? true : false;
|
||
}
|
||
}
|
||
|
||
//public TaskStatusEntity GetTaskShowInfo()
|
||
//{
|
||
// // sql = $@"SELECT t1.id, t1.Source,t1.Target,ti.PalletCode
|
||
// //,CASE t1.Status WHEN 0 THEN '未执行' WHEN 1 THEN '执行中' WHEN 2 THEN '执行完成' ELSE '未执行' END 'Status'
|
||
// // FROM TTaskRecord t1
|
||
// //LEFT JOIN TPalletInfo ti ON t1.PalletId=ti.Id
|
||
// //ORDER BY t1.Id DESC LIMIT 1";
|
||
// return (from t in Context.Set<TTaskRecord>()
|
||
// join p in Context.Set<TPalletInfo>() on t.PalletId equals p.Id
|
||
// orderby t.Id descending
|
||
// select new TaskStatusEntity
|
||
// {
|
||
// Source = t.Source,
|
||
// Target = t.Target,
|
||
// Status = ((ETaskStep)t.Status).GetDescription(),
|
||
// PalletCode = p.PalletCode
|
||
// }).FirstOrDefault();
|
||
//}
|
||
}
|
||
}
|