using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using JXC_MIS.MyClass  ;
using JXC_MIS.DataTier ;

namespace JXC_MIS.UserTier.ToolForm
{
	/// <summary>
	/// frmDataTable 的摘要说明。
	/// </summary>
	public class frmDataTable : System.Windows.Forms.Form
	{
		ClsInitData InitData=new ClsInitData();
		ClsDataBaseAccess AdoDB=new ClsDataBaseAccess();
		ClsDataBase newDb=new ClsDataBase();

		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnHeader3;
		private System.Windows.Forms.ColumnHeader columnHeader4;
		private System.Windows.Forms.ListView lv_List;
		private System.Windows.Forms.Splitter spt_splitter;
		private System.Windows.Forms.TextBox txt_SqlEdit;
		private System.Windows.Forms.TextBox txt_Text;
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.CheckBox chk_Table;
		private System.Windows.Forms.CheckBox chk_Expr;
		private System.Windows.Forms.CheckBox chk_Other;
		private System.Windows.Forms.ComboBox cbo_TableName;
		private System.Windows.Forms.Button cmd_MakeSQl;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.ComboBox cbo_SQLType;
		private System.Windows.Forms.ComboBox cbo_Char;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmDataTable()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.lv_List = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
			this.spt_splitter = new System.Windows.Forms.Splitter();
			this.txt_SqlEdit = new System.Windows.Forms.TextBox();
			this.txt_Text = new System.Windows.Forms.TextBox();
			this.panel1 = new System.Windows.Forms.Panel();
			this.cbo_SQLType = new System.Windows.Forms.ComboBox();
			this.button2 = new System.Windows.Forms.Button();
			this.cmd_MakeSQl = new System.Windows.Forms.Button();
			this.cbo_TableName = new System.Windows.Forms.ComboBox();
			this.chk_Other = new System.Windows.Forms.CheckBox();
			this.chk_Expr = new System.Windows.Forms.CheckBox();
			this.chk_Table = new System.Windows.Forms.CheckBox();
			this.cbo_Char = new System.Windows.Forms.ComboBox();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// lv_List
			// 
			this.lv_List.AllowColumnReorder = true;
			this.lv_List.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.lv_List.CheckBoxes = true;
			this.lv_List.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																					  this.columnHeader1,
																					  this.columnHeader2,
																					  this.columnHeader3,
																					  this.columnHeader4});
			this.lv_List.FullRowSelect = true;
			this.lv_List.GridLines = true;
			this.lv_List.HideSelection = false;
			this.lv_List.LabelEdit = true;
			this.lv_List.Location = new System.Drawing.Point(2, 38);
			this.lv_List.Name = "lv_List";
			this.lv_List.Size = new System.Drawing.Size(652, 260);
			this.lv_List.Sorting = System.Windows.Forms.SortOrder.Ascending;
			this.lv_List.TabIndex = 2;
			this.lv_List.View = System.Windows.Forms.View.Details;
			this.lv_List.Click += new System.EventHandler(this.lv_List_Click);
			this.lv_List.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.lv_List_ItemCheck);
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "代码1";
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "名称";
			this.columnHeader2.Width = 92;
			// 
			// columnHeader3
			// 
			this.columnHeader3.Text = "规格";
			// 
			// columnHeader4
			// 
			this.columnHeader4.Text = "数量";
			this.columnHeader4.Width = 40;
			// 
			// spt_splitter
			// 
			this.spt_splitter.Cursor = System.Windows.Forms.Cursors.HSplit;
			this.spt_splitter.Dock = System.Windows.Forms.DockStyle.Top;
			this.spt_splitter.Location = new System.Drawing.Point(0, 0);
			this.spt_splitter.Name = "spt_splitter";
			this.spt_splitter.Size = new System.Drawing.Size(658, 6);
			this.spt_splitter.TabIndex = 3;
			this.spt_splitter.TabStop = false;
			this.spt_splitter.SplitterMoved += new System.Windows.Forms.SplitterEventHandler(this.spt_splitter_SplitterMoved);
			// 
			// txt_SqlEdit
			// 
			this.txt_SqlEdit.Location = new System.Drawing.Point(0, 300);
			this.txt_SqlEdit.Multiline = true;
			this.txt_SqlEdit.Name = "txt_SqlEdit";
			this.txt_SqlEdit.Size = new System.Drawing.Size(654, 122);
			this.txt_SqlEdit.TabIndex = 4;
			this.txt_SqlEdit.Text = "";
			this.txt_SqlEdit.Resize += new System.EventHandler(this.spt_splitter_Resize);
			// 
			// txt_Text
			// 
			this.txt_Text.Location = new System.Drawing.Point(0, 422);
			this.txt_Text.Name = "txt_Text";
			this.txt_Text.Size = new System.Drawing.Size(654, 21);
			this.txt_Text.TabIndex = 5;
			this.txt_Text.Text = "";
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.cbo_Char);
			this.panel1.Controls.Add(this.cbo_SQLType);
			this.panel1.Controls.Add(this.button2);
			this.panel1.Controls.Add(this.cmd_MakeSQl);
			this.panel1.Controls.Add(this.cbo_TableName);
			this.panel1.Controls.Add(this.chk_Other);
			this.panel1.Controls.Add(this.chk_Expr);
			this.panel1.Controls.Add(this.chk_Table);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
			this.panel1.Location = new System.Drawing.Point(0, 6);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(658, 30);
			this.panel1.TabIndex = 6;
			// 
			// cbo_SQLType
			// 
			this.cbo_SQLType.Items.AddRange(new object[] {
															 "<无>",
															 "SELECT",
															 "INSERT INTO",
															 "UPDATE",
															 "DELETE"});
			this.cbo_SQLType.Location = new System.Drawing.Point(458, 6);
			this.cbo_SQLType.Name = "cbo_SQLType";
			this.cbo_SQLType.Size = new System.Drawing.Size(98, 20);
			this.cbo_SQLType.TabIndex = 6;
			this.cbo_SQLType.Text = "<无>";
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(2, 4);
			this.button2.Name = "button2";
			this.button2.Size = new System.Drawing.Size(75, 22);
			this.button2.TabIndex = 5;
			this.button2.Text = "查找表：";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// cmd_MakeSQl
			// 
			this.cmd_MakeSQl.Location = new System.Drawing.Point(560, 4);
			this.cmd_MakeSQl.Name = "cmd_MakeSQl";
			this.cmd_MakeSQl.Size = new System.Drawing.Size(94, 22);
			this.cmd_MakeSQl.TabIndex = 4;
			this.cmd_MakeSQl.Text = "生成SQL语句";
			this.cmd_MakeSQl.Click += new System.EventHandler(this.cmd_MakeSQl_Click);
			// 
			// cbo_TableName
			// 
			this.cbo_TableName.Location = new System.Drawing.Point(82, 6);
			this.cbo_TableName.Name = "cbo_TableName";
			this.cbo_TableName.Size = new System.Drawing.Size(108, 20);
			this.cbo_TableName.TabIndex = 3;
			// 
			// chk_Other
			// 
			this.chk_Other.Location = new System.Drawing.Point(328, 8);
			this.chk_Other.Name = "chk_Other";
			this.chk_Other.Size = new System.Drawing.Size(80, 18);
			this.chk_Other.TabIndex = 2;
			this.chk_Other.Text = "只有别名";
			// 
			// chk_Expr
			// 
			this.chk_Expr.Location = new System.Drawing.Point(266, 8);
			this.chk_Expr.Name = "chk_Expr";
			this.chk_Expr.Size = new System.Drawing.Size(58, 18);
			this.chk_Expr.TabIndex = 1;
			this.chk_Expr.Text = "别名";
			// 
			// chk_Table
			// 
			this.chk_Table.Location = new System.Drawing.Point(196, 8);
			this.chk_Table.Name = "chk_Table";
			this.chk_Table.Size = new System.Drawing.Size(66, 18);
			this.chk_Table.TabIndex = 0;
			this.chk_Table.Text = "表前缀";
			// 
			// cbo_Char
			// 
			this.cbo_Char.Items.AddRange(new object[] {
														  "|",
														  ";",
														  ",",
														  "\'",
														  "\"",
														  "="});
			this.cbo_Char.Location = new System.Drawing.Point(402, 6);
			this.cbo_Char.Name = "cbo_Char";
			this.cbo_Char.Size = new System.Drawing.Size(36, 20);
			this.cbo_Char.TabIndex = 7;
			this.cbo_Char.Text = "|";
			// 
			// frmDataTable
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(658, 445);
			this.Controls.Add(this.panel1);
			this.Controls.Add(this.txt_Text);
			this.Controls.Add(this.txt_SqlEdit);
			this.Controls.Add(this.spt_splitter);
			this.Controls.Add(this.lv_List);
			this.MaximizeBox = false;
			this.Name = "frmDataTable";
			this.Text = "数据字典";
			this.Load += new System.EventHandler(this.frmDataTable_Load);
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		private void frmDataTable_Load(object sender, System.EventArgs e)
		{
			InitData.InitComboBox(cbo_TableName,"Select distinct 表名 from Sys_ViewDataDic");
			cbo_TableName.Text ="R_SellDetail";
			LoadData();
		}

		private void LoadData()
		{
			string sSQL="Select 表名, 描述, 字段顺序, 字段名, 字段描述, 类型长度, 数据类型 from Sys_ViewDataDic ";
			if (cbo_TableName.Text!="<全部>")
			{
				sSQL=sSQL+" Where 表名='"+cbo_TableName.Text +"'";
			}
			
			InitData.InitListView(lv_List,newDb.GetDataArray(AdoDB.GetDataSet(sSQL)),"数 据 表 名, 表 名 描  述,字段顺序, 字 段 名 称, 字段描述, 类型长度, 数据类型");
		}

		private void lv_List_Click(object sender, System.EventArgs e)
		{
//			txt_Text.Text =lv_List.SelectedItems[0].SubItems[3].Text ; 
//			//			lv_List.SelectedItems[0].Remove();
//			txt_SqlEdit.Text =txt_SqlEdit.Text +txt_Text.Text+",";
		}

		private void ChangeHeight()
		{
			//			this.grdClassLevel.Refresh();		
			this.txt_SqlEdit.Height =this.Height -this.spt_splitter.Top-spt_splitter.Height  ;
		}

		private void spt_splitter_Resize(object sender, System.EventArgs e)
		{
			this.ChangeHeight();
		}

		private void spt_splitter_SplitterMoved(object sender, System.Windows.Forms.SplitterEventArgs e)
		{
//						this.ChangeHeight();
		}

		private void lv_List_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
		{
			
		}

		private void cmd_MakeSQl_Click(object sender, System.EventArgs e)
		{
			int cs=0;
			string sTableName="" ;
			
			//			lv_List.SelectedItems[0].Remove();
			//			if (lv_List.SelectedItems[0].Checked ==true)
			//			{
			txt_SqlEdit.Text ="";
			cs=lv_List.CheckedItems.Count ;
			//			if(e.NewValue==CheckState.Unchecked)
			//			{

			if (cs>0)
			{
				for (int c=0;c<cs;c++)
				{
					sTableName=lv_List.CheckedItems[c].SubItems[0].Text;
					txt_Text.Text =lv_List.CheckedItems[c].SubItems[3].Text ;
					
					if (chk_Other.Checked==true)
						{
							txt_Text.Text=lv_List.CheckedItems[c].SubItems[4].Text+cbo_Char.Text;
							if (c==cs-1)
							{
								txt_SqlEdit.Text =txt_SqlEdit.Text+lv_List.CheckedItems[c].SubItems[4].Text;
							}
							else
							{
								txt_SqlEdit.Text =txt_SqlEdit.Text + txt_Text.Text;
							}
						}
					else
					{
							if (chk_Table.Checked==true){txt_Text.Text=lv_List.CheckedItems[c].SubItems[0].Text+"."+txt_Text.Text;}
							if (chk_Expr.Checked==true){txt_Text.Text=txt_Text.Text+" AS "+lv_List.CheckedItems[c].SubItems[4].Text;}
							if (c==cs-1)
							{
								txt_SqlEdit.Text =txt_SqlEdit.Text +txt_Text.Text;
							}
							else
							{
								txt_SqlEdit.Text =txt_SqlEdit.Text +txt_Text.Text+",";
							}
					}

				}
				if (cbo_SQLType.Text =="SELECT")
				{
					txt_SqlEdit.Text =cbo_SQLType.Text +" "+txt_SqlEdit.Text+" From "+sTableName;
				}
				if (cbo_SQLType.Text =="INSERT INTO")
				{
					txt_SqlEdit.Text =cbo_SQLType.Text +" "+sTableName+"("+txt_SqlEdit.Text+") Values()";
				} 
				if (cbo_SQLType.Text =="UPDATE")
				{
					txt_SqlEdit.Text =cbo_SQLType.Text +" "+sTableName+" Set "+txt_SqlEdit.Text+" Where ";
				} 
				if (cbo_SQLType.Text =="DELETE")
				{
					txt_SqlEdit.Text =cbo_SQLType.Text +" "+txt_SqlEdit.Text+" From "+sTableName;
				}
			}
			else
			{txt_SqlEdit.Text ="";}
			//				txt_Text.Text =lv_List.SelectedItems[0].SubItems[3].Text ; 		
			//				txt_SqlEdit.Text =txt_SqlEdit.Text +txt_Text.Text+",";
			//

			//			}

		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			LoadData();

		}











	}
}
