//===========================================================================
// Smarclient FPMS Demo Project a sample project for SmartClient technology.
// Smarclient FPMS Demo is designed and implemented bases Microsoft Application Block tech.
// Author: SFPMS Team, Date 2004-11
// Vision 1.0
// Revison History
//=============================================================================
using System;
using System.Collections; 
using System.Data;
using System.Configuration;
using System.Windows.Forms;
using JSW.Tool;
using JSW.FPMS.Tool;
using JSW.FPMS.ShuJuKu;

namespace JSW.FPMS.Xian.ShuJu
{
	/// <summary>
	/// clsShuJuGuanLi：数据管理类，管理数据载入，数据存放和数据同步等。
	/// 并且提供对HSS系统数据的访问接口。
	/// MVC中的Model
	/// </summary>
	public class clsShuJuGuanLi
	{
		#region 成员变量区域
		//乡数据
		private Hashtable			xianShuJu = null;				
		
		//数据库
		private clsShuJuKuFangWen	shujuKu    = null; 

		#endregion

		#region 私有方法区域
		/// <summary>
		/// 数据集合之间的比较大小
		/// </summary>
		/// <param name="ShuJu1"></param>
		/// <param name="ShuJu2"></param>
		/// <returns>true是相同，false是不同</returns>
		private bool ShuJuJiHeBiJiao(DataSet ShuJu1,DataSet ShuJu2) 
		{			
			//比较表数量
			if(ShuJu1.Tables.Count != ShuJu2.Tables.Count) 
			{
				return false;				
			}		
			if(ShuJu1.Tables.Count == ShuJu2.Tables.Count && ShuJu1.Tables.Count == 0 ) 
			{
				return true;
			}
			//简单考虑单表的情况
			DataTable shujuBiao1 = ShuJu1.Tables[0];
			DataTable shujuBiao2 = ShuJu2.Tables[0];			
			if(shujuBiao1.Columns.Count != shujuBiao2.Columns.Count) 
			{
				return false;
			}
			if(shujuBiao1.Rows.Count !=  shujuBiao2.Rows.Count) 
			{
				return false;
			}
			//忽略的具体的值判断
			return true; 
		}

		/// <summary>
		/// 获得数据集，如果没有找到数据返回一个空数据集合
		/// </summary>
		/// <param name="SQL"></param>
		/// <param name="BiaoMing"></param>
		/// <returns></returns>
		private DataSet HuoDeShuJuJi(String SQL,String BiaoMing) 
		{
			DataSet lingshiShuJu = shujuKu.HuoDeDataSet(SQL,BiaoMing);
			if(lingshiShuJu.Tables[0].Rows.Count == 0 ) 
			{
				return new DataSet();
			}			
			return lingshiShuJu;
		}
		
		/// <summary>
		/// 构造添加SQL语句
		/// </summary>
		private String GouZaoTianJia(string BiaoMing,String ZhuJian,bool ZiDongZhuJian,Hashtable ShuJu) 
		{
			string SQL ="INSERT INTO " + BiaoMing;			

			IDictionaryEnumerator jiHe = ShuJu.GetEnumerator();			
			string zuo = "";
			string you = "";
			while ( jiHe.MoveNext() )
			{
				if(jiHe.Key.Equals(ZhuJian)) 
				{
					if(ZiDongZhuJian)//自动主键就进入下一次循环
					{
						continue;
					}
				}				
				zuo = zuo + jiHe.Key +"," ;
				you = you + "'" + jiHe.Value +"'" + ",";
			}	
			if(zuo.Length ==0) 
			{
				return null;
			}
			else 
			{
				zuo = zuo.Substring(0,zuo.Length-1);
				you = you .Substring(0,you.Length-1);
			}
			SQL = SQL + " (" + zuo +") VALUES (" + you +")";
			return SQL;

		}

