//===========================================================================
// 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.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using JSW.FPMS.Tool;
using JSW.FPMS.ShuJuKu;

namespace JSW.FPMS.Web.XianFuWu
{
	/// <summary>
	/// 县级网络服务主类
	/// </summary>
	public class XianFuWu : System.Web.Services.WebService
	{
		#region 成员变量区域
		private clsShuJuKuFangWen shujuKu = null;
		#endregion

		#region 私有方法区域
		/// <summary>
		///  保存数据到数据库
		///  数据库表[Biao_XuQiu]
		/// </summary>
		/// <param name="caijishuju">从乡级采集到的数据</param>
		private void BaoCunShuJu(System.Data.DataSet caijishuju)
		{
			string sql="";
			string sqlDanTiao = "" ;
			string xinZengLie ="" ;
			string shanChuLie = "" ;
			string lieMing = "" ;
			string zhiYu = "" ;		
			string zhi = "" ;
			string shuJuLeiXing = "" ;		

			//从数据库中查询出[Biao_XuQiu]的所有列
			sql = "select Top 1 * from Biao_XuQiu" ;
			
			System.Data.DataSet lieMingJiHe = this.shujuKu.HuoDeDataSet(sql) ;
			if (caijishuju.Tables["ShuJu"].Rows.Count==0) return ;
			sql = "" ;
			for (int j=0;j<caijishuju.Tables["ShuJu"].Rows.Count;j++)
			{
				sqlDanTiao = "INSERT INTO Biao_XuQiu(" ;
				zhiYu = " VALUES (" ;
				for (int i=0;i<caijishuju.Tables["ShuJu"].Columns.Count ;i++)
				{
					lieMing = caijishuju.Tables["ShuJu"].Columns[i].ColumnName ;
					zhi = caijishuju.Tables["ShuJu"].Rows[j][lieMing].ToString() ;
					//日期行数据处理
					System.Data.DataRow[] rwshuJuLeiXing = caijishuju.Tables["PeiZhi"].Select("ShuJuXiangKJM='" + lieMing + "'") ;
					shuJuLeiXing = rwshuJuLeiXing[0]["ShuJuXiangKJLX"].ToString().Trim() ;
					if (shuJuLeiXing=="日期数据类型")
					{
						if (zhi=="")
							zhi = "null" ;
						else
							zhi = Convert.ToDateTime(zhi).ToShortDateString() ;
					}
					//插入新值
					if (i==0)
					{
						sqlDanTiao += lieMing ;
						if (zhi=="null")
							zhiYu += "null" ;
						else
							zhiYu += "'" + zhi + "'" ;
					}
					else
					{
						sqlDanTiao += "," + lieMing ;
						if (zhi=="null")
							zhiYu += ",null" ;
						else
							zhiYu += ",'" + zhi + "'" ;
					}
				}
				sqlDanTiao += ")" ;
				zhiYu += ");  " ;
				sql += sqlDanTiao + zhiYu ;
			}

			xinZengLie = XinZengJieGou(caijishuju,lieMingJiHe) ;
			shanChuLie = ShanChuJieGou(caijishuju,lieMingJiHe) ;
			//执行SQL；
			ArrayList SQLs = new ArrayList();
			if (xinZengLie.Length>0)
			{			
				SQLs.Add(xinZengLie);//增加列数据库结构
			}
			if (shanChuLie.Length>0)
			{			
				SQLs.Add(shanChuLie);//删除列数据库结构
			}
			SQLs.Add(sql);
			this.shujuKu.ZhiXingGengXin(SQLs,true);
		}

