using FHT.Agency.CommonTypes; using FHT.Agency.UI.InputModels.Notification; using FHT.Agency.UI.InputModels.Search; using Microsoft.AspNet.Identity; using System; using System.Collections.Generic; using System.Web.Mvc; using FHT.Core.UI.ViewModels; using FHT.Management.Interfaces.DependentInterfaces.Services; using FHT.Notifications.Interfaces; using FHT.Notifications.Interfaces.Models; using System.Linq; using Hangfire; using System.Threading.Tasks; using FHT.DDD; using System.Net; using FHT.MvcCommon; using FHT.Core.UI.ViewModels.Company; namespace FHT.Agency.Web.Areas.Manage.Controllers { public class NotificationController : Controller { private readonly Core.Queries.ImplementedInterfaces.IUserQuery _coreUserQuery; private readonly Core.Queries.ImplementedInterfaces.ICompanyQuery _companyQuery; private readonly IExternalMessageService _externalMessageService; private readonly IBroadcastService _broadcastService; private readonly Dictionary NotificationTypeMaps = new Dictionary { { OperationNotificationType.SpecificUserNotice, NotificationType.OperationSingleUser }, { OperationNotificationType.SpecificCompanyNotice, NotificationType.OperationSingleCompany }, }; public NotificationController( Core.Queries.ImplementedInterfaces.IUserQuery coreUserQuery, Core.Queries.ImplementedInterfaces.ICompanyQuery companyQuery, IBroadcastService operationNotificationService, IExternalMessageService externalMessageService ) { _coreUserQuery = coreUserQuery; _companyQuery = companyQuery; _broadcastService = operationNotificationService; _externalMessageService = externalMessageService; } /// /// /// /// /// /// [HttpGet] public async Task< ActionResult> CoreNotifications(SearchBroadcastNoticeInputModel args) { if (args.TargetType == OperationType.Broadcast) { var broadcastType = (BroadcastType?)null; if (args.NotificationType.HasValue) { if (args.NotificationType.Value == OperationNotificationType.AllCompanyAdmins) { broadcastType = BroadcastType.AllCompanyAdmins; } else if (args.NotificationType.Value == OperationNotificationType.AllUsers) { broadcastType = BroadcastType.AllUsers; } } ViewBag.Notifications =await _broadcastService.SearchBroadcastsAsync(args.Title, broadcastType, args.Limit, args.Offset()); } else { NotificationType? notificationType = args.NotificationType.HasValue ? NotificationTypeMaps[args.NotificationType.Value] : (NotificationType?)null; QueryResult notifications =await _broadcastService.SearchNotificationAsync(args.Title, notificationType, args.Limit, args.Offset()); List users =await _coreUserQuery.GetAuditUsersByIdsAsync(notifications.Data.Select(n => n.TargetId).ToList()); notifications.Data.ForEach(n => { Core.UI.ViewModels.Account.UserAuditListItemViewModel user = users.FirstOrDefault(u => u.UserId == n.TargetId); if (user != null) { n.TargetName = user.RealName; } }); ViewBag.Notifications = notifications; } return View(args); } /// /// Core中消息系统推送 /// /// [HttpGet] public ActionResult CoreSend() { return View(new BroadcastNoticeInputModel()); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult CoreSend(BroadcastNoticeInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); DateTime createTime = DateTime.Now; DateTime sendTime = createTime; if (model.Pushed.HasValue && model.Pushed.Value > DateTime.Now.AddMinutes(10)) { sendTime = model.Pushed.Value; } bool isTimedPush = sendTime != createTime; if (model.NotificationType == OperationNotificationType.AllCompanyAdmins) { if (isTimedPush) { BackgroundJob.Schedule(() => _externalMessageService.BroadcastAllCompany(model.Title, authenticatedUserId, model.Content, createTime, sendTime), sendTime); } else { _externalMessageService.BroadcastAllCompany(model.Title, authenticatedUserId, model.Content, createTime, sendTime); } } else if (model.NotificationType == OperationNotificationType.SpecificCompanyNotice) { if (isTimedPush) { BackgroundJob.Schedule(() => _externalMessageService.PushSingleCompany(model.Title, authenticatedUserId, model.Content, model.TargetId.Value, createTime, sendTime), sendTime); } else { _externalMessageService.PushSingleCompany(model.Title, authenticatedUserId, model.Content, model.TargetId.Value, createTime, sendTime); } } else if (model.NotificationType == OperationNotificationType.AllUsers) { if (isTimedPush) { BackgroundJob.Schedule(() => _externalMessageService.BroadcastAllUser(model.Title, authenticatedUserId, model.Content, createTime, sendTime), sendTime); } else { _externalMessageService.BroadcastAllUser(model.Title, authenticatedUserId, model.Content, createTime, sendTime); } } else if (model.NotificationType == OperationNotificationType.SpecificUserNotice) { if (isTimedPush) { BackgroundJob.Schedule(() => _externalMessageService.PushSingleUser(model.Title, authenticatedUserId, model.Content, model.TargetId.Value, createTime, sendTime), sendTime); } else { _externalMessageService.PushSingleUser(model.Title, authenticatedUserId, model.Content, model.TargetId.Value, createTime, sendTime); } } else { // throw new InvalidOperationException("该类型通知,没有对应处理方法!"); } return new HttpStatusCodeResult(HttpStatusCode.NoContent); } [IsAjaxJsonResult] public async Task GetCoreTargetByNotificationType(OperationNotificationType type, string q) { var res = new List(); if (type == OperationNotificationType.SpecificUserNotice) { res =await _coreUserQuery.SearchUsersAsync(q); } else if (type == OperationNotificationType.SpecificCompanyNotice) { List companies = await _companyQuery.SearchCompanyAsync(q); res = companies.Select(c => new AutoCompleteViewModel { Text = c.Name, Value = c.CompanyId }).ToList(); } return Json(res, JsonRequestBehavior.AllowGet); } } }