private GetTestCard.Response GetTestCardHandler(GetTestCard command)
        {
            var response = new GetTestCard.Response();
            var testCard = Platform.Query <ITestCard>().Include(t => t.Absorptions).FirstOrDefault(t => t.WorkOrderId == command.WorkOrderId);

            if (testCard == null && command.WindchillIntegration)
            {
                WindchillTestConnectorLibrary.TestCardParameter wTestCard = null;
                string revision = command.ProductRevision == "n/a" ? string.Empty : command.ProductRevision;
                using (var connector = new WindchillTestCardConnector())
                {
                    wTestCard = connector.GetTestCard(command.ProductCode, revision);
                }
                if (wTestCard != null)
                {
                    testCard                       = Platform.Create <ITestCard>();
                    testCard.WorkOrderId           = command.WorkOrderId;
                    testCard.CodiceProdotto        = command.ProductCode;
                    testCard.CorrenteASecco        = wTestCard.CorrenteASecco;
                    testCard.CorrenteASeccoPercent = wTestCard.CorrenteASeccoPercent;
                    testCard.PotenzaASecco         = wTestCard.PotenzaASecco;
                    testCard.PotenzaASeccoPercent  = wTestCard.PotenzaASeccoPercent;

                    foreach (var a in wTestCard.Assorbimenti)
                    {
                        var absorption = Platform.Create <IAbsorption>();
                        absorption.Ampere           = a.Ampere;
                        absorption.AmperePercent    = a.AmperePercent;
                        absorption.Nome             = a.Nome;
                        absorption.Portata          = a.Portata;
                        absorption.PortataPercent   = a.PortataPercent;
                        absorption.Pressione        = a.Pressione;
                        absorption.PressionePercent = a.PressionePercent;
                        absorption.Watt             = a.Watt;
                        absorption.WattPercent      = a.WattPercent;
                        testCard.Absorptions.Add(absorption);
                    }
                    Platform.Submit(testCard);
                }
            }
            if (testCard == null)
            {
                response.SetError(-1005, $"Nessuna scheda di collaudo trovata per il codice prodotto: {command.ProductCode}");
                return(response);
            }

            response.TestCard = new Types.TestCardParameter
            {
                CodiceProdotto        = testCard.CodiceProdotto,
                CorrenteASecco        = testCard.CorrenteASecco,
                CorrenteASeccoPercent = testCard.CorrenteASeccoPercent,
                PotenzaASecco         = testCard.PotenzaASecco,
                PotenzaASeccoPercent  = testCard.PotenzaASeccoPercent,
                Assorbimenti          = new List <Types.AbsorptionParameter>()
            };
            foreach (var a in testCard.Absorptions)
            {
                response.TestCard.Assorbimenti.Add(new Types.AbsorptionParameter
                {
                    Nome             = a.Nome,
                    Ampere           = a.Ampere,
                    AmperePercent    = a.AmperePercent,
                    Portata          = a.Portata,
                    PortataPercent   = a.PortataPercent,
                    Pressione        = a.Pressione,
                    PressionePercent = a.PressionePercent,
                    Watt             = a.Watt,
                    WattPercent      = a.WattPercent
                });
            }

            return(response);
        }
        public TestCardParameter GetTestCard(string productCode, string productRevision)
        {
            var testCard = new TestCardParameter
            {
                CodiceProdotto = productCode,
                Assorbimenti   = new List <AbsorptionParameter>()
            };
            string documentnumber = GetDocNumber(productCode, productRevision);

            if (String.IsNullOrEmpty(documentnumber))
            {
                return(null);
            }
            var genericBusinessObject = _testClient.Query(string.Empty, "intra.adw.industrialization_data", $"number='{documentnumber}'", string.Empty, new string[] { "*" });

            if (genericBusinessObject == null || genericBusinessObject.Length == 0 || !genericBusinessObject[0].properties.Any())
            {
                return(null);
            }

            decimal?potenzaASecco    = null;
            decimal?potenzaPercent   = null;
            decimal?correnteASecco   = null;
            decimal?amperePercent    = null;
            decimal?pressionePercent = null;
            decimal?portataPercent   = null;

            //power1y
            //Power1t
            //current1y
            //current1t
            //power1a
            //Power1t
            //current1a
            //current1t
            //head1a
            //head1t
            //flowrate1a
            //flowrate1t
            var absorptionDictionary = new Dictionary <string, AbsorptionParameter>();

            foreach (var kvp in genericBusinessObject[0].properties)
            {
                //kvp.
                decimal tempvalue = 0;
                string  name      = kvp.name.ToLowerInvariant();
                if (name == "power1y")
                {
                    potenzaASecco = decimal.TryParse(kvp.value, out tempvalue) ? tempvalue : (decimal?)null;
                }
                else if (name == "power1t")
                {
                    potenzaPercent = decimal.TryParse(kvp.value, out tempvalue) ? tempvalue : (decimal?)null;
                }
                else if (name == "current1y")
                {
                    correnteASecco = decimal.TryParse(kvp.value, out tempvalue) ? tempvalue : (decimal?)null;
                }
                else if (name == "current1t")
                {
                    amperePercent = decimal.TryParse(kvp.value, out tempvalue) ? tempvalue : (decimal?)null;
                }
                else if (name == "head1t")
                {
                    pressionePercent = decimal.TryParse(kvp.value, out tempvalue) ? tempvalue : (decimal?)null;
                }
                else if (name == "flowrate1t")
                {
                    portataPercent = decimal.TryParse(kvp.value, out tempvalue) ? tempvalue : (decimal?)null;
                }
                else if (name.StartsWith("power") && name.EndsWith("a"))
                {
                    string absorptionName = GetAbsorptionName(name);
                    if (!absorptionDictionary.ContainsKey(absorptionName))
                    {
                        absorptionDictionary.Add(absorptionName, new AbsorptionParameter());
                        absorptionDictionary[absorptionName].Nome = absorptionName;
                    }
                    absorptionDictionary[absorptionName].Watt = decimal.TryParse(kvp.value, out tempvalue) ? tempvalue : (decimal?)null;
                }
                else if (name.StartsWith("current") && name.EndsWith("a"))
                {
                    string absorptionName = GetAbsorptionName(name);
                    if (!absorptionDictionary.ContainsKey(absorptionName))
                    {
                        absorptionDictionary.Add(absorptionName, new AbsorptionParameter());
                        absorptionDictionary[absorptionName].Nome = absorptionName;
                    }
                    absorptionDictionary[absorptionName].Ampere = decimal.TryParse(kvp.value, out tempvalue) ? tempvalue : (decimal?)null;
                }
                else if (name.StartsWith("head") && name.EndsWith("a"))
                {
                    string absorptionName = GetAbsorptionName(name);
                    if (!absorptionDictionary.ContainsKey(absorptionName))
                    {
                        absorptionDictionary.Add(absorptionName, new AbsorptionParameter());
                        absorptionDictionary[absorptionName].Nome = absorptionName;
                    }
                    absorptionDictionary[absorptionName].Pressione = decimal.TryParse(kvp.value, out tempvalue) ? tempvalue : (decimal?)null;
                }
                else if (name.StartsWith("flowrate") && name.EndsWith("a"))
                {
                    string absorptionName = GetAbsorptionName(name);
                    if (!absorptionDictionary.ContainsKey(absorptionName))
                    {
                        absorptionDictionary.Add(absorptionName, new AbsorptionParameter());
                        absorptionDictionary[absorptionName].Nome = absorptionName;
                    }
                    absorptionDictionary[absorptionName].Portata = decimal.TryParse(kvp.value, out tempvalue) ? tempvalue : (decimal?)null;
                }
                else
                {
                    continue;
                }
            }
            testCard.CorrenteASecco = correnteASecco;
            testCard.PotenzaASecco  = potenzaASecco;
            if (testCard.CorrenteASecco.HasValue)
            {
                testCard.CorrenteASeccoPercent = amperePercent;
            }
            if (testCard.PotenzaASecco.HasValue)
            {
                testCard.PotenzaASeccoPercent = potenzaPercent;
            }
            foreach (var abs in absorptionDictionary.Values)
            {
                if (abs.Ampere.HasValue)
                {
                    abs.AmperePercent = amperePercent;
                }
                if (abs.Portata.HasValue)
                {
                    abs.PortataPercent = portataPercent;
                }
                if (abs.Pressione.HasValue)
                {
                    abs.PressionePercent = pressionePercent;
                }
                if (abs.Watt.HasValue)
                {
                    abs.WattPercent = potenzaPercent;
                }
            }
            testCard.Assorbimenti.AddRange(absorptionDictionary.Values.OrderBy(a => a.Nome));

            return(testCard);
        }