using Learun.Util;
using System;
using System.Collections.Generic;
namespace Learun.Application.Extention.TaskScheduling
{
///
/// 版 本 Learun-ADMS V7.0.3 力软敏捷开发框架
/// Copyright (c) 2013-2018 上海力软信息技术有限公司
/// 创 建:超级管理员
/// 日 期:2019-01-09 16:07
/// 描 述:任务计划模板信息
///
public class TSSchemeBLL : TSSchemeIBLL
{
private TSSchemeService schemeService = new TSSchemeService();
private TSProcessIBLL tSProcessIBLL = new TSProcessBLL();
#region 获取数据
///
/// 获取页面显示列表数据
///
/// 分页参数
/// 查询参数
///
public IEnumerable GetPageList(Pagination pagination, string queryJson)
{
try
{
var list = (List)schemeService.GetPageList(pagination, queryJson);
var list2 = list.FindAll(t=>(t.F_State == 1 || t.F_State == 2) && t.F_EndTime < DateTime.Now);
foreach (var item in list2) {
item.F_State = 4;
TSProcessEntity tSProcessEntity = new TSProcessEntity()
{
F_Id = item.F_PorcessId,
F_State = 4
};
tSProcessIBLL.SaveEntity(item.F_PorcessId, tSProcessEntity);
}
return list;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 获取模板的历史数据
///
/// 分页参数
/// 查询参数
///
public IEnumerable GetSchemePageList(Pagination pagination, string queryJson)
{
try
{
return schemeService.GetSchemePageList(pagination, queryJson);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 获取表实体数据
/// 主键
///
///
public TSSchemeInfoEntity GetSchemeInfoEntity(string keyValue)
{
try
{
return schemeService.GetSchemeInfoEntity(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 获取表实体数据
/// 主键
///
///
public TSSchemeEntity GetSchemeEntity(string keyValue)
{
try
{
return schemeService.GetSchemeEntity(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 获取表实体数据
/// 模板信息主键
///
///
public TSSchemeEntity GetSchemeEntityByInfo(string keyValue)
{
try
{
return schemeService.GetSchemeEntityByInfo(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
#endregion
#region 提交数据
///
/// 删除实体数据
/// 主键
///
///
public void DeleteEntity(string keyValue)
{
try
{
schemeService.DeleteEntity(keyValue);
QuartzHelper.DeleteJob(keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 保存实体数据(新增、修改)
/// 主键
///
///
public void SaveEntity(string keyValue, TSSchemeInfoEntity schemeInfoEntity, TSSchemeEntity schemeEntity)
{
try
{
TSProcessEntity tSProcessEntity = schemeService.SaveEntity(keyValue,schemeInfoEntity, schemeEntity);
if (tSProcessEntity != null) {
QuartzHelper.DeleteJob(keyValue);
QuartzHelper.AddJob(schemeInfoEntity.F_Id, tSProcessEntity.F_Id, schemeEntity.F_Scheme.ToObject());
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 保存实体数据(新增、修改)
/// 主键
///
///
public void SaveEntity(string keyValue, TSSchemeInfoEntity entity)
{
try
{
schemeService.SaveEntity(keyValue, entity);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
#endregion
#region 扩展应用
///
/// 暂停任务
///
/// 任务进程主键
public void PauseJob(string processId) {
try
{
TSProcessEntity tSProcessEntity = tSProcessIBLL.GetProcessEntity(processId);
if (tSProcessEntity.F_State == 1 || tSProcessEntity.F_State == 2)
{
tSProcessEntity.F_State = 3;
tSProcessIBLL.SaveEntity(processId, tSProcessEntity);
QuartzHelper.DeleteJob(tSProcessEntity.F_SchemeInfoId);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
///
/// 启动任务
///
/// 任务进程主键
public void EnAbleJob(string processId)
{
try
{
TSProcessEntity tSProcessEntity = tSProcessIBLL.GetProcessEntity(processId);
TSSchemeEntity tSSchemeEntity = GetSchemeEntityByInfo(tSProcessEntity.F_SchemeInfoId);
TSSchemeModel tSSchemeModel = tSSchemeEntity.F_Scheme.ToObject();
if (tSProcessEntity.F_SchemeId != tSSchemeEntity.F_Id)
{
tSProcessEntity.F_State = 10;
tSProcessIBLL.SaveEntity(tSProcessEntity.F_Id, tSProcessEntity);
// 如果模板更改需要重新创建一个任务进程
tSProcessEntity = new TSProcessEntity()
{
F_SchemeId = tSSchemeEntity.F_Id,
F_SchemeInfoId = tSProcessEntity.F_SchemeInfoId,
F_State = 2,
F_EndType = tSSchemeModel.endType,
F_EndTime = tSSchemeModel.endTime
};
if (tSSchemeModel.startType == 1)
{
tSProcessEntity.F_BeginTime = DateTime.Now;
}
else
{
tSProcessEntity.F_BeginTime = tSSchemeModel.startTime;
}
if (tSSchemeModel.endType == 1)
{
tSProcessEntity.F_EndTime = DateTime.MaxValue;
}
tSProcessIBLL.SaveEntity("", tSProcessEntity);
}
else
{
tSProcessEntity.F_State = 2;
tSProcessIBLL.SaveEntity(tSProcessEntity.F_Id, tSProcessEntity);
}
QuartzHelper.AddJob(tSProcessEntity.F_SchemeInfoId, tSProcessEntity.F_Id, tSSchemeModel);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowBusinessException(ex);
}
}
}
#endregion
}
}