物联网平台CockroachDB子查询统计设备数量点相关代码修改,将内部子查询替换为外部循环统计

Former-commit-id: 1bd43fc08f23bd4d4d08a10cdaa1a347982bbac6
TangShanKaiPing
wanggang 5 years ago
parent 8ecf8e58f7
commit f9b140b050

Binary file not shown.

Binary file not shown.

@ -18,14 +18,17 @@ namespace UserCenter.Controllers
private readonly IConfiguration _cfg; private readonly IConfiguration _cfg;
private readonly IRepository<Node> _nodeRepo; private readonly IRepository<Node> _nodeRepo;
private readonly IRepository<NodeCategory> _nodeCategoryRepo; private readonly IRepository<NodeCategory> _nodeCategoryRepo;
private readonly IRepository<Device> _deviceRepo;
public NodeController(IConfiguration cfg, public NodeController(IConfiguration cfg,
IRepository<Node> nodeRepo, IRepository<Node> nodeRepo,
IRepository<NodeCategory> nodeCategoryRepo) IRepository<NodeCategory> nodeCategoryRepo,
IRepository<Device> deviceRepo)
{ {
this._cfg = cfg; this._cfg = cfg;
this._nodeRepo = nodeRepo; this._nodeRepo = nodeRepo;
this._nodeCategoryRepo = nodeCategoryRepo; this._nodeCategoryRepo = nodeCategoryRepo;
this._deviceRepo = deviceRepo;
} }
[HttpPost] [HttpPost]
@ -34,6 +37,7 @@ namespace UserCenter.Controllers
try try
{ {
var model = this._nodeRepo.ReadOnlyTable() var model = this._nodeRepo.ReadOnlyTable()
.ToList()
.Select(o => new .Select(o => new
{ {
o.Id, o.Id,
@ -41,7 +45,7 @@ namespace UserCenter.Controllers
o.Number, o.Number,
o.Image, o.Image,
o.DisplayOrder, o.DisplayOrder,
Count = o.Devices.Count() Count = _deviceRepo.ReadOnlyTable().Count(d => d.NodeId == o.Id)
}); });
return Ok(model); return Ok(model);
} }

@ -35,6 +35,7 @@ namespace UserCenter.Controllers
{ {
var model = this._productRepo.ReadOnlyTable() var model = this._productRepo.ReadOnlyTable()
.OrderBy(o => o.DisplayOrder) .OrderBy(o => o.DisplayOrder)
.ToList()
.Select(o => new .Select(o => new
{ {
o.Id, o.Id,
@ -42,7 +43,7 @@ namespace UserCenter.Controllers
o.Number, o.Number,
o.Image, o.Image,
o.DisplayOrder, o.DisplayOrder,
Count = o.Devices.Count() Count = _deviceRepo.ReadOnlyTable().Count(d => d.ProductId == o.Id)
}); });
return Ok(model); return Ok(model);
} }

@ -13,41 +13,49 @@ namespace IoTCenter.Areas.Admin.Controllers
{ {
private readonly IRepository<Node> _nodeRepo; private readonly IRepository<Node> _nodeRepo;
private readonly IRepository<Product> _productRepo; private readonly IRepository<Product> _productRepo;
private readonly IRepository<Device> _deviceRepo;
private readonly IRepository<Category> _categoryRepo; private readonly IRepository<Category> _categoryRepo;
public HomeController(IRepository<Node> nodeRepo, IRepository<Category> categoryRepo, IRepository<Product> productRepo) public HomeController(IRepository<Node> nodeRepo, IRepository<Category> categoryRepo, IRepository<Product> productRepo, IRepository<Device> deviceRepo)
{ {
this._nodeRepo = nodeRepo; this._nodeRepo = nodeRepo;
this._categoryRepo = categoryRepo; this._categoryRepo = categoryRepo;
this._productRepo = productRepo; this._productRepo = productRepo;
this._deviceRepo = deviceRepo;
} }
public IActionResult Index() public IActionResult Index()
{ {
var model = new HomeIndexViewModel var model = new HomeIndexViewModel
{ {
Nodes = this._nodeRepo.ReadOnlyTable().Select(o => new NodeViewModel Nodes = this._nodeRepo.ReadOnlyTable()
.ToList()
.Select(o => new NodeViewModel
{ {
Id = o.Id, Id = o.Id,
Number = o.Number, Number = o.Number,
Name = o.Name, Name = o.Name,
Image = o.Image, Image = o.Image,
DeviceCount = o.Devices.Count() DeviceCount = _deviceRepo.ReadOnlyTable().Count(d => d.NodeId == o.Id)
}).ToList(), }).ToList(),
Categories = this._categoryRepo.ReadOnlyTable().Select(o => new CategoryViewModel Categories = this._categoryRepo.ReadOnlyTable()
.ToList()
.Select(o => new CategoryViewModel
{ {
Id = o.Id, Id = o.Id,
Name = o.Name, Name = o.Name,
Image = o.Image, Image = o.Image,
Number = o.Number, Number = o.Number,
DeviceCount = o.Products.SelectMany(o => o.Devices).Count() DeviceCount = _deviceRepo.ReadOnlyTable().Count(d => d.Product.CategoryId == o.Id)
}).ToList(), }).ToList(),
Products = this._productRepo.ReadOnlyTable().Select(o => new ProductViewModel Products = this._productRepo.ReadOnlyTable()
.ToList()
.Select(o => new ProductViewModel
{ {
Id = o.Id, Id = o.Id,
Name = o.Name, Name = o.Name,
Image = o.Image, Image = o.Image,
DeviceCount = o.Devices.Count() DeviceCount = _deviceRepo.ReadOnlyTable().Count(d => d.ProductId == o.Id)
}).ToList() }).ToList()
}; };
return View(model); return View(model);

@ -92,6 +92,7 @@ namespace IoTCenter.Controllers
.Include(o => o.Scenes) .Include(o => o.Scenes)
.OrderBy(o => o.DisplayOrder) .OrderBy(o => o.DisplayOrder)
.ThenBy(o => o.Name) .ThenBy(o => o.Name)
.ToList()
.Select(o => new .Select(o => new
{ {
o.Id, o.Id,
@ -100,7 +101,7 @@ namespace IoTCenter.Controllers
o.Image, o.Image,
o.DisplayOrder, o.DisplayOrder,
o.Scenes, o.Scenes,
DeviceCount = o.Devices.Count() DeviceCount = this._deviceRepo.ReadOnlyTable().Count(d => d.NodeId == o.Id)
}) })
.ToList(); .ToList();
return Json(model); return Json(model);

@ -12,7 +12,7 @@
"redis": "127.0.0.1:6379,allowAdmin=true" "redis": "127.0.0.1:6379,allowAdmin=true"
}, },
"AppSettings": { "AppSettings": {
"database": "sqlite", "database": "postgresql",
"UseCookieSessionStore": false "UseCookieSessionStore": false
} }
} }
Loading…
Cancel
Save