using Application.Domain.Entities; using Application.Models; using Infrastructure.Application; using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web.Mvc; using IoTCenter.Services; using IoTShared.Controllers; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; namespace IoTCenter.Areas.Admin.Controllers { [Authorize] [Area(nameof(Admin))] public class SceneCommandController : CrudController, EditSceneCommandModel, EditSceneCommandModel> { private readonly IRepository _nodeRepo; private readonly IRepository _sceneRepo; private readonly IRepository _apiRepo; private readonly IRepository _deviceRepo; private readonly IRepository _repo; private readonly AjaxController _ajax; private readonly IHubContext _pageHubContext; public SceneCommandController(IRepository nodeRepo, IRepository sceneRepo, IRepository apiRepo, IRepository deviceRepo, IRepository repo, AjaxController ajax, IHubContext pageHubContext) : base(repo) { this._repo = repo; this._nodeRepo = nodeRepo; this._sceneRepo = sceneRepo; this._apiRepo = apiRepo; this._deviceRepo = deviceRepo; this._ajax = ajax; this._pageHubContext = pageHubContext; } public override IQueryable Include(IQueryable query) { return query.Include(o => o.Scene).ThenInclude(o => o.Node).Include(o => o.Command).ThenInclude(o => o.Device); } public override IQueryable Query(PagedListModel model, IQueryable query) { return query.Where(o => o.Scene.NodeId != null); } public override void ToModel(SceneCommand entity, EditSceneCommandModel model) { if (entity != null) { model.NodeId = entity.Scene.NodeId; } ViewData.SelectList(o => model.NodeId, () => this._ajax.GetNodeSelectList(model.SceneId)); ViewData.SelectList(o => model.SceneId, () => this._ajax.GetSceneSelectList(model.NodeId.Value, model.SceneId), model.NodeId.HasValue); ViewData.SelectList(o => model.CommandId, () => this._ajax.GetCommandSelectList(model.NodeId.Value, model.CommandId), model.NodeId.HasValue); } public override IActionResult Add(EditSceneCommandModel model) { if (ModelState.IsValid) { var number = this._nodeRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == model.NodeId).Number; this._pageHubContext.Clients.Group(number).SendAsync(Methods.ServerToClient, Methods.EditSceneCommandRequest, model.ToJson(), null); return RedirectTo(); } ModelState.AddModelError("", "服务器出现异常,请稍后重试"); return View(model); } public override IActionResult Edit(EditSceneCommandModel model) { if (ModelState.IsValid) { var number = this._nodeRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == model.NodeId).Number; this._pageHubContext.Clients.Group(number).SendAsync(Methods.ServerToClient, Methods.EditSceneCommandRequest, model.ToJson(), null); return RedirectTo(); } ModelState.AddModelError("", "服务器出现异常,请稍后重试"); return View(model); } public override IActionResult Delete(List list) { if (list is null) { throw new ArgumentNullException(nameof(list)); } foreach (var id in list) { try { var entity = this._repo.ReadOnlyTable().Include(o => o.Scene).ThenInclude(o => o.Node).FirstOrDefault(o => o.Id == id); var model = entity.To(); this._pageHubContext.Clients.Group(entity.Scene.Node.Number).SendAsync(Methods.ServerToClient, Methods.DeleteSceneCommandRequest, model.ToJson(), null); } catch (Exception ex) { ex.PrintStack(); return RedirectTo(rawMesage: ex.Message); } } return RedirectTo(); } } }