using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Web.Security;
using SK.MemberBLL;

namespace SK.MemberWeb
{
	/// <summary>
	/// Member 的摘要说明。
	/// </summary>
	public class Member : System.Web.Services.WebService
	{
		MemberSystem member = new MemberSystem();
		/*public Member(string Password)
		{
			//CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
			if(Password!="123")
				throw new Exception("密码错误");
			InitializeComponent();
		}*/
		public Member()
		{
			InitializeComponent();
		}

		#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

		// WEB 服务示例
		// HelloWorld() 示例服务返回字符串 Hello World
		// 若要生成，请取消注释下列行，然后保存并生成项目
		// 若要测试此 Web 服务，请按 F5 键

		//		[WebMethod]
		//		public string HelloWorld()
		//		{
		//			return "Hello World";
		//		}
		[WebMethod]
		public MemberInfo Info(object MemberID)
		{
				return member.Info_Struct(MemberID);
			
		}
		
		[WebMethod]
		public string MemberName(object MemberID)
		{
			return member.MemberName(MemberID);
		}
		[WebMethod]
		public int Point(int MemberID)
		{
			DataRow dr = member.Info(MemberID) ;
			if(dr!=null)
				return Convert.ToInt32(member.Info(MemberID)["Point"]);
			else
				return 0;
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="MemberID"></param>
		/// <param name="Point"></param>
		/// <returns>0表示C币不足，1表示扣币成功，2表示扣币失败</returns>
		[WebMethod]
		public int Deduct_Point(int MemberID,int Point,string Remark)
		{
			int rev = 0;
			try
			{	
				if(Convert.ToInt32(member.Info(MemberID)["Point"])<Point)
				{
					rev = 0;
				}
				else
				{
					member.Deduct_Point(MemberID,Point,Remark);
					rev = 1;
				}
			}
			catch
			{
				rev = 2;
			}
			return rev;
		}
		[WebMethod]
		public bool LoginOK(object MemberName,object Password)
		{
			return member.LoginOK(MemberName,Password);
		}
		[WebMethod]
		public object Login(string MemberName,string Password)
		{
			/*BasePape des = new BasePape();
			des.EncryptKey = "sorkednc";
			des.InputString = Password.ToString();
			des.DesEncrypt();
			string EncryptString = des.OutString;*/
			object MemberID = member.Exists_MemberName(MemberName);
			if(MemberID!=null)
			{
				DataRow dr = member.Info(MemberID);
				if(!dr.IsNull("Password"))
				{
					MemberID = member.Login(MemberName,Password);
			
					if(MemberID==DBNull.Value)
						return null;
					else
						return MemberID;
				}
				else
				{
					BasePape des = new BasePape();
					des.DecryptKey = "sorkednc";
					des.InputString = Password.ToString();
					des.DesDecrypt();
					string DecryptString = des.OutString;
					string sPassword = FormsAuthentication.HashPasswordForStoringInConfigFile(DecryptString,"md5");
					MemberID = member.Login_MD5(MemberName,sPassword);
					if(MemberID == null)
						return null;
					else
					{
						member.UpdatePassword_MD5(MemberID,Password);
						return MemberID;
					}
					
				}
			}
			return null;
		}
		[WebMethod]
		public bool MonthlyTime_Expired(object MemberID,object ProductNO)
		{
			MonthlySystem monthly = new MonthlySystem();
			ProductSystem product = new ProductSystem();
			object ProductID = product.ProductID(ProductNO);
			return monthly.Expired(MemberID,ProductID);
		}
		[WebMethod]
		public int Register(object MemberNO,object MemberName,object TrueName,object Password,object Sex,object Email,
			object Email2,object QuestionID,object Answer,object BirthDay,object Zip,object CountryID,
			object ProvinceID,object City,object EmailHasValid,object EmailValidCode)
		{
			if(member.Exists_MemberName(MemberName)!=null)
				return 0;
			if(member.Exists_Email(Email))
				return 2;
			try
			{
				member.Add( MemberNO, MemberName, TrueName, Password, Sex, Email,
					Email2, QuestionID, Answer, BirthDay, Zip, CountryID,
					ProvinceID, City, EmailHasValid, EmailValidCode);
			}
			catch
			{
				return 3;
			}
			return 1;
		}
		[WebMethod]
		public int PM_NoRead_Count(object ReceiveMemberID)
		{
			MessageSystem msg = new MessageSystem();
			return msg.PM_NoRead_Count(ReceiveMemberID);
			
		}
		[WebMethod]
		public DataSet PM_List(object ReceiveMemberID,object PageSize,object PageIndex)
		{
			MessageSystem msg = new MessageSystem();
			return msg.List(ReceiveMemberID,PageSize,PageIndex);
		}
		[WebMethod]
		public int PM_List_Count(object ReceiveMemberID)
		{
			MessageSystem msg = new MessageSystem();
			return msg.List_Count(ReceiveMemberID);
		}
		[WebMethod]
		public void PM_Send(object Subject,object Body,object SendMemberID,object ReceiveMemberID)
		{
			MessageSystem msg = new MessageSystem();
			msg.Add(Subject,Body,SendMemberID,ReceiveMemberID);
		}
		[WebMethod]
		public void PM_Read(object MessageID)
		{
			MessageSystem msg = new MessageSystem();
			msg.Read(MessageID);
		}
		[WebMethod]
		public void PM_Delete(object MessageID)
		{
			MessageSystem msg = new MessageSystem();
			msg.Delete(MessageID);
		}
		[WebMethod]
		public DataSet PM_Info(object MessageID)
		{
			MessageSystem msg = new MessageSystem();
			return msg.PM_Info(MessageID);
		}
		[WebMethod]
		public object Exists_MemberName(object MemberName)
		{
			return member.Exists_MemberName(MemberName);
		}
		[WebMethod]
		public int Member_Count()
		{
			return member.Count();
		}
		[WebMethod]
		public int Member_Count_Today()
		{
			return member.Count_Today();
		}
		[WebMethod]
		public DataSet MemberList(object MemberName)
		{
			return member.MemberList(MemberName);
		}

		[WebMethod]
		public DataSet MemberList_Company()
		{
			return member.MemberList_Company();
		}

		[WebMethod]
		public DataSet MemberList_Member()
		{
			return member.MemberList_Member();
		}

		[WebMethod]
		public DataSet CompanyHadMember(object MemberID)
		{
			return member.CompanyHadMember(MemberID);
		}

		[WebMethod]
		public int Reg(string MemberName)
		{
			BasePape des = new BasePape();
			des.EncryptKey = "sorkednc";
			des.InputString = "111111";
			des.DesEncrypt();
			string EncryptString = des.OutString;
			try
			{
				member.Add("",MemberName,"",EncryptString,"1","","",null,"",DateTime.Now.AddYears(-30),"",null,null,null,1,Utils.CreateRndStr(20));
				return 1;
			}
			catch
			{
				return 0;
			}

		}


	}
}
