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/AjaxController.cs

113 lines
4.3 KiB

using Infrastructure.Data;
using Infrastructure.Extensions;
using IoT.Shared.Application.Domain.Entities;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Platform.Areas.IoTCenter.Controllers
{
[Area("IoTCenter")]
public class AjaxController : IoT.Shared.Areas.IoTCenter.Controlls.AjaxController
{
private readonly IRepository<Area> _areaRepo;
private readonly IRepository<Building> _buildingRepo;
private readonly IRepository<Role> _roleRepo;
public AjaxController(ILogger<AjaxController> logger,
IHttpContextAccessor httpContxt,
IRepository<IoTProductCategory> categoryRepo,
IRepository<IoTProduct> productRepo,
IRepository<Organ> organRepo,
IRepository<BuildingIoTGateway> buildingIoTGatewayRepo,
IRepository<OrganIoTScene> organSceneRepo,
IRepository<IoTGateway> nodeRepo,
IRepository<IoTScene> sceneRepo,
IRepository<IoTCommand> commandRepo,
IRepository<IoTDevice> deviceRepo,
IRepository<IoTData> dataRepo,
IRepository<IoTApi> apiRepo,
IRepository<IoTSceneTigger> sceneTiggerRepo,
IRepository<Area> areaRepo,
IRepository<Building> buildingRepo,
IRepository<Role> roleRepo)
: base(logger,
httpContxt,
categoryRepo,
productRepo,
organRepo,
buildingIoTGatewayRepo,
organSceneRepo,
nodeRepo,
sceneRepo,
commandRepo,
deviceRepo,
dataRepo,
apiRepo,
sceneTiggerRepo)
{
this._areaRepo = areaRepo;
this._buildingRepo = buildingRepo;
this._roleRepo = roleRepo;
}
public JsonResult GetArea(Guid? selected = null, Guid? currentId = null, string search = null)
{
var list = this._areaRepo.ReadOnlyTable()
.Where(o => o.ParentId != null)
.WhereIf(selected.HasValue, o => o.Id == selected.Value)
.WhereIf(!string.IsNullOrEmpty(search), o => o.Name.Contains(search) || o.Number.Contains(search))
.WhereIf(currentId.HasValue, o => o.Id != currentId.Value)
.OrderBy(o => o.Parent)
.ThenBy(o => o.DisplayOrder)
.Select(o => new { o.Id, Name = $"{o.Name}:{o.Number}" })
.Take(20)
.ToList();
return new JsonResult(new SelectList(list, "Id", "Name", selected));
}
public JsonResult GetOrganBuilding(Guid parentId, Guid? selected = null, Guid? currentId = null)
{
var list = this._buildingRepo.ReadOnlyTable()
.Where(o => o.ParentId != null)
.Where(o => o.OrganId == parentId)
.WhereIf(currentId.HasValue, o => o.Id != currentId.Value)
.OrderBy(o => o.Parent)
.ThenBy(o => o.DisplayOrder)
.Select(o => new { o.Id, Name = $"{o.Name}({o.Number})" })
.ToList();
return new JsonResult(new SelectList(list, "Id", "Name", selected));
}
public MultiSelectList GetOrganMultiSelectList(List<Guid> selected)
{
if (selected == null)
{
selected = new List<Guid>();
}
var list = this._organRepo.ReadOnlyTable()
.Where(o => o.ParentId != null)
.Select(o => new { o.Id, o.Name })
.ToList();
return new MultiSelectList(list, "Id", "Name", selected);
}
[ApiExplorerSettings(IgnoreApi = true)]
public MultiSelectList GetRoleMultiSelectList(IEnumerable<Guid> selected)
{
if (selected == null)
{
selected = new List<Guid>();
}
var list = this._roleRepo.ReadOnlyTable()
.Select(o => new { o.Id, o.Name })
.ToList();
return new MultiSelectList(list, "Id", "Name", selected);
}
}
}