		/// <summary>
		/// 新增列SQL语句
		/// </summary>
		private string XinZengJieGou(System.Data.DataSet caijishuju,System.Data.DataSet lieMingJiHe)
		{
			string xinZengLie = "" ;
			string lieMing = "" ;
			System.Collections.Hashtable shuJuKuZiDuanMing = new System.Collections.Hashtable() ;
			System.Collections.Hashtable jieGouZiDuanMing = new System.Collections.Hashtable() ;
			for (int i=0;i< lieMingJiHe.Tables[0].Columns.Count;i++)
			{
				shuJuKuZiDuanMing.Add(lieMingJiHe.Tables[0].Columns[i].ColumnName,"") ;
			}
			
			//新增数据库[Biao_XuQiu]中不存在的列
			for (int i=0;i<caijishuju.Tables["ShuJu"].Columns.Count ;i++)
			{
				lieMing = caijishuju.Tables["ShuJu"].Columns[i].ColumnName ;
				//新增数据库[Biao_XuQiu]中不存在的列
				if (!shuJuKuZiDuanMing.ContainsKey(lieMing))
				{
					if (xinZengLie=="")
					{
						xinZengLie = "ALTER TABLE  Biao_XuQiu ADD " ;
						xinZengLie += lieMing + " VARCHAR(20) NULL" ;
					}
					else
						xinZengLie += "," + lieMing + " VARCHAR(20) NULL" ;
				}
			}
			return xinZengLie ;
		}
		/// <summary>
		/// 删除列SQL语句
		/// </summary>
		private string ShanChuJieGou(System.Data.DataSet caijishuju,System.Data.DataSet lieMingJiHe)
		{
			string shanChuLie = "" ;
			System.Collections.Hashtable shuJuKuZiDuanMing = new System.Collections.Hashtable() ;
			System.Collections.Hashtable jieGouZiDuanMing = new System.Collections.Hashtable() ;
			for (int i=0;i< lieMingJiHe.Tables[0].Columns.Count;i++)
			{
				shuJuKuZiDuanMing.Add(lieMingJiHe.Tables[0].Columns[i].ColumnName,"") ;
			}
			
			for (int i=0;i< caijishuju.Tables["ShuJu"].Columns.Count;i++)
			{
				jieGouZiDuanMing.Add( caijishuju.Tables["ShuJu"].Columns[i].ColumnName,"") ;
			}
			
			//删除数据库[Biao_XuQiu]中存在的列
			foreach (string lieMing in shuJuKuZiDuanMing.Keys)
			{
				if (lieMing=="BianHao") continue ;
				if (!jieGouZiDuanMing.ContainsKey(lieMing))
				{
					if (shanChuLie=="" )
					{
						shanChuLie = "ALTER TABLE Biao_XuQiu DROP COLUMN  ";
						shanChuLie += lieMing ;
					}
					else
						shanChuLie += "," + lieMing ;
				}
			}	
			return shanChuLie;
		}
		/// <summary>
		/// 需求反馈
		/// </summary>
		private void BaoCunFanKui(ArrayList XuQiuFanKui) 
		{
			string bianhao	= XuQiuFanKui[0].ToString();
			string dafuren	= XuQiuFanKui[1].ToString();
			string dafushijian = XuQiuFanKui[2].ToString().Substring(0,10);
			string dafuqingkuang = XuQiuFanKui[3].ToString();
			string gengxinsql = "UPDATE Biao_XuQiu SET DaFuRen = " + "'"+ dafuren +"'";
			gengxinsql += ", DaFuShiJian = " + "'"+dafushijian +"'";
			gengxinsql += ", DaFuQingKuang = " + "'" +dafuqingkuang +"'";
			gengxinsql += " WHERE BianHao = "+ "'"+ bianhao + "'";
			string xuanzesql = "select dafuren from biao_xuqiu where bianhao = "+"'"+bianhao +"'";
			//检验是否有人已经反馈了该条需求
			string yiyoudafuren = this.shujuKu.HuoDeJianDan(xuanzesql);
			if(yiyoudafuren.Length>0) 
			{
				throw new Exception("已经有人反馈了这个信息，请刷新需求信息！");
			}
			//执行更新
			this.shujuKu.ZhiXingGengXin(gengxinsql);
			
		}

		#endregion

		#region 公共方法区域
		public XianFuWu()
		{
			//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
			InitializeComponent();
			//获得数据库实例
			shujuKu = clsShuJuKuFangWen.Instance; 

		}
		#endregion

		#region 组件设计器生成的代码
		
		//Web 服务设计器所必需的
		private IContainer components = null;
				
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
		}

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

		#region Web服务区域
		/// <summary>
		/// 提交新的需求
		/// </summary>
		/// <param name="XuQiuShuJu">需求数据</param>
		[System.Web.Services.WebMethod()]
		public void XuQiuTiJiao(System.Data.DataSet XuQiuShuJu)
		{			
			BaoCunShuJu(XuQiuShuJu);							
		}
		
		/// <summary>
		/// 需求反馈,专门提供给移动客户端访问
		/// </summary>
		/// <param name="XuQiuFanKui">反馈信息</param>
		[System.Web.Services.WebMethod()]
		public void XuQiuFanKui(System.Collections.ArrayList XuQiuFanKui)
		{			
			BaoCunFanKui(XuQiuFanKui);
		}
		

		/// <summary>
		/// 查询县级系统的所有反馈需求
		/// </summary>
		/// <returns>DataSet:需求数据集</returns>
		[System.Web.Services.WebMethod()]
		public System.Data.DataSet XuQiuChaXun() 
		{
			DataSet shuJu = shujuKu.HuoDeDataSet("select * from Biao_XuQiu","XuQiu");
			if(shuJu.Tables[0].Rows.Count == 0 ) 
			{
				return new DataSet();
			}			
			return shuJu;
		}

		/// <summary>
		/// 获得基本数据
		/// </summary>
		/// <returns>DataSet:基本数据集</returns>
		[System.Web.Services.WebMethod()]
		public System.Data.DataSet HuoDeJiBenShuJu() 
		{
			DataSet shuJu1 = shujuKu.HuoDeDataSet("select * from Biao_XinXiDian","XinXiDian");
			DataSet shuJu2 = shujuKu.HuoDeDataSet("select * from Biao_XuQiuFenLei","XuQiuFenLei");
			shuJu1.Merge(shuJu2);
			if(shuJu1.Tables[0].Rows.Count == 0 ) 
			{
				return new DataSet();
			}			
			return shuJu1;
		}
		
		/// <summary>
		/// 获得配置信息
		/// </summary>
		/// <returns>DataSet:配置信息集</returns>
		[System.Web.Services.WebMethod()]
		public System.Data.DataSet HuoDePeiZhi() 
		{
			DataSet shuJu = shujuKu.HuoDeDataSet("select * from Biao_PeiZhi","PeiZhi");
			if(shuJu.Tables[0].Rows.Count == 0 ) 
			{
				return new DataSet();
			}			
			return shuJu;
		}

		/// <summary>
		/// 获得状态
		/// </summary>
		/// <returns>DataSet:状态信息集</returns>
		[System.Web.Services.WebMethod()]
		public System.Data.DataSet HuoDeZhuangTai() 
		{
			return new DataSet();
		}
		#endregion
		
	}
}
