using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SVS_XDWooSync_Isolcasa
{
    static public class fXD
    {

        //Faz o get do stock de um artigo em especifico
        public static string getStockXD(string itemkeyid)
        {
            string strQueryGetValue = "select CurrentStock from items where keyid='" + itemkeyid + "'";
            string strValue = GetValueFromDBUsing(strQueryGetValue);
            return strValue;
        }

        //Executa query ao XD para retornar apenas 1 valor
        public static string GetValueFromDBUsing(string strQuery)
        {
            string strData = "";

            try
            {
                if (string.IsNullOrEmpty(strQuery) == true)
                    return string.Empty;

                using (var mysqlconnection = new MySqlConnection(globals.csXD))
                {
                    mysqlconnection.Open();
                    using (MySqlCommand cmd = mysqlconnection.CreateCommand())
                    {
                        cmd.CommandType = CommandType.Text;
                        cmd.CommandTimeout = 300;
                        cmd.CommandText = strQuery;

                        object objValue = cmd.ExecuteScalar();
                        if (objValue == null)
                        {
                            cmd.Dispose();
                            return string.Empty;
                        }
                        else
                        {
                            strData = objValue.ToString();//cmd.ExecuteScalar();
                            cmd.Dispose();
                        }

                        mysqlconnection.Close();

                        if (strData == null)
                            return string.Empty;
                        else
                            return strData;
                    }
                }
            }
            catch (MySqlException ex)
            {
                // LogException(ex);
                return string.Empty;
            }
            catch (Exception ex)
            {
                //LogException(ex);
                return string.Empty;
            }
            finally
            {
            }
        }

    }
}
