using System;
using JXC_MIS.DataTier  ;
using System.Windows.Forms;

namespace JXC_MIS.MyClass
{
	
	/// <summary>
	/// ClsQueryAttrib   {0SN;1标题;2默认值;3字段类型;4参照类型;5参照数据源;6锁定参照(只读=True,否则false或空);7;8;9	}
	/// </summary>
	

	/// <summary>
	/// 过滤属性结构定义
	/// </summary>
	public struct sFilterAttrib
		{
			public string IndexSN  ;						   //索引序号
			public string RowCaption  ;						   //行标题
			public string TextDefault  ;					   //默认值;
			public string FieldType  ;							//数据字段类型
			public bool ReadOnly  ;							   //是否编辑
			public string RefertoType  ;					   //参照类型
			public string RefertoDataSource  ;				   //参照数据源
		}

	public class ClsQueryAttrib
	{
		/// <summary>
		/// 参照类型枚举定义
		/// </summary>
		enum MyRefertoType {DateList,TreeList,ListBox,DriveList,ViewList}
		private sFilterAttrib m_FilterAttrib=new sFilterAttrib();
		public MyReferto Referto;
		ClsDataBaseAccess newADO=new ClsDataBaseAccess();
		private string m_FormatString  ;						  // [0SN;1标题;2默认值;3字段类型;4参照类型;5参照数据源;6锁定参照(只读=True,否则false或空);]
        private string m_ParameterList;								//参数列表
		private string m_ConnectReceipt  ;							 //连接原始单据
		private string m_ConnectNextQuery  ;					   //联结下一个查询
		private string m_QueryName  ;							  //查询存储的名称
		private string m_QueryDesc  ;							  //查询描述
		private string m_ColCaption1  ;								 //显示的列标题
		private string m_ColCaption2  ;								 //显示的列标题
		private string m_ColSum;                                        //列求和
		private int m_KeyId;
		/// <summary>
		/// 格式化的字符串
		/// </summary>
		public string FormatString
		{ 
			get {return m_FormatString;} 
			set
			{ 
				m_FormatString=value;
				if (m_FormatString!=null)
				{
					SplitChar(m_FormatString);
				}
			} 
		} 
		/// <summary>
		///  过滤属性
		/// </summary>
		public sFilterAttrib FilterAttrib
		{ 
			get {return m_FilterAttrib;} 
			set { m_FilterAttrib=value;} 
		} 

		/// <summary>
		/// 查询名称（存储过程）
		/// </summary>
		public string QueryName
		{ 
			get {return m_QueryName;} 
			set { m_QueryName=value;} 
		} 

		/// <summary>
		/// 查询描述
		/// </summary>
		public string QueryDesc
		{ 
			get {return m_QueryDesc;} 
			set { m_QueryDesc=value;} 
		} 

		/// <summary>
		/// 唯一标识
		/// </summary>
		public int KeyId
		{ 
			get {return m_KeyId;} 
			set { m_KeyId=value;} 
		} 

		/// <summary>
		/// 参数列表
		/// </summary>
		public string ParameterList
		{ 
			get {return m_ParameterList;} 
			set { m_ParameterList=value;} 
		} 


		/// <summary>
		/// 列标题
		/// </summary>
		public string ColCaption1
		{ 
			get {return m_ColCaption1;} 
			set { m_ColCaption1=value;} 
		} 

		/// <summary>
		/// 列标题
		/// </summary>
		public string ColCaption2
		{ 
			get {return m_ColCaption2;} 
			set { m_ColCaption2=value;} 
		} 

		/// <summary>
		/// 连接下一级查询
		/// </summary>
		public string ConnectNextQuery
		{ 
			get {return m_ConnectNextQuery;} 
			set { m_ConnectNextQuery=value;} 
		} 
		/// <summary>
		/// 连接原始单据
		/// </summary>
		public string ConnectReceipt
		{ 
			get {return m_ConnectReceipt;} 
			set { m_ConnectReceipt=value;} 
		} 
		/// <summary>
		/// 
		/// </summary>
		public string ColSum
		{ 
			get {return m_ColSum;} 
			set { m_ColSum=value;} 
		} 
		
