using System.Security.Cryptography;
using System.Text;
using System.Web;
using JNPF.Common.Core.Manager;
using JNPF.Common.Dtos.Message;
using JNPF.Common.Enums;
using JNPF.Common.Extension;
using JNPF.Common.Filter;
using JNPF.Common.Options;
using JNPF.Common.Security;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.Extras.Thirdparty.DingDing;
using JNPF.Extras.Thirdparty.Email;
using JNPF.Extras.Thirdparty.Sms;
using JNPF.Extras.Thirdparty.WeChat;
using JNPF.FriendlyException;
using JNPF.Message.Entitys.Dto.MessageTemplate;
using JNPF.Message.Entitys.Dto.SendMessage;
using JNPF.Message.Entitys.Entity;
using JNPF.Message.Entitys.Model.MessageTemplate;
using JNPF.Message.Interfaces.Message;
using JNPF.RemoteRequest.Extensions;
using JNPF.Systems.Entitys.Permission;
using JNPF.Systems.Interfaces.Permission;
using JNPF.Systems.Interfaces.System;
using JNPF.WorkFlow.Entitys.Entity;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Senparc.Weixin.MP.AdvancedAPIs.TemplateMessage;
using SqlSugar;
namespace JNPF.Message.Service;
///
/// 发送配置
/// 版 本:V3.2
/// 版 权:拓通智联科技有限公司(http://www.tuotong-tech.com)
/// 日 期:2021-06-01.
///
[ApiDescriptionSettings(Tag = "Message", Name = "SendMessage", Order = 240)]
[Route("api/message/SendMessageConfig")]
public class SendMessageService : ISendMessageService, IDynamicApiController, ITransient
{
private readonly ISqlSugarRepository _repository;
private readonly IUsersService _usersService;
private readonly IMessageService _messageService;
private readonly ISynThirdInfoService _synThirdInfoService;
private readonly IShortLinkService _shortLinkService;
private readonly MessageOptions _messageOptions = App.GetConfig("Message", true);
private readonly IUserManager _userManager;
public SendMessageService(
ISqlSugarRepository repository,
IUsersService usersService,
IMessageService messageService,
ISynThirdInfoService synThirdInfoService,
IShortLinkService shortLinkService,
IUserManager userManager)
{
_repository = repository;
_usersService = usersService;
_messageService = messageService;
_synThirdInfoService = synThirdInfoService;
_shortLinkService = shortLinkService;
_userManager = userManager;
}
#region Get
///
/// 列表.
///
/// 请求参数.
///
[HttpGet("")]
public async Task GetList([FromQuery] MessageTemplateQuery input)
{
var list = await _repository.AsSugarClient().Queryable()
.Where(a => a.DeleteMark == null)
.WhereIF(input.messageSource.IsNotEmptyOrNull(), a => a.MessageSource == input.messageSource)
.WhereIF(input.templateType.IsNotEmptyOrNull(), a => a.TemplateType == input.templateType)
.WhereIF(input.enabledMark.IsNotEmptyOrNull(), a => a.EnabledMark == input.enabledMark)
.WhereIF(input.keyword.IsNotEmptyOrNull(), a => a.FullName.Contains(input.keyword) || a.EnCode.Contains(input.keyword))
.OrderBy(a => a.SortCode)
.OrderBy(a => a.CreatorTime, OrderByType.Desc)
.OrderBy(a => a.LastModifyTime, OrderByType.Desc)
.Select(a => new SendMessageListOutput
{
id = a.Id,
fullName = a.FullName,
enCode = a.EnCode,
templateType = a.TemplateType,
messageSource = SqlFunc.Subqueryable().Where(u => u.Type == "4" && u.EnCode == a.MessageSource).Select(u => u.FullName),
creatorUser = SqlFunc.Subqueryable().Where(u => u.Id == a.CreatorUserId).Select(u => SqlFunc.MergeString(u.RealName, "/", u.Account)),
creatorTime = a.CreatorTime,
lastModifyTime = a.LastModifyTime,
sortCode = a.SortCode,
enabledMark = a.EnabledMark,
}).ToPagedListAsync(input.currentPage, input.pageSize);
foreach (var item in list.list)
{
item.messageType = await _repository.AsSugarClient()
.Queryable((a, b) => new JoinQueryInfos(JoinType.Left, a.MessageType == b.EnCode && b.Type == "1"))
.Where((a, b) => a.SendConfigId == item.id && a.DeleteMark == null)
.Select((a, b) => new MessageTypeModel
{
fullName = b.FullName,
type = a.MessageType,
}).ToListAsync();
}
return PageResult.SqlSugarPageResult(list);
}
[HttpGet("getSendConfigList")]
public async Task GetSendList([FromQuery] MessageTemplateQuery input)
{
var list = await _repository.AsSugarClient().Queryable()
.Where(a => a.DeleteMark == null)
.WhereIF(input.messageSource.IsNotEmptyOrNull(), a => a.MessageSource == input.messageSource)
.WhereIF(input.templateType.IsNotEmptyOrNull(), a => a.TemplateType == input.templateType)
.WhereIF(input.enabledMark.IsNotEmptyOrNull(), a => a.EnabledMark == input.enabledMark)
.WhereIF(input.keyword.IsNotEmptyOrNull(), a => a.FullName.Contains(input.keyword) || a.EnCode.Contains(input.keyword))
.OrderBy(a => a.SortCode)
.OrderBy(a => a.CreatorTime, OrderByType.Desc)
.OrderBy(a => a.LastModifyTime, OrderByType.Desc)
.Select(a => new SendMessageListOutput
{
id = a.Id,
fullName = a.FullName,
enCode = a.EnCode,
}).ToPagedListAsync(input.currentPage, input.pageSize);
foreach (var item in list.list)
{
item.templateJson = await SendTest(item.id);
}
return PageResult.SqlSugarPageResult(list);
}
///
/// 详情.
///
///
[HttpGet("{id}")]
public async Task GetInfo(string id)
{
var output = (await _repository.GetFirstAsync(x => x.Id == id && x.DeleteMark == null)).Adapt();
output.sendConfigTemplateList = await _repository.AsSugarClient().Queryable((a, b, c) => new JoinQueryInfos(JoinType.Left, a.TemplateId == b.Id, JoinType.Left, a.AccountConfigId == c.Id))
.Where(a => a.SendConfigId == id && a.DeleteMark == null)
.Select((a, b, c) => new SendTemplateModel
{
id = a.Id,
messageType = a.MessageType,
sendConfigId = a.SendConfigId,
templateId = a.TemplateId,
accountConfigId = a.AccountConfigId,
templateCode = b.EnCode,
templateName = b.FullName,
accountCode = c.EnCode,
accountName = c.FullName,
enabledMark = a.EnabledMark,
sortCode = a.SortCode,
description = a.Description
}).ToListAsync();
return output;
}
#endregion
#region POST
///
/// 新建.
///
/// 实体对象.
///
[HttpPost("")]
public async Task Create([FromBody] SendMessageInfoOutput input)
{
if (await _repository.IsAnyAsync(x => (x.EnCode == input.enCode || x.FullName == input.fullName) && x.DeleteMark == null))
throw Oops.Oh(ErrorCode.COM1004);
var entity = input.Adapt();
var result = await _repository.AsInsertable(entity).IgnoreColumns(ignoreNullColumn: true).CallEntityMethod(m => m.Creator()).ExecuteReturnEntityAsync();
if (input.sendConfigTemplateList.Any())
{
foreach (var item in input.sendConfigTemplateList)
{
var sendTemplateEntity = item.Adapt();
sendTemplateEntity.SendConfigId = result.Id;
await _repository.AsSugarClient().Insertable(sendTemplateEntity).IgnoreColumns(ignoreNullColumn: true).CallEntityMethod(m => m.Creator()).ExecuteCommandAsync();
}
}
if (result.IsNullOrEmpty())
throw Oops.Oh(ErrorCode.COM1000);
}
///
/// 修改.
///
/// 主键值.
/// 实体对象.
///
[HttpPut("{id}")]
public async Task Update(string id, [FromBody] SendMessageInfoOutput input)
{
if (await _repository.IsAnyAsync(x => x.Id != id && (x.EnCode == input.enCode || x.FullName == input.fullName) && x.DeleteMark == null))
throw Oops.Oh(ErrorCode.COM1004);
var entity = input.Adapt();
await _repository.AsSugarClient().Deleteable(x => x.SendConfigId == id).ExecuteCommandAsync();
if (input.sendConfigTemplateList.Any())
{
foreach (var item in input.sendConfigTemplateList)
{
var sendTemplateEntity = item.Adapt();
sendTemplateEntity.SendConfigId = id;
await _repository.AsSugarClient().Insertable(sendTemplateEntity).IgnoreColumns(ignoreNullColumn: true).CallEntityMethod(m => m.Creator()).ExecuteCommandAsync();
}
}
var isOk = await _repository.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).CallEntityMethod(m => m.LastModify()).ExecuteCommandHasChangeAsync();
if (!isOk)
throw Oops.Oh(ErrorCode.COM1001);
}
///
/// 删除.
///
/// 主键.
///
[HttpDelete("{id}")]
public async Task Delete(string id)
{
var entity = await _repository.GetFirstAsync(x => x.Id == id && x.DeleteMark == null);
if (entity == null)
throw Oops.Oh(ErrorCode.COM1005);
if (await _repository.AsSugarClient().Queryable().AnyAsync(x => x.FlowTemplateJson.Contains(id)))
throw Oops.Oh(ErrorCode.D1007);
await _repository.AsSugarClient().Deleteable(x => x.SendConfigId == id).ExecuteCommandAsync();
var isOk = await _repository.AsUpdateable(entity).CallEntityMethod(m => m.Delete()).UpdateColumns(it => new { it.DeleteMark, it.DeleteTime, it.DeleteUserId }).ExecuteCommandHasChangeAsync();
if (!isOk)
throw Oops.Oh(ErrorCode.COM1002);
}
///
/// 复制.
///
/// 主键值.
///
[HttpPost("copy/{id}")]
public async Task ActionsCopy(string id)
{
var entity = await _repository.GetFirstAsync(x => x.Id == id && x.DeleteMark == null);
var random = RandomExtensions.NextLetterAndNumberString(new Random(), 5).ToLower();
entity.FullName = string.Format("{0}副本{1}", entity.FullName, random);
entity.EnCode = string.Format("{0}{1}", entity.EnCode, random);
entity.Id = SnowflakeIdHelper.NextId();
if (entity.FullName.Length >= 50 || entity.EnCode.Length >= 50)
throw Oops.Oh(ErrorCode.COM1009);
var sendTemplateList = await _repository.AsSugarClient().Queryable().Where(x => x.SendConfigId == id && x.DeleteMark == null).ToListAsync();
foreach (var item in sendTemplateList)
{
var sendTemplateEntity = item.Adapt();
sendTemplateEntity.SendConfigId = entity.Id;
await _repository.AsSugarClient().Insertable(sendTemplateEntity).IgnoreColumns(ignoreNullColumn: true).CallEntityMethod(m => m.Creator()).ExecuteCommandAsync();
}
var isOk = await _repository.AsInsertable(entity).IgnoreColumns(ignoreNullColumn: true).CallEntityMethod(m => m.Create()).ExecuteCommandAsync();
if (isOk < 1)
throw Oops.Oh(ErrorCode.COM1008);
}
///
/// 测试发送.
///
/// 主键值.
///
[HttpPost("getTestConfig/{id}")]
public async Task SendTest(string id)
{
var list = await _repository.AsSugarClient().Queryable((a, b) => new JoinQueryInfos(JoinType.Left, a.TemplateId == b.Id))
.Where((a, b) => a.SendConfigId == id && a.DeleteMark == null && b.DeleteMark == null)
.Select((a, b) => new MessageSendModel
{
accountConfigId = a.AccountConfigId,
id = a.Id,
messageType = SqlFunc.Subqueryable().Where(u => u.Type == "1" && u.EnCode == a.MessageType).Select(u => u.FullName),
msgTemplateName = b.FullName,
sendConfigId = a.SendConfigId,
templateId = a.TemplateId,
}).ToListAsync();
foreach (var item in list)
{
// 是否存在参数.
var flag = await _repository.AsSugarClient().Queryable().AnyAsync(x => x.TemplateId == item.templateId && x.DeleteMark == null);
if (flag)
{
item.paramJson = await _repository.AsSugarClient().Queryable((a, b, c) => new JoinQueryInfos(JoinType.Left, a.TemplateId == b.Id, JoinType.Left, a.TemplateId == c.TemplateId))
.Where((a, b, c) => a.TemplateId == item.templateId && a.DeleteMark == null && b.DeleteMark == null && a.Field == c.Field && a.Field != "@flowLink")
.Select((a, b) => new MessageSendParam
{
field = a.Field,
fieldName = a.FieldName,
id = a.Id,
templateCode = b.TemplateCode,
templateId = a.TemplateId,
templateName = b.FullName,
templateType = b.TemplateType
}).ToListAsync();
}
else
{
item.paramJson = await _repository.AsSugarClient().Queryable((a, b) => new JoinQueryInfos(JoinType.Left, a.TemplateId == b.Id))
.Where((a, b) => a.TemplateId == item.templateId && a.DeleteMark == null && b.DeleteMark == null && a.Field != "@flowLink")
.Where((a, b) => b.Title.Contains(a.Field) || b.Content.Contains(a.Field))
.Select((a, b) => new MessageSendParam
{
field = a.Field,
fieldName = a.FieldName,
id = a.Id,
templateCode = b.TemplateCode,
templateId = a.TemplateId,
templateName = b.FullName,
templateType = b.TemplateType
}).ToListAsync();
}
}
return list;
}
///
/// 测试发送.
///
/// 主键值.
///
[HttpPost("testSendConfig")]
public async Task SendTest([FromBody] List input)
{
var resultList = new List