using Learun.DataBase.Repository;
using Learun.Util;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Learun.Application.Base.AuthorizeModule;
using Learun.Application.Organization;
using Learun.Application.TwoDevelopment.LR_Desktop;
using Microsoft.AspNet.SignalR.Client;
using Newtonsoft.Json;
using Learun.Application.TwoDevelopment.EducationalAdministration;
namespace Learun.Application.WorkFlow
{
///
/// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
/// Copyright (c) 2013-2018 北京泉江科技有限公司
/// 创建人:陈彬彬
/// 日 期:2017.04.17
/// 描 述:任务实例
///
public class WfTaskService : RepositoryFactory
{
#region 获取数据
///
/// 获取未完成的流程实例任务列表
///
/// 流程实例主键
///
public IEnumerable GetList(string processId)
{
try
{
return this.BaseRepository().FindList(t => t.F_ProcessId == processId && t.F_IsFinished == 0);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取当前任务节点主键
///
/// 流程实例主键
///
public List GetCurrentNodeIds(string processId)
{
try
{
var strSql = new StringBuilder();
strSql.Append(@"SELECT
t.F_NodeId
FROM
LR_WF_Task t
WHERE
t.F_IsFinished = 0 AND t.F_ProcessId = @processId
GROUP BY
t.F_ProcessId,
t.F_NodeId
");
var taskList = this.BaseRepository().FindList(strSql.ToString(), new { processId = processId });
List list = new List();
foreach (var item in taskList)
{
list.Add(item.F_NodeId);
}
return list;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取任务实体
///
/// 主键
///
public WfTaskEntity GetEntity(string keyValue)
{
try
{
return this.BaseRepository().FindEntity(t => t.F_Id == keyValue);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取任务实体
///
/// 流程实例主键
/// 节点主键
///
public WfTaskEntity GetEntityUnFinish(string processId, string nodeId)
{
try
{
return this.BaseRepository().FindEntity(t => t.F_ProcessId == processId && t.F_NodeId == nodeId && t.F_IsFinished == 0);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取任务实体
///
/// 流程实例主键
/// 节点主键
///
public WfTaskEntity GetEntity(string processId, string nodeId)
{
try
{
return this.BaseRepository().FindEntity(t => t.F_ProcessId == processId && t.F_NodeId == nodeId);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取未处理任务列表
///
/// 用户信息
/// 翻页信息
/// 查询条件
///
public IEnumerable GetActiveList(UserInfo userInfo, Pagination pagination, string queryJson, List delegateList)
{
try
{
string userId = userInfo.userId;
string postIds = "'" + userInfo.postIds?.Replace(",", "','") + "'";
string roleIds = "'" + userInfo.roleIds?.Replace(",", "','") + "'";
string companyId = userInfo.companyId;
string departmentId = userInfo.departmentId;
// 获取委托信息
var strSql = new StringBuilder();
strSql.Append(@"SELECT
a.F_TaskId,
a.F_TaskType,
a.F_TaskName,
p.F_Id,
p.F_SchemeId,
p.F_SchemeCode,
p.F_SchemeName,
p.F_ProcessName,
p.F_ProcessLevel,
p.F_EnabledMark,
p.F_IsFinished,
p.F_IsAgain,
p.F_Description,
p.F_CreateDate,
p.F_CreateUserId,
p.F_CreateUserName,
p.F_CompanyId,
p.F_DepartmentId,
p.F_IsChildFlow,
p.F_ProcessParentId
FROM
(
SELECT
MAX(t.F_Id) AS F_TaskId,
MAX(t.F_TaskType) AS F_TaskType,
t.F_ProcessId,
t.F_NodeName AS F_TaskName
FROM
LR_WF_Task t WHERE t.F_IsFinished = 0 ");
strSql.Append(" AND ( t.F_AuditorId = '1' OR ");
strSql.Append(" t.F_AuditorId = @userId ");
if (!string.IsNullOrEmpty(userInfo.postIds))
{
strSql.Append(" OR (t.F_AuditorId in (" + postIds + ") AND t.F_CompanyId = '1' AND t.F_DepartmentId = '1' ) ");
strSql.Append(" OR (t.F_AuditorId in (" + postIds + ") AND t.F_CompanyId = @companyId ) ");
strSql.Append(" OR (t.F_AuditorId in (" + postIds + ") AND t.F_DepartmentId = @departmentId ) ");
}
if (!string.IsNullOrEmpty(userInfo.roleIds))
{
strSql.Append(" OR (t.F_AuditorId in (" + roleIds + ") AND t.F_CompanyId = '1' AND t.F_DepartmentId = '1' ) ");
strSql.Append(" OR (t.F_AuditorId in (" + roleIds + ") AND t.F_CompanyId = @companyId ) ");
strSql.Append(" OR (t.F_AuditorId in (" + roleIds + ") AND t.F_DepartmentId = @departmentId) ");
}
// 添加委托信息
foreach (var item in delegateList)
{
string processId = "'" + item.wfProcessId.Replace(",", "','") + "'";
string postIds2 = "'" + item.postIds?.Replace(",", "','") + "'";
string roleIds2 = "'" + item.roleIds?.Replace(",", "','") + "'";
string userI2 = "'" + item.userId + "'";
string companyId2 = "'" + item.companyId + "'";
string departmentId2 = "'" + item.departmentId + "'";
strSql.Append(" OR (t.F_AuditorId =" + userI2 + " AND t.F_ProcessId in (" + processId + ") )");
if (!string.IsNullOrEmpty(item.postIds))
{
strSql.Append(" OR (t.F_AuditorId in (" + postIds2 + ") AND t.F_CompanyId = '1' AND t.F_DepartmentId = '1' AND t.F_ProcessId in (" + processId + ") ) ");
strSql.Append(" OR (t.F_AuditorId in (" + postIds2 + ") AND t.F_CompanyId = " + companyId2 + " AND t.F_ProcessId in (" + processId + ") ) ");
strSql.Append(" OR (t.F_AuditorId in (" + postIds2 + ") AND t.F_DepartmentId = " + departmentId2 + " AND t.F_ProcessId in (" + processId + ") ) ");
}
if (!string.IsNullOrEmpty(item.roleIds))
{
strSql.Append(" OR (t.F_AuditorId in (" + roleIds2 + ") AND t.F_CompanyId = '1' AND t.F_DepartmentId = '1' AND t.F_ProcessId in (" + processId + ") ) ");
strSql.Append(" OR (t.F_AuditorId in (" + roleIds2 + ") AND t.F_CompanyId = " + companyId2 + " AND t.F_ProcessId in (" + processId + ") ) ");
strSql.Append(" OR (t.F_AuditorId in (" + roleIds2 + ") AND t.F_DepartmentId = " + departmentId2 + " AND t.F_ProcessId in (" + processId + ") ) ");
}
}
strSql.Append(@" ) GROUP BY
t.F_ProcessId,
t.F_NodeId,
t.F_NodeName )a LEFT JOIN LR_WF_ProcessInstance p ON p.F_Id = a.F_ProcessId WHERE 1=1 AND (p.F_IsFinished = 0 OR a.F_TaskType = 3) AND p.F_EnabledMark = 1 ");
var queryParam = queryJson.ToJObject();
DateTime startTime = DateTime.Now, endTime = DateTime.Now;
if (!queryParam["StartTime"].IsEmpty() && !queryParam["EndTime"].IsEmpty())
{
startTime = queryParam["StartTime"].ToDate();
endTime = queryParam["EndTime"].ToDate();
strSql.Append(" AND ( p.F_CreateDate >= @startTime AND p.F_CreateDate <= @endTime ) ");
}
string keyword = "";
if (!queryParam["keyword"].IsEmpty())
{
keyword = "%" + queryParam["keyword"].ToString() + "%";
strSql.Append(" AND ( p.F_ProcessName like @keyword OR p.F_SchemeName like @keyword ) ");
}
return this.BaseRepository().FindList(strSql.ToString(), new { userId, companyId, departmentId, startTime, endTime, keyword }, pagination);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取已处理任务列表
///
/// 用户主键
/// 翻页信息
/// 查询条件
///
public IEnumerable GetHasList(string userId, Pagination pagination, string queryJson)
{
try
{
var strSql = new StringBuilder();
strSql.Append(@"SELECT
t.F_Id AS F_TaskId,
t.F_TaskType,
t.F_NodeName AS F_TaskName,
p.F_Id,
p.F_SchemeId,
p.F_SchemeCode,
p.F_SchemeName,
p.F_ProcessName,
p.F_ProcessLevel,
p.F_EnabledMark,
p.F_IsFinished,
p.F_IsAgain,
p.F_Description,
p.F_CreateDate,
p.F_CreateUserId,
p.F_CreateUserName,
p.F_CompanyId,
p.F_DepartmentId,
p.F_IsChildFlow,
p.F_ProcessParentId
FROM
LR_WF_Task t
LEFT JOIN LR_WF_ProcessInstance p ON t.F_ProcessId = p.F_Id
WHERE
t.F_IsFinished = 1 AND t.F_ModifyUserId = @userId
");
var queryParam = queryJson.ToJObject();
DateTime startTime = DateTime.Now, endTime = DateTime.Now;
if (!queryParam["StartTime"].IsEmpty() && !queryParam["EndTime"].IsEmpty())
{
startTime = queryParam["StartTime"].ToDate();
endTime = queryParam["EndTime"].ToDate();
strSql.Append(" AND ( p.F_CreateDate >= @startTime AND p.F_CreateDate <= @endTime ) ");
}
string keyword = "";
if (!queryParam["keyword"].IsEmpty())
{
keyword = "%" + queryParam["keyword"].ToString() + "%";
strSql.Append(" AND ( p.F_ProcessName like @keyword OR p.F_SchemeName like @keyword ) ");
}
return this.BaseRepository().FindList(strSql.ToString(), new { userId = userId, startTime = startTime, endTime = endTime, keyword = keyword }, pagination);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 获取委托人信息列表
///
/// 我的用户Id
///
public List GetDelegateTask(string userId)
{
try
{
List delegateUserlist = new List();
DateTime datatime = DateTime.Now;
IEnumerable wfDelegateRuleList = this.BaseRepository().FindList(t => t.F_ToUserId == userId && t.F_BeginDate >= datatime && t.F_EndDate <= datatime);
foreach (var item in wfDelegateRuleList)
{
UserInfo userinfo = new UserInfo();
userinfo.userId = item.F_CreateUserId;
var strSql = new StringBuilder();
strSql.Append(@"SELECT
p.F_Id
FROM
LR_WF_DelegateRuleRelation d
LEFT JOIN LR_WF_SchemeInfo s ON s.F_Id = d.F_SchemeInfoId
LEFT JOIN LR_WF_ProcessInstance p ON p.F_SchemeCode = s.F_Code
WHERE
p.F_Id IS NOT NULL
AND p.F_IsFinished = 0 AND d.F_DelegateRuleId = @DelegateRuleId ");
DataTable dt = this.BaseRepository().FindTable(strSql.ToString(), new { DelegateRuleId = item.F_Id });
userinfo.wfProcessId = "";
foreach (DataRow dr in dt.Rows)
{
if (!string.IsNullOrEmpty(dr[0].ToString()))
{
if (!string.IsNullOrEmpty(userinfo.wfProcessId))
{
userinfo.wfProcessId += ",";
}
userinfo.wfProcessId += dr[0].ToString();
}
}
if (!string.IsNullOrEmpty(userinfo.wfProcessId))
{
delegateUserlist.Add(userinfo);
}
}
return delegateUserlist;
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
#region 提交数据
///
/// 保存或更新流程实例任务
///
/// 实体
public void SaveEntity(WfTaskEntity entity)
{
try
{
entity.Create();
this.BaseRepository().Insert(entity);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 保存或更新流程实例任务
///
/// 实体
/// 公司主键
/// 部门主键
public void SaveEntitys(WfTaskEntity entity, string companyId, string departmentId)
{
try
{
if (entity.auditors.Count > 0)
{
foreach (var auditor in entity.auditors)
{
WfTaskEntity wfTaskEntity = new WfTaskEntity();
wfTaskEntity.F_ProcessId = entity.F_ProcessId;
wfTaskEntity.F_NodeId = entity.F_NodeId;
wfTaskEntity.F_NodeName = entity.F_NodeName;
wfTaskEntity.F_TaskType = entity.F_TaskType;
wfTaskEntity.F_TimeoutAction = entity.F_TimeoutAction;
wfTaskEntity.F_TimeoutNotice = entity.F_TimeoutNotice;
wfTaskEntity.F_PreviousId = entity.F_PreviousId;
wfTaskEntity.F_PreviousName = entity.F_PreviousName;
wfTaskEntity.F_CreateUserId = entity.F_CreateUserId;
wfTaskEntity.F_CreateUserName = entity.F_CreateUserName;
wfTaskEntity.F_AuditorId = auditor.auditorId;
wfTaskEntity.F_AuditorName = auditor.auditorName;
wfTaskEntity.F_CompanyId = "1";
wfTaskEntity.F_DepartmentId = "1";
if (auditor.condition == 1)//1.同一个部门2.同一个公司
{
wfTaskEntity.F_DepartmentId = departmentId;
}
else if (auditor.condition == 2)
{
wfTaskEntity.F_CompanyId = companyId;
}
wfTaskEntity.Create();
this.BaseRepository().Insert(wfTaskEntity);
//读取信息推送管理-待办流程推送(04)的配置
var informationPushEntity = this.BaseRepository().FindEntity(x => x.PushItem == "04");
if (informationPushEntity != null && informationPushEntity.Status == true)
{
//微信推送
try
{
PushWeixin(wfTaskEntity);
}
catch (Exception e)
{
}
}
}
}
else
{
entity.F_AuditorId = "1";
entity.Create();
this.BaseRepository().Insert(entity);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
public void PushWeixin(WfTaskEntity wfTaskEntity)
{
try
{
var processinstance = BaseRepository()
.FindEntity(m => m.F_Id == wfTaskEntity.F_ProcessId);
var needpostuserlist = new List();
var userinfontp = this.BaseRepository()
.FindEntity(m => m.F_UserId == wfTaskEntity.F_AuditorId);
if (userinfontp != null)
{
needpostuserlist.Add(userinfontp);
}
//增加角色、岗位判断
var alluserlist = this.BaseRepository()
.FindList(m => !string.IsNullOrEmpty(m.OpenIdForWeixin));
foreach (UserEntity user in alluserlist)
{
var rolelist = BaseRepository()
.FindList(m => m.F_UserId == user.F_UserId && m.F_Category == 1)
.Select(m => "'" + m.F_ObjectId + "'").ToList();
if (rolelist.Count > 0)
{
string roleIds = string.Join(",", rolelist);
if (roleIds.Contains(wfTaskEntity.F_AuditorId) &&
wfTaskEntity.F_CompanyId == user.F_CompanyId && !needpostuserlist.Contains(user))
{
needpostuserlist.Add(user);
}
if (roleIds.Contains(wfTaskEntity.F_AuditorId) &&
wfTaskEntity.F_DepartmentId == user.F_DepartmentId && !needpostuserlist.Contains(user))
{
needpostuserlist.Add(user);
}
}
var postlist = BaseRepository()
.FindList(m => m.F_UserId == user.F_UserId && m.F_Category == 2)
.Select(m => "'" + m.F_ObjectId + "'").ToList();
if (postlist.Count > 0)
{
string postids = string.Join(",", postlist);
if (postids.Contains(wfTaskEntity.F_AuditorId) &&
wfTaskEntity.F_CompanyId == user.F_CompanyId && !needpostuserlist.Contains(user))
{
needpostuserlist.Add(user);
}
if (postids.Contains(wfTaskEntity.F_AuditorId) &&
wfTaskEntity.F_DepartmentId == user.F_DepartmentId && !needpostuserlist.Contains(user))
{
needpostuserlist.Add(user);
}
}
}
var WeChatConfigentity = BaseRepository().FindEntity(m => m.IsEnable == true);
string appid = WeChatConfigentity.APPId;
string secret = WeChatConfigentity.secret;
var wechatemplete = BaseRepository()
.FindEntity(m => m.WeID == WeChatConfigentity.ID && m.TCode == "task");
string weixintaskurl = wechatemplete.TUrl;
string weixintasktempid = wechatemplete.TempId;
var responsejson = Util.HttpMethods.HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret);
foreach (UserEntity userinfo in needpostuserlist)
{
if (userinfo != null && !string.IsNullOrEmpty(userinfo.OpenIdForWeixin) && processinstance != null)
{
//执行推送任务
if (!string.IsNullOrEmpty(appid) && !string.IsNullOrEmpty(secret) && !string.IsNullOrEmpty(weixintaskurl) && !string.IsNullOrEmpty(weixintasktempid))
{
if (!string.IsNullOrEmpty(responsejson))
{
var weixintokenobj = JsonConvert.DeserializeObject(responsejson);
if (string.IsNullOrEmpty(weixintokenobj.errcode))
{
string access_token = weixintokenobj.access_token;
string jsondata = "{\"touser\":\"" + userinfo.OpenIdForWeixin + "\"," +
"\"template_id\":\"" + weixintasktempid + "\"," +
"\"url\":\"" + weixintaskurl + "\"," +
"\"data\":{" +
"\"first\": {\"value\":\"" + processinstance.F_SchemeName + "\",\"color\":\"#173177\"}," +
"\"keyword1\":{\"value\":\"" + processinstance.F_SchemeName + "\",\"color\":\"#173177\"}," +
"\"keyword2\": {\"value\":\"" + processinstance.F_ProcessName + "\",\"color\":\"#173177\"}," +
"\"keyword3\": {\"value\":\"" + wfTaskEntity.F_NodeName + "\",\"color\":\"#173177\"}," +
"\"keyword4\": {\"value\":\"您有新的待办流程【" + processinstance.F_SchemeName + "】\",\"color\":\"#173177\"}" +
//"\"remark\":{\"value\":\"欢迎再次购买!\",\"color\":\"#173177\"}" +
"}" +
"}";
string pushresult = Util.HttpMethods.HttpPost("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token, jsondata);
}
}
}
}
}
//推送飞星
Task.Run(async () =>
{
using (var hubConnection = new HubConnection(ConfigurationManager.AppSettings["CommunicationServeraddress"]))
{
var hubProxy = hubConnection.CreateHubProxy("SignalRHub");
await hubConnection.Start();
await hubProxy.Invoke("PushAnnouncement", processinstance.F_Id, processinstance.F_SchemeName, processinstance.F_ProcessName, "task", string.Join(",", needpostuserlist.Select(m => m.F_UserId)), "");
}
});
}
catch (Exception e)
{
}
}
///
/// 更新任务状态
///
/// 主键
/// 状态 1 完成 2 关闭(会签
public void UpdateState(string keyValue, int state)
{
try
{
WfTaskEntity wfTaskEntity = new WfTaskEntity();
wfTaskEntity.Modify(keyValue);
wfTaskEntity.F_IsFinished = state;
this.BaseRepository().Update(wfTaskEntity);
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
///
/// 更新任务完成状态
///
/// 流程实例主键
/// 节点主键
/// 任务节点Id
/// 用户主键
/// 用户名称
public void UpdateStateByNodeId(string processId, string nodeId, string taskId, string userId, string userName)
{
try
{
var list = this.BaseRepository().FindList(t => t.F_ProcessId == processId && t.F_IsFinished == 0 && t.F_NodeId == nodeId);
foreach (var item in list)
{
WfTaskEntity wfTaskEntity = new WfTaskEntity();
wfTaskEntity.Modify(item.F_Id);
wfTaskEntity.F_IsFinished = 1;
if (taskId == item.F_Id)
{
wfTaskEntity.F_ModifyUserId = userId;
}
wfTaskEntity.F_ModifyUserName = userName;
this.BaseRepository().Update(wfTaskEntity);
}
}
catch (Exception ex)
{
if (ex is ExceptionEx)
{
throw;
}
else
{
throw ExceptionEx.ThrowServiceException(ex);
}
}
}
#endregion
}
}