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.
322 lines
14 KiB
322 lines
14 KiB
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using Infrastructure.Web;
|
|
using 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<User> _userRepo;
|
|
|
|
private readonly IRepository<Area> _areaRepo;
|
|
|
|
private readonly IRepository<Organ> _organRepo;
|
|
|
|
private readonly IRepository<Building> _buildingRepo;
|
|
private readonly IRepository<Statistic> _statisticRepo;
|
|
|
|
public HomeController(IUserService userService,
|
|
IRepository<User> userRepo,
|
|
IRepository<Area> areaRepo,
|
|
IRepository<Organ> organRepo,
|
|
IRepository<Building> buildingRepo,
|
|
IRepository<Statistic> statisticRepo)
|
|
{
|
|
this._useService = userService;
|
|
this._userRepo = userRepo;
|
|
this._areaRepo = areaRepo;
|
|
this._organRepo = organRepo;
|
|
this._buildingRepo = buildingRepo;
|
|
this._statisticRepo = statisticRepo;
|
|
}
|
|
|
|
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 userOrganId = User.GetOrganId().Value;//当前用户机构Id
|
|
var organ = this._organRepo.ReadOnlyTable()
|
|
.Include(o => o.Buildings)
|
|
.FirstOrDefault(o => o.Id == userOrganId);
|
|
organ.Buildings.ToTree();
|
|
organ.Buildings = organ.Buildings
|
|
.Where(o => o.Parent == null)
|
|
.ToList();
|
|
var model = new {
|
|
Organ= organ,
|
|
MaxLight = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{userOrganId}]MaxLight").FirstOrDefault()?.Value,
|
|
MinLight = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{userOrganId}]MinLight").FirstOrDefault()?.Value,
|
|
MaxTemperature = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{userOrganId}]MaxTemperature").FirstOrDefault()?.Value,
|
|
MinTemperature = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{userOrganId}]MinTemperature").FirstOrDefault()?.Value,
|
|
MaxHumidity = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{userOrganId}]MaxHumidity").FirstOrDefault()?.Value,
|
|
MinHumidity = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{userOrganId}]MinHumidity").FirstOrDefault()?.Value,
|
|
DeviceOpenCount = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{userOrganId}]DeviceOpenCount").FirstOrDefault()?.Value,
|
|
DeviceCloseCount = this._statisticRepo.ReadOnlyTable().Where(o => o.Key == $"[{userOrganId}]DeviceCloseCount").FirstOrDefault()?.Value,
|
|
};
|
|
return Json(model);
|
|
}
|
|
|
|
public IActionResult GetBuilding(Guid id)
|
|
{
|
|
var model = new
|
|
{
|
|
Building = this._buildingRepo.ReadOnlyTable()
|
|
.Include(o=>o.Scenes)
|
|
.Include(o => o.IoTGateways)
|
|
.ThenInclude(o => o.Devices).ThenInclude(o => o.Data)
|
|
.ToList()
|
|
|
|
};
|
|
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();
|
|
}
|
|
|
|
public string Mac(string id)
|
|
{
|
|
return Helper.Instance.MacEncrypt(id);
|
|
}
|
|
}
|
|
}
|