using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using NPOI.DDF; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using XYY.Api.WarningNotice.WarningNoticeService; using XYY.Common.Standard; using XYY.Data.Standard; using XYY.Data.Standard.WX; using XYY.Service.Standard.DingDing; using XYY.Service.Strandard.EMailService; namespace XYY.Api.WarningNotice.Controllers { [Route("[controller]/[action]")] [ApiController] public class WarningNoticeController : ControllerBase { IEmailService emailService; IWXBindRepository wXBindRepository; IDingDingService _dingDingService; IWebHook wxWebHook; IUserInfoRepository userInfoRepository; public WarningNoticeController(IDingDingService dingDingService, IWebHook wxWebHook, IWXBindRepository wXBindRepository, IEmailService emailService, IUserInfoRepository userInfoRepository) { _dingDingService = dingDingService; this.wxWebHook = wxWebHook; this.wXBindRepository = wXBindRepository; this.emailService = emailService; this.userInfoRepository = userInfoRepository; } /// /// 以文本方式通知 /// /// 通知内容 /// public async Task NoticeText(TextMessageNoticeTarget target) { var result = await Task.Run(async () => { if (target.ServiceType == "钉钉") { DingTalkWarningNoticeService dingTalkWarningNoticeService = new DingTalkWarningNoticeService(_dingDingService); dingTalkWarningNoticeService.NoticeText(target); } else if (target.ServiceType == "微信") { string hook = target.Hook ?? "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5eb4675a-773a-40a0-9bad-745501122580"; QYWXWebHook webHook = new QYWXWebHook(hook); if (target.UserId != null && target.UserId.Length > 0) { //通知用户时,我们取一次关联数据 var list = await userInfoRepository.QueryAsync>(@"SELECT a.WXUserId as Item1,b.NickName as Item2 FROM [xyy_wms].[dbo].[QWUser_Info](nolock) a join User_Info(nolock) b on a.UserId=b.Id where NickName in @UserId", new { UserId = target.UserId }); List wxUsers = new List(); foreach (var item in target.UserId) { if (!list.Any(x => x.Item2 == item)) throw new Exception($"在微信通讯录中,未找到用户{item},请检查系统内有无该用户信息,企微通讯录内的姓名是否错误"); else { wxUsers.Add(list.First(x => x.Item2 == item).Item1); } } webHook.SendMarkdownAt(target.TargetName, target.Content, wxUsers.ToArray()); if (!string.IsNullOrEmpty(target.FileStringBase64)) { var file = await webHook.PostFileAsBase64(target.FileStringBase64, target.FileName); webHook.SendFile(file); } } else wxWebHook.SendMarkdown(target.TargetName, target.Content, target.TargetName, 2 * 60); } else { return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" }); } return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" }); }); return result; } public async Task NoticeTextToUser(TextMessageNoticeTarget target) { var result = await Task.Run(() => { if (target.ServiceType == "微信") { QYWXWebHook webHook = new QYWXWebHook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5eb4675a-773a-40a0-9bad-745501122580"); webHook.SendText(target.TargetName, target.Content, target.UserId); } else { return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" }); } return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" }); }); return result; } public async Task NoticeTextToXYYCustomer(TextMessageNoticeTarget target) { if (target.CustomerId == null || target.CustomerId.Count() == 0) throw new Exception("传入客户ID必须不为空"); var user = await wXBindRepository.GetWXUserId(target.UserId.Select(x => int.Parse(x)).ToArray()); string wxid = user.First().WXUserId; int[] customerIds = null; try { customerIds = target.CustomerId.Select(x => int.Parse(x)).ToArray(); } catch (Exception ex) { throw new Exception("传入客户ID必须为不为空的纯数字数组"); } var userInfo = await wXBindRepository.GetCustomerWXUserId(customerIds); var result = await Task.Run(() => { if (target.ServiceType == "微信") { QYWXWebHook webHook = new QYWXWebHook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5eb4675a-773a-40a0-9bad-745501122580"); webHook.SendMarkdownToCustomer(target.TargetName, target.Content, userInfo.Select(x => x.WXUserId).ToArray(), wxid); } else { return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" }); } return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" }); }); return result; } public async Task NoticeTextToXYYUser(TextMessageNoticeTarget target) { if (target.UserId == null || target.UserId.Count() == 0) throw new Exception("传入用户ID必须不为空"); int[] userIds = null; try { userIds = target.UserId.Select(x => int.Parse(x)).ToArray(); } catch (Exception ex) { throw new Exception("传入用户ID必须为不为空的纯数字数组"); } var userInfo = await wXBindRepository.GetWXUserId(userIds); var result = await Task.Run(() => { if (target.ServiceType == "微信") { QYWXWebHook webHook = new QYWXWebHook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5eb4675a-773a-40a0-9bad-745501122580"); webHook.SendMarkdown(target.TargetName, target.Content, userInfo.Select(x => x.WXUserId).ToArray()); } else { return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" }); } return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" }); }); return result; } public async Task NoticeEmail(EMailContent config) { try { emailService.Send(new List { config.To }, config.Title, config.HTMLBody, true, !string.IsNullOrEmpty(config.CC) ? config.CC.Split(',').ToList() : null); return new OkObjectResult(new { IsSuccess = true, Message = "" }); } catch (Exception ex) { return new OkObjectResult(new { IsSuccess = false, Message = ex.Message }); } } /// /// 以文本方式通知 /// /// 通知内容 /// public async Task NoticeCard(ActionCardNoticeTarget target) { if (target.ServiceType == "钉钉") { DingTalkWarningNoticeService dingTalkWarningNoticeService = new DingTalkWarningNoticeService(_dingDingService); await dingTalkWarningNoticeService.NoticeActionCard(target); } else { return new OkObjectResult(new { IsSuccess = false, ErrMsg = "未实现该服务" }); } return new OkObjectResult(new { IsSuccess = true, ErrMsg = "" }); } } }