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 /// /// Init vuoto /// public RecipeModel() { } /// /// Init da configurazione di base /// /// idx PODL /// /// Dizionario argomenti x decodifica (es cod articolo, descrizione...) public RecipeModel(int IdxPODL, RecipeConfig OrigConfig, Dictionary 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)); } } /// /// Aggiunta righe /// /// public List getNewRow(Dictionary CalcArgs) { List rowList = new List(); rowList = ElementConverter(this.RowsConf.ListKeys, CalcArgs); return rowList; } #endregion Public Constructors #region Public Properties /// /// Configurazione ricetta x header /// public RecipeBlockConfig HeadConf { get; set; } = new RecipeBlockConfig(); /// /// Lista element x testata ricetta /// public List HeadVal { get; set; } = new List(); [BsonId] [BsonRepresentation(BsonType.ObjectId)] public string? Id { get; set; } /// /// idx ODL di riferimento /// public int IdxODL { get; set; } = 0; /// /// Idx PODL di riferimento /// public int IdxPODL { get; set; } = 0; /// /// Configurazione ricetta x rows /// public RecipeBlockConfig RowsConf { get; set; } = new RecipeBlockConfig(); /// /// Dizionario di righe, ognuna come Lista element /// public Dictionary> RowsVal { get; set; } = new Dictionary>(); /// /// File di configurazione template ricetta /// public string TemplateFile { get; set; } = ""; #endregion Public Properties #region Public Methods /// /// Converte il dizionario in List Element /// public static List ElementConverter(Dictionary ListKeys, Dictionary CalcArgs) { List ListObj = ListKeys .Select(x => new Element(x.Key, x.Value, CalcArgs)) .ToList(); return ListObj; } #endregion Public Methods #region Public Classes /// /// Elemento unitario con cui costruire la ricetta /// public class Element { #region Public Constructors /// /// Init classe da valore kvp /// /// Chiave item /// Valore grezzo item public Element(string rawKey, string rawVal, Dictionary 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 /// /// Tipi di valore ammessi /// public enum KeyType { None = 0, /// /// Campo calcolato (NON modificabile) /// Calc, /// /// Valore da Enum /// Enum, /// /// Valore fisso /// Fixed, /// /// Valore libero /// Free, /// /// Campo suggerito (=calcolato modificabile) /// 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 } }