using System;
using System.Data;
using System.Collections;
using Core.Mes.ServerFrameWork;
using Core.Mes.IBaseInterface;
namespace Core.XgMes.Server.JGKC.SteelPlateManager
{
    public class Common : Core.Mes.ServerFrameWork.IComponent
    {
        public Common()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }

        public override int maxValue
        {
            get
            {
                return 100;
            }
        }
        public override int minValue
        {
            get
            {
                return 50;
            }
        }

        public static string CheckNullStr(object obj)
        {
            if (obj == null || obj.GetType() == typeof(System.DBNull))
            {
                return "";
            }
            else
            {
                return Convert.ToString(obj);
            }
        }
        public static object[] FixValues(object[] values)
        {
            for (int i = 0; i < values.Length; i++)
            {
                if (values[i] == null)
                {
                    values[i] = DBNull.Value;
                }
                else if (values[i].GetType().FullName == "System.String")
                {
                    values[i] = ((string)values[i]).Length != 0 ? values[i] : DBNull.Value;
                }
            }
            return values;
        }


    

        /// <summary>
        /// 对目标对象进行字符串的转换
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ObjToStr(object obj)
        {
            if (obj == null || obj.GetType() == typeof(System.DBNull))
            {
                return "";
            }
            else
            {
                return Convert.ToString(obj);
            }
        }

        /// <summary>
        /// 对目标对象进行数字(小数)的转换
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static System.Decimal ObjToDecimal(object obj)
        {
            try
            {
                if (obj == null || obj.GetType() == typeof(System.DBNull) || ObjToStr(obj).Length == 0)
                {
                    return 0;
                }
                else
                {
                    return Convert.ToDecimal(obj);
                }
            }
            catch (System.Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                return 0;
            }
        }

        /// <summary>
        /// 初始化ComboBox,返回哈希表(针对scm_base_info表)
        /// </summary>
        /// <param name="_sortCode ">scm_base_info中sort_code字段</param>
        /// <returns></returns>
        public ReturnObject IniComboBox(string _sortCode)
        {
            string strSql;
            string strOut = "";
            DataSet ds;
            System.Collections.Hashtable ht = new Hashtable();

            try
            {

                strSql = "select name_ ,id_ from SELECT BUTTRESSCODE FROM KCJ_BUTTRESS_ZW WHERE BUTTRESSTYPE='0' AND ISVALID='1' where sort_code = '" + _sortCode + "'" + " and substr(id_,1,4) ='" + _sortCode + "'";

                ds = this.DBManager.ExecuteQuery(strSql, out strOut);

                if (ds != null && ds.Tables.Count == 1)
                {
                    foreach (System.Data.DataRow row in ds.Tables[0].Rows)
                    {
                        ht.Add(row["id_"].ToString(), row["name_"].ToString());

                    }
                    return new ReturnObject(ht);

                }
                return new ReturnObject(strOut);

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                return new ReturnObject(ex.ToString());
            }

        }


        /// <summary>
        /// 初始化ComboBox,返回哈希表(针对所有表表)
        /// </summary>
        /// <param name="_sql ">SQL(Select语句)</param>
        /// <param name="_keyCol ">Key字段</param>
        /// <param name="_valuesCol ">Values字段</param>
        /// <returns></returns>
        public ReturnObject IniAllComboBox(string _sql, string _keyCol, string _valuesCol)
        {
            string strOut = "";
            DataSet ds;
            System.Collections.Hashtable ht = new Hashtable();

            try
            {

                ds = this.DBManager.ExecuteQuery(_sql, out strOut);

                if (ds != null && ds.Tables.Count == 1)
                {
                    foreach (System.Data.DataRow row in ds.Tables[0].Rows)
                    {
                        ht.Add(row[_keyCol].ToString(), row[_valuesCol].ToString());

                    }
                    return new ReturnObject(ht);

                }
                return new ReturnObject(strOut);

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                return new ReturnObject(ex.ToString());
            }

        }



        //得到区域编码_AreaType=0原料，1成品
        public ReturnObject GetAreaCode(string _AreaType)
        {
            string strOut = "";
            ArrayList al = new ArrayList();
            string sql = "";
            try
            {
                
                    sql = "select distinct(areacode) from KCJ_AREA_ZW where isvalid = '1'and areatype = '" + _AreaType + "'  ";
                

                DataSet ds = this.DBManager.ExecuteQuery(sql, out strOut);

                if (strOut != "")
                {
                    return new ReturnObject(strOut);
                }

                foreach (System.Data.DataRow row in ds.Tables[0].Rows)
                {
                    al.Add(row["areacode"].ToString());
                }
                return new ReturnObject(al);

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
                return new ReturnObject(ex.ToString());
            }

        }
        public ReturnObject ExecuteNone(string sql)
        {
            string outerr = "";
            this.DBManager.ExecuteNonQuery(sql, out outerr);
            if (outerr.Length == 0)
            {
                return new ReturnObject("1");//success
            }
            else
            {
                return new ReturnObject(outerr.ToString());//cuowu
            }
        }
        public ReturnObject GetDataSet(string _sqlstr)
        {
            string strOut = "";
            DataSet ds = this.DBManager.ExecuteQuery(_sqlstr, out strOut);
            //DataSet ds  = ((STMes.DBManager)DBManagerList["ODPDB"]).ExecuteQuery(_sqlstr, out strOut);
            return new ReturnObject(ds, strOut);

        }

    }
}
