|
|
|
@ -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<int>(query, "Light");
|
|
|
|
|
model.MinLight = light.Item1;
|
|
|
|
|
model.MaxLight = light.Item2;
|
|
|
|
|
var temperature = this.GetDataValue<float>(query, "Temperature");
|
|
|
|
|
model.MinTemperaturest = temperature.Item1;
|
|
|
|
|
model.MaxTemperatures = temperature.Item2;
|
|
|
|
|
var humidity = this.GetDataValue<float>(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<int>(query, "Light");
|
|
|
|
|
model.MinLight = light.Item1;
|
|
|
|
|
model.MaxLight = light.Item2;
|
|
|
|
|
var temperature = this.GetDataValue<float>(query, "Temperature");
|
|
|
|
|
model.MinTemperaturest = temperature.Item1;
|
|
|
|
|
model.MaxTemperatures = temperature.Item2;
|
|
|
|
|
var humidity = this.GetDataValue<float>(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<T?, T?> GetDataValue<T>(IQueryable<IoTDevice> 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<T>(o));
|
|
|
|
|
return new Tuple<T?, T?>(
|
|
|
|
|
list.Any() ? new T?(list.Min()) : null,
|
|
|
|
|
list.Any() ? new T?(list.Max()) : null
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private T ConvertTo<T>(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();
|
|
|
|
|
}
|
|
|
|
|