OpenAuth.Net/OpenAuth.Mvc/Controllers/CommonAppliesController.cs

170 lines
4.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
2016-09-05 20:07:10 +08:00
using System.Linq;
using System.Web.Mvc;
using Infrastructure;
using OpenAuth.App;
using OpenAuth.App.SSO;
2016-09-05 20:07:10 +08:00
using OpenAuth.App.ViewModel;
using OpenAuth.Domain;
using OpenAuth.Mvc.Models;
using OptimaJet.Workflow.Core.Runtime;
using ProcessStatus = OptimaJet.Workflow.Core.Persistence.ProcessStatus;
namespace OpenAuth.Mvc.Controllers
{
2016-09-09 17:51:33 +08:00
/// <summary>
/// 通用申请流程处理
/// <para>李玉宝新增于2016-09-08 19:21:59</para>
/// </summary>
2016-09-07 16:58:08 +08:00
public class CommonAppliesController : BaseController
{
2016-09-07 16:58:08 +08:00
private CommonApplyApp _app;
2016-09-07 16:58:08 +08:00
public CommonAppliesController()
{
2016-09-07 16:58:08 +08:00
_app = AutofacExt.GetFromFac<CommonApplyApp>();
}
public ActionResult Index()
{
return View();
}
2016-09-07 16:58:08 +08:00
public string Load(string type, int pageCurrent = 1, int pageSize = 30)
{
2016-09-07 16:58:08 +08:00
return JsonHelper.Instance.Serialize(_app.Load(AuthUtil.GetCurrentUser().User.Id, type, pageCurrent, pageSize));
}
[HttpPost]
2016-09-07 16:58:08 +08:00
public string Edit(CommonApply apply)
{
try
{
apply.UserId = AuthUtil.GetCurrentUser().User.Id;
_app.AddOrUpdate(apply);
CreateWorkflowIfNotExists(apply.Id,apply.WorkflowName);
2016-09-07 11:11:34 +08:00
}
catch (Exception ex)
{
2016-10-14 11:22:16 +08:00
Result.Status = false;
Result.Message = ex.Message;
}
2016-10-14 11:22:16 +08:00
return JsonHelper.Instance.Serialize(Result);
}
public ActionResult Detail(Guid id)
{
try
{
2016-09-07 16:58:08 +08:00
CommonApplyVM apply = _app.Get(id);
2016-09-05 20:07:10 +08:00
apply.Commands = GetCommands(id);
return View(apply);
}
catch (Exception e)
{
Console.WriteLine(e);
}
return View();
}
2016-09-06 10:55:55 +08:00
/// <summary>
/// 删除申请
/// </summary>
2016-09-05 20:07:10 +08:00
public string Delete(Guid id)
{
try
{
_app.Del(id);
}
catch (Exception ex)
{
2016-10-14 11:22:16 +08:00
Result.Status = false;
Result.Message = ex.Message;
2016-09-05 20:07:10 +08:00
throw;
}
2016-10-14 11:22:16 +08:00
return JsonHelper.Instance.Serialize(Result);
2016-09-05 20:07:10 +08:00
}
2016-09-06 10:55:55 +08:00
/// <summary>
/// 执行命令
/// </summary>
2016-09-05 20:07:10 +08:00
[HttpPost]
public string ExeCmd(Guid id, string cmd)
{
try
{
ExecuteCommand(id, cmd, null);
}
catch (Exception ex)
{
2016-10-14 11:22:16 +08:00
Result.Status = false;
Result.Message = ex.Message;
2016-09-05 20:07:10 +08:00
throw;
}
2016-10-14 11:22:16 +08:00
return JsonHelper.Instance.Serialize(Result);
2016-09-05 20:07:10 +08:00
}
private void CreateWorkflowIfNotExists(Guid id, string schemecode)
{
if (WorkflowInit.Runtime.IsProcessExists(id))
return;
using (var sync = new WorkflowSync(WorkflowInit.Runtime, id))
{
WorkflowInit.Runtime.CreateInstance(schemecode, id);
sync.StatrtWaitingFor(new List<ProcessStatus> { ProcessStatus.Initialized, ProcessStatus.Initialized });
sync.Wait(new TimeSpan(0, 0, 10));
}
}
2016-09-05 20:07:10 +08:00
/// <summary>
/// 获取当前登陆用户可执行的命令
/// </summary>
/// <param name="id">流程实体ID</param>
private CommandModel[] GetCommands(Guid id)
{
var result = new List<CommandModel>();
var commands = WorkflowInit.Runtime.GetAvailableCommands(id, AuthUtil.GetCurrentUser().User.Id.ToString());
foreach (var workflowCommand in commands) //去除相同的
{
if (result.Count(c => c.Key == workflowCommand.CommandName) == 0)
2016-09-06 10:55:55 +08:00
result.Add(new CommandModel {
Key = workflowCommand.CommandName,
Value = workflowCommand.LocalizedName,
Classifier = workflowCommand.Classifier });
2016-09-05 20:07:10 +08:00
}
return result.ToArray();
}
/// <summary>
/// 执行指令
/// </summary>
/// <param name="id">流程实例ID</param>
/// <param name="commandName">命令名称</param>
2016-09-07 18:57:58 +08:00
/// <param name="apply">申请实体</param>
private void ExecuteCommand(Guid id, string commandName, CommonApply apply)
2016-09-05 20:07:10 +08:00
{
var currentUser =AuthUtil.GetCurrentUser().User.Id.ToString();
var commands = WorkflowInit.Runtime.GetAvailableCommands(id, currentUser);
var command =
commands.FirstOrDefault(
c => c.CommandName.Equals(commandName, StringComparison.CurrentCultureIgnoreCase));
if (command == null)
return;
WorkflowInit.Runtime.ExecuteCommand(id, currentUser, currentUser, command);
}
2016-09-07 11:11:34 +08:00
}
}