using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.Mvc;
using QMFrameWork.WebUI.Attribute;
using QMAPP.Web.Models.Sys;
using QMAPP.Common.Web.Controllers;
using QMFrameWork.Data;
using QMAPP.Entity.Sys;
using QMFrameWork.WebUI.DataSource;
using QMFrameWork.Common.Serialization;
using QMAPP.Common.Web.Util;
using QMAPP.ServicesAgent;
namespace QMAPP.Web.Controllers
{
///
/// 数据处理任务控制器
/// 创建者:李炳海
/// 创建日期:2014.12.18
///
[SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
public class DataTaskPlanController : QController
{
#region 数据处理计划控制器
///
/// 任务信息列表加载
///
///
[HandleException]
public ActionResult DataTaskPlanList(bool? callBack)
{
DataTaskPlanModel seachModel = new DataTaskPlanModel();
if (callBack == true)
{
TryGetSelectBuffer(out seachModel);
}
seachModel.rownumbers = false;
seachModel.url = "/DataTaskPlan/GetList";
return View("DataTaskPlanList", seachModel);
}
///
/// 获取任务信息列表
///
/// 是否回调
///
[HandleException]
public ActionResult GetList(bool? callBack)
{
List plans = new List();
DataTaskPlanModel seachModel = null;
DataPage page = null;
DataTaskPlan condition = null;
try
{
//获取查询对象
seachModel = GetModel();
#region 获取缓存值
if (callBack != null)
{
TryGetSelectBuffer(out seachModel);
}
else
{
//保存搜索条件
SetSelectBuffer(seachModel);
}
#endregion
//获取前台分页设置信息
page = this.GetDataPage(seachModel);
//获取查询条件
condition = CopyToModel(seachModel);
//调用服务
QMAPP.ServicesAgent.TaskService.TaskServiceClient agent = new ServiceAgent().GetTaskService();
List tasks = agent.GetTaskList().ToList();
QMAPP.ServicesAgent.TaskService.PlanInfo[] list = agent.GetPlanList();
plans = this.GetDeakedPlans(list, tasks, condition.PlanName);
DataGridResult result = new DataGridResult();
result.Total = plans.Count;
result.Rows = plans;
return Content(result.GetJsonSource());
}
catch (Exception ex)
{
throw ex;
}
}
private List GetDeakedPlans(QMAPP.ServicesAgent.TaskService.PlanInfo[] list
, List tasks
,string planName)
{
List plans = new List();
foreach (QMAPP.ServicesAgent.TaskService.PlanInfo info in list)
{
//根据查询条件筛选符合条件的记录
if (!string.IsNullOrEmpty(planName))
{
if (string.IsNullOrEmpty(planName) || info.PlanName.IndexOf(planName) < 0)
{
continue;
}
}
var task = tasks.Find(p => p.TaskID == info.TaskID);
DataTaskPlanModel model = new DataTaskPlanModel
{
PlanID = info.PlanID,
PlanName = info.PlanName,
TaskID = task.TaskName,
PeriodType = info.PeriodType,
ExecuteDay = info.ExecuteDay,
Interval = info.Interval.ToString(),
IntervalType = info.IntervalType,
DayStartTime = info.DayStartTime,
DayEndTime = info.DayEndTime,
PlanStartDate = info.PlanStartDate.ToString("yyyy-MM-dd"),
PlanEndDate = info.PlanEndDate == new DateTime() ? "无" : info.PlanEndDate.ToString("yyyy-MM-dd"),
IsUse = info.IsUse.ToLower() == "true" ? "是" : "否",
Remark = info.Remark
};
string periodType = "";
string intervalType = "";
switch (info.IntervalType)
{
case "s":
intervalType = "秒";
break;
case "m":
intervalType = "分";
break;
case "h":
intervalType = "小时";
break;
}
switch (info.PeriodType)
{
case "d":
if (string.IsNullOrEmpty(info.OnceTime) == false)
{
periodType = "每天";
}
else
{
periodType = "每" + info.Interval + intervalType;
}
break;
case "w":
periodType = "每周";
break;
case "m":
if (info.Period == 1)
{
periodType = "每月";
}
else
{
periodType = "每" + info.Period.ToString() + "月";
}
break;
}
model.Interval = periodType;
plans.Add(model);
}
return plans;
}
#endregion
#region 任务信息添加、修改、删除
///
/// 任务信息编辑
///
/// 锁定信息
[HandleException]
public ActionResult DataTaskPlanEdit()
{
string planID = Request.Params["planID"];
DataTaskPlanModel model = new DataTaskPlanModel();
QMAPP.ServicesAgent.TaskService.PlanInfo plan = null;
try
{
if (string.IsNullOrEmpty(planID) == false)
{
//获取原信息
QMAPP.ServicesAgent.TaskService.TaskServiceClient agent = new ServiceAgent().GetTaskService();
plan = agent.GetPlanModel(planID);
model = CopyToModel(plan);
switch (plan.PeriodType)
{
case "d":
model.DayPeriod = plan.Period;
break;
case "w":
string[] weeks = plan.ExecuteDay.Split(",".ToCharArray());
foreach (string w in weeks)
{
switch (w)
{
case "1":
model.Week1 = true;
break;
case "2":
model.Week2 = true;
break;
case "3":
model.Week3 = true;
break;
case "4":
model.Week4 = true;
break;
case "5":
model.Week5 = true;
break;
case "6":
model.Week6 = true;
break;
case "7":
model.Week7 = true;
break;
}
}
break;
case "m":
if (plan.ExecuteDay.IndexOf("/") > 0)
{
//间隔
model.MonthPeriod2 = plan.Period;
string[] days = plan.ExecuteDay.Split("/".ToCharArray());
model.MonthDay3 = int.Parse(days[1]);
model.MonthMode = "2";
}
else
{
//范围
model.MonthPeriod1 = plan.Period;
string[] days = plan.ExecuteDay.Split("-".ToCharArray());
model.MonthDay1 = int.Parse(days[0]);
model.MonthDay2 = int.Parse(days[1]);
model.MonthMode = "1";
}
break;
}
if (string.IsNullOrEmpty(plan.OnceTime) == false)
{
//每天执行一次
model.DayMode = "1";
if (string.IsNullOrEmpty(plan.OnceTime) == false)
model.OnceTime = plan.OnceTime;
}
else
{
//循环执行
model.OnceTime = "00:00:00";
model.DayMode = "2";
model.Interval = plan.Interval.ToString();
model.IntervalType = plan.IntervalType;
if (string.IsNullOrEmpty(plan.DayStartTime) == false)
model.DayStartTime = plan.DayStartTime;
if (string.IsNullOrEmpty(plan.DayEndTime) == false)
model.DayEndTime = plan.DayEndTime;
}
model.PlanStartDate = plan.PlanStartDate.ToString("yyyy-MM-dd HH:mm:ss");
if (plan.PlanEndDate != new DateTime())
{
model.PlanEndDate = plan.PlanEndDate.ToString("yyyy-MM-dd HH:mm:ss");
model.PlanDateMode = "1";
}
else
{
model.PlanEndDate = "";
model.PlanDateMode = "2";
}
model.Remark = plan.Remark;
model.IsUseFlg = plan.IsUse=="true"?true:false;
}
else
{
model.Interval = "1";
model.IntervalType = "m";
model.PeriodType = "d";
model.DayPeriod = 1;
model.MonthMode = "1";
model.DayMode = "2";
model.PlanDateMode = "2";
model.OnceTime = "00:00:00";
model.DayStartTime = "00:00:00";
model.DayEndTime = "23:59:59";
model.MonthDay1 = 1;
model.MonthDay2 = 1;
model.MonthDay3 = 1;
model.MonthPeriod1 = 1;
model.MonthPeriod2 = 1;
model.DayPeriod = 1;
}
return View(model);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 任务信息保存
///
/// 任务信息
///
[HandleException]
public ActionResult DataTaskPlanSave(DataTaskPlanModel model)
{
QMAPP.ServicesAgent.TaskService.PlanInfo plan = new ServicesAgent.TaskService.PlanInfo();
try
{
plan = CopyToModel(model);
plan.PlanName = model.PlanName;
plan.TaskID = model.TaskID;
plan.PeriodType = model.PeriodType;
switch (plan.PeriodType)
{
case "d":
plan.Period = model.DayPeriod;
break;
case "w":
string weeks = "";
weeks = string.Format("{0}{1}{2}{3}{4}{5}{6}"
, model.Week1== true ? ",1" : ""
, model.Week2 == true ? ",2" : ""
, model.Week3 == true ? ",3" : ""
, model.Week4 == true ? ",4" : ""
, model.Week5 == true ? ",5" : ""
, model.Week6 == true ? ",6" : ""
, model.Week7 == true ? ",7" : "");
if (weeks != "")
{
weeks = weeks.Substring(1);
}
plan.ExecuteDay = weeks;
if (plan.ExecuteDay == "")
{
//MessageBox.Show("请至少选择一天。");
///return;
}
break;
case "m":
if (model.MonthMode == "1")
{
//范围执行
plan.Period = model.MonthPeriod1;
plan.ExecuteDay = string.Format("{0}-{1}", model.MonthDay1, model.MonthDay2);
}
else
{
//间隔执行
plan.Period = model.MonthPeriod2;
plan.ExecuteDay = string.Format("1/{0}", model.MonthDay3);
}
break;
}
if (model.DayMode == "1")
{
//每天执行一次
plan.OnceTime = DateTime.Parse(model.OnceTime).Hour.ToString()
+ ":" + DateTime.Parse(model.OnceTime).Minute.ToString()
+ ":" + DateTime.Parse(model.OnceTime).Second.ToString();
}
else
{
//循环执行
plan.Interval = int.Parse(model.Interval);
plan.IntervalType = model.IntervalType;
plan.OnceTime = "";
}
plan.DayStartTime = model.DayStartTime;
plan.DayEndTime = model.DayEndTime;
plan.PlanStartDate = DateTime.Parse(model.PlanStartDate);
if (model.PlanDateMode == "2")
{
plan.PlanEndDate = new DateTime();
}
else
{
plan.PlanEndDate = DateTime.Parse(model.PlanEndDate);
}
plan.Remark = model.Remark;
plan.IsUse = model.IsUseFlg == true ? "true" : "false";
QMAPP.ServicesAgent.TaskService.TaskServiceClient agent = new ServiceAgent().GetTaskService();
if (string.IsNullOrEmpty(plan.PlanID) == true)
{
plan.PlanID = Guid.NewGuid().ToString();
agent.AddPlan(plan);
}
else
{
agent.UpdatePlan(plan);
}
return this.GetJsViewResult(string.Format("parent.List();parent.showTitle('{0}');parent.closeAppWindow1();"
, AppResource.SaveMessge));
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 任务信息删除
///
///
[HandleException]
public ActionResult DataTaskPlanDelete()
{
string selectKey = Request.Form["selectKey"];
string[] list = selectKey.Split(":".ToCharArray());
try
{
QMAPP.ServicesAgent.TaskService.TaskServiceClient agent = new ServiceAgent().GetTaskService();
//删除
foreach (string planID in list)
{
agent.DeletePlan(new ServicesAgent.TaskService.PlanInfo {PlanID=planID });
}
return DataTaskPlanList(true);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 任务信息启用
///
///
[HandleException]
public ActionResult DataTaskPlanStart()
{
string selectKey = Request.Form["selectKey"];
string[] list = selectKey.Split(":".ToCharArray());
try
{
QMAPP.ServicesAgent.TaskService.TaskServiceClient agent = new ServiceAgent().GetTaskService();
//启用
foreach (string planID in list)
{
agent.StartPlan(new ServicesAgent.TaskService.PlanInfo { PlanID = planID });
}
return DataTaskPlanList(true);
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 任务信息启用
///
///
[HandleException]
public ActionResult DataTaskPlanStop()
{
string selectKey = Request.Form["selectKey"];
string[] list = selectKey.Split(":".ToCharArray());
try
{
QMAPP.ServicesAgent.TaskService.TaskServiceClient agent = new ServiceAgent().GetTaskService();
//启用
foreach (string planID in list)
{
agent.StopPlan(new ServicesAgent.TaskService.PlanInfo { PlanID = planID });
}
return DataTaskPlanList(true);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取任务列表
///
/// 获取任务列表
///
/// 数据源
[HandleException]
public ActionResult GetTaskList()
{
ComboboxResult model = new ComboboxResult();
try
{
QMAPP.ServicesAgent.TaskService.TaskServiceClient agent = new ServiceAgent().GetTaskService();
List tasks = agent.GetTaskList().ToList();
model.Add(new ComboboxItem { ID = "", Text = new DictController().EmptyItemTitle });
foreach (QMAPP.ServicesAgent.TaskService.TaskInfo task in tasks)
{
model.Add(new ComboboxItem { ID = task.TaskID, Text = task.TaskName });
}
return Content(model.ToString());
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取周期类型列表
///
/// 获取周期类型列表
///
/// 数据源
[HandleException]
public ActionResult GetPeriodTypeList()
{
ComboboxResult model = new ComboboxResult();
try
{
Dictionary periodTypes=new Dictionary();
QMFrameWork.Common.Util.ModelDictionaryHandler.TryGetModelDictionary("PeriodType", out periodTypes);
foreach (string key in periodTypes.Keys)
{
model.Add(new ComboboxItem { ID = key, Text = periodTypes[key] });
}
return Content(model.ToString());
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取频率类型列表
///
/// 获取频率类型列表
///
/// 数据源
[HandleException]
public ActionResult GetIntervalTypeList()
{
ComboboxResult model = new ComboboxResult();
try
{
Dictionary periodTypes = new Dictionary();
QMFrameWork.Common.Util.ModelDictionaryHandler.TryGetModelDictionary("IntervalType", out periodTypes);
foreach (string key in periodTypes.Keys)
{
model.Add(new ComboboxItem { ID = key, Text = periodTypes[key] });
}
return Content(model.ToString());
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}