using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using JuSNS.Factory;
using JuSNS.Profile;
using JuSNS.Model;

namespace JuSNS.SQLServer
{
    public class Dict : DbBase, IDict
    {
        /// <summary>
        /// 获取省市列表
        /// </summary>
        /// <returns></returns>
        List<DictAreaInfo> IDict.GetArea()
        {
            List<DictAreaInfo> list = new List<DictAreaInfo>();
            string Sql = "select ID,ParentID,Name from NT_Dict_Area where IsLock=0";
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            while (rd.Read())
            {
                DictAreaInfo info = new DictAreaInfo(rd.GetInt32(0), rd.GetInt32(1), rd.GetString(2));
                list.Add(info);
            }
            rd.Close();
            return list;
        }

        /// <summary>
        /// 取得城市列表
        /// </summary>
        /// <param name="ParertID"></param>
        /// <returns></returns>
        public List<DictAreaInfo> CityList(int ParertID)
        {
            List<DictAreaInfo> list = new List<DictAreaInfo>();
            string Sql = "select ID,ParentID,Name from NT_Dict_Area where IsLock=0 And ParentID=" + ParertID;
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            while (rd.Read())
            {
                DictAreaInfo info = new DictAreaInfo(rd.GetInt32(0), rd.GetInt32(1), rd.GetString(2));
                list.Add(info);
            }
            rd.Close();
            return list;
        }

        public int GetParentCity(int Cid)
        {
            SqlParameter param = new SqlParameter("@cid", Cid);
            return Convert.ToInt32(DbHelper.ExecuteScalar(CommandType.StoredProcedure, "JuSNS_GetParentCity", param));
        }

        /// <summary>
        /// 取得某个省市详细信息
        /// </summary>
        /// <param name="ID">省市编号</param>
        /// <returns></returns>
        public DictAreaInfo Info(int ID)
        {
            DictAreaInfo info = new DictAreaInfo(0, 0, "");
            string Sql = "select ID,ParentID,Name,IsLock from NT_Dict_Area where ID=" + ID;
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, Sql, null);
            while (rd.Read())
            {
                info.ID = Convert.ToInt32(rd["ID"].ToString());
                info.ParentID = Convert.ToInt32(rd["ParentID"].ToString());
                if (rd["Name"] != DBNull.Value) info.Name = rd["Name"].ToString();
                info.IsLock = Convert.ToBoolean(rd["IsLock"].ToString());
            }
            rd.Close();
            return info;
        }
         /// <summary>
        /// 取得地区的名字
        /// </summary>
        /// <param name="areaId"></param>
        /// <returns></returns>
        string IDict.GetAreaName(int areaId)
        {
            string Sql = "select Name from NT_Dict_Area where ID="+ areaId;
            return Convert.ToString(DbHelper.ExecuteScalar(CommandType.Text, Sql, null));
        }

        /// <summary>
        /// 根据名称取得地区ID
        /// </summary>
        /// <param name="areaname">地域名称</param>
        /// <returns></returns>
        public int GetAreaID(string areaname)
        {
            SqlParameter parm = new SqlParameter("areaname", SqlDbType.NVarChar);
            parm.Value = areaname;
            string sql = "select ID from NT_Dict_area where [name]=@areaname";
            IDataReader rd = DbHelper.ExecuteReader(CommandType.Text, sql, parm);
            if (rd.Read())
            {
                rd.Close();
                return Convert.ToInt32(rd[0]);
            }
            else
            {
                rd.Close();
                return 0;
            }
        }
    }
}
