Files
6098/Cowain.Bake.BLL/TaskTypeService.cs

157 lines
5.2 KiB
C#

using Cowain.Bake.Common.Core;
using Cowain.Bake.Common.Enums;
using Cowain.Bake.Model;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Unity;
namespace Cowain.Bake.BLL
{
public class TaskTypeService : ServiceBase
{
public TaskTypeService(IUnityContainer unityContainer) : base(unityContainer)
{
}
public List<TTaskType> GetAll()
{
using (var Context = new BakingEntities())
{
return Context.Set<TTaskType>().OrderBy(x => x.Id).ToList();
}
}
public TTaskType GetTaskType()
{
using (var Context = new BakingEntities())
{
var task = (from t in Context.Set<TTaskType>()
join r in Context.Set<TTaskRecord>() on t.Id equals r.TaskTypeId
where r.Status != (int)ETaskStatus.ExecutionCompleted
select t).FirstOrDefault();
return task;
}
}
public bool IsPullInBaker(int taskTypeId)
{
using (var Context = new BakingEntities())
{
var task = (from t in Context.Set<TTaskType>()
where t.Id == taskTypeId
select t).FirstOrDefault();
if (null == task
|| 1 != task.Id) // 1: 上料满夹具->烤箱
{
return false;
}
return true;
}
}
/// <summary>
/// 修改任务
/// </summary>
public int UpdateEnableTask(int id, bool enable)
{
using (var Context = new BakingEntities())
{
var pi = (from ts in Context.Set<TTaskType>()
where ts.Id == id
select ts).FirstOrDefault();
if (pi == null)
{
//任务不存在,无法修改
return 0;
}
pi.Enable = enable;
Context.Entry(pi).State = EntityState.Modified;
return Context.SaveChanges();
}
}
/// <summary>
/// 添加任务
/// </summary>
public int AddTaskConfig(TTaskType task)
{
using (var Context = new BakingEntities())
{
//var q = (from ts in Context.Set<TTaskType>()
// where ts.Priority == task.Priority
// select ts).Count();
//if (q > 0)
//{
// //优先级已经存在
// LogHelper.Instance.Error("优先级已经存在");
// return 0;
//}
var q = (from ts in Context.Set<TTaskType>()
where ts.SourceDeviceType == task.SourceDeviceType
&& ts.TargetDeviceType == task.TargetDeviceType
&& ts.PalletStatus == task.PalletStatus
select ts).Count();
if (q > 0)
{
//相同的任务已经存在
LogHelper.Instance.Error("相同的任务已经存在,【源设备类型】,【目标设置类型】,【状态】三个合起来,构成一个任务!", true);
return 0;
}
//可以新增
Context.Set<TTaskType>().Add(task);
return Context.SaveChanges();
}
}
/// <summary>
/// 修改任务
/// </summary>
public int EditTaskConfig(TTaskType task)
{
//【源设备类型】,【目标设置类型】,【状态】,三个合起来,构成一个唯一 ,否则就闪退
using (var Context = new BakingEntities())
{
//bool has = Context.Set<TTaskType>().Any(u => u.SourceDeviceType == task.SourceDeviceType
//&& u.TargetDeviceType == task.TargetDeviceType
//&& u.PalletStatus == task.PalletStatus);
//if (has)
//{
// return 0;
//}
var pi = (from ts in Context.Set<TTaskType>()
where ts.Id == task.Id
select ts).FirstOrDefault();
if (pi == null)
{
//任务不存在,无法修改
LogHelper.Instance.Error("任务不存在,无法修改");
return 0;
}
pi.Name = task.Name;
pi.SourceDeviceType = task.SourceDeviceType;
pi.TargetDeviceType = task.TargetDeviceType;
pi.PalletStatus = task.PalletStatus;
pi.Priority = task.Priority;
pi.Enable = task.Enable;
pi.Json = task.Json;
//可以新增
Context.Entry(pi).State = EntityState.Modified;
return Context.SaveChanges();
}
}
}
}