laatzen/LaaProductionWeb/LaaProductionWeb.API/Areas/Shipment/Models/ScanModel.cs
2023-05-11 09:31:14 +02:00

79 lines
3.5 KiB
C#

namespace LaaProductionWeb.API.Areas.Shipment.Models
{
using LaaProductionWeb.API.Areas.Shipment.Models.Interfaces;
using LaaProductionWeb.API.Models;
using LaaProductionWeb.API.Models.SMTP;
using LaaProductionWeb.Data.Interfaces;
using System.Collections.Generic;
using System.Threading.Tasks;
public class ScanModel : IScanModel
{
private readonly ISqlClient sqlClient;
private readonly SMTPClient smtpClient;
public ScanModel(ISqlClient sqlClient, SMTPClient smtpClient)
{
this.sqlClient = sqlClient;
this.smtpClient = smtpClient;
}
public async Task<ModelResult> SerialScannedAsync(string serialNo)
{
const string TEST_STATE = "25";
var watermeterTestPassed = this.sqlClient
.FirstOrDefault(
query: $@"
SELECT [PS].[AuftragNr]
, CASE WHEN MAX([APS].[StatusFertigung]) > '{TEST_STATE}'
THEN CAST(1 AS BIT)
ELSE CAST(0 AS BIT) END
FROM [PalettenScan] AS [PS]
LEFT JOIN [AuftragPositionSerienNr] AS [APS]
ON [APS].[AuftragNr] = [PS].[AuftragNr]
AND [APS].[PositionNr] = [PS].[PositionNr]
AND (REPLACE([APS].[KundeneigeneSerienNr], ' ', '') = [PS].[SerienNr]
OR CAST([APS].[SerienNr] AS VARCHAR) = [PS].[SerienNr])
WHERE [PS].[SerienNr] = @{nameof(serialNo)}
GROUP BY [PS].[AuftragNr]",
parameters: parameters => parameters.Add(nameof(serialNo), serialNo),
reader: reader => new KeyValuePair<string, bool>(
key: reader.GetString(),
value: reader.GetValue<bool>()));
if (watermeterTestPassed.Value)
{
var orderNo = watermeterTestPassed.Key;
var unpackedWatermeterCount = this.sqlClient
.FirstOrDefault(
query: $@"
SELECT COUNT(1)
FROM [AuftragPositionSerienNr] AS [APS]
LEFT JOIN [PalettenScan] AS [PS]
ON [PS].[AuftragNr] = [APS].[AuftragNr]
AND [PS].[PositionNr] = [APS].[PositionNr]
AND (REPLACE([APS].[KundeneigeneSerienNr], ' ', '') = [PS].[SerienNr]
OR CAST([APS].[SerienNr] AS VARCHAR) = [PS].[SerienNr])
WHERE [PS].[AuftragNr] = @{nameof(orderNo)}
AND [APS].[StatusFertigung] > '{TEST_STATE}'
AND [PS].[ID] IS NULL",
parameters: parameters => parameters.Add(nameof(orderNo), orderNo),
reader: reader => reader.GetValue<int>());
if (unpackedWatermeterCount == 0)
{
return await this.smtpClient.SendAsync(
subject: "Subject",
body: "Html or text body",
from: "laa_production@xylem.com",
to: new[] { "Roland.Drabesch@Xylem.com", "Stoyan.Zlatev@Xylem.com" });
}
}
return false;
}
}
}