Files
2025-03-14 17:30:34 +01:00

276 lines
8.7 KiB
C#

using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MP.Core.Conf;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
namespace MP.Data.MgModels
{
[BsonIgnoreExtraElements]
public class RecipeModel
{
#region Public Constructors
/// <summary>
/// Init vuoto
/// </summary>
public RecipeModel()
{ }
/// <summary>
/// Init da configurazione di base
/// </summary>
/// <param name="IdxPODL">idx PODL</param>
/// <param name="OrigConfig"></param>
/// <param name="CalcArgs">Dizionario argomenti x decodifica (es cod articolo, descrizione...)</param>
public RecipeModel(int IdxPODL, RecipeConfig OrigConfig, Dictionary<string, string> CalcArgs)
{
this.IdxPODL = IdxPODL;
this.TemplateFile = OrigConfig.TemplateFile;
this.HeadConf = OrigConfig.HeadConf;
this.RowsConf = OrigConfig.RowsConf;
// init oggetti tipizzati da valori conf ricevuti
this.HeadVal = ElementConverter(this.HeadConf.ListKeys, CalcArgs);
// aggiungo args x gestione contatori righe...
CalcArgs.Add("RowNum", $"0");
CalcArgs.Add("RowTot", $"{OrigConfig.NumRow}");
// aggiungo righe elementi..
for (int i = 1; i <= OrigConfig.NumRow; i++)
{
// valore calcolato numero riga gestito ad ogni iterazione
CalcArgs["RowNum"] = $"{i}";
this.RowsVal.Add($"{i}", ElementConverter(this.RowsConf.ListKeys, CalcArgs));
}
}
/// <summary>
/// Aggiunta righe
/// </summary>
/// <param name="CalcArgs"></param>
public List<Element> getNewRow(Dictionary<string, string> CalcArgs)
{
List<Element> rowList = new List<Element>();
rowList = ElementConverter(this.RowsConf.ListKeys, CalcArgs);
return rowList;
}
#endregion Public Constructors
#region Public Properties
/// <summary>
/// Configurazione ricetta x header
/// </summary>
public RecipeBlockConfig HeadConf { get; set; } = new RecipeBlockConfig();
/// <summary>
/// Lista element x testata ricetta
/// </summary>
public List<Element> HeadVal { get; set; } = new List<Element>();
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string? Id { get; set; }
/// <summary>
/// idx ODL di riferimento
/// </summary>
public int IdxODL { get; set; } = 0;
/// <summary>
/// Idx PODL di riferimento
/// </summary>
public int IdxPODL { get; set; } = 0;
/// <summary>
/// Configurazione ricetta x rows
/// </summary>
public RecipeBlockConfig RowsConf { get; set; } = new RecipeBlockConfig();
/// <summary>
/// Dizionario di righe, ognuna come Lista element
/// </summary>
public Dictionary<string, List<Element>> RowsVal { get; set; } = new Dictionary<string, List<Element>>();
/// <summary>
/// File di configurazione template ricetta
/// </summary>
public string TemplateFile { get; set; } = "";
#endregion Public Properties
#region Public Methods
/// <summary>
/// Converte il dizionario in List Element
/// </summary>
public static List<Element> ElementConverter(Dictionary<string, string> ListKeys, Dictionary<string, string> CalcArgs)
{
List<Element> ListObj = ListKeys
.Select(x => new Element(x.Key, x.Value, CalcArgs))
.ToList();
return ListObj;
}
#endregion Public Methods
#region Public Classes
/// <summary>
/// Elemento unitario con cui costruire la ricetta
/// </summary>
public class Element
{
#region Public Constructors
/// <summary>
/// Init classe da valore kvp
/// </summary>
/// <param name="rawKey">Chiave item</param>
/// <param name="rawVal">Valore grezzo item</param>
public Element(string rawKey, string rawVal, Dictionary<string, string> CalcArgs)
{
this.Key = rawKey;
this.OrigVal = rawVal;
this.Value = rawVal;
// cerco se ho uno dei 3 caratteri (C/E/F/S):
if (rawVal.Length >= 2 && rawVal.Substring(1, 1) == ":")
{
string selTipo = rawVal.Substring(0, 2);
switch (selTipo)
{
case "C:":
this.Type = KeyType.Calc;
// recupero item x ricerca in dizionario...
string cKey = rawVal.Substring(2);
if (CalcArgs.ContainsKey(cKey))
{
this.Value = CalcArgs[cKey];
}
else
{
this.Value = cKey;
}
break;
case "E:":
this.Type = KeyType.Enum;
this.Value = "";
this.EnumType = rawVal.Substring(2);
break;
case "F:":
this.Type = KeyType.Fixed;
this.Value = rawVal.Substring(2);
break;
case "S:":
this.Type = KeyType.Calc;
// recupero item x ricerca in dizionario...
string sKey = rawVal.Substring(2);
if (CalcArgs.ContainsKey(sKey))
{
this.Value = CalcArgs[sKey];
}
else
{
this.Value = sKey;
}
break;
default:
this.Type = KeyType.None;
break;
}
}
else
{
this.Type = KeyType.Free;
}
}
#endregion Public Constructors
public override bool Equals(object obj)
{
if (!(obj is Element item))
return false;
if (Key != item.Key)
return false;
if (Value != item.Value)
return false;
if (OrigVal != item.OrigVal)
return false;
if (Type != item.Type)
return false;
if (EnumType != item.EnumType)
return false;
return true;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
#region Public Enums
/// <summary>
/// Tipi di valore ammessi
/// </summary>
public enum KeyType
{
None = 0,
/// <summary>
/// Campo calcolato (NON modificabile)
/// </summary>
Calc,
/// <summary>
/// Valore da Enum
/// </summary>
Enum,
/// <summary>
/// Valore fisso
/// </summary>
Fixed,
/// <summary>
/// Valore libero
/// </summary>
Free,
/// <summary>
/// Campo suggerito (=calcolato modificabile)
/// </summary>
Suggested
}
#endregion Public Enums
#region Public Properties
[NotMapped]
public string EnumType { get; set; } = "";
[NotMapped]
public string Key { get; set; } = "";
public string OrigVal { get; set; } = "";
[NotMapped]
public KeyType Type { get; set; } = KeyType.None;
[NotMapped]
public string Value { get; set; } = "";
#endregion Public Properties
}
#endregion Public Classes
}
}