using ce.autofac.extension;
using learun.iapplication;
using learun.util;
using learun.wechat;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace learun.application
{
///
/// 版 本 EasyCode EC管理后台
/// Copyright (c) 2019-present EC管理有限公司
/// 创建人:tobin
/// 日 期:2019.11.05
/// 描 述:消息策略
///
public class MStrategyInfoBLL : BLLBase, MStrategyInfoIBLL, BLL
{
private readonly MStrategyInfoService mStrategyInfoService = new MStrategyInfoService();
private readonly UserRelationIBLL _userRelationIBLL;
private readonly UserIBLL _userIBLL;
private readonly IMSysUserIBLL _iMSysUserIBLL;
///
///
///
///
///
///
public MStrategyInfoBLL(UserIBLL userIBLL, UserRelationIBLL userRelationIBLL, IMSysUserIBLL iMSysUserIBLL)
{
_userRelationIBLL = userRelationIBLL;
_userIBLL = userIBLL;
_iMSysUserIBLL = iMSysUserIBLL;
}
#region 获取数据
///
/// 获取列表数据
///
///
public Task> GetList()
{
return mStrategyInfoService.GetList();
}
///
/// 获取列表分页数据
///
/// 分页参数
/// 查询参数
///
public Task> GetPageList(Pagination pagination, string queryJson)
{
return mStrategyInfoService.GetPageList(pagination, queryJson);
}
///
/// 获取实体数据
///
/// 主键
///
public Task GetEntity(string keyValue)
{
return mStrategyInfoService.GetEntity(keyValue);
}
///
/// 根据策略编码获取策略
///
/// 策略编码
///
public Task GetEntityByCode(string code)
{
return mStrategyInfoService.GetEntityByCode(code);
}
#endregion 获取数据
#region 提交数据
///
/// 删除实体数据
///
/// 主键
///
public async Task DeleteEntity(string keyValue)
{
await mStrategyInfoService.DeleteEntity(keyValue);
}
///
/// 保存实体数据(新增、修改)
///
/// 主键
/// 实体数据
///
public async Task SaveEntity(string keyValue, MStrategyInfoEntity entity)
{
await mStrategyInfoService.SaveEntity(keyValue, entity);
}
#endregion 提交数据
#region 扩展方法(发送消息)
///
/// 消息处理,在此处处理好数据,然后调用消息发送方法
///
/// 消息策略编码
/// 消息内容
/// 用户列表信息
///
public async Task SendMessage(string code, string content, string userlist)
{
ResParameter resParameter = new ResParameter();
if (string.IsNullOrEmpty(code))//判断code编码是否输入
{
resParameter.code = ResponseCode.fail;
resParameter.info = "code编码为空";
}
else if (string.IsNullOrEmpty(content))//判断是否输入信息内容
{
resParameter.code = ResponseCode.fail;
resParameter.info = "content内容为空";
}
else
{
var strategyInfoEntity = await GetEntityByCode(code);//根据编码获取消息策略
if (strategyInfoEntity == null)//如果获取不到消息策略则code编码无效
{
resParameter.code = ResponseCode.fail;
resParameter.info = "code编码无效";
}
else
{
#region 用户信息处理
List list = new List();//消息发送对象
if (string.IsNullOrEmpty(userlist))
{
if (string.IsNullOrEmpty(strategyInfoEntity.F_SendRole))
{
resParameter.code = ResponseCode.fail;
resParameter.info = "消息策略无发送角色,需要输入人员userlist信息";
}
else
{
String[] rolecontent = strategyInfoEntity.F_SendRole.Split(',');//根据角色id获取用户信息
foreach (var item in rolecontent)
{
var data = await _userRelationIBLL.GetUserIdList(item);
string userIds = "";
foreach (var items in data)
{
if (userIds != "")
{
userIds += ",";
}
userIds += items.F_UserId;
}
var userList = await _userIBLL.GetListByKeyValues(userIds);
foreach (var user in userList)
{
list.Add(user);
}
}
}
}
else
{
list = userlist.ToList();
}
#endregion 用户信息处理
if (list.Count <= 0)//判断用户列表有一个或一个以上的用户用于发送消息
{
resParameter.code = ResponseCode.fail;
resParameter.info = "找不到发送人员";
}
else
{
if (string.IsNullOrEmpty(strategyInfoEntity.F_MessageType))
{
resParameter.code = ResponseCode.fail;
resParameter.info = "消息类型为空,无法发送消息";
}
else
{
string[] typeList = strategyInfoEntity.F_MessageType.Split(',');
foreach (var type in typeList)
{
switch (type)
{
case "1"://邮箱,调用邮箱发送方法
await EmailSend(content, list);
break;
case "2"://微信,调用微信发送方法
await WeChatSend(content, list);
break;
case "3": //短信,调用短信发送方法
SMSSend(content, list);
break;
case "4": //系统IM,效用系统IM发送方法
await IMSend(content, list);
break;
default:
break;
}
}
}
}
}
}
resParameter.code = ResponseCode.success;
resParameter.info = "发送成功";
return resParameter;
}
///
/// 邮件发送
///
/// 消息内容
/// 用户列表信息
///
public async Task EmailSend(string content, IEnumerable list)
{
foreach (var item in list)
{
if (!string.IsNullOrEmpty(item.F_Email))
{
await MailHelper.Send(item.F_Email, "系统消息", content.Replace("-", ""));
}
}
}
///
/// 微信发送(企业号)
///
/// 消息内容
/// 用户列表
///
public async Task WeChatSend(string content, List list)
{
string SalesManager = "";
foreach (var item in list)
{
if (SalesManager != "")
{
SalesManager += "|";
}
SalesManager += item.F_Account;
}
var text = new SendText()
{
agentid = ConfigHelper.GetConfig().CorpAppId,//应用ID
touser = SalesManager,//@all:所有人,多个用户名用 “|”隔开
text = new SendText.SendItem()
{
content = content
}
};
MessageSendResult result = await text.Send();//发送消息,并返回结果
}
///
/// 短息发送
///
/// 消息内容
/// 用户列表
///
public void SMSSend(string content, List list)
{
// 自行对接响应的短息平台
}
///
/// 系统IM发送
///
/// 消息内容
/// 用户列表
///
public async Task IMSend(string content, List list)
{
List userList = new List();
foreach (var user in list)
{
userList.Add(user.F_UserId);
}
await _iMSysUserIBLL.SendMsg("IMSystem", userList, content);
}
#endregion 扩展方法(发送消息)
}
}