using iWareCc.DecomposeTask.Entity;
using iWareCc.Properties;
using iWareCommon.Common.Entity;
using iWareCommon.Utils;
using iWareDataCore.ORM;
using iWareDataCore.TASK.Entity;
using iWareDataCore.TASK.EnumType;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace iWareCc.DecomposeTask.Chain
{
public class 按策略获取一个待分解任务: IHandler
{
private IHandler nextHandler = null;
///
/// 该节点的下一个节点
///
public IHandler NextHandler
{
set { nextHandler = value; }
}
///
/// 主任务容器
///
private MainTaskContainer MainTaskContainer;
///
/// 从数据库里提取一个待分解的主任务
///
public 按策略获取一个待分解任务(MainTaskContainer mainTaskContainer)
{
this.MainTaskContainer = mainTaskContainer;
}
///
/// 从数据库中提取一个未分解的主任务(按照分解次数,优先级,创建时间排序)
///
public void Handle()
{
using (var dbModel = new DbModelCore())
{
try
{
TASKMainTask mTask = null;
var taskPart = dbModel.TASKPartTasks.FirstOrDefault(x => x.maintasktype == (int)EMainTaskType.出库任务 && x.isfinished == 0);
if (taskPart != null)
{
mTask = dbModel.TASKMainTasks.OrderBy(x => x.decompositiontimes).ThenBy(x => x.sendtime).ThenBy(x => x.priority).FirstOrDefault(x => x.status == (int)EMainTaskStatus.未分解 && x.tasktype == (int)EMainTaskType.入库任务);
}
else
{
mTask = dbModel.TASKMainTasks.OrderBy(x => x.tasktype == (int)EMainTaskType.出库任务).OrderBy(x => x.decompositiontimes).ThenBy(x => x.sendtime).ThenBy(x => x.priority).FirstOrDefault(x => x.status == (int)EMainTaskStatus.未分解);
if (mTask == null)
{
mTask = dbModel.TASKMainTasks.OrderBy(x => x.decompositiontimes).ThenBy(x => x.sendtime).ThenBy(x => x.priority).FirstOrDefault(x => x.status == (int)EMainTaskStatus.未分解);
}
}
if (mTask == null)
{
LogTextHelper.WriteLine(Resources.LogDir + @"/分解任务流程", "获取第一个分解任务:暂无待分解的任务");
return;
}
if (taskPart != null && mTask.tasktype == (int)EMainTaskType.出库任务)
{
LogTextHelper.WriteLine(Resources.LogDir + @"/分解任务流程", "当前存在未完成的出库任务");
return;
}
mTask.decompositiontimes += 1;
if (mTask.decompositiontimes >= 100)
{
mTask.decompositiontimes = 0;
}
dbModel.SaveChanges();
MainTaskContainer.MainTask = new MainTaskEntity(mTask);
LogTextHelper.WriteLine(Resources.LogDir + @"/分解任务流程", "获取第一个分解任务:{0}", mTask.id);
if (nextHandler != null)
{
nextHandler.Handle();
}
}
catch (Exception ex)
{
LogTextHelper.WriteLog(Resources.LogDir, this.ToString(), "Handle", ex.Message);
}
}
}
}
}