		/// <summary>
		/// 构造修改SQL语句
		/// </summary>
		private String GouZaoXiuGai(string BiaoMing,String ZhuJian,Hashtable ShuJu) 
		{
			string SQL ="UPDATE " + BiaoMing + " SET ";			

			IDictionaryEnumerator jiHe = ShuJu.GetEnumerator();			
			string zuo = "";		
			string zhujianZhi = "";
			while ( jiHe.MoveNext() )
			{
				if(!jiHe.Key.Equals(ZhuJian) )
				{
					zuo = zuo + jiHe.Key +"=" +"'" +jiHe.Value+"'," ;
				} 
				else 
				{
					zhujianZhi = jiHe.Value.ToString(); 
				}
				
			}	
			if(zuo.Length ==0) 
			{
				return null;
			}
			else 
			{
				zuo = zuo.Substring(0,zuo.Length-1);
			}
			SQL = SQL + zuo + " WHERE " + ZhuJian + " = '" + zhujianZhi + "'";
			return SQL;

		}
		
		/// <summary>
		/// 构造删除SQL语句
		/// </summary>
		private String GouZaoShanChu(string BiaoMing,String ZhuJian,Hashtable ShuJu) 
		{
			string SQL ="DELETE FROM " + BiaoMing + " WHERE ";	
			IDictionaryEnumerator jiHe = ShuJu.GetEnumerator();							
			string zhujianZhi = "";
			while ( jiHe.MoveNext() )
			{
				if(jiHe.Key.Equals(ZhuJian) )
				{
					zhujianZhi = jiHe.Value.ToString(); 
				} 
				
			}
			SQL = SQL + ZhuJian + " = '" + zhujianZhi + "'";
			return SQL;

		}
		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name="Jian"></param>
		private void GengXinShuJu(String Jian) 
		{
			//键判断			
			DataSet xinShuJu = null;
			switch(Jian) 
			{
				case clsChangLiang.SHUJU_XUQIU :
					xinShuJu = HuoDeShuJuJi("select * from Biao_XuQiu","XuQiu");
					break;
				case clsChangLiang.SHUJU_JIBENSHUJU_XINXIDIAN:
					xinShuJu = HuoDeShuJuJi("select * from Biao_XinXiDian","XinXiDian");
					break;
				case clsChangLiang.SHUJU_JIBENSHUJU_XUQIUFENLEI :
					xinShuJu = HuoDeShuJuJi("select * from Biao_XuQiuFenLei","XuQiuFenLei");
					break;
				case clsChangLiang.SHUJU_PEIZHIXINXI :
					xinShuJu = HuoDeShuJuJi("select * from Biao_PeiZhi","PeiZhiXinXi");
					break;
			}
			this.xianShuJu.Remove(Jian);
			this.xianShuJu .Add(Jian,xinShuJu);
		}
		#endregion

		#region 公有方法区域
		/// <summary>
		/// 构造器
		/// </summary>
		public clsShuJuGuanLi()
		{
			
			xianShuJu  = new Hashtable();	
			shujuKu =  clsShuJuKuFangWen.Instance;
			
		}
		/// <summary>
		/// 把数据载入到内存中
		/// </summary>
		public void ZaiRuShuJu() 
		{	
			
			DataSet xuqiuShuJu = HuoDeShuJuJi("select * from Biao_XuQiu","XuQiu");
			DataSet xinxindianShuJu = HuoDeShuJuJi("select * from Biao_XinXiDian","XinXiDian");
			DataSet xuqiufenleiShuJu = HuoDeShuJuJi("select * from Biao_XuQiuFenLei","XuQiuFenLei");
			DataSet peizhixinxiShuJu = HuoDeShuJuJi("select * from Biao_PeiZhi","PeiZhiXinXi");
			
			xianShuJu.Add(clsChangLiang.SHUJU_XUQIU,xuqiuShuJu);
			xianShuJu.Add(clsChangLiang.SHUJU_JIBENSHUJU_XINXIDIAN,xinxindianShuJu);
			xianShuJu.Add(clsChangLiang.SHUJU_JIBENSHUJU_XUQIUFENLEI,xuqiufenleiShuJu);
			xianShuJu.Add(clsChangLiang.SHUJU_PEIZHIXINXI,peizhixinxiShuJu);

		}
	
		
		/// <summary>
		/// 需求数据更新
		/// </summary>
		public void XuQiuShuJuGengXin() 
		{
			this.GengXinShuJu(clsChangLiang.SHUJU_XUQIU);
		}

