using Infrastructure.Data; using Infrastructure.Extensions; using Infrastructure.Web; using IoT.Shared.Application.Domain.Entities; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Platform.ViewModels; using System.Collections.Generic; using System.Linq; using System; namespace Platform.Controllers { [Authorize] [ApiExplorerSettings(IgnoreApi = true)] public class HomeController : BaseController { private readonly IUserService _useService; private readonly IRepository _userRepo; private readonly IRepository _areaRepo; private readonly IRepository _organRepo; private readonly IRepository _buildingRepo; public HomeController(IUserService userService, IRepository userRepo, IRepository areaRepo, IRepository organRepo, IRepository buildingRepo) { this._useService = userService; this._userRepo = userRepo; this._areaRepo = areaRepo; this._organRepo = organRepo; this._buildingRepo = buildingRepo; } 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 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.Scenes = this._buildingRepo.ReadOnlyTable() .Where(o => o.OrganId == model.Organ.Id) .SelectMany(o=>o.Scenes) .Where(o => o.Hidden == false) .WhereIf(model.BuildingId.HasValue,o => o.BuildingId == model.BuildingId.Value) .ToList(); } 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.Product) .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); } 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(); } public string Mac(string id) { return Helper.Instance.MacEncrypt(id); } } }