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/Api/Api/SiteController.cs

161 lines
7.9 KiB

using Application.Domain.Entities;
using Infrastructure.Application.Services.Settings;
using Infrastructure.Data;
using Infrastructure.Extensions;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
namespace Platform.Api
{
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]/[action]")]
[ApiController]
[Authorize]
public class SiteController : ControllerBase
{
private readonly ISettingService _settingService;
private readonly IRepository<User> _userRepo;
private readonly IRepository<AppModule> _appModuleRepo;
private readonly IRepository<OrganUser> _organUserRepo;
private readonly IRepository<Area> _areaRepo;
private readonly IRepository<Organ> _organRepo;
private readonly IRepository<Building> _buildingRepo;
private readonly IRepository<Statistic> _statisticRepo;
public SiteController(ISettingService settingService,
IRepository<User> userRepo,
IRepository<AppModule> appModuleRepo,
IRepository<OrganUser> organUserRepo,
IRepository<Area> areaRepo,
IRepository<Organ> organRepo,
IRepository<Building> buildingRepo,
IRepository<Statistic> statisticRepo)
{
this._settingService = settingService;
this._userRepo = userRepo;
this._appModuleRepo = appModuleRepo;
this._organUserRepo = organUserRepo;
this._areaRepo = areaRepo;
this._organRepo = organRepo;
this._buildingRepo = buildingRepo;
this._statisticRepo = statisticRepo;
}
[HttpPost]
public IActionResult Layout(string user = null)
{
try
{
var modules = this._appModuleRepo.ReadOnlyTable()
.Include(o => o.PermissionCategories)
.ThenInclude(o => o.Permissions)
.ToList();
var userName = User != null ? User.Identity.Name : user;
var platformUser = this._userRepo.ReadOnlyTable().FirstOrDefault(o => o.UserName == userName);
var organs = userName == null ? null : this._organUserRepo.ReadOnlyTable()
.Where(o => o.User.UserName == userName)
.Select(o => o.Organ)
.ToList();
var organId = User.GetOrganId();
var temp = this._organUserRepo.ReadOnlyTable()
.Where(o => o.User.UserName == userName)
.Include(o=>o.User).Include(o=>o.Organ)
.Include(o => o.UserRoles).ThenInclude(o => o.OrganRole)
.ToList();
var model = new LayoutModel
{
Name = this._settingService.GetValue("name"),
Logo = this._settingService.GetValue("logo"),
Copyright = this._settingService.GetValue("copyright"),
Version = Helper.Instance.GetVersion(),
Modules = modules,
Organs = organs,
CurrentOrganId = User.GetOrganId(),
User = User == null ? null : User.Identity as ClaimsIdentity,
Roles = User == null
? new List<string>()
: this._organUserRepo.ReadOnlyTable()
.Where(o => o.User.UserName == userName)
.Where(o => o.OrganId == organId)
.SelectMany(o => o.UserRoles)
.Select(o => o.OrganRole.Name)
.ToList()
};
if (platformUser != null && model.User != null)
{
model.User.AddClaim(new Claim("RealName", platformUser.RealName ?? platformUser.UserName));
if (!string.IsNullOrEmpty(platformUser.Avatar))
{
model.User.AddClaim(new Claim("Avatar", platformUser.Avatar));
}
}
return new JsonResult(model);
}
catch (Exception ex)
{
ex.PrintStack();
return Problem(ex.Message);
}
}
[HttpPost]
public IActionResult GetOrgan()
{
var organId = User.GetOrganId().Value;
var organ = this._organRepo.ReadOnlyTable()
.Where(o => o.Id == organId)
.Include(o => o.Buildings)
.FirstOrDefault();
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 == $"[{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);
}
}
}