		/// 更新数据
		/// </summary>
		/// <param name="ShuJuJian">数据键值</param>
		/// <param name="ShuJu">需要更新的数据</param>
		public void ShuJuGengXin(string ShuJuJian,DataSet xinShuJu) 
		{			
			xianShuJu.Remove(ShuJuJian);
			xianShuJu.Add(ShuJuJian,xinShuJu);
		}

		/// <summary>
		/// 获得基本数据
		/// </summary>
		/// <returns></returns>
		public DataSet HuoDeJiBenShuJu() 
		{
			DataSet shuju = new DataSet();
			shuju.Merge(this.HuoDeShuJu(clsChangLiang.SHUJU_JIBENSHUJU_XINXIDIAN));
			shuju.Merge(this.HuoDeShuJu(clsChangLiang.SHUJU_JIBENSHUJU_XUQIUFENLEI));  
			return shuju;
		}
		/// <summary>
		/// 获得相应的数据
		/// </summary>
		/// <param name="ShuJuJian">数据种类键值</param>
		/// <returns>数据</returns>
		public DataSet HuoDeShuJu(string ShuJuJian) 
		{
			//是否有该键值
			if(!this.xianShuJu.ContainsKey(ShuJuJian)) 
			{
				return null;
			}
			//获得相应的值
			IDictionaryEnumerator jiHe = xianShuJu.GetEnumerator();			
			while ( jiHe.MoveNext() )
			{
				if(jiHe.Key.Equals(ShuJuJian) )
				{
					break;					
				}
			}
			
			//返回数据
			DataSet ShuJu = jiHe.Value as DataSet;
			if(ShuJu.Tables.Count>0) 
			{
				ShuJu.Tables[0].DefaultView.AllowNew		= true; 
			}
			//ShuJu.Tables[0].DefaultView.AllowDelete		= false; 
			return ShuJu; 

		}	


		/// <summary>
		/// 数据维护
		/// </summary>
		/// <param name="Jian">数据键值</param>
		/// <param name="MoShi">更新模式</param>
		/// <param name="ZhuJian">主键</param>
		/// <param name="ShuJu">数据</param>
		public void ShuJuWeiHu(String Jian,String MoShi,String ZhuJian,Hashtable ShuJu) 
		{
			ShuJuWeiHu(Jian,MoShi,ZhuJian,false,ShuJu);
		}

		/// <summary>
		/// 数据维护
		/// </summary>
		/// <param name="Jian">数据键值</param>
		/// <param name="MoShi">更新模式</param>
		/// <param name="ZhuJian">主键</param>
		/// <param name="ZhuJianZiCeng">主键是否是自动数据</param> 
		/// <param name="ShuJu">数据</param>
		public void ShuJuWeiHu(String Jian,String MoShi,String ZhuJian,bool ZiDongZhuJian,Hashtable ShuJu)  
		{			
			string biaoMing = "";
			//键判断
			switch(Jian) 
			{
				case clsChangLiang.SHUJU_JIBENSHUJU_XINXIDIAN:
					biaoMing = "Biao_XinXiDian";
					break;
				case clsChangLiang.SHUJU_JIBENSHUJU_XUQIUFENLEI :
					biaoMing = "Biao_XuQiuFenLei";
					break;
				case clsChangLiang.SHUJU_PEIZHIXINXI :
					biaoMing = "Biao_PeiZhi";
					break;

			}
			//模式判断
			string SQL = "";
			switch(MoShi)
			{
				case clsChangLiang.SHUJU_TIANJIA	:
					SQL = GouZaoTianJia(biaoMing,ZhuJian,ZiDongZhuJian,ShuJu);
					break;
				case clsChangLiang.SHUJU_XIUGAI		:
					SQL = GouZaoXiuGai(biaoMing,ZhuJian,ShuJu);
					break;
				case clsChangLiang.SHUJU_SHANCHU	:
					SQL = GouZaoShanChu(biaoMing,ZhuJian,ShuJu);
					break;
			}
			//执行数据库更新			
			this.shujuKu.ZhiXingGengXin(SQL);
			//获得更新后的数据源
			GengXinShuJu(Jian);
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name="SQL"></param>
		public void GengXinShuJuBySQL(String SQL) 
		{
			this.shujuKu.ZhiXingGengXin(SQL);
		}
		#endregion
		
	}
}
