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.
127 lines
4.9 KiB
127 lines
4.9 KiB
using Application.Domain.Entities;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq;
|
|
|
|
namespace IoTCenter.Api.Controllers
|
|
{
|
|
[ApiVersion("1.0")]
|
|
[Route("api/v{version:apiVersion}/[controller]/[action]")]
|
|
[ApiController]
|
|
public class ProductController : ControllerBase
|
|
{
|
|
private readonly IRepository<OrganNode> _organNodeRepo;
|
|
private readonly IRepository<Product> _productRepo;
|
|
private readonly IRepository<Node> _nodeRepo;
|
|
private readonly IRepository<Device> _deviceRepo;
|
|
|
|
public ProductController(IRepository<OrganNode> organNodeRepo,
|
|
IRepository<Product> productRepo,
|
|
IRepository<Node> nodeRepo,
|
|
IRepository<Device> deviceRepo)
|
|
{
|
|
this._organNodeRepo = organNodeRepo;
|
|
this._productRepo = productRepo;
|
|
this._nodeRepo = nodeRepo;
|
|
this._deviceRepo = deviceRepo;
|
|
}
|
|
|
|
[HttpPost]
|
|
public ActionResult GetProducts(string organNumber)
|
|
{
|
|
try
|
|
{
|
|
var model = this._organNodeRepo.ReadOnlyTable()
|
|
.WhereIf(!string.IsNullOrEmpty(organNumber), o => o.Organ.Number == organNumber)
|
|
.SelectMany(o => o.Node.Devices)
|
|
.GroupBy(o => new
|
|
{
|
|
o.Product.Id,
|
|
o.Product.Name,
|
|
o.Product.Number,
|
|
o.Product.Image,
|
|
o.Product.DisplayOrder
|
|
})
|
|
.Select(o => new
|
|
{
|
|
o.Key.Id,
|
|
o.Key.Name,
|
|
o.Key.Number,
|
|
o.Key.Image,
|
|
o.Key.DisplayOrder,
|
|
Count = o.Count()
|
|
})
|
|
.ToList();
|
|
return Ok(model);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return Problem(ex.Message);
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
public ActionResult GetProduct([Required(ErrorMessage = nameof(RequiredAttribute))] string number, string organNumber)
|
|
{
|
|
try
|
|
{
|
|
var model = this._productRepo.ReadOnlyTable().Where(o => o.Number == number).FirstOrDefault();
|
|
model.Devices = this._organNodeRepo.ReadOnlyTable()
|
|
.WhereIf(!string.IsNullOrEmpty(organNumber), o => o.Organ.Number == organNumber)
|
|
.SelectMany(o => o.Node.Devices)
|
|
.Include(o => o.Data)
|
|
.ToList()
|
|
.Where(o => o.ProductId == model.Id)
|
|
.ToList();
|
|
return Ok(model);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return Problem(ex.Message);
|
|
}
|
|
}
|
|
|
|
public IActionResult GetSummary(string organNumber)
|
|
{
|
|
try
|
|
{
|
|
var products = this._organNodeRepo.ReadOnlyTable()
|
|
.WhereIf(!string.IsNullOrEmpty(organNumber), o => o.Organ.Number == organNumber)
|
|
.SelectMany(o => o.Node.Devices)
|
|
.GroupBy(o => o.Product.Name)
|
|
.Select(o => new
|
|
{
|
|
Name = o.Key,
|
|
Online = o.Sum(c => c.IsOnline ? 1 : 0),
|
|
Offline = o.Sum(c => c.IsOnline ? 0 : 1),
|
|
})
|
|
.ToList()
|
|
.Select(o => new
|
|
{
|
|
o.Name,
|
|
o.Online,
|
|
o.Offline,
|
|
Total = o.Online + o.Offline
|
|
});
|
|
return Ok(new
|
|
{
|
|
Total = products.Sum(o => o.Total),
|
|
Online = products.Sum(o => o.Online),
|
|
Offline = products.Sum(o => o.Offline),
|
|
Products = products
|
|
});
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
return Problem(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
} |