using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FPJ0000
{
    public static class DatabaseManager
    {
        public static System.Data.SqlClient.SqlConnection getCn()
        {
            string cs = Properties.Settings.Default.gwcs;
            System.Data.SqlClient.SqlConnection cn = new System.Data.SqlClient.SqlConnection();
            cn.ConnectionString = cs;
            return cn;
        }

        public struct sPMPdata
        {
            public string sid { get; set; }
            public int qty { get; set; }
            public string partname { get; set; }
            public string partno { get; set; }
        }
        public static List<sPMPdata> GetPMPQtybySID(string sid)
        {
            List<sPMPdata> retval = new List<sPMPdata>();
            var cn = getCn();
            var sql = "select isnull(SIDNo,'') ,isnull(PartName,'') ,isnull(PartNo,'') , isnull(CurrentQty,0)   from SPMaster where SIDNo = '{0}' and Division like '장비기술%'";
            sql = string.Format(sql, sid);

            System.Data.SqlClient.SqlCommand cmd = null;
            System.Data.SqlClient.SqlDataReader rdr = null;
            try
            {
                cn.Open();
                cmd = new System.Data.SqlClient.SqlCommand(sql, cn);
                rdr = cmd.ExecuteReader();
                while(rdr.Read())
                {
                    //데이터를 가져온다
                    var v_sidno = rdr[0];
                    var v_name = rdr[1];
                    var v_no = rdr[2];
                    var v_qty = rdr[3];

                    var newdr = new sPMPdata();
                    if (v_sidno != DBNull.Value) newdr.sid = v_sidno.ToString();
                    if (v_name != DBNull.Value) newdr.partname = v_name.ToString();
                    if (v_no != DBNull.Value) newdr.partno = v_no.ToString();
                    if (v_qty != DBNull.Value) newdr.qty = int.Parse(v_qty.ToString());
                }
            }
            catch
            {
                
            }
            finally
            {
                if (cmd != null) cmd.Dispose();
                if (rdr != null && rdr.IsClosed == false) rdr.Close();
            }

            cn.Close();
            cn.Dispose();
            return retval;
        }
        public static int GetPMPQtybyModel(string model)
        {
            int retval = 0;
            var cn = getCn();
            var sql = "select isnull(currentqty,0) from SPMaster where PartNo = '{0}' and Division like '장비기술%'";
            sql = string.Format(sql, model);

            System.Data.SqlClient.SqlCommand cmd = null;
            System.Data.SqlClient.SqlDataReader rdr = null;
            try
            {
                cn.Open();
                cmd = new System.Data.SqlClient.SqlCommand(sql, cn);
                var data = cmd.ExecuteScalar();
                retval = int.Parse(data.ToString());
            }
            catch
            {
                retval = -1;
            }
            finally
            {
                if (cmd != null) cmd.Dispose();
                if (rdr != null && rdr.IsClosed == false) rdr.Close();
            }

            cn.Close();
            cn.Dispose();
            return retval;
        }


        public static List<String> getEQGroupLiist(string GroupColumn, string table)
        {
            List<string> retval = new List<string>();
            var cn = getCn();
            cn.Open();
            var sql = "select {0} from {1} where isnull({0},'') != '' group by {0} order by {0}";
            sql = string.Format(sql, "[" + GroupColumn + "]", table);
            var cmd = new System.Data.SqlClient.SqlCommand(sql, cn);
            var rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                retval.Add(rdr[0].ToString());
            }
            cmd.Dispose();
            cn.Close();
            cn.Dispose();
            return retval;
        }
    }
}
