//===========================================================================
// 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 2.0.0.0,support xuqiu fenglei
// Revison History XCY 2004-11-17
//=============================================================================
using System;
using System.Collections; 
using System.Data;
using System.Configuration;
using System.Windows.Forms;
using Microsoft.ApplicationBlocks.SmartClient.Offline;
using JSW.Tool;
using JSW.FPMS.Tool;

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

		//MOAB引用
		private OfflineBlockBuilder		lixianKongZhi	=	null;

		//需求数据是否变化
		private bool xuqiuShiFouBianHua		=  false;

		//数据文件路径
		private string shujuWenJianLuJing = "shuju.xml";
		private string peizhiWenJianLuJing = "peizhi.xml";

		//系统数据
		private string[]				xitongShuJuZu ={"CaiJiShiJian","TiChuRen","YuFuBianMa",
														   "XuQiuLeiBie","DaFuRen","DaFuShiJian",
														   "XinXiDianBianHao","XuQiuManYiDu"};

		#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 && ShuJu1.Tables.Count == 0 ) 
			{
				return true;
			}			
			//忽略的具体的值判断,都默认为不同的。
			return false; 
		}
		#endregion

		#region 公有方法区域
		/// <summary>
		/// 构造器
		/// </summary>
		public clsShuJuGuanLi()
		{
			//初始化
			xiangShuJu  = new Hashtable();			
			//MOAB实例
			lixianKongZhi = OfflineBlockBuilder.Instance; 
			//2004-11-22 He
			this.shujuWenJianLuJing = ConfigurationSettings.AppSettings[clsChangLiang.WENJIAN_LUJING] + "\\" + shujuWenJianLuJing;
			this.peizhiWenJianLuJing = ConfigurationSettings.AppSettings[clsChangLiang.WENJIAN_LUJING] + "\\" + peizhiWenJianLuJing;
			
		}
		/// <summary>
		/// 把数据载入到内存中
		/// </summary>
		public void ZaiRuShuJu() 
		{			
			try 
			{				
				//从数据文件中载入数据，并且分割为基本数据和需求数据
				DataSet shuJu      = new DataSet();				 
				shuJu.ReadXml(shujuWenJianLuJing);
				DataSet jibenShuJu =new DataSet();
				DataSet xuqiuShuJu =new DataSet();

				for(int Loop=0;Loop<shuJu.Tables.Count;Loop++) 
				{
					DataTable shujuBiao = shuJu.Tables[Loop];					
					if(shujuBiao.TableName.Equals("XuQiu"))//如果是需求数据
					{
						xuqiuShuJu.Merge(shujuBiao);						
					}
					else //如果是基本数据
					{
						jibenShuJu.Merge(shujuBiao);
					}
				}			
				xiangShuJu.Add(clsChangLiang.SHUJU_JIBENSHUJU,jibenShuJu);
				xiangShuJu.Add(clsChangLiang.SHUJU_XUQIU,xuqiuShuJu);
				//2.0.0.0 
				ArrayList shuju = this.QuFengXuQiuShuJu(xuqiuShuJu);				
				xiangShuJu.Add(clsChangLiang.SHUJU_XUQIU_WEIDAFU,shuju[0]);
				xiangShuJu.Add(clsChangLiang.SHUJU_XUQIU_YIDAFU,shuju[1]);
			}
			catch(Exception e) 
			{
				if( e.GetType() == typeof(System.IO.DirectoryNotFoundException)) 
				{
					clsRiZhiChuLi.JiLuRiZhi("数据路径错误，不能找到数据文件的路径:"+e.Message);
				}
				//如果出现异常，意味着基础数据环境可能发生了问题，初始化数据环境
				xiangShuJu.Add(clsChangLiang.SHUJU_JIBENSHUJU,new DataSet());
				xiangShuJu.Add(clsChangLiang.SHUJU_XUQIU,new DataSet());
				//2.0.0.0
				xiangShuJu.Add(clsChangLiang.SHUJU_XUQIU_WEIDAFU,new DataSet());
				xiangShuJu.Add(clsChangLiang.SHUJU_XUQIU_YIDAFU,new DataSet());
			}

			try 
			{
				//从配置文件中载入配置信息
				DataSet    peizhiXinXi = new DataSet();
				peizhiXinXi.ReadXml(peizhiWenJianLuJing); 
				xiangShuJu.Add(clsChangLiang.SHUJU_PEIZHIXINXI,peizhiXinXi); 				 				
			}
			catch(Exception e) 
			{	
				if( e.GetType() == typeof(System.IO.DirectoryNotFoundException)) 
				{}
				//如果出现异常，意味着基础数据环境可能发生了问题，初始化数据环境
				xiangShuJu.Add(clsChangLiang.SHUJU_PEIZHIXINXI,new DataSet()); 
			}
		}

		/// <summary>
		/// 重新发送数据 初始化时将XML文件为提交的数据重新提交
		/// zhangwj  11－22
		/// </summary>
		public System.Data.DataSet ChongXinFaSong()
		{
			//数据合法性判断
			if(this.HuoDeShuJu(clsChangLiang.SHUJU_PEIZHIXINXI).Tables.Count <1 ||
				this.HuoDeShuJu(clsChangLiang.SHUJU_XUQIU).Tables.Count<1 ) 
			{
				return new DataSet();
			}
			//构造发送数据集
			System.Data.DataSet dsxinFaSong = ChuangJianShuJuJi() ;
			System.Collections.Hashtable  table = new Hashtable() ;
			string lieMing ="" ;

			for (int i=0;i<dsxinFaSong.Tables["ShuJu"].Columns.Count;i++)
				table.Add(dsxinFaSong.Tables["ShuJu"].Columns[i].ColumnName,"") ;
			//将未提交数据导入到发送数据集中
			DataSet shuJu = this.HuoDeShuJu(clsChangLiang.SHUJU_XUQIU);		
			for (int i=0;i<shuJu.Tables["XuQiu"].Rows.Count;i++)
			{
				if (shuJu.Tables["XuQiu"].Rows[i]["Bianhao"].ToString().Trim()=="-1")
				{
					System.Data.DataRow xinHang = dsxinFaSong.Tables["ShuJu"].NewRow() ;
					for (int j=0;j<shuJu.Tables["XuQiu"].Columns.Count;j++)
					{
						lieMing = shuJu.Tables["XuQiu"].Columns[j].ColumnName;
						if (table.ContainsKey(lieMing))
						{
							xinHang[lieMing] = shuJu.Tables["XuQiu"].Rows[i][lieMing].ToString() ;
						}
					}
					dsxinFaSong.Tables["ShuJu"].Rows.Add(xinHang) ;
				}
			}
			
			return dsxinFaSong ;
		}

		/// <summary>
		///数据存入到本地文件中
		/// </summary>
		public void CunRuShuJu()
		{
			try
			{
				//检查文件路径是否存在，如果没有生成新的文件路径,2004-11-22
				if(!System.IO.Directory.Exists(ConfigurationSettings.AppSettings[clsChangLiang.WENJIAN_LUJING]))
				{					
					System.IO.Directory.CreateDirectory(ConfigurationSettings.AppSettings[clsChangLiang.WENJIAN_LUJING]);
				}
				//存入配置信息数据到peizhi.xml
				HuoDeShuJu(clsChangLiang.SHUJU_PEIZHIXINXI).WriteXml(this.peizhiWenJianLuJing);
				//存入所有数据信息到shuju.xml
				DataSet jibenShuJu = HuoDeShuJu(clsChangLiang.SHUJU_JIBENSHUJU);
				DataSet xuqiuShuJu = HuoDeShuJu(clsChangLiang.SHUJU_XUQIU);				
				DataSet zongShuJu = new DataSet();
				//2004-11-22 He
				zongShuJu.Merge(xuqiuShuJu);
				zongShuJu.Merge(jibenShuJu); 
				zongShuJu.WriteXml(this.shujuWenJianLuJing); 
			}
			catch(Exception e) 
			{	
				if( e.GetType() == typeof(System.IO.DirectoryNotFoundException) ||
						e.GetType() == typeof(System.IO.FileNotFoundException) ) 
				{
					clsRiZhiChuLi.JiLuRiZhi("数据路径错误"+e.Message);
				}
			}
		}

		/// <summary>
		/// 更新数据
		/// </summary>
		/// <param name="ShuJuJian">数据键值</param>
		/// <param name="ShuJu">需要更新的数据</param>
		public void ShuJuGengXin(string ShuJuJian) 
		{
			//从本地缓存的参考数据中获得最近的县数据			
			DataSet xianShuJu = lixianKongZhi.ReferenceDataCache.Retrieve(ShuJuJian) as DataSet;  
			if(xianShuJu == null) 
			{
				return;
			}
			bool bianHua = false;
			lock(this) 
			{
				//如果最新的参考数据不等同于已有的数据，则保存最新的数据。
				if(!ShuJuJiHeBiJiao(xianShuJu,HuoDeShuJu(ShuJuJian)))
				{				
					xiangShuJu.Remove(ShuJuJian);
					xiangShuJu.Add(ShuJuJian,xianShuJu);
					//clsRiZhiChuLi.JiLuRiZhi("数据"+ShuJuJian+"发生了更新"); 

					//zhangwj  11－22  每次更新存入文件
					CunRuShuJu();
					bianHua = true;

					//分割需求,2.0.0.0
					if(ShuJuJian.Equals(clsChangLiang.SHUJU_XUQIU) )
					{
						ArrayList shuju = this.QuFengXuQiuShuJu(xianShuJu);	
						xiangShuJu.Remove(clsChangLiang.SHUJU_XUQIU_WEIDAFU);
						xiangShuJu.Add(clsChangLiang.SHUJU_XUQIU_WEIDAFU,shuju[0]);
						xiangShuJu.Remove(clsChangLiang.SHUJU_XUQIU_YIDAFU);
						xiangShuJu.Add(clsChangLiang.SHUJU_XUQIU_YIDAFU,shuju[1]);
					}
				}
				//处理需求变化队列
				if(bianHua && ShuJuJian.Equals(clsChangLiang.SHUJU_XUQIU)) 
				{
					xuqiuShiFouBianHua =  true;//变化
				}
				else if(!bianHua && ShuJuJian.Equals(clsChangLiang.SHUJU_XUQIU)) 
				{
					xuqiuShiFouBianHua = false;//没有变化
				}
				
			}
		}

		/// <summary>
		/// 获得相应的数据
		/// </summary>
		/// <param name="ShuJuJian">数据种类键值</param>
		/// <returns>数据</returns>
		public DataSet HuoDeShuJu(string ShuJuJian) 
		{
			lock(this) 
			{
				//是否有该键值
				if(!this.xiangShuJu.ContainsKey(ShuJuJian)) 
				{
					return null;
				}
				//获得相应的值
				IDictionaryEnumerator jiHe = xiangShuJu.GetEnumerator();			
				while ( jiHe.MoveNext() )
				{
					if(jiHe.Key.Equals(ShuJuJian) )
					{
						break;					
					}
				}	
				//返回数据
				return (jiHe.Value as DataSet); 
			}

		}
		/// <summary>
		/// 需求数据是否变化
		/// </summary>
		/// <returns>true 变化；false 没有变化</returns>
		public bool XuQiuBianHua() 
		{
			return xuqiuShiFouBianHua;

		}

		/// <summary>
		/// 是否需要等待数据刷新
		/// </summary>
		/// <returns>true：需要数据刷新 false:不需要数据刷新</returns>
		public bool ShiFouXuYaoXiaZai() 
		{
			DataSet jibenShuJu = this.HuoDeShuJu(clsChangLiang.SHUJU_JIBENSHUJU);
			DataSet peizhiXinXi = this.HuoDeShuJu(clsChangLiang.SHUJU_PEIZHIXINXI); 
			if(jibenShuJu.Tables.Count == 0 || peizhiXinXi.Tables.Count==0) 
			{
				return true;
			}
			return false;
		}

		/// <summary>
		/// 添加一个新得需求
		/// </summary>
		/// <param name="xinXuQiu"></param>
		/// <returns></returns>
		public  DataSet XuQiuTianJian(DataTable xinXuQiu) 
		{
			lock(this) 
			{
				//1.0.0.0
				//DataSet shuJu = this.HuoDeShuJu(clsChangLiang.SHUJU_XUQIU);
				//2.0.0.0
				DataSet shuJu = this.HuoDeShuJu(clsChangLiang.SHUJU_XUQIU_WEIDAFU);
				DataSet xinShuJu = null;
				if(shuJu.Tables.Count <1 ) 
				{
					xinShuJu = new DataSet();
					xinXuQiu.TableName="XuQiu";
					xinShuJu.Merge(xinXuQiu);  
				}
				else 
				{
					//zhangwi 11-23 将原来只构造系统数据改为根据xinXuQiu中数据构造，同时增加一个BiaoHao列
					xinShuJu = shuJu;
					
					string lieMing = "" ;
					//将xinShuJu.Tables["XuQiu"]中的所有列保存到HashTable中
					System.Collections.Hashtable table = new Hashtable() ;
					for (int i=0;i<xinShuJu.Tables["XuQiu"].Columns.Count;i++)
						table.Add(xinShuJu.Tables["XuQiu"].Columns[i].ColumnName,"") ;
					
					for (int i=0;i<xinXuQiu.Columns.Count;i++)
					{
						lieMing = xinXuQiu.Columns[i].ColumnName ;
						if (!table.ContainsKey(lieMing))
						{
							System.Data.DataColumn xinLie= new DataColumn() ;
							xinLie.ColumnName = lieMing ;
							xinLie.AllowDBNull = true ;
							xinShuJu.Tables["XuQiu"].Columns.Add(xinLie) ;
						}
					}

					//增加编号列
					if (!table.ContainsKey("BianHao"))
					{
						System.Data.DataColumn xinLie= new DataColumn() ;
						xinLie.ColumnName = "BianHao" ;
						xinLie.AllowDBNull = true ;
						xinShuJu.Tables["XuQiu"].Columns.Add(xinLie) ;
					}

					DataRow xinHang = xinShuJu.Tables["XuQiu"].NewRow(); 
					for (int i=0;i<xinXuQiu.Columns.Count;i++)
					{
						lieMing = xinXuQiu.Columns[i].ColumnName ;
						if (!xinXuQiu.Rows[0].IsNull(lieMing))
							xinHang[lieMing] = xinXuQiu.Rows[0][lieMing].ToString() ;
					}
					xinHang["BianHao"]="-1";
					xinShuJu.Tables["XuQiu"].Rows.Add(xinHang);
				}
				//2.0.0.0->begin
				this.xiangShuJu.Remove(clsChangLiang.SHUJU_XUQIU_WEIDAFU);
				this.xiangShuJu.Add(clsChangLiang.SHUJU_XUQIU_WEIDAFU,xinShuJu);	
				//更新总数据
				/*special code block, bad custom
				DataSet zongShuJu = this.HuoDeShuJu(clsChangLiang.SHUJU_XUQIU); 
				zongShuJu.Merge(this.HuoDeShuJu(clsChangLiang.SHUJU_XUQIU_YIDAFU)); */
				DataSet zongShuJu = new DataSet();
				zongShuJu.Merge(this.HuoDeShuJu(clsChangLiang.SHUJU_XUQIU_WEIDAFU)); 
				zongShuJu.Merge(this.HuoDeShuJu(clsChangLiang.SHUJU_XUQIU_YIDAFU)); 
				this.xiangShuJu.Remove(clsChangLiang.SHUJU_XUQIU);
				this.xiangShuJu.Add(clsChangLiang.SHUJU_XUQIU,zongShuJu);
				//2.0.0.0->end

				//zhangwj  11－22  每次更新存入文件
				CunRuShuJu();
				return xinShuJu;
			}
		}
		
		/// <summary>
		/// 区分需求数据,2.0.0.0新特性
		/// </summary>
		/// <param name="ZongXuQiu">总的需求数据</param>
		/// <returns></returns>
		private ArrayList QuFengXuQiuShuJu(DataSet ZongXuQiu) 
		{
			ArrayList lingshi= new ArrayList();
			if(ZongXuQiu.Tables.Count < 1) 
			{
				lingshi.Add(new DataSet());
				lingshi.Add(new DataSet());
				
			} 
			else 
			{	
				DataTable xuqiuShuJuYiDaFu =ZongXuQiu.Tables["XuQiu"].Clone();
				xuqiuShuJuYiDaFu.Clear();
				DataTable xuqiuShuJu = ZongXuQiu.Tables["XuQiu"].Clone();
				xuqiuShuJu.Clear();
				
				//区分数据
				for(int Loop=0;Loop<ZongXuQiu.Tables["XuQiu"].Rows.Count;Loop++) 
				{
					
					DataRow hang = ZongXuQiu.Tables["XuQiu"].Rows[Loop];
					//判断是否答复
					if(hang["DaFuRen"].ToString().Trim().Length > 0) //已答复
					{							
						DataRow xinHang = xuqiuShuJuYiDaFu.NewRow();  
						//拷贝数据					 
						for(int iLoop = 0;iLoop<ZongXuQiu.Tables["XuQiu"].Columns.Count;iLoop++) 
						{						
							xinHang[ZongXuQiu.Tables["XuQiu"].Columns[iLoop].ColumnName]
								= hang[ZongXuQiu.Tables["XuQiu"].Columns[iLoop].ColumnName];
						}                  						
						xuqiuShuJuYiDaFu.Rows.Add(xinHang);
					}
					else //没有答复
					{						
						DataRow xinHang =xuqiuShuJu.NewRow();  
						//拷贝数据					 
						for(int iLoop = 0;iLoop<ZongXuQiu.Tables["XuQiu"].Columns.Count;iLoop++) 
						{						
							xinHang[ZongXuQiu.Tables["XuQiu"].Columns[iLoop].ColumnName]
								= hang[ZongXuQiu.Tables["XuQiu"].Columns[iLoop].ColumnName];
						}
						xuqiuShuJu.Rows.Add(xinHang);
					}

				}
				//生成DataSet
				DataSet xuqiuShuJuJH = new DataSet();
				if(xuqiuShuJu.Rows.Count>0) 
				{
					xuqiuShuJuJH.Merge(xuqiuShuJu);
				}
				DataSet xuqiuShuJuYiDaFuJH = new DataSet();				
				if(xuqiuShuJuYiDaFu.Rows.Count>0) 
				{
					xuqiuShuJuYiDaFuJH.Merge(xuqiuShuJuYiDaFu);
				}
				//需求没有答复数据在第一位，已答复数据在第二位
				lingshi.Add(xuqiuShuJuJH);
				lingshi.Add(xuqiuShuJuYiDaFuJH);
			}
			return lingshi;

		}

		/// <summary>
		/// 构造界面数据集
		/// 包括两个DataTable： JieGou  界面元素描述 ； ShuJu 存放界面中得到的数据
		///   其中：JieGou的DataTable为从服务器端得到的配置dataTable ,
		///         ShuJu 的DataTable 列名为结构DataTable 中的ShuJuXiangKJM的值
		/// </summary>
		/// <returns></returns>
		public System.Data.DataSet ChuangJianShuJuJi()
		{
			System.Data.DataSet ret = new System.Data.DataSet() ;
			
			//JieGou
			System.Data.DataSet fuWuQiMiaoShu = HuoDeShuJu(clsChangLiang.SHUJU_PEIZHIXINXI) ;
			ret.Tables.Add(fuWuQiMiaoShu.Tables["PeiZhi"].Copy()) ;
			
			//ShuJu
			System.Data.DataTable shuJuDatable = new System.Data.DataTable("ShuJu") ;
			System.Data.DataColumn xinlie ;
			for (int i=0;i<fuWuQiMiaoShu.Tables["PeiZhi"].Rows.Count;i++)
			{
				xinlie = new System.Data.DataColumn() ;
				xinlie.ColumnName = fuWuQiMiaoShu.Tables["PeiZhi"].Rows[i]["ShuJuXiangKJM"].ToString().Trim() ;
				
				shuJuDatable.Columns.Add(xinlie) ; 
			}

			ret.Tables.Add(shuJuDatable) ;

			fuWuQiMiaoShu = HuoDeShuJu(clsChangLiang.SHUJU_JIBENSHUJU) ;
			ret.Tables.Add(fuWuQiMiaoShu.Tables["XinXiDian"].Copy()) ;
			ret.Tables.Add(fuWuQiMiaoShu.Tables["XuQiuFenLei"].Copy()) ;

			return ret ;
		}

		#endregion

		
	}
}
