﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICaiBan.ModelDB;
using ICaiBan.Framework.ORM;
using ICaiBan.ModelBL.Common;
using ICaiBan.DAL.Common;
using System.Data;
using ICaiBan.Framework.DB;
using ICaiBan.Framework.Core.Caching;

namespace ICaiBan.DAL
{
    /// <summary>
    /// 省份管理
    /// </summary>
    /// <remarks>aizg add:2013-12-02</remarks>
    public class BaseProvinceDAL
    {
        internal static EntityDbContext<Tab_Base_Province> caiBanDbContext=EntityDbContext<Tab_Base_Province>.GetInstance(Config.ConnString);

        public static bool Add(Tab_Base_Province model, out string newid)
        {
            return caiBanDbContext.Insert(model, out newid);
        }

        #region 获取省份

        /// <summary>
        /// 获取省份列表(查询所有字段)
        /// </summary>
        /// <returns>省份列表</returns>
        /// <remarks>aizg add:2013-12-02</remarks>
        public static List<BaseProvinceBL> GetProvinceList()
        {
            int ckTime=ConvertObjectData.ConvertToInt(ICaiBan.Config.GetConfig().
                GetAppSettingValue("BaseProvinceDAL.GetProvinceList_Time"));
            bool ckMethod=ConvertObjectData.ConvertToBool(ICaiBan.Config.GetConfig().
                GetAppSettingValue("BaseProvinceDAL.GetProvinceList_Method"));
            CacheKeyType ckType=(CacheKeyType) ConvertObjectData.ConvertToInt(ICaiBan.Config.GetConfig().
                GetAppSettingValue("BaseProvinceDAL.GetProvinceList_KeyType"));
            CacheKey<List<BaseProvinceBL>> accCache=new CacheKey<List<BaseProvinceBL>>("allBankSimp", ckMethod, ckType, ckTime);
            List<BaseProvinceBL> list=null;
            list=DistCache.Get(accCache, () =>
            {
                #region sql及参数

                string sql=@" select Province_Id,Province_Name from Tab_Base_Province  order by Order_Seq asc; ";

                #endregion

                DataSet ds=SqlDbHelper.Execute(Config.ConnString, CommandType.Text, sql, null);
                if(ds!=null && ds.Tables.Count > 0)
                    list=ConvertDataTable(ds.Tables[0]);
                else
                    list=null;
                return list;
            });
            return list;
        }

        #endregion

        internal static List<BaseProvinceBL> ConvertDataTable(DataTable dt)
        {
            List<BaseProvinceBL> list=null;
            if(dt!=null && dt.Rows.Count > 0)
            {
                list=new List<BaseProvinceBL>();
                BaseProvinceBL pro;
                for (int i=0; i < dt.Rows.Count; i++)
                {
                    pro=new BaseProvinceBL();

                    #region 赋值

                    if(dt.Columns.Contains("Province_Id"))
                        pro.ProvinceId=ConvertObjectData.ConvertToDecimal(dt.Rows[i]["Province_Id"]);
                    if(dt.Columns.Contains("Province_Name"))
                        pro.ProvinceName=ConvertObjectData.ConvertToString(dt.Rows[i]["Province_Name"]);
                    if(dt.Columns.Contains("Order_Seq"))
                        pro.OrderSeq=ConvertObjectData.ConvertToNullOrInt(dt.Rows[i]["Order_Seq"]);

                    #endregion

                    list.Add(pro);
                }
            }
            return list;
        }
    }
}