398 lines
19 KiB
C#
398 lines
19 KiB
C#
using Adaptation.Shared;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.SqlClient;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Threading;
|
|
|
|
namespace Adaptation.FileHandlers.OpenInsight;
|
|
|
|
public class FromIQS
|
|
{
|
|
|
|
#nullable enable
|
|
|
|
private static string GetCommandText(DateTime dateTime)
|
|
{ // cSpell:disable
|
|
List<string> results = new();
|
|
TimeSpan timeSpan = new(dateTime.ToUniversalTime().AddDays(-1).Ticks - new DateTime(1970, 01, 01).Ticks);
|
|
long infinityQS = (long)Math.Floor(timeSpan.TotalSeconds);
|
|
results.Add(" select count_se_sgtm CountSeSgtm, ");
|
|
results.Add(" dateadd(HH, -7, (dateadd(SS, convert(bigint, max_se_sgtm), '19700101'))) DateTime, ");
|
|
results.Add(" max_max_se_test MaxMaxSeTest, ");
|
|
results.Add(" ( ");
|
|
results.Add(" select td.f_name ");
|
|
results.Add(" from [SPCEPIWORLD].[dbo].[TEST_DAT] td ");
|
|
results.Add(" where td.f_test = max_max_se_test ");
|
|
results.Add(" ) MaxMaxSeTestName, ");
|
|
results.Add(" max_max_se_val MaxMaxSeVal, ");
|
|
results.Add(" max_se_lot MaxSeLot, ");
|
|
results.Add(" ( ");
|
|
results.Add(" select pl.f_name ");
|
|
results.Add(" from [SPCEPIWORLD].[dbo].[PART_LOT] pl ");
|
|
results.Add(" where max_se_lot = pl.f_lot ");
|
|
results.Add(" ) MaxSeLotName, ");
|
|
results.Add(" max_se_part MaxSePart, ");
|
|
results.Add(" ( ");
|
|
results.Add(" select rd.f_name ");
|
|
results.Add(" from [SPCEPIWORLD].[dbo].[PART_DAT] rd ");
|
|
results.Add(" where max_se_part = rd.f_part ");
|
|
results.Add(" ) MaxSePartName, ");
|
|
results.Add(" max_se_prcs MaxSePrcs, ");
|
|
results.Add(" ( ");
|
|
results.Add(" select rd.f_name ");
|
|
results.Add(" from [SPCEPIWORLD].[dbo].[PRCS_DAT] rd ");
|
|
results.Add(" where max_se_prcs = rd.f_prcs ");
|
|
results.Add(" ) MaxSePrcsName, ");
|
|
results.Add(" max_se_sgrp MaxSeSgrp, ");
|
|
results.Add(" min_min_se_test MinMinSeTest, ");
|
|
results.Add(" ( ");
|
|
results.Add(" select td.f_name ");
|
|
results.Add(" from [SPCEPIWORLD].[dbo].[TEST_DAT] td ");
|
|
results.Add(" where td.f_test = min_min_se_test ");
|
|
results.Add(" ) MinMinSeTestName, ");
|
|
results.Add(" min_min_se_val MinMinSeVal, ");
|
|
results.Add(" min_se_sgrp MinSeSgrp ");
|
|
results.Add(" from ( ");
|
|
results.Add(" select count_se_sgtm, ");
|
|
results.Add(" max_se_lot, ");
|
|
results.Add(" max_se_part, ");
|
|
results.Add(" max_se_prcs, ");
|
|
results.Add(" max_se_sgrp, ");
|
|
results.Add(" max_se_sgtm, ");
|
|
results.Add(" min_se_sgrp, ");
|
|
results.Add(" max(max_se_val) max_max_se_val, ");
|
|
results.Add(" min(min_se_val) min_min_se_val, ");
|
|
results.Add(" max(max_se_test) max_max_se_test, ");
|
|
results.Add(" min(min_se_test) min_min_se_test ");
|
|
results.Add(" from ( ");
|
|
results.Add(" select count_se_sgtm, ");
|
|
results.Add(" max_se_lot, ");
|
|
results.Add(" max_se_val, ");
|
|
results.Add(" min_se_val, ");
|
|
results.Add(" max_se_part, ");
|
|
results.Add(" max_se_prcs, ");
|
|
results.Add(" max_se_sgrp, ");
|
|
results.Add(" max_se_sgtm, ");
|
|
results.Add(" max_se_test, ");
|
|
results.Add(" min_se_sgrp, ");
|
|
results.Add(" min_se_test ");
|
|
results.Add(" from ( ");
|
|
results.Add(" select ");
|
|
results.Add(" max(se.f_lot) max_se_lot, ");
|
|
results.Add(" max(se.f_val) max_se_val, ");
|
|
results.Add(" min(se.f_lot) min_se_lot, ");
|
|
results.Add(" min(se.f_val) min_se_val, ");
|
|
results.Add(" max(se.f_part) max_se_part, ");
|
|
results.Add(" max(se.f_prcs) max_se_prcs, ");
|
|
results.Add(" max(se.f_sgrp) max_se_sgrp, ");
|
|
results.Add(" max(se.f_sgtm) max_se_sgtm, ");
|
|
results.Add(" max(se.f_test) max_se_test, ");
|
|
results.Add(" min(se.f_part) min_se_part, ");
|
|
results.Add(" min(se.f_prcs) min_se_prcs, ");
|
|
results.Add(" min(se.f_sgrp) min_se_sgrp, ");
|
|
results.Add(" min(se.f_sgtm) min_se_sgtm, ");
|
|
results.Add(" min(se.f_test) min_se_test, ");
|
|
results.Add(" count(se.f_sgtm) count_se_sgtm ");
|
|
results.Add(" from [spcepiworld].[dbo].[sgrp_ext] se ");
|
|
results.Add(" where se.f_tsno = 1 ");
|
|
results.Add(" and se.f_flag = 0 ");
|
|
results.Add($" and se.f_sgtm > {infinityQS} ");
|
|
results.Add(" group by se.f_sgtm, se.f_prcs, se.f_lot, se.f_test, se.f_val ");
|
|
results.Add(" ) qa ");
|
|
results.Add(" where qa.count_se_sgtm > 1 ");
|
|
results.Add(" and min_se_lot = max_se_lot ");
|
|
results.Add(" and min_se_val = max_se_val ");
|
|
results.Add(" and min_se_val = max_se_val ");
|
|
results.Add(" and min_se_part = max_se_part ");
|
|
results.Add(" and min_se_prcs = max_se_prcs ");
|
|
results.Add(" and min_se_sgtm = max_se_sgtm ");
|
|
results.Add(" and min_se_test = max_se_test ");
|
|
results.Add(" ) qb ");
|
|
results.Add(" group by count_se_sgtm, ");
|
|
results.Add(" max_se_lot, ");
|
|
results.Add(" max_se_part, ");
|
|
results.Add(" max_se_prcs, ");
|
|
results.Add(" max_se_sgrp, ");
|
|
results.Add(" max_se_sgtm, ");
|
|
results.Add(" min_se_sgrp ");
|
|
results.Add(" ");
|
|
results.Add(" ) qc ");
|
|
results.Add(" order by max_se_sgrp desc ");
|
|
results.Add(" for json path ");
|
|
return string.Join(Environment.NewLine, results);
|
|
} // cSpell:restore
|
|
|
|
private static string GetCommandText(Logistics logistics, pcl.Description description, string dateTime, long? subGroupId)
|
|
{ // cSpell:disable
|
|
List<string> results = new();
|
|
results.Add(" select iq.ev_count, iq.cl_count, iq.sl_count, iq.se_sgrp, iq.se_sgtm, iq.se_tsno, iq.td_test, iq.pr_name, iq.jd_name, iq.pl_name, iq.pd_name, iq.td_name, iq.se_val ");
|
|
results.Add(" from ( ");
|
|
results.Add(" select ");
|
|
results.Add(" se.f_sgrp se_sgrp, ");
|
|
results.Add(" se.f_sgtm se_sgtm, ");
|
|
results.Add(" se.f_tsno se_tsno, ");
|
|
results.Add(" se.f_val se_val, ");
|
|
results.Add(" pr.f_name pr_name, ");
|
|
results.Add(" jd.f_name jd_name, ");
|
|
results.Add(" pl.f_name pl_name, ");
|
|
results.Add(" pd.f_name pd_name, ");
|
|
results.Add(" td.f_test td_test, ");
|
|
results.Add(" td.f_name td_name, ");
|
|
results.Add(" (select count(cl.f_part) ");
|
|
results.Add(" from [spcepiworld].[dbo].[ctrl_lim] cl ");
|
|
results.Add(" where cl.f_part = pd.f_part ");
|
|
results.Add(" and cl.f_test = td.f_test ");
|
|
results.Add(" ) cl_count, ");
|
|
results.Add(" (select count(sl.f_part) ");
|
|
results.Add(" from [spcepiworld].[dbo].[spec_lim] sl ");
|
|
results.Add(" where sl.f_part = pd.f_part ");
|
|
results.Add(" and sl.f_test = td.f_test ");
|
|
results.Add(" ) sl_count, ");
|
|
results.Add(" (select count(ev.f_evnt) ");
|
|
results.Add(" from [spcepiworld].[dbo].[evnt_inf] ev ");
|
|
results.Add(" where ev.f_prcs = pr.f_prcs ");
|
|
results.Add(" and ev.f_part = pd.f_part ");
|
|
results.Add(" and ev.f_sgtm = se.f_sgtm ");
|
|
results.Add(" ) ev_count ");
|
|
results.Add(" from [spcepiworld].[dbo].[sgrp_ext] se ");
|
|
results.Add(" join [spcepiworld].[dbo].[prcs_dat] pr ");
|
|
results.Add(" on se.f_prcs = pr.f_prcs ");
|
|
results.Add(" join [spcepiworld].[dbo].[job_dat] jd ");
|
|
results.Add(" on se.f_job = jd.f_job ");
|
|
results.Add(" join [spcepiworld].[dbo].[part_lot] pl ");
|
|
results.Add(" on se.f_lot = pl.f_lot ");
|
|
results.Add(" join [spcepiworld].[dbo].[part_dat] pd ");
|
|
results.Add(" on se.f_part = pd.f_part ");
|
|
results.Add(" join [spcepiworld].[dbo].[test_dat] td ");
|
|
results.Add(" on se.f_test = td.f_test ");
|
|
results.Add(" where se.f_flag = 0 ");
|
|
if (subGroupId is not null)
|
|
results.Add($" and se.f_sgrp = {subGroupId} ");
|
|
if (!string.IsNullOrEmpty(description.RDS))
|
|
results.Add($" and pl.f_name = '{description.RDS}' ");
|
|
results.Add($" and pr.f_name = '{description.Reactor}' ");
|
|
results.Add($" and pd.f_name = '{description.PSN}' ");
|
|
results.Add(" and jd.f_name in ('HGCV1', 'HGCV2', 'HGCV3') ");
|
|
results.Add($" and jd.f_name = '{logistics.MesEntity}' ");
|
|
results.Add($" and dateadd(HH, -7, (dateadd(SS, convert(bigint, se.f_sgtm), '19700101'))) = '{dateTime}' ");
|
|
results.Add(" ) as iq ");
|
|
results.Add(" order by iq.ev_count desc, iq.cl_count desc, iq.sl_count desc, iq.se_sgrp, iq.se_tsno, iq.td_test ");
|
|
results.Add(" for json path ");
|
|
return string.Join(Environment.NewLine, results);
|
|
} // cSpell:restore
|
|
|
|
private static StringBuilder GetForJsonPath(string connectionString, string commandText)
|
|
{
|
|
StringBuilder stringBuilder = new();
|
|
using SqlConnection sqlConnection = new(connectionString);
|
|
sqlConnection.Open();
|
|
using SqlCommand sqlCommand = new(commandText, sqlConnection);
|
|
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.SequentialAccess);
|
|
while (sqlDataReader.Read())
|
|
_ = stringBuilder.Append(sqlDataReader.GetString(0));
|
|
return stringBuilder;
|
|
}
|
|
|
|
private static string GetCommandText(List<long> subGroups)
|
|
{ // cSpell:disable
|
|
List<string> results = new();
|
|
results.Add(" update [spcepiworld].[dbo].[sgrp_ext] ");
|
|
results.Add(" set f_flag = 1 ");
|
|
results.Add(" where f_flag = 0 ");
|
|
results.Add($" and f_sgrp in ({string.Join($",{Environment.NewLine} ", subGroups)}) ");
|
|
return string.Join(Environment.NewLine, results);
|
|
} // cSpell:enable
|
|
|
|
private static int? ExecuteNonQuery(string connectionString, string commandText)
|
|
{
|
|
int? result;
|
|
if (string.IsNullOrEmpty(connectionString))
|
|
result = null;
|
|
else
|
|
{
|
|
using SqlConnection sqlConnection = new(connectionString);
|
|
sqlConnection.Open();
|
|
using SqlCommand sqlCommand = new(commandText, sqlConnection);
|
|
result = sqlCommand.ExecuteNonQuery();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private static void FlagDuplicates(string connectionString, string json)
|
|
{
|
|
JsonElement[]? jsonElements = JsonSerializer.Deserialize<JsonElement[]>(json);
|
|
JsonSerializerOptions jsonSerializerOptions = new() { PropertyNameCaseInsensitive = true };
|
|
if (jsonElements is not null && jsonElements.Length != 0 && jsonElements[0].ValueKind == JsonValueKind.Object)
|
|
{
|
|
Root? root;
|
|
List<long> collection = new();
|
|
JsonElement[] array = jsonElements.ToArray();
|
|
foreach (JsonElement jsonElement in array)
|
|
{
|
|
root = JsonSerializer.Deserialize<Root>(jsonElement.ToString(), jsonSerializerOptions);
|
|
if (root is null || root.MaxSeSgrp < 1)
|
|
continue;
|
|
if (collection.Count > 99)
|
|
break;
|
|
collection.Add(root.MaxSeSgrp);
|
|
}
|
|
if (collection.Count > 0)
|
|
{
|
|
string commandText = GetCommandText(collection);
|
|
File.WriteAllText("D:/.sql", commandText);
|
|
_ = ExecuteNonQuery(connectionString, commandText);
|
|
}
|
|
}
|
|
}
|
|
|
|
internal static (long?, int?, string) GetCommandText(string connectionString, Logistics logistics, pcl.Description description, long breakAfter, long preWait)
|
|
{
|
|
DateTime dateTime;
|
|
int? count = null;
|
|
string commandText;
|
|
long? result = null;
|
|
StringBuilder stringBuilder;
|
|
string dateFormat = pcl.Description.GetDateFormat();
|
|
if (DateTime.TryParseExact(description.Date, dateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateTimeParsed))
|
|
dateTime = dateTimeParsed;
|
|
else if (DateTime.TryParse(description.Date, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTimeParsed))
|
|
dateTime = dateTimeParsed;
|
|
else
|
|
dateTime = logistics.DateTimeFromSequence;
|
|
commandText = GetCommandText(dateTime);
|
|
try
|
|
{
|
|
stringBuilder = GetForJsonPath(connectionString, commandText);
|
|
if (stringBuilder.Length > 0)
|
|
FlagDuplicates(connectionString, stringBuilder.ToString());
|
|
}
|
|
catch (Exception)
|
|
{ stringBuilder = new(); }
|
|
_ = stringBuilder.Clear();
|
|
commandText = GetCommandText(logistics, description, dateTime.ToString("yyyy-MM-dd HH:mm:ss"), subGroupId: null);
|
|
for (short i = 0; i < short.MaxValue; i++)
|
|
{
|
|
if (DateTime.Now.Ticks > preWait)
|
|
break;
|
|
Thread.Sleep(100);
|
|
}
|
|
for (short z = 0; z < short.MaxValue; z++)
|
|
{
|
|
stringBuilder = GetForJsonPath(connectionString, commandText);
|
|
if (stringBuilder.Length > 0)
|
|
{
|
|
long postBreakAfter = DateTime.Now.AddSeconds(5).Ticks;
|
|
for (short y = 0; y < short.MaxValue; y++)
|
|
{
|
|
if (DateTime.Now.Ticks > postBreakAfter)
|
|
break;
|
|
Thread.Sleep(250);
|
|
}
|
|
stringBuilder = GetForJsonPath(connectionString, commandText);
|
|
break;
|
|
}
|
|
if (DateTime.Now.Ticks > breakAfter)
|
|
// throw new Exception($"After {breakAfterSeconds} seconds, didn't find sub group id!");
|
|
break;
|
|
Thread.Sleep(250);
|
|
}
|
|
if (stringBuilder.Length == 0)
|
|
commandText = stringBuilder.ToString();
|
|
else
|
|
{
|
|
JsonElement[]? jsonElements = JsonSerializer.Deserialize<JsonElement[]>(stringBuilder.ToString());
|
|
if (jsonElements is null || jsonElements.Length == 0 || jsonElements[0].ValueKind != JsonValueKind.Object)
|
|
commandText = stringBuilder.ToString();
|
|
else
|
|
{
|
|
JsonProperty[] jsonProperties = jsonElements[0].EnumerateObject().ToArray();
|
|
if (jsonProperties.Length == 0 || jsonProperties[3].Name != "se_sgrp" || !long.TryParse(jsonProperties[3].Value.ToString(), out long subGroupId))
|
|
commandText = stringBuilder.ToString();
|
|
else
|
|
{
|
|
result = subGroupId;
|
|
if (jsonProperties.Length != 0 && jsonProperties[0].Name == "ev_count" && int.TryParse(jsonProperties[0].Value.ToString(), out int evCount))
|
|
count = evCount;
|
|
}
|
|
}
|
|
}
|
|
return new(result, count, commandText);
|
|
}
|
|
|
|
private static string GetJson(Logistics logistics, ProcessDataStandardFormat processDataStandardFormat, pcl.Description description)
|
|
{
|
|
string result;
|
|
StringBuilder stringBuilder = new();
|
|
var @object = new
|
|
{
|
|
description.MesEntity,
|
|
description.Employee,
|
|
description.Layer,
|
|
description.PSN,
|
|
description.RDS,
|
|
description.Reactor,
|
|
description.Recipe,
|
|
description.Zone,
|
|
logistics.DateTimeFromSequence.Ticks
|
|
};
|
|
string[] pair;
|
|
string safeValue;
|
|
string[] segments;
|
|
string serializerValue;
|
|
foreach (string line in processDataStandardFormat.Logistics)
|
|
{
|
|
segments = line.Split('\t');
|
|
if (segments.Length < 2)
|
|
continue;
|
|
segments = segments[1].Split(';');
|
|
_ = stringBuilder.Append('{');
|
|
foreach (string segment in segments)
|
|
{
|
|
pair = segment.Split('=');
|
|
if (pair.Length != 2 || pair[0].Length < 3)
|
|
continue;
|
|
serializerValue = JsonSerializer.Serialize(pair[1]);
|
|
safeValue = serializerValue.Substring(1, serializerValue.Length - 2);
|
|
_ = stringBuilder.Append('"').Append(pair[0].Substring(2)).Append('"').Append(':').Append('"').Append(safeValue).Append('"').Append(',');
|
|
}
|
|
if (stringBuilder.Length > 0)
|
|
_ = stringBuilder.Remove(stringBuilder.Length - 1, 1);
|
|
_ = stringBuilder.Append('}').Append(',');
|
|
}
|
|
if (stringBuilder.Length > 0)
|
|
_ = stringBuilder.Remove(stringBuilder.Length - 1, 1);
|
|
_ = stringBuilder.Append(']').Append('}');
|
|
_ = stringBuilder.Insert(0, ",\"Logistics\":[");
|
|
string json = JsonSerializer.Serialize(@object);
|
|
_ = stringBuilder.Insert(0, json.Substring(0, json.Length - 1));
|
|
JsonElement? jsonElement = JsonSerializer.Deserialize<JsonElement>(stringBuilder.ToString());
|
|
result = jsonElement is null ? "{}" : JsonSerializer.Serialize(jsonElement, new JsonSerializerOptions { WriteIndented = true });
|
|
return result;
|
|
}
|
|
|
|
internal static void Save(string openInsightApiECDirectory, Logistics logistics, string reportFullPath, ProcessDataStandardFormat processDataStandardFormat, pcl.Description description, long? subGroupId, string weekOfYear)
|
|
{
|
|
string checkFile;
|
|
string fileName = Path.GetFileName(reportFullPath);
|
|
string json = GetJson(logistics, processDataStandardFormat, description);
|
|
string? ecPathRoot = Path.GetPathRoot(openInsightApiECDirectory);
|
|
bool ecExists = ecPathRoot is not null && Directory.Exists(ecPathRoot);
|
|
string weekYear = $"{logistics.DateTimeFromSequence:yyyy}_Week_{weekOfYear}";
|
|
string ecDirectory = Path.Combine(openInsightApiECDirectory, weekYear, $"-{description.PSN}", $"-{description.Reactor}", $"-{description.RDS}", $"-{subGroupId}");
|
|
if (ecExists && !Directory.Exists(ecDirectory))
|
|
_ = Directory.CreateDirectory(ecDirectory);
|
|
checkFile = Path.Combine(ecDirectory, fileName);
|
|
if (ecExists && !File.Exists(checkFile))
|
|
File.Copy(reportFullPath, checkFile);
|
|
checkFile = Path.Combine(ecDirectory, $"{logistics.DateTimeFromSequence.Ticks}.json");
|
|
if (ecExists && !File.Exists(checkFile))
|
|
File.WriteAllText(checkFile, json);
|
|
}
|
|
|
|
} |