laatzen/LaaProductionWeb/LaaProductionWeb.Blazor/Components/HeliumTester/Models/HeliumTesterService.cs
2025-03-17 16:03:24 +01:00

165 lines
6.9 KiB
C#

namespace LaaProductionWeb.Blazor.Components.HeliumTester.Models
{
using LaaPackages.Data.HeliumTester;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
public class HeliumTesterService(HeliumTesterDbContext heliumTesterDbContext)
{
private readonly HeliumTesterDbContext heliumTesterDbContext = heliumTesterDbContext;
public IEnumerable<TestVariant> LoadTestVariants()
=> this.heliumTesterDbContext
.TestVariants
.ToList();
public IEnumerable<HeliumRecipeModel> LoadHeliumRecieps()
=> this.heliumTesterDbContext
.HeliumRecipes
.Select(HeliumRecipeModel.ToModel);
public IEnumerable<WaterRecipeModel> LoadWaterRecieps()
=> this.heliumTesterDbContext
.WaterRecipes
.Select(WaterRecipeModel.ToModel);
public void UpdateRecipe(object recipe)
{
this.heliumTesterDbContext.Update(recipe);
this.heliumTesterDbContext.SaveChanges();
}
public IEnumerable<OverallItem> GetLatestResultIdents(int? ident, MatchCollectionModel matchCollectionModel)
{
var where = default(string);
var regions = matchCollectionModel
.Regions
.Where(x => x.Selected)
.Select(x => x.Key);
if (regions.Any())
{
where = $"{where}{Environment.NewLine} AND tv.RegionCode IN ('{string.Join("', '", regions)}')";
}
var meterSizes = matchCollectionModel
.MeterSizes
.Where(x => x.Selected)
.Select(x => x.Key);
if (meterSizes.Any())
{
where = $"{where}{Environment.NewLine} AND tv.MeterSizeCode IN ('{string.Join("', '", meterSizes)}')";
}
var bodyLengths = matchCollectionModel
.BodyLengths
.Where(x => x.Selected)
.Select(x => x.Key);
if (bodyLengths.Any())
{
where = $"{where}{Environment.NewLine} AND tv.BodyLengthCode IN ('{string.Join("', '", bodyLengths)}')";
}
var pressureSensors = matchCollectionModel
.PressureSensors
.Where(x => x.Selected)
.Select(x => x.Key);
if (pressureSensors.Any())
{
where = $"{where}{Environment.NewLine} AND tv.PressureSensorCode IN ('{string.Join("', '", pressureSensors)}')";
}
if (ident is not null)
{
where = $"{where}{Environment.NewLine} AND oar.ResultIdent LIKE '%{ident}%'";
}
var query = $@"
SELECT DISTINCT
oar.Id AS OverallId
, oar.ResultIdent AS PcbId
, CAST(0 AS BIT) AS Selected
, tv.Region + ' ' + CASE WHEN tv.RegionCode = 'E' THEN 'DN' ELSE '' END
+ tv.MeterSize + ' L='
+ CAST(tv.BodyLength AS VARCHAR(5)) + ' '
+ tv.PressureSensorCode AS Description
FROM ht.TestVariants AS tv
LEFT JOIN ht.HeliumRecipes AS hr
ON tv.Id = hr.VariantId
LEFT JOIN ht.WaterRecipes AS wr
ON tv.Id = wr.VariantId
LEFT JOIN ht.OverallResults AS oar
ON hr.Id = oar.HeliumRecipeId
OR wr.Id = oar.WaterRecipeId
LEFT JOIN ht.HeliumMessurementsHistory AS hmh
ON oar.Id = hmh.OverallResultId
LEFT JOIN ht.WaterMessurementsHistory AS wmh
ON oar.Id = wmh.OverallResultId
WHERE oar.Id IS NOT NULL
AND (hmh.Id IS NOT NULL OR wmh.Id IS NOT NULL) {where}";
return this.heliumTesterDbContext
.Database
.SqlQueryRaw<OverallItem>(query);
}
public IEnumerable<HeliumMessurementsHistory> GetHeHistory(int overallResultId)
=> this.heliumTesterDbContext
.HeliumMessurementsHistories
.Where(h => h.OverallResultId == overallResultId)
.ToList();
public IEnumerable<WaterMessurementsHistory> GetWaHistory(int overallResultId)
=> this.heliumTesterDbContext
.WaterMessurementsHistories
.Where(h => h.OverallResultId == overallResultId)
.ToList();
public IEnumerable<OverallMatch> GetOverallMatchItems()
=> this.heliumTesterDbContext
.Database
.SqlQuery<OverallMatch>($@"
SELECT DISTINCT
htv.Id AS HeliumVariantId
, htv.RegionCode AS HeliumRegionCode
, htv.Region AS HeliumRegion
, htv.MeterSizeCode AS HeliumMeterSizeCode
, htv.MeterSize AS HeliumMeterSize
, htv.BodyLengthCode AS HeliumBodyLengthCode
, htv.BodyLength AS HeliumBodyLength
, htv.PressureSensorCode AS HeliumPressureSensorCode
, htv.HasPressureSensor AS HeliumHasPressureSensor
, wtv.Id AS WaterVariantId
, wtv.RegionCode AS WaterRegionCode
, wtv.Region AS WaterRegion
, wtv.MeterSizeCode AS WaterMeterSizeCode
, wtv.MeterSize AS WaterMeterSize
, wtv.BodyLengthCode AS WaterBodyLengthCode
, wtv.BodyLength AS WaterBodyLength
, wtv.PressureSensorCode AS WaterPressureSensorCode
, wtv.HasPressureSensor AS WaterHasPressureSensor
FROM ht.OverallResults AS oar
LEFT JOIN ht.HeliumMessurementsHistory AS hmh
ON oar.Id = hmh.OverallResultId
LEFT JOIN ht.HeliumRecipes AS hr
ON oar.HeliumRecipeId = hr.Id
LEFT JOIN ht.TestVariants AS htv
ON hr.VariantId = htv.Id
LEFT JOIN ht.WaterMessurementsHistory AS wmh
ON oar.Id = wmh.OverallResultId
LEFT JOIN ht.WaterRecipes AS wr
ON oar.HeliumRecipeId = wr.Id
LEFT JOIN ht.TestVariants AS wtv
ON hr.VariantId = wtv.Id
WHERE oar.HeliumRecipeId IS NOT NULL
AND oar.WaterRecipeId IS NOT NULL");
}
}