using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Configuration;
using DeiNiu.Utils;
namespace DeiNiu.Data.BaseObject
{
    [Serializable]
    public abstract class BaseModel :BaseOperModel
    {
        internal int custOperFlag = 0;     
        public BaseModel()
        {
            getImp();
        }
        public  BaseModel(DataRow dr)
        {
            getModel(dr); 
        }
        public BaseModel(System.Data.SqlClient.SqlConnection _Conn)
             
        {
            getImp();
            model_imp._Conn = _Conn;
        }
        public BaseModel(int operid,string connStr)
        {
            getImp();
            model_imp.operid = operid;
            model_imp.connString = connStr;
        }
       protected BaseModel_Imp model_imp;
        public int Add()
        {
          //  _operater = _operater >0 ?_operater : DeiNiu.Utils.LoginInfo.UserId; 
            return model_imp.Add(this);
        }
        public int Update()
        {
            //  _operater = DeiNiu.Utils. LoginInfo.UserId > 0 ? DeiNiu.Utils. LoginInfo.UserId : _operater;
            if (this.ID <= 0)
            {
                return 0;
            }
            _lastmodified = getDateTime();
            return model_imp.Update(this);
        }
        public int UpdateNoCompare()
        {
            //  _operater = DeiNiu.Utils. LoginInfo.UserId > 0 ? DeiNiu.Utils. LoginInfo.UserId : _operater;
            _lastmodified = getDateTime();
            return model_imp.UpdateNoCompare(this);
        }
        public int Delete()
        {
           return model_imp.Delete(this);
        }
        public int rownumStart
        {
            get { return model_imp.rownumStart; }
            set { model_imp.rownumStart = value; }
        }
        public int rownumEnd
        {
            get { return model_imp.rownumEnd; }
            set { model_imp.rownumEnd = value; }
        }
        public int QueryCount()
        {
            string tmp = model_imp.QueryCount().Tables[0].Rows[0][0].ToString();
            return tmp != string.Empty
                       ? Convert.ToInt32(tmp)
                       : 0;
        }
        public DataSet QueryActived()
        {
            return model_imp.QueryActived();
        }
        public DataSet Query(BaseModel exampleObj)
        {
            return model_imp.Query(exampleObj);
        }
        public DataSet Query()
        {
            return model_imp.Query();
        }
        protected DataSet CustQuery(int custOperFlag)
        {
            this.custOperFlag = custOperFlag;
            return model_imp.CustQuery(this);
        }
        protected int queryCount(int custOperFlag)
        {
            this.custOperFlag = custOperFlag;
            DataTable dt = model_imp.CustQuery(this).Tables[0];
            if (dt.Rows.Count ==1)
            {
                if (dt.Columns.Count > 0)
                {
                    return Convert.ToInt32( dt.Rows[0][0].ToString());
                }
            }
            return 0;
        }
        protected int CustOper(int custOperFlag)
        {
            this.custOperFlag = custOperFlag;
            return model_imp.CustOper(this);
        }
      //  public int colone()
       // {
            
      //  }
        public void getModel(DataRow dr)
        {
            try
            {
                model_imp.getModel(this, dr);
            }
            catch (Exception er )
            {
                throw er;
            }
        }
        public void getModel()
        {
            custOperFlag = 0;  //2010.1.3
           model_imp.getModel(this);
        }
        public void getModel(int custOperFlag)
        {
            this.custOperFlag = custOperFlag;
            model_imp.getModel(this);
        }
        /*private void getImp()
        {
            Type tp = GetType();
            String impClassName = getImpClassName(tp.FullName);
           // impClassName = "DeiNiu.wms.Data.Model.Department_Imp";
            Type tpImp = tp.Assembly.GetType(impClassName);
             model_imp = (BaseModel_Imp)System.Activator.CreateInstance(tpImp);
        }
        */
        protected virtual void getImp()
        {
        }
        private String getImpClassName(String ClassName)
        {
            return ClassName + "_Imp";
          
        }
        public DataSet Query(string condition)
        {
            cmdParameters[0] = condition;
            return CustQuery(99);
           /* try {  }
            catch
            {
                return new DataSet();
            }*/
          
        }
        public int getNextSeq(enumCreateOrderType orderType)
        {
            string seq = "seq" + Utils.Util.getPrefix(orderType);
           // LogHelper.debug(GetType(), " seq name is " + seq);
            return model_imp.getSequence("seq" + Utils.Util.getPrefix(orderType) );
        }
        public int getNextSeq(string seqName)
        {
            return model_imp.getSequence(seqName);
        }
        /// 
        /// 获取数据库服务器时间
        /// 
        /// 
        public string getDateTime()
        {
            return model_imp.getDateTime();
        }
        protected string getCondition(string querystr, int start, int end)
        {
            if (end > start && start > 0)
            {
                this.rownumStart = start;
                this.rownumEnd = end;
            }
            //  DataSet ds = string.IsNullOrEmpty(querystr) ? _obj.Query() : _obj.Query(querystr);
            if (string.IsNullOrEmpty(querystr))
            {
                return "";
            }
          
            return getCondition( querystr);
        }
        public string getCondition(string querystr)
        {
            string condition = "";
            //   querystr = filtRiskChar(querystr);
            string[] parameters = querystr.Split(";".ToCharArray());
            foreach (string value in parameters)
            {
                if (string.IsNullOrEmpty(value))
                {
                    continue;
                }
                string[] par = value.Split("#".ToCharArray());
                condition += par.Length == 0 ? "" : " and ";
                foreach (string p in par)
                {
                    condition += p;
                }
            }
            return condition;
        }
        public override string ToString()
        {
            return Util.getJson(this);
        }
    }
    
}