using System.Data;
using System.Text;
using BrilliantSightClient.Model.Entity.ApiEntity;
using BrilliantSightClient.Model.Extension;
using BrilliantSightClient.Model.GradeLevel;
using BrilliantSightClient.Model.GradeLevel.Entity.DatabaseEntity;
using BrilliantSightClient.Model.GradeLevel.Exceptions;
using BrilliantSightClient.Model.GradeLevel.Helper;
using BrilliantSightClient.Model.GradeResult.Entity;
using BrilliantSightClient.Model.GradeResult.Entity.Enums;
using BrilliantSightClient.Model.Helper;
using log4net;
using log4net.Repository.Hierarchy;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NPOI.Util.ArrayExtensions;
namespace BrilliantSightClient.Model.GradeResult.Helper;
///
/// 视图 DataInfo工具类
///
public class ViewDataInfoHelper
{
private static readonly ILog Logger = LogManager.GetLogger(typeof(ViewDataInfoHelper));
public static GradeLevelCalculator? levelCalculator { get; set; }
public static List GenerateDataInfos(AlgorithmResultEntity result)
{
JToken jData = JToken.Parse(JsonConvert.SerializeObject(result.Measurements));
List dataInfos = new List();
int shape = EntityHelper.GetValueFromName(result.Shape)??-1;
if(shape == -1) throw new Exception("Unsupported shapes");
string spec = getSpec(result.ErrorMsg);
string viewDataSql = $"SELECT * FROM ViewData WHERE Shape = {shape} AND Specifications = '{spec}' AND RunMode = {Common.RunMode}";
List showViewData = DataBaseHelper.ExecuteQuery(viewDataSql);
levelCalculator = new GradeLevelCalculator(result);
foreach (var viewData in showViewData)
{
DataInfo dataInfo = new DataInfo();
dataInfo.TestItemId = viewData.InfoPath;
dataInfo.TestItemName = "Name".Equals(MultilingualHelper.getString("ColumnNameBase"))? viewData.Name : viewData.EName;
if(!viewData.AvgKey.IsNullOrEmpty())
dataInfo.Avg = DoubleDataFormatHelper.FormatDouble(jData[viewData.AvgKey], viewData.AvgType.ToAccuracy());
if (!viewData.DevKey.IsNullOrEmpty())
{
dataInfo.Dev = DoubleDataFormatHelper.FormatDouble(jData[viewData.DevKey], viewData.DevType.ToAccuracy());
}
if (!viewData.MaxKey.IsNullOrEmpty())
{
dataInfo.Max = DoubleDataFormatHelper.FormatDouble(jData[viewData.MaxKey], viewData.MaxType.ToAccuracy());
}
if (!viewData.MinKey.IsNullOrEmpty())
{
dataInfo.Min = DoubleDataFormatHelper.FormatDouble(jData[viewData.MinKey], viewData.MinType.ToAccuracy());
}
if (!viewData.DtlKey.IsNullOrEmpty())
{
List dtlDoubles = GenerateDtlDoubles(jData, viewData);
dataInfo.Dtl1 = dtlDoubles[0];
dataInfo.Dtl2 = dtlDoubles[1];
dataInfo.Dtl3 = dtlDoubles[2];
dataInfo.Dtl4 = dtlDoubles[3];
dataInfo.Dtl5 = dtlDoubles[4];
dataInfo.Dtl6 = dtlDoubles[5];
dataInfo.Dtl7 = dtlDoubles[6];
dataInfo.Dtl8 = dtlDoubles[7];
}
try
{
GradeLevelSet? cutLevel = levelCalculator.GetCutGradeLevelSet(dataInfo.TestItemId, dataInfo);
dataInfo.CutLevel = MultilingualLevelSet(cutLevel);
dataInfo.GradeList = InitSymCommbox();
GradeLevelSet? symLevel = levelCalculator.GetSymGradeLevelSet(dataInfo.TestItemId, dataInfo);
dataInfo.SymLevel = symLevel == null ? -1 : symLevel.Short;
dataInfo.isEnabled = symLevel != null;
}
catch (Exception ex)
{
}
dataInfos.Add(dataInfo);
}
return dataInfos;
}
public static List GenerateDtlDoubles(JToken data, ViewData viewData)
{
List result = new List();
int.TryParse(viewData.DtlCt, out int dtlCt);
JToken? jToken = data[viewData.DtlKey];
if (jToken == null) return result;
for (int i = 1; i <= dtlCt; i++)
{
try
{
result.Add(DoubleDataFormatHelper.FormatDouble(jToken[$"{viewData.DtlAttr}_{i}"],
viewData.DtlType.ToAccuracy()));
}
catch (Exception ex)
{
}
}
return ProcessList(result);
}
///
/// 处理字符串列表,确保返回列表长度为8
/// 超过8个元素时保留最大值和最小值并缩减到8个元素
/// 不足8个元素时用空字符串补足
///
/// 输入的字符串列表,所有元素应可转换为double
/// 处理后的字符串列表,长度固定为8
public static List ProcessList(List result)
{
if (result == null)
throw new ArgumentNullException(nameof(result));
if (result.Count < 8)
{
var paddedList = new List(result);
while (paddedList.Count < 8)
{
paddedList.Add(string.Empty);
}
return paddedList;
}
var indexedValues = result
.Select((value, index) => new { Value = double.Parse(value), Index = index })
.ToList();
var max = indexedValues.OrderByDescending(x => x.Value).First();
var min = indexedValues.OrderBy(x => x.Value).First();
var remaining = indexedValues
.Where(x => x.Index != max.Index && x.Index != min.Index)
.ToList();
if (remaining.Count <= 6)
{
var resultList = new List
{
result[max.Index],
result[min.Index]
};
resultList.AddRange(remaining.Select(x => result[x.Index]));
while (resultList.Count < 8)
{
resultList.Add(string.Empty);
}
return resultList;
}
var sampledIndices = new List();
for (int i = 0; i < 6; i++)
{
int index = (int)Math.Round(i * (remaining.Count - 1) / 5.0);
sampledIndices.Add(remaining[index].Index);
}
var finalList = new List { result[max.Index] };
finalList.AddRange(sampledIndices.Select(i => result[i]));
finalList.Add(result[min.Index]);
return finalList;
}
public static string MultilingualLevelSet(GradeLevelSet? levelSet)
{
if (levelSet == null) return string.Empty;
string flg = MultilingualHelper.getString("LanguageIdentifiers");
if ("zhpro".Equals(flg)) return levelSet.AName;
if ("zhcn".Equals(flg)) return levelSet.Name;
if ("enus".Equals(flg)) return levelSet.AName;
return levelSet.EName;
}
private static DataTable InitSymCommbox()
{
DataTable dt = new DataTable();
dt.TableName = "GRADE";
dt.Columns.Add("NAME");
dt.Columns.Add("EN_NAME");
dt.Columns.Add("EN_ALL_NAME");
dt.Columns.Add("GRADE_ORDER");
if (levelCalculator != null && levelCalculator.IsInitialized)
{
foreach (var gradeSet in levelCalculator.GradeLevelSymSets)
{
DataRow dr = dt.NewRow();
dr["NAME"] = gradeSet.Name;
dr["EN_NAME"] = gradeSet.AName;
dr["EN_ALL_NAME"] = gradeSet.EName;
dr["GRADE_ORDER"] = gradeSet.Short;
dt.Rows.Add(dr);
}
}
return dt;
}
private static string getSpec(string spec)
{
StringBuilder sbSpec = new StringBuilder();
string[] strs = spec.Split(' ');
for (int i = 1; i < strs.Length; i++)
{
sbSpec.Append(strs[i]);
sbSpec.Append("-");
}
return sbSpec.ToString().Substring(0, sbSpec.Length - 1);
}
}