		public ClsQueryAttrib()
		{
			//
			// TODO: 在此处添加构造函数逻辑
			//
		}
		/// <summary>
		/// 加载查询描述
		/// </summary>
		/// <param name="p_Name"></param>
		public  void LoadQueryDesc(string p_Name)
		{
			System.Data.DataSet dst=null;
			System.Data.DataRow dr =null;
			dst=newADO.GetDataSet("Select fcDesc,fcName,fcParameterList,fcConnectReceipt,fcConnectNextQuery,id from S_ProcedureInf where fcDesc='"+p_Name+"'");
			if (dst==null){return ;}
			if (dst.Tables[0].Rows.Count <1){return ;}

			dr = dst.Tables[0].Rows[0];
			m_QueryDesc=dr[0].ToString();
			m_QueryName=dr[1].ToString();
			m_ParameterList=dr[2].ToString();
			m_ConnectReceipt=dr[3].ToString();
			m_ConnectNextQuery=dr[4].ToString();
			m_KeyId=int.Parse(dr[5].ToString()) ;
		}

		/// <summary>
		/// 记录保存（修改或插入）
		/// </summary>
		/// <param name="p_EidtFlag"></param>
		/// <returns>修改=true 插入=false</returns>
		public bool RecordSave(bool p_EidtFlag)
		{
			string sSQL="";
			if (m_QueryDesc=="") 
			{
				MessageBox.Show("存储描述不能为空！", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information );
				return false;
			}

			if (m_QueryName=="") 
			{
				MessageBox.Show("存储名不能为空！", "提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information );
				return false;
			}


			if (p_EidtFlag==false)
			{

				sSQL="Insert Into S_ProcedureInf ( fcDesc,fcName, fcParameterList,fcConnectReceipt,fcConnectNextQuery,fcColSum)" ;
				sSQL=sSQL+"	 Values ('"+m_QueryDesc+"','"+m_QueryName+"','"+m_ParameterList+"','"+m_ConnectReceipt +"','"+m_ConnectNextQuery+"','"+m_ColSum+"')";
			}
			else
			{
				sSQL="Update S_ProcedureInf set fcDesc='"+m_QueryDesc+"',fcName='"+m_QueryName+"', fcParameterList='"+m_ParameterList+"',fcConnectReceipt='"+m_ConnectReceipt+"', fcConnectNextQuery='"+m_ConnectNextQuery+"', fcColSum='"+m_ColSum+"'";
				sSQL=sSQL+"	 Where Id='"+KeyId+"'";

			}
				 
			if (newADO.ExeSql(sSQL)>=0)
			{
				return true;
			} 
			else {return false;}
		}
		public bool RemoveRecord(string p_KeyId)
		{
			string sSQL="";
			sSQL="Delete  FROM S_ProcedureInf Where Id='"+p_KeyId+"'";
			try
			{
				if (newADO.ExeSql(sSQL)>=0)
				{
					return true;
				}
				else{return false;}
			}
			catch
			{
				return false;
			}
		}
		public string GetQuerySQL()
		{
          return "Select  fcDesc [存 储 描 述],fcName [存 储 名 称], fcParameterList [参 数 及 参 照 列 表                 ],fcConnectReceipt[原始单据连接  ],fcConnectNextQuery [下一级存储连接],fcColsum [计算求和的列 ], ID 唯一标识 from S_ProcedureInf";
	
		}
		/// <summary>
		/// 获取字符串
		/// </summary>
		public string GetFormatString()
		{
			string s="";
			s=this.m_FilterAttrib.IndexSN +";";
			s=s +this.m_FilterAttrib.RowCaption+";";
			s=s+this.m_FilterAttrib.TextDefault +";";
			s=s+this.m_FilterAttrib.FieldType +";";
			s=s+this.m_FilterAttrib.RefertoType +";";
			s=s+this.m_FilterAttrib.RefertoDataSource+";";
			if (this.m_FilterAttrib.ReadOnly==false)
			{
				s=s+";";
			}
			else
			{
				s=s+this.m_FilterAttrib.ReadOnly.ToString() +";";
			}
//			s=s+m_ConnectReceipt +";";
//			s=s+m_ConnectNextQuery +";";

			string [] ArrSplit = s.Split(';'); //     临时数组变量
			int j=ArrSplit.Length ;
			int n=0;
			for (int i=j-1;j>=0;i--)
			{
				if (i!=-1 && ArrSplit[i]=="") 
				{n++;}
				else
				{break;}
			}

			if (j==n)
			{s="";} 
			else {s=s.Substring(0,s.Length-n);}
			return s;
		}
	
		/// <summary>
		/// 0SN;1标题;2默认值;3字段类型;4参照类型;5参照数据源;6锁定参照(只读=True,否则false或空);7;8;9
		/// </summary>
		public sFilterAttrib SplitChar(string ReadText)
		{
			string [] ArrSplit = null; //     临时数组变量
			int j=0;
			this.m_FilterAttrib.IndexSN=""  ;	               //对齐方式
			this.m_FilterAttrib.TextDefault=""   ;	                //默认值;
			this.m_FilterAttrib.ReadOnly =false ;	                    //是否编辑
			this.m_FilterAttrib.RefertoType=""   ;			       //参照类型
			this.m_FilterAttrib.RefertoType="";
			this.m_FilterAttrib.RefertoDataSource=""   ;	        //参照数据源
			this.m_FilterAttrib.FieldType=""   ;					//数据保存时数据验证类型（0或空=可以为空，1=不为空，2=日期验证，3=数值验证)
			this.m_FilterAttrib.RowCaption=""   ;		
			m_ConnectReceipt="";
			ArrSplit=ReadText.Split(';'); 
			j=ArrSplit.Length ;
			string ReadText1="";
			j=ArrSplit.Length ;
			if (j>0 && ArrSplit[0].Trim()!="")//解析 
			{
				this.m_FilterAttrib.IndexSN=ArrSplit[0].Trim();
			}
			//标题
			if (j>1 && ArrSplit[1].Trim()!="")
			{
				this.m_FilterAttrib.RowCaption=ArrSplit[1].Trim();
			}

			if (j>2 && ArrSplit[2].Trim()!="")	//解析 默认值
			{
				ReadText1=ArrSplit[2].Trim();
				this.m_FilterAttrib.TextDefault=ReadText1;		
                       
			}
			if (j>3 && ArrSplit[3].Trim()!="")	//解析 3字段类型
			{
				ReadText1=ArrSplit[3].Trim();
				this.m_FilterAttrib.TextDefault=ReadText1;		
			}

			if (j>4 && ArrSplit[4].Trim()!="")//解析 3参照类型(0,1,2,3,4,5)
			{
				ReadText1=ArrSplit[4].Trim();
				this.m_FilterAttrib.RefertoType=ReadText1.Trim();
			}
			if (j>5 && ArrSplit[5].Trim()!="")//解析 3参照数据源()
			{
				this.m_FilterAttrib.RefertoDataSource=ArrSplit[5].Trim();
			}

			if (j>6 && ArrSplit[6].Trim()!="")//解析  2是否编辑(T,F)
			{
				ReadText1=ArrSplit[6].Trim();
				if (ReadText1=="T" || ReadText1=="t" || ReadText1=="True" || ReadText1=="true" || ReadText1=="是")
				{
					this.m_FilterAttrib.ReadOnly =true;
				}
				else
				{this.m_FilterAttrib.ReadOnly =false;}

			}
//			if (j>7 && ArrSplit[7].Trim()!="")//解析 
//			{
//				m_ConnectReceipt=ArrSplit[7].Trim();
//			}
//
//			if (j>8 && ArrSplit[8].Trim()!="")//解析 
//			{
//				m_ConnectNextQuery=ArrSplit[8].Trim();
//			}
				return m_FilterAttrib;
		}
		
		






















	}
}
