using JNPF.Common.Core.Manager;
using JNPF.Common.Dtos.Message;
using JNPF.Common.Extension;
using JNPF.Common.Security;
using JNPF.Message.Interfaces.Message;
using JNPF.Systems.Interfaces.Permission;
using JNPF.Systems.Interfaces.System;
using JNPF.WorkFlow.Entitys.Entity;
using JNPF.WorkFlow.Entitys.Model;
using JNPF.WorkFlow.Entitys.Model.Conifg;
using JNPF.WorkFlow.Interfaces.Repository;
using Newtonsoft.Json.Linq;
namespace JNPF.WorkFlow.Manager;
public class FlowTaskMsgUtil
{
private readonly ISendMessageService _sendMessageService;
private readonly IDataInterfaceService _dataInterfaceService;
public readonly IFlowTaskRepository _flowTaskRepository;
public readonly IUserManager _userManager;
public readonly IUsersService _usersService;
public FlowTaskMsgUtil(ISendMessageService sendMessageService, IFlowTaskRepository flowTaskRepository, IUserManager userManager, IUsersService usersService, IDataInterfaceService dataInterfaceService)
{
_sendMessageService = sendMessageService;
_flowTaskRepository = flowTaskRepository;
_userManager = userManager;
_usersService = usersService;
_dataInterfaceService = dataInterfaceService;
}
#region 消息推送
///
/// 通过消息模板获取消息通知.
///
/// 消息配置.
/// 通知人员.
/// 任务参数.
/// 默认站内信编码.
/// 跳转数据.
///
public async Task Alerts(MsgConfig msgConfig, List users, FlowTaskParamter flowTaskParamter, string enCode, Dictionary bodyDic = null)
{
//自定义消息
if (msgConfig.on == 1 || msgConfig.on == 2)
{
foreach (var item in msgConfig.templateJson)
{
item.toUser = users;
GetMsgContent(item.paramJson, flowTaskParamter);
await _sendMessageService.SendMessage(item, bodyDic);
}
}
//默认消息
if (msgConfig.on == 3)
{
var crUser = await _usersService.GetUserName(flowTaskParamter.flowTaskEntity.CreatorUserId, false);
var flowName = _flowTaskRepository.GetFlowTemplateJsonInfo(x => x.Id == flowTaskParamter.flowTaskEntity.FlowId).FullName;
await _sendMessageService.SendMessageDefult(enCode, users, crUser, flowName, bodyDic);
}
}
///
/// 获取消息模板内容.
///
/// 消息模板json.
/// 任务参数.
public Dictionary GetMsgContent(List templateJsonItems, FlowTaskParamter flowTaskParamter)
{
var jObj = flowTaskParamter.flowTaskEntity.FlowFormContentJson.ToObject();
var dic = new Dictionary();
foreach (var item in templateJsonItems)
{
var value = string.Empty;
if (item.relationField.Equals("@flowOperatorUserId"))
{
value = _userManager.UserId;
}
else if (item.relationField.Equals("@taskId"))
{
value = flowTaskParamter.flowTaskEntity.Id;
}
else if (item.relationField.Equals("@taskNodeId"))
{
value = flowTaskParamter.flowTaskNodeEntity.Id;
}
else if (item.relationField.Equals("@taskFullName"))
{
value = flowTaskParamter.flowTaskEntity.FullName;
}
else if (item.relationField.Equals("@launchUserId"))
{
value = flowTaskParamter.flowTaskEntity.CreatorUserId;
}
else if (item.relationField.Equals("@launchUserName"))
{
value = _usersService.GetInfoByUserId(flowTaskParamter.flowTaskEntity.CreatorUserId).RealName;
}
else if (item.relationField.Equals("@flowOperatorUserName"))
{
value = _userManager.User.RealName;
}
else if (item.relationField.Equals("@flowId"))
{
value = flowTaskParamter.flowTaskEntity.FlowId;
}
else if (item.relationField.Equals("@flowFullName"))
{
value = flowTaskParamter.flowTaskEntity.FlowName;
}
else
{
if (item.isSubTable)
{
var fields = item.relationField.Split("-").ToList();
// 子表键值
var tableField = fields[0];
// 子表字段键值
var keyField = fields[1];
if (jObj.ContainsKey(tableField) && jObj[tableField] is JArray)
{
var jar = jObj[tableField] as JArray;
value = jar.Where(x => x.ToObject().ContainsKey(keyField)).Select(x => x.ToObject()[keyField]).ToJsonString();
}
}
else
{
value = jObj.ContainsKey(item.relationField) ? jObj[item.relationField].ToString() : string.Empty;
}
}
item.value = value;
dic.Add(item.field, value);
}
templateJsonItems.Add(new MessageSendParam
{
field = "@taskFullName",
value = flowTaskParamter.flowTaskEntity.FullName
});
return dic;
}
///
/// 组装消息跳转详情参数.
///
/// 流程实例
/// 节点id.
/// 通知人员.
/// 经办实例.
/// 1:发起,2:待办,3:抄送
///
///
public Dictionary GetMesBodyText(FlowTaskParamter flowTaskParamter, List userList, List flowTaskOperatorEntities, int type, string remark = "")
{
var dic = new Dictionary();
if (flowTaskOperatorEntities.IsNotEmptyOrNull() && flowTaskOperatorEntities.Count > 0)
{
foreach (var item in flowTaskOperatorEntities)
{
var value = new
{
enCode = flowTaskParamter.flowTaskEntity.FlowCode,
flowId = flowTaskParamter.flowTaskEntity.FlowId,
status = type == 1 ? 0 : 1,
processId = item.TaskId,
taskNodeId = item.TaskNodeId,
taskOperatorId = item.Id,
type = type,
remark = remark
};
dic.Add(item.HandleId, value);
if (type == 2)
{
var toUserId = _flowTaskRepository.GetToUserId(item.HandleId, flowTaskParamter.flowTaskEntity.TemplateId);
toUserId.ForEach(u => dic[u + "-delegate"] = value);
}
}
}
else
{
var value = new
{
enCode = flowTaskParamter.flowTaskEntity.FlowCode,
flowId = flowTaskParamter.flowTaskEntity.FlowId,
status = type == 1 ? 0 : 1,
processId = flowTaskParamter.flowTaskEntity.Id,
taskNodeId = flowTaskParamter.flowTaskNodeEntity?.Id,
taskOperatorId = flowTaskParamter.flowTaskOperatorEntity?.Id,
type = type,
remark = remark
};
userList.ForEach(u => dic.Add(u, value));
}
return dic;
}
///
/// 事件请求.
///
/// 事件配置.
/// 表单数据.
///
public async Task RequestEvents(FuncConfig funcConfig, FlowTaskParamter flowTaskParamter)
{
if (funcConfig.IsNotEmptyOrNull() && funcConfig.on && funcConfig.interfaceId.IsNotEmptyOrNull())
{
var parameters = GetMsgContent(funcConfig.templateJson, flowTaskParamter);
await _dataInterfaceService.GetResponseByType(funcConfig.interfaceId, 3, _userManager.TenantId, null, parameters);
}
}
///
/// 委托消息通知.
///
/// 委托类型:发起,审批.
/// 通知人员.
/// 流程名.
///
public async Task SendDelegateMsg(string delegateType, string ToUserId, string flowName)
{
await _sendMessageService.SendMessageDelegate(delegateType, ToUserId, flowName);
}
#endregion
}