using Learun.Application.Base.AuthorizeModule; using Learun.Application.IM; using Learun.Application.Organization; using Learun.Application.WeChat; using Learun.Util; using System; using System.Collections.Generic; namespace Learun.Application.Message { /// /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架 /// Copyright (c) 2013-2018 Hexagon PPM /// 创 建:超级管理员 /// 日 期:2018-10-16 16:24 /// 描 述:消息策略 /// public class LR_StrategyInfoBLL : LR_StrategyInfoIBLL { private LR_StrategyInfoService lR_StrategyInfoService = new LR_StrategyInfoService(); private UserRelationIBLL userRelationIBLL = new UserRelationBLL(); private UserIBLL userIBLL = new UserBLL(); private IMSysUserIBLL iMSysUserIBLL = new IMSysUserBLL(); #region 获取数据 /// /// 获取列表数据 /// /// public IEnumerable GetList(string queryJson) { try { return lR_StrategyInfoService.GetList(queryJson); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// /// 获取列表分页数据 /// 分页参数 /// /// public IEnumerable GetPageList(Pagination pagination, string queryJson) { try { return lR_StrategyInfoService.GetPageList(pagination, queryJson); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// /// 获取实体数据 /// 主键 /// /// public LR_MS_StrategyInfoEntity GetEntity(string keyValue) { try { return lR_StrategyInfoService.GetEntity(keyValue); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// /// 根据策略编码获取策略 /// /// 策略编码 /// public LR_MS_StrategyInfoEntity GetEntityByCode(string code) { try { return lR_StrategyInfoService.GetEntityByCode(code); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } #endregion #region 提交数据 /// /// 删除实体数据 /// 主键 /// /// public void DeleteEntity(string keyValue) { try { lR_StrategyInfoService.DeleteEntity(keyValue); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// /// 保存实体数据(新增、修改) /// 主键 /// /// public void SaveEntity(string keyValue, LR_MS_StrategyInfoEntity entity) { try { lR_StrategyInfoService.SaveEntity(keyValue, entity); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// ///策略编码不能重复 /// /// 主键 /// 编码 /// public bool ExistStrategyCode(string keyValue, string F_StrategyCode) { try { return lR_StrategyInfoService.ExistStrategyCode(keyValue, F_StrategyCode); } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } #endregion #region 扩展方法(发送消息) /// /// 消息处理,在此处处理好数据,然后调用消息发送方法 /// /// 消息策略编码 /// 消息内容 /// 用户列表信息 /// public ResParameter SendMessage(string code, string content, string userlist) { try { 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 { LR_MS_StrategyInfoEntity strategyInfoEntity = 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 = userRelationIBLL.GetUserIdList(item,""); string userIds = ""; foreach (var items in data) { if (userIds != "") { userIds += ","; } userIds += items.F_UserId; } var userList = userIBLL.GetListByUserIds(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"://邮箱,调用邮箱发送方法 EmailSend(content, list); break; case "2"://微信,调用微信发送方法 WeChatSend(content, list); break; case "3": //短信,调用短信发送方法 SMSSend(content, list); break; case "4": //系统IM,效用系统IM发送方法 IMSend(content, list); break; default: break; } } } } } } resParameter.code = ResponseCode.success; resParameter.info = "发送成功"; return resParameter; } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// /// 邮件发送 /// /// 消息内容 /// 用户列表信息 /// public void EmailSend(string content, List list) { try { string SystemName = Config.GetValue("SystemName");//系统名称 foreach (var item in list) { if (!string.IsNullOrEmpty(item.F_Email)) { bool res = MailHelper.Send(item.F_Email, SystemName + " - 发送消息", content.Replace("-", "")); if (!res) { //如果发送不成功,则插入消息记录表 MES_MSG_RecordEntity entity = new MES_MSG_RecordEntity(); // 取得当前登录用户 UserInfo userInfo = LoginUserInfo.Get(); // 发送人 entity.Sender = userInfo.userId; // 发送次数 entity.SendCnt = 1; entity.Title = SystemName + " - 发送消息"; entity.Content = content.Replace("-", ""); // 接收人账号 entity.Accounter = item.F_Account; // 接收邮件地址 entity.MailAddress = item.F_Email; // 发送失败 entity.Status = "fail"; entity.ErrContent = "邮件发送失败"; //类型 entity.MsgType = "1"; new MSG_RecordBLL().SaveEntity("", entity); } } } } catch (Exception ex) { if (ex is ExceptionEx) { throw; } else { throw ExceptionEx.ThrowBusinessException(ex); } } } /// /// 微信发送(企业号) /// /// 消息内容 /// 用户列表 /// public void WeChatSend(string content, List list) { string SystemName = Config.GetValue("SystemName");//系统名称 string SalesManager = ""; foreach (var item in list) { if (SalesManager != "") { SalesManager += "|"; } SalesManager += item.F_Account; } var text = new SendText() { agentid = Config.GetValue("AppId"),//应用ID touser = SalesManager,//@all:所有人,多个用户名用 “|”隔开 text = new SendText.SendItem() { content = content } }; MessageSendResult result = text.Send();//发送消息,并返回结果 if (result.errcode != 0) { //发送成不成功,则插入消息记录表 MES_MSG_RecordEntity entity = new MES_MSG_RecordEntity(); // 取得当前登录用户 UserInfo userInfo = LoginUserInfo.Get(); // 发送人 entity.Sender = userInfo.userId; // 发送次数 entity.SendCnt = 1; entity.Title = SystemName + " - 发送消息"; entity.Content = content; // 接收人 entity.Receiver = SalesManager; // 发送失败 entity.Status = "fail"; entity.ErrContent = result.errmsg; //类型 entity.MsgType = "2"; new MSG_RecordBLL().SaveEntity("", entity); } } /// /// /// /// 消息内容 /// 用户列表 /// public void SMSSend(string content, List list) { } /// /// 系统IM发送 /// /// 消息内容 /// 用户列表 /// public void IMSend(string content, List list) { List userList = new List(); foreach (var user in list) { userList.Add(user.F_UserId); } iMSysUserIBLL.SendMsg("IMSystem", userList, content); } #endregion } }