You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/Platform/Areas/IoTCenter/Controllers/IoTTimerController.cs

92 lines
4.0 KiB

using Infrastructure.Application;
using Infrastructure.Data;
using Infrastructure.Extensions;
using Infrastructure.Web.Mvc;
using Application.Domain.Entities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Platform.Application.Models;
using System.Linq;
namespace Platform.Areas.IoTCenter.Controllers
{
[Authorize]
[ApiController]
[Route("IoTCenter/[controller]/[action]")]
[Area("IoTCenter")]
[ControllerScope(ControllerScopeType.Platform)]
public class IoTTimerController : CrudController<IoTTimer, EditIoTTimerModel>
{
private readonly AjaxController _ajax;
private readonly IRepository<Organ> _organRepo;
private readonly IRepository<Building> _buildingRepo;
public IoTTimerController(IRepository<IoTTimer> repo,
AjaxController ajax,
IRepository<Organ> organRepo,
IRepository<Building> buildingRepo) : base(repo)
{
this._ajax = ajax;
this._organRepo = organRepo;
this._buildingRepo = buildingRepo;
}
public override IQueryable<IoTTimer> Query(PagedListModel<EditIoTTimerModel> model, IQueryable<IoTTimer> query)
{
return query
.WhereIf(model.Query.OrganId.HasValue, o => o.IoTScene.Building.OrganId == model.Query.OrganId.Value)
.WhereIf(model.Query.BuildingId.HasValue, o => o.IoTScene.BuildingId == model.Query.BuildingId.Value)
.WhereIf(model.Query.IoTSceneId.HasValue, o => o.IoTSceneId == model.Query.IoTSceneId.Value)
.WhereIf(!string.IsNullOrEmpty(model.Query.Name), o => o.Name.Contains(model.Query.Name))
.WhereIf(model.Query.Disabled.HasValue, o => o.Disabled == model.Query.Disabled.Value);
}
public override IQueryable<IoTTimer> Include(IQueryable<IoTTimer> query)
{
return query.Include(o => o.IoTScene).ThenInclude(o => o.Building).ThenInclude(o => o.Organ);
}
public override void EntityToModel(IoTTimer entity, EditIoTTimerModel model)
{
model.BuildingId = entity.IoTScene?.BuildingId;
model.OrganId = entity.IoTScene?.Building?.OrganId;
}
public override void ToDisplayModel(IoTTimer entity, EditIoTTimerModel model)
{
if (model.BuildingId.HasValue)
{
var name = this._buildingRepo.ReadOnlyTable()
.Where(o => o.ParentId != null)
.Where(o => o.Left <= entity.IoTScene.Building.Left && o.Right >= entity.IoTScene.Building.Right)
.ToList()
.ToTree()
.FirstOrDefault(o => o.Id == model.BuildingId.Value)?.GetDisplayName();
ViewData.Add(model.BuildingId.Value, name);
}
if (model.OrganId.HasValue)
{
var name = this._organRepo.ReadOnlyTable()
.Where(o => o.ParentId != null)
.Where(o => o.Left <= entity.IoTScene.Building.Organ.Left && o.Right >= entity.IoTScene.Building.Organ.Right)
.ToList()
.ToTree()
.FirstOrDefault(o => o.Id == entity.IoTScene.Building.OrganId)?.GetDisplayName();
ViewData.Add(model.OrganId, name);
}
ViewData.Add(model.IoTSceneId, entity?.IoTScene?.Name);
}
public override void ToEditModel(IoTTimer entity, EditIoTTimerModel model)
{
ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrgan(model.OrganId).SelectList());
ViewData.SelectList(o => model.BuildingId, () => this._ajax.GetBuilding(model.OrganId.Value, model.BuildingId).SelectList(), model.OrganId.HasValue);
ViewData.SelectList(o => model.IoTSceneId, () => this._ajax.GetIoTScene(model.BuildingId.Value, model.IoTSceneId).SelectList(), model.BuildingId.HasValue);
}
}
}