using FHT.Agency.CommonTypes; using FHT.Agency.UI.InputModels.Notification; using Microsoft.AspNet.Identity; using System; using System.Collections.Generic; 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.Web.Http; using System.Web.Http.Description; using FHT.Agency.Web.Filters; using FHT.ApiCommon; using FHT.Agency.Queries.ImplementedInterfaces; using FHT.Agency.UI.ViewModels; using FHT.Core.UI.ViewModels.Company; using System.Web.Http.Cors; namespace FHT.Agency.Web.ApiControllers { [Authorize] [RoutePrefix("api/notifications")] [EnableCors(origins: "*", headers: "*", methods: "*")] public class NotificationApiController : ApiController { private readonly Core.Queries.ImplementedInterfaces.IUserQuery _coreUserQuery; private readonly Core.Queries.ImplementedInterfaces.ICompanyQuery _coreCompanyQuery; private readonly IExternalMessageService _externalMessageService; private readonly IBroadcastService _broadcastService; private readonly INotificationQuery _notificationQuery; private readonly Services.ImplementedInterfaces.INotificationService _notificationService; private readonly IUserQuery _userQuery; private readonly Dictionary NotificationTypeMaps = new Dictionary { { OperationNotificationType.SpecificUserNotice, NotificationType.OperationSingleUser }, { OperationNotificationType.SpecificCompanyNotice, NotificationType.OperationSingleCompany }, }; public NotificationApiController( IBroadcastService operationNotificationService, IExternalMessageService externalMessageService, INotificationQuery notificationQuery, Services.ImplementedInterfaces.INotificationService notificationService, Core.Queries.ImplementedInterfaces.IUserQuery coreUserQuery, Core.Queries.ImplementedInterfaces.ICompanyQuery coreCompanyQuery, IUserQuery userQuery ) { _broadcastService = operationNotificationService; _externalMessageService = externalMessageService; _notificationQuery = notificationQuery; _notificationService = notificationService; _coreUserQuery = coreUserQuery; _coreCompanyQuery = coreCompanyQuery; _userQuery = userQuery; } #region 前台运营通知 [HttpGet] [ResponseType(typeof(QueryResult))] [Route("_searchcorebroadcastnotification")] [ClaimsAuthorize(FhtUserClaimType.BroadcastNoticeList)] public async Task SearchCoreBroadcastNotificationAsync(OperationNotificationType? operationNotificationType=null, string q="", int currPage = 1, int limit = 20) { int offset = (currPage < 2 ? 0 : currPage - 1) * limit; var broadcastType = (BroadcastType?)null; if (operationNotificationType.HasValue) { if (operationNotificationType.Value == OperationNotificationType.AllCompanyAdmins) { broadcastType = BroadcastType.AllCompanyAdmins; } else if (operationNotificationType.Value == OperationNotificationType.AllUsers) { broadcastType = BroadcastType.AllUsers; } } QueryResult notifications = await _broadcastService.SearchBroadcastsAsync(q, broadcastType, limit, offset); return Ok(notifications); } [HttpGet] [ResponseType(typeof(QueryResult))] [Route("_searchcorespecialnotification")] [ClaimsAuthorize(FhtUserClaimType.SystemNoticeList)] public async Task SearchCoreSpecialNotificationAsync(OperationNotificationType? operationNotificationType = null, string q="", int currPage = 1, int limit = 20) { int offset = (currPage < 2 ? 0 : currPage - 1) * limit; NotificationType? notificationType = operationNotificationType.HasValue ? NotificationTypeMaps[operationNotificationType.Value] : (NotificationType?)null; QueryResult notifications = await _broadcastService.SearchNotificationAsync(q, notificationType, limit, offset); List users = await _coreUserQuery.GetAuditUsersByIdsAsync(notifications.Data.Select(n => n.TargetId).ToList()); notifications.Data.ForEach(n => { Core.UI.ViewModels.Account.UserAuditListItemViewModel user = users.Find(u => u.UserId == n.TargetId); if (user != null) { n.TargetName = user.RealName; } }); return Ok(notifications); } [HttpPost] [ValidateModel] [ResponseType(typeof(void))] [Route("_coresend")] [ClaimsAuthorize(FhtUserClaimType.SystemNotice)] public IHttpActionResult 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); } } return Ok(); } #endregion #region 代理商通知 [HttpGet] [ResponseType(typeof(QueryResult))] [Route("_search")] [ClaimsAuthorize(FhtUserClaimType.AgencyNotificationManage)] public async Task SearchAsync(AgencyNotificationType? notificationType = null, string q="", int currPage = 1, int limit = 20) { int offset = (currPage < 2 ? 0 : currPage - 1) * limit; QueryResult notifications = await _notificationQuery.SearchAsync(notificationType, q, offset, limit); return Ok(notifications); } [HttpPost] [ValidateModel] [Route("_send")] [ResponseType(typeof(void))] [ClaimsAuthorize(FhtUserClaimType.SendAgencyNotification)] public IHttpActionResult Send(NotificationInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); _notificationService.CreateNotification(authenticatedUserId, model.Title, model.Content, model.NotificationType, model.TargetId, null); return Ok(); } #endregion #region 检索目标 [HttpGet] [ResponseType(typeof(List))] [Route("_searchtargetbynotificationtype")] [ClaimsAuthorize(FhtUserClaimType.AgencyNotificationManage)] public async Task SearchTargetByNotificationTypeAsync(AgencyNotificationType type, string q) { var targets = new List(); if (type == AgencyNotificationType.Agency) { targets = await _userQuery.SearchAgencyOrChannelAsync(q); } else if (type == AgencyNotificationType.Channel) { targets = await _userQuery.SearchAgencyOrChannelAsync(q, false); } return Ok(targets); } [ResponseType(typeof(List))] [Route("_searchcoretargetbynotificationtype")] [ClaimsAuthorize(FhtUserClaimType.SystemNotice)] [HttpGet] public async Task SearchCoreTargetByNotificationTypeAsync(OperationNotificationType type, string q) { var targets = new List(); if (type == OperationNotificationType.SpecificUserNotice) { targets =await _coreUserQuery.SearchUsersAsync(q); } else if (type == OperationNotificationType.SpecificCompanyNotice) { List < SearchSimpleCompanyViewModel > companies = await _coreCompanyQuery.SearchCompanyAsync(q); targets = companies.Select(c => new AutoCompleteViewModel { Text = c.Name, Value = c.CompanyId }).ToList(); } return Ok(targets); } #endregion } }