diff --git a/projects/Platform/Api/InfluxController.cs b/projects/Platform/Api/Api/InfluxController.cs similarity index 98% rename from projects/Platform/Api/InfluxController.cs rename to projects/Platform/Api/Api/InfluxController.cs index 71e1a096..2df7bf62 100644 --- a/projects/Platform/Api/InfluxController.cs +++ b/projects/Platform/Api/Api/InfluxController.cs @@ -3,7 +3,6 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; -using System.Linq; using System.Net.Http; using System.Text; using System.Threading.Tasks; diff --git a/projects/Platform/Api/Api/SiteController.cs b/projects/Platform/Api/Api/SiteController.cs index 9d44f385..57cc414c 100644 --- a/projects/Platform/Api/Api/SiteController.cs +++ b/projects/Platform/Api/Api/SiteController.cs @@ -15,19 +15,34 @@ namespace Platform.Api [Route("api/v{version:apiVersion}/[controller]/[action]")] [ApiController] [Authorize] - public class SiteController : Controller + public class SiteController : ControllerBase { private readonly ISettingService _settingService; private readonly IRepository _userRepo; private readonly IRepository _appModuleRepo; private readonly IRepository _organUserRepo; + private readonly IRepository _areaRepo; + private readonly IRepository _organRepo; + private readonly IRepository _buildingRepo; + private readonly IRepository _statisticRepo; - public SiteController(ISettingService settingService, IRepository userRepo, IRepository appModuleRepo, IRepository organUserRepo) + public SiteController(ISettingService settingService, + IRepository userRepo, + IRepository appModuleRepo, + IRepository organUserRepo, + IRepository areaRepo, + IRepository organRepo, + IRepository buildingRepo, + IRepository statisticRepo) { this._settingService = settingService; this._userRepo = userRepo; this._appModuleRepo = appModuleRepo; - this._organUserRepo = organUserRepo; + this._organUserRepo = organUserRepo; + this._areaRepo = areaRepo; + this._organRepo = organRepo; + this._buildingRepo = buildingRepo; + this._statisticRepo = statisticRepo; } [HttpPost] @@ -72,5 +87,61 @@ namespace Platform.Api return Problem(ex.Message); } } + + [HttpPost] + public IActionResult GetOrgan() + { + var organId = User.GetOrganId().Value;//当前用户机构Id + var organ = this._organRepo.ReadOnlyTable() + .Where(o => o.Id == organId) + .Include(o => o.Buildings) + .FirstOrDefault(); + organ.Buildings.ToTree(); + var rootBuildingId = this._buildingRepo.ReadOnlyTable().Where(o => o.ParentId == null).Select(o => o.Id).FirstOrDefault(); + organ.Buildings = organ.Buildings + .Where(o => o.ParentId == rootBuildingId) + .ToList(); + var model = new + { + Organ = organ, + MaxLight = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}]MaxLight").FirstOrDefault()?.Value, + MinLight = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}]MinLight").FirstOrDefault()?.Value, + MaxTemperature = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}]MaxTemperature").FirstOrDefault()?.Value, + MinTemperature = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}]MinTemperature").FirstOrDefault()?.Value, + MaxHumidity = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}]MaxHumidity").FirstOrDefault()?.Value, + MinHumidity = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}]MinHumidity").FirstOrDefault()?.Value, + DeviceOpenCount = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}]DeviceOpenCount").FirstOrDefault()?.Value, + DeviceCloseCount = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}]DeviceCloseCount").FirstOrDefault()?.Value, + }; + return new JsonResult(model); + } + + + [HttpPost] + public IActionResult GetBuilding(Guid id) + { + var organId = User.GetOrganId().Value;//当前用户机构Id + var building = this._buildingRepo.ReadOnlyTable() + .Where(o => o.Id == id) + .Include(o => o.Scenes) + .Include(o => o.IoTGateways) + .ThenInclude(o => o.Devices).ThenInclude(o => o.Data) + .Include(o => o.IoTGateways).ThenInclude(o => o.Devices).ThenInclude(o => o.IoTProduct) + .FirstOrDefault(); + var model = new + { + Building = building, + MaxLight = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}][{id}]MaxLight").FirstOrDefault()?.Value, + MinLight = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}][{id}]MinLight").FirstOrDefault()?.Value, + MaxTemperature = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}][{id}]MaxTemperature").FirstOrDefault()?.Value, + MinTemperature = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}][{id}]MinTemperature").FirstOrDefault()?.Value, + MaxHumidity = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}][{id}]MaxHumidity").FirstOrDefault()?.Value, + MinHumidity = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}][{id}]MinHumidity").FirstOrDefault()?.Value, + DeviceOpenCount = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}][{id}]DeviceOpenCount").FirstOrDefault()?.Value, + DeviceCloseCount = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}][{id}]DeviceCloseCount").FirstOrDefault()?.Value, + }; + return new JsonResult(model); + } + } } \ No newline at end of file diff --git a/projects/Platform/Controllers/HomeController.cs b/projects/Platform/Controllers/HomeController.cs index 3c351a9d..71a26deb 100644 --- a/projects/Platform/Controllers/HomeController.cs +++ b/projects/Platform/Controllers/HomeController.cs @@ -4,10 +4,6 @@ using Infrastructure.Extensions; using Infrastructure.Web; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore; -using Platform.ViewModels; -using System; -using System.Linq; namespace Platform.Controllers { @@ -32,287 +28,6 @@ namespace Platform.Controllers } public IActionResult Index() - { - var area = this._areaRepo.ReadOnlyTable() - .Where(o => o.Name == "南关区") - .Include(o => o.Parent).ThenInclude(o => o.Parent).First(); - return View(); - } - - public IActionResult GetOrgan() - { - var organId = User.GetOrganId().Value;//当前用户机构Id - var organ = this._organRepo.ReadOnlyTable() - .Where(o => o.Id == organId) - .Include(o => o.Buildings) - .FirstOrDefault(); - organ.Buildings.ToTree(); - var rootBuildingId = this._buildingRepo.ReadOnlyTable().Where(o => o.ParentId == null).Select(o => o.Id).FirstOrDefault(); - organ.Buildings = organ.Buildings - .Where(o => o.ParentId == rootBuildingId) - .ToList(); - var model = new - { - Organ = organ, - MaxLight = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}]MaxLight").FirstOrDefault()?.Value, - MinLight = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}]MinLight").FirstOrDefault()?.Value, - MaxTemperature = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}]MaxTemperature").FirstOrDefault()?.Value, - MinTemperature = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}]MinTemperature").FirstOrDefault()?.Value, - MaxHumidity = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}]MaxHumidity").FirstOrDefault()?.Value, - MinHumidity = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}]MinHumidity").FirstOrDefault()?.Value, - DeviceOpenCount = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}]DeviceOpenCount").FirstOrDefault()?.Value, - DeviceCloseCount = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}]DeviceCloseCount").FirstOrDefault()?.Value, - }; - return Json(model); - } - - public IActionResult GetBuilding(Guid id) - { - var organId = User.GetOrganId().Value;//当前用户机构Id - var building = this._buildingRepo.ReadOnlyTable() - .Where(o => o.Id == id) - .Include(o => o.Scenes) - .Include(o => o.IoTGateways) - .ThenInclude(o => o.Devices).ThenInclude(o => o.Data) - .Include(o => o.IoTGateways).ThenInclude(o => o.Devices).ThenInclude(o => o.IoTProduct) - .FirstOrDefault(); - var model = new - { - Building = building, - MaxLight = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}][{id}]MaxLight").FirstOrDefault()?.Value, - MinLight = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}][{id}]MinLight").FirstOrDefault()?.Value, - MaxTemperature = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}][{id}]MaxTemperature").FirstOrDefault()?.Value, - MinTemperature = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}][{id}]MinTemperature").FirstOrDefault()?.Value, - MaxHumidity = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}][{id}]MaxHumidity").FirstOrDefault()?.Value, - MinHumidity = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}][{id}]MinHumidity").FirstOrDefault()?.Value, - DeviceOpenCount = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}][{id}]DeviceOpenCount").FirstOrDefault()?.Value, - DeviceCloseCount = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{organId}][{id}]DeviceCloseCount").FirstOrDefault()?.Value, - }; - return Json(model); - } - - public IActionResult Building(HomeModel model) - { - var userOrganId = User.GetOrganId().Value;//当前用户机构Id - var currentUserOrgan = this._organRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == userOrganId);//当前用户机构 - var organList = this._organRepo.ReadOnlyTable()//当前用户机构及下级机构 - .Where(o => o.ParentId != null) - .Where(o => o.Left >= currentUserOrgan.Left && o.Right <= currentUserOrgan.Right) - .Include(o => o.Buildings) - .ToList(); - organList.ToTree(); - var rootOrganId = this._organRepo.ReadOnlyTable().Where(o => o.Number == "root").Select(o => o.Id).FirstOrDefault(); - model.Organs = organList.Where(o => o.Parent == null).ToList(); - if (model.BuildingId.HasValue)//选中建筑,则建筑所属机构为当前机构 - { - model.Organ = organList.SelectMany(o => o.Buildings).FirstOrDefault(o => o.Id == model.BuildingId.Value).Organ; - model.Scenes = this._buildingRepo.ReadOnlyTable() - .Where(o => o.Id == model.BuildingId.Value) - .SelectMany(o => o.Scenes) - .Where(o => o.Hidden == false) - .ToList(); - } - else - { - if (model.OrganId.HasValue) - { - model.Organ = organList.FirstOrDefault(o => o.Id == model.OrganId.Value); - } - else - {//即未选中建筑也未选中机构,则用户所在机构为当前机构 - model.Organ = organList.FirstOrDefault(o => o.Id == userOrganId); - } - } - model.OrganId = model.Organ.Id; - - var rootBuildingId = this._buildingRepo.ReadOnlyTable().Where(o => o.Number == "root").Select(o => o.Id).FirstOrDefault(); - if (model.BuildingId.HasValue) - { - model.Building = model.Organ.Buildings.FirstOrDefault(o => o.Id == model.BuildingId.Value); - model.Buildings = model.Organ.Buildings.Where(o => o.ParentId == model.BuildingId.Value).ToList(); - } - else - { - model.Buildings = model.Organ.Buildings.Where(o => o.ParentId == rootBuildingId).ToList(); - } - - var currentBuilding = model.BuildingId.HasValue ? - this._buildingRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == model.BuildingId.Value) : - null; - var query = this._buildingRepo.ReadOnlyTable() - .Where(o => o.ParentId != null) - .Where(o => o.OrganId == model.OrganId.Value) - .WhereIf(!model.ShowAll && model.BuildingId.HasValue, o => o.Id == model.BuildingId.Value) - .WhereIf(!model.ShowAll && !model.BuildingId.HasValue, o => false) - .WhereIf(model.ShowAll && model.BuildingId.HasValue, o => o.Left >= currentBuilding.Left && o.Right <= currentBuilding.Right) - .SelectMany(o => o.IoTGateways) - .SelectMany(o => o.Devices); - model.TotalCount = query.Count(); - model.Deviceses = query - .Include(o => o.Data) - .Include(o => o.IoTProduct) - .Skip(model.PageSize * (model.PageIndex - 1)) - .Take(model.PageSize).ToList(); - - foreach (var item in organList) - { - item.Buildings = item.Buildings.ToTree().Where(o => o.ParentId == rootBuildingId).ToList(); - } - // - var light = this.GetDataValue(query, "Light"); - model.MinLight = light.Item1; - model.MaxLight = light.Item2; - var temperature = this.GetDataValue(query, "Temperature"); - model.MinTemperaturest = temperature.Item1; - model.MaxTemperatures = temperature.Item2; - var humidity = this.GetDataValue(query, "Humidity"); - model.MinHumidity = humidity.Item1; - model.MaxHumidity = humidity.Item2; - var query2 = query.Where(o => o.Name.Contains("开关") || o.Name.Contains("插座")).SelectMany(o => o.Data).Where(o => o.Key == "State"); - var hasDevices = query2.Any(); - model.Open = hasDevices ? new int?(query2.Where(o => o.Value == "开").Count()) : null; - model.Close = hasDevices ? new int?(query2.Where(o => o.Value == "关").Count()) : null; - return View(model); - } - - public IActionResult BuildingData(HomeModel model) - { - var userOrganId = User.GetOrganId().Value;//当前用户机构Id - var currentUserOrgan = this._organRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == userOrganId);//当前用户机构 - var organList = this._organRepo.ReadOnlyTable()//当前用户机构及下级机构 - .Where(o => o.ParentId != null) - .Where(o => o.Left >= currentUserOrgan.Left && o.Right <= currentUserOrgan.Right) - .Include(o => o.Buildings) - .ToList(); - organList.ToTree(); - var rootOrganId = this._organRepo.ReadOnlyTable().Where(o => o.Number == "root").Select(o => o.Id).FirstOrDefault(); - model.Organs = organList.Where(o => o.Parent == null).ToList(); - if (model.BuildingId.HasValue)//选中建筑,则建筑所属机构为当前机构 - { - model.Organ = organList.SelectMany(o => o.Buildings).FirstOrDefault(o => o.Id == model.BuildingId.Value).Organ; - model.Scenes = this._buildingRepo.ReadOnlyTable() - .Where(o => o.Id == model.BuildingId.Value) - .SelectMany(o => o.Scenes) - .Where(o => o.Hidden == false) - .ToList(); - } - else - { - if (model.OrganId.HasValue) - { - model.Organ = organList.FirstOrDefault(o => o.Id == model.OrganId.Value); - } - else - {//即未选中建筑也未选中机构,则用户所在机构为当前机构 - model.Organ = organList.FirstOrDefault(o => o.Id == userOrganId); - } - } - model.OrganId = model.Organ.Id; - - var rootBuildingId = this._buildingRepo.ReadOnlyTable().Where(o => o.Number == "root").Select(o => o.Id).FirstOrDefault(); - if (model.BuildingId.HasValue) - { - model.Building = model.Organ.Buildings.FirstOrDefault(o => o.Id == model.BuildingId.Value); - model.Buildings = model.Organ.Buildings.Where(o => o.ParentId == model.BuildingId.Value).ToList(); - } - else - { - model.Buildings = model.Organ.Buildings.Where(o => o.ParentId == rootBuildingId).ToList(); - } - - var currentBuilding = model.BuildingId.HasValue ? - this._buildingRepo.ReadOnlyTable().FirstOrDefault(o => o.Id == model.BuildingId.Value) : - null; - var query = this._buildingRepo.ReadOnlyTable() - .Where(o => o.ParentId != null) - .Where(o => o.OrganId == model.OrganId.Value) - .WhereIf(!model.ShowAll && model.BuildingId.HasValue, o => o.Id == model.BuildingId.Value) - .WhereIf(!model.ShowAll && !model.BuildingId.HasValue, o => false) - .WhereIf(model.ShowAll && model.BuildingId.HasValue, o => o.Left >= currentBuilding.Left && o.Right <= currentBuilding.Right) - .SelectMany(o => o.IoTGateways) - .SelectMany(o => o.Devices); - model.TotalCount = query.Count(); - model.Deviceses = query - .Include(o => o.Data) - .Include(o => o.IoTProduct) - .Skip(model.PageSize * (model.PageIndex - 1)) - .Take(model.PageSize).ToList(); - - foreach (var item in organList) - { - item.Buildings = item.Buildings.ToTree().Where(o => o.ParentId == rootBuildingId).ToList(); - } - // - var light = this.GetDataValue(query, "Light"); - model.MinLight = light.Item1; - model.MaxLight = light.Item2; - var temperature = this.GetDataValue(query, "Temperature"); - model.MinTemperaturest = temperature.Item1; - model.MaxTemperatures = temperature.Item2; - var humidity = this.GetDataValue(query, "Humidity"); - model.MinHumidity = humidity.Item1; - model.MaxHumidity = humidity.Item2; - var query2 = query.Where(o => o.Name.Contains("开关") || o.Name.Contains("插座")).SelectMany(o => o.Data).Where(o => o.Key == "State"); - var hasDevices = query2.Any(); - model.Open = hasDevices ? new int?(query2.Where(o => o.Value == "开").Count()) : null; - model.Close = hasDevices ? new int?(query2.Where(o => o.Value == "关").Count()) : null; - return Json(model); - } - - private Tuple GetDataValue(IQueryable query, string key) where T : struct - { - var list = query.SelectMany(o => o.Data) - .Where(o => o.Key == key).Select(o => o.Value) - .ToList() - .Select(o => ConvertTo(o)); - return new Tuple( - list.Any() ? new T?(list.Min()) : null, - list.Any() ? new T?(list.Max()) : null - ); - } - - private T ConvertTo(string value) where T : struct - { - T result = (T)Convert.ChangeType(value, typeof(T)); - return result; - } - - public IActionResult Alarm() - { - return View(); - } - - public IActionResult Product() - { - return View(); - } - - public IActionResult Device() - { - return View(); - } - - public IActionResult Organ() - { - return View(); - } - - public IActionResult Nodes() - { - return View(); - } - - public IActionResult Node() - { - return View(); - } - - public IActionResult Index3() - { - return View(); - } - - public IActionResult Index4() { return View(); } diff --git a/projects/Platform/Views/Shared/_Menu.cshtml b/projects/Platform/Views/Shared/_Menu.cshtml index c6cb5267..fc0775fd 100644 --- a/projects/Platform/Views/Shared/_Menu.cshtml +++ b/projects/Platform/Views/Shared/_Menu.cshtml @@ -1,7 +1,7 @@