using FHT.Agency.CommonTypes; using FHT.Agency.Queries.ImplementedInterfaces; using FHT.Agency.UI.InputModels.Notification; using FHT.Agency.UI.InputModels.Search; using FHT.Agency.UI.ViewModels; using Microsoft.AspNet.Identity; using System.Collections.Generic; using System.Web.Mvc; using FHT.Management.Interfaces.DependentInterfaces.Services; using FHT.DDD; using System.Threading.Tasks; using FHT.MvcCommon; using FHT.MvcCommon; using System.Net; namespace FHT.Agency.Web.Controllers { public class NotificationController : Controller { private readonly INotificationQuery _notificationQuery; private readonly Services.ImplementedInterfaces.INotificationService _notificationService; private readonly IUserQuery _userQuery; private readonly Core.Queries.ImplementedInterfaces.IUserQuery _coreUserQuery; private readonly Core.Queries.ImplementedInterfaces.ICompanyQuery _companyQuery; // private readonly Core.Queries.ImplementedInterfaces.INotificationQuery _notificationTempletQuery; private readonly IExternalMessageService _externalMessageService; public NotificationController( INotificationQuery notificationQuery, Services.ImplementedInterfaces.INotificationService notificationService, IUserQuery userQuery, Core.Queries.ImplementedInterfaces.IUserQuery coreUserQuery, Core.Queries.ImplementedInterfaces.ICompanyQuery companyQuery, // Core.Queries.ImplementedInterfaces.INotificationQuery notificationTempletQuery, IExternalMessageService externalMessageService ) { _notificationQuery = notificationQuery; _notificationService = notificationService; _userQuery = userQuery; _coreUserQuery = coreUserQuery; _companyQuery = companyQuery; // _notificationTempletQuery = notificationTempletQuery; _externalMessageService = externalMessageService; } [HttpGet] public async Task Index(SearchNotificationInputModel args) { QueryResult notifications =await _notificationQuery.SearchAsync(args.NotificationType,args.Title,args.Offset(),args.Limit); ViewBag.Notifications = notifications; return View(args); } /// /// 代理商中消息推送 /// /// [HttpGet] public ActionResult Send() { return View(new NotificationInputModel()); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Send(NotificationInputModel model) { int authenticatedUserId = User.Identity.GetUserId(); _notificationService.CreateNotification(authenticatedUserId, model.Title, model.Content, model.NotificationType, model.TargetId, null); return new HttpStatusCodeResult(HttpStatusCode.NoContent); } //[HttpGet] //public ActionResult CoreNotifications(SearchBroadcastNoticeInputModel args) //{ // var notifications = _managementMarketBroadcastNoticeService.Search(args.Title, args.NotificationType, args.Limit, args.Offset()); // ViewBag.Notifications = notifications; // return View(args); //} ///// ///// Core中消息系统推送 ///// ///// //[HttpGet] //public ActionResult CoreSend() //{ // return View(new BroadcastNoticeInputModel()); //} //[HttpPost] //[ValidateAntiForgeryToken] //public ActionResult CoreSend(BroadcastNoticeInputModel model) //{ // if (!model.TargetId.HasValue && (model.NotificationType == ManagementNotificationType.SpecificCompanyNotice || // model.NotificationType == ManagementNotificationType.SpecificUserNotice )) // { // throw new ArgumentNullException("指定发送对象ID为null"); // } // var authenticatedUserId = User.Identity.GetUserId(); // var noticeId = _managementMarketBroadcastNoticeService.Insert(authenticatedUserId, model); // var isTimedPush = model.Pushed.HasValue && model.Pushed.Value > DateTime.Now.AddSeconds(60); // TimeSpan? timeSpan = null; // if (isTimedPush) // timeSpan = model.Pushed.Value - DateTime.Now; // if (model.NotificationType == ManagementNotificationType.SystemCompanyNotice) // { // if (isTimedPush) // { // BackgroundJob.Schedule(() => _externalMessageService.BroadcastAllCompany(model.Title, noticeId, model.Content, DateTime.Now, model.Pushed, DateTime.Now), timeSpan.Value); // } // else // { // _externalMessageService.BroadcastAllCompany(model.Title, noticeId, model.Content, DateTime.Now, model.Pushed, DateTime.Now); // } // } // else if (model.NotificationType == ManagementNotificationType.SpecificCompanyNotice) // { // if (isTimedPush) // { // BackgroundJob.Schedule(() => _externalMessageService.BroadcastSingleCompany(model.Title, noticeId, model.Content, model.TargetId.Value, DateTime.Now, model.Pushed, DateTime.Now), timeSpan.Value); // } // else // { // _externalMessageService.BroadcastSingleCompany(model.Title, noticeId, model.Content, model.TargetId.Value, DateTime.Now, model.Pushed, DateTime.Now); // } // } // else if (model.NotificationType == ManagementNotificationType.SystemUserNotice) // { // if (isTimedPush) // { // BackgroundJob.Schedule(() => _externalMessageService.BroadcastAllUser(model.Title, noticeId, model.Content, DateTime.Now, model.Pushed, DateTime.Now), timeSpan.Value); // } // else // { // _externalMessageService.BroadcastAllUser(model.Title, noticeId, model.Content, DateTime.Now, model.Pushed, DateTime.Now); // } // } // else if (model.NotificationType == ManagementNotificationType.SpecificUserNotice) // { // if (isTimedPush) // { // BackgroundJob.Schedule(() => _externalMessageService.BroadcastSingleUser(model.Title, noticeId, model.Content, model.TargetId.Value, DateTime.Now, model.Pushed, DateTime.Now), timeSpan.Value); // } // else // { // _externalMessageService.BroadcastSingleUser(model.Title, noticeId, model.Content, model.TargetId.Value, DateTime.Now, model.Pushed, DateTime.Now); // } // } // else // { // throw new InvalidOperationException("该类型通知,没有对应处理方法!"); // } // return View(); //} [IsAjaxJsonResult] public async Task GetTargetByNotificationType(AgencyNotificationType type, string q) { var users = new List(); if (type == AgencyNotificationType.Agency) { users =await _userQuery.SearchAgencyOrChannelAsync(q); } else if (type == AgencyNotificationType.Channel) { users =await _userQuery.SearchAgencyOrChannelAsync(q, false); } return Json(users, JsonRequestBehavior.AllowGet); } //public JsonResult GetCoreTargetByNotificationType(ManagementNotificationType type, string q) //{ // var users = new List(); // if (type == ManagementNotificationType.SpecificUserNotice) // { // users = _coreUserQuery.SearchUsers(q); // } // else if (type == ManagementNotificationType.SpecificCompanyNotice) // { // users = _companyQuery.SearchSelectListItem(q); // } // return Json(users, JsonRequestBehavior.AllowGet); //} } }