using System.Web;
using System.Web.Services;

namespace Stat.web.API
{
    /// <summary>
    /// $codebehindclassname$ 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class AddWebsite : IHttpHandler
    {
        Model.Users userModel = new Stat.Model.Users();
        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            //信息获取
            userModel.UserName = Plugin.Common.Rq("uName");
            userModel.UserPwd = Plugin.Common.Rq("uPwd");
            //
            if (userModel.UserName.Length == 0)
            {
                context.Response.Write("错误：用户名称不能为空！");
                context.Response.End();
                return;
            }
            //
            if (Plugin.Common.IsNumeric(userModel.UserName))
            {
                context.Response.Write("错误：用户名称不能够全是数字组合！");
                context.Response.End();
                return;
            }
            //建立该用户
            if (userModel.UserPwd.Length > 0)
            {
                if (!AddUser(context))
                {
                    context.Response.Write("错误：创建新用户发生错误！");
                    context.Response.End();
                }
            }
            //读取用户信息
            userModel = BLL.Users.Get(userModel.UserName);
            if (userModel != null && userModel.UserId > 0)
            {
                //获取网站信息
                Model.WebSites siteModel = new Stat.Model.WebSites();
                siteModel.WebName = Plugin.Common.Rq("wName");
                siteModel.WebUrl = Plugin.Common.Rq("wUrl");
                siteModel.UserId = userModel.UserId;
                siteModel.OpenItem = Plugin.Common.Rq("wOpenItem");
                siteModel.IsReFresh = Plugin.Common.Rq("wIsReFresh") == "1" ? true : false;
                siteModel.IsCodeLock = Plugin.Common.Rq("wIsLock") == "1" ? true : false;
                siteModel.ShowStype = Plugin.Common.Rq("wShowStype");
                //判断数据正确性
                if (siteModel.WebName.Length == 0)
                {
                    context.Response.Write("错误：网站名称不能够为空！");
                    context.Response.End();
                }
                //保存网站
                if (BLL.WebSites.Add(siteModel))
                {
                    //成功
                    context.Response.Write("ok");
                    context.Response.End();
                }
                else
                {
                    context.Response.Write("失败：站点保存失败！");
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("错误：该用户不存在或读取发生错误！");
                context.Response.End();
            }
        }

        private bool AddUser(HttpContext context)
        {
            //
            int uCount = BLL.Users.CheckUserName(userModel.UserName);
            //判断是否记录该用户
            if (uCount > 0)
            {
                context.Response.Write("错误：该用户已经存在，不能够重复添加！");
                context.Response.End();
                return false;
            }
            else if (uCount == -1)
            {
                context.Response.Write("错误：网络错误，请稍候再试！");
                context.Response.End();
                return false;
            }
            else if (uCount == 0)
            {
                Stat.Cache.IConfig config = Stat.Cache.CacheFatory.CreateConfig();
                userModel.IsPwdMD5 = config.IsUserPwdMd5();
                //添加新用户
                return BLL.Users.Add(userModel);
            }

            return false;
        }
        /// <summary>
        /// 
        /// </summary>
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
