using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNet.FrameWork.Data;
using System.Web.SessionState;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HteamDd.Web.ashx
{
    /// <summary>
    /// Dd_attention_manage 的摘要说明
    /// </summary>
    public class Dd_attention_manage : IHttpHandler,IRequiresSessionState
    {
        protected StringBuilder json = new StringBuilder();
        HaTeam.Dd.Model.Dd_Attention attention_model = new HaTeam.Dd.Model.Dd_Attention();
        HaTeam.Dd.BLL.Dd_Attention attention_bll = new HaTeam.Dd.BLL.Dd_Attention();
        
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            json.Append("[");
            string userInfo=null;
            int _num = 0;
            if (context.Request["state"]=="cancel_follow")//取消关注
            {
                if (context.Session["user_Message"]!=null)
                {
                    userInfo = context.Session["user_Message"].ToString();
                    string[] user= userInfo.Split(',');
                    string Attented_By_user = context.Request["AttentionByuserID"].ToString();
                    string sql = "delete Dd_Attention where Attented_By_user =@Attented_By_user and Attention_UerID=@Attention_UerID";
                    SqlParameter[] sp=new  SqlParameter[]{
                    new SqlParameter("@Attented_By_user",Attented_By_user),
                    new SqlParameter("@Attention_UerID",user[0])
                    };
                    try
                    {
                       _num= SqlHelper.ExecuteNonQuery(CommandType.Text, sql, sp);
                    }
                    catch
                    {
                        _num = 0;
                    }
                }
                context.Response.Write(_num);
             
            }
            if (context.Request["state"] == "do_follow")
            {
                if (context.Session["user_Message"] != null)
                {
                    userInfo = context.Session["user_Message"].ToString();
                    string[] user = userInfo.Split(',');
                    string Attented_By_user = context.Request["AttentionByuserID"].ToString();
                    attention_model.Attented_By_user = Convert.ToInt32(Attented_By_user);
                    attention_model.Attention_BlackList = 0;
                    attention_model.Attention_Count = 0;
                    attention_model.Attention_Datetime = System.DateTime.Now;
                    attention_model.Attention_Delete = 0;
                    attention_model.Attention_Lock = 0;
                    attention_model.Attention_Order = 0;
                    attention_model.Attention_Remark = null;
                    attention_model.Attention_State = 0;
                    attention_model.Attention_UerID = Convert.ToInt32(user[0]);
                    attention_model.Attention_UpDatetime = System.DateTime.Now;
                    attention_model.Attention_Verify = 0;
                    int num = 0;
                    try
                    {
                        num = attention_bll.Add(attention_model);
                    }
                    catch
                    {
                        num = 0;
                    }
                    context.Response.Write(num);
                }
            }
            if (context.Request["state"]=="SeachUser")
            {
                string username = context.Request["SeachName"].ToString();
                try
                {
                    bindCheckDataToJson(username);
                }
                catch
                {
                    context.Response.Write("fuck");
                }
                context.Response.Write(json);
            }
        }


        public void bindCheckDataToJson(string username)
        {

            string sql = "select * from Dd_User where User_BlogName like '%" + username + "%' or User_Email like '" + username + "'";
            DataSet ds = DbHelperSQL.GetDataSet(sql);
            dataToJson(ds);
        }

        public void dataToJson(DataSet ds)
        {
            string pic = "";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                pic = dr["User_HeadPhoto"].ToString() == "" ? "/images/avatar_64_64_$1095.png" : dr["User_HeadPhoto"].ToString();
                json.Append("{\"id\":" + "\"" + dr["User_ID"].ToString()+"\",");
                json.Append("\"url\":" + "\"" + string2Json(pic) + "\",");
                json.Append("\"username\":" + "\"" + dr["User_BlogName"].ToString()+"\"},");
            }
            json.Append("]");
        }

        //转换
        public string string2Json(string s)
        {
            StringBuilder sb = new StringBuilder();

            char[] c = s.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                switch (c[i])
                {
                    case '\"':
                        sb.Append("\\\"");
                        break;
                    case '\\':
                        sb.Append("\\\\");
                        break;
                    case '/':
                        sb.Append("\\/");
                        break;
                    case '\b':
                        sb.Append("\\b");
                        break;
                    case '\f':
                        sb.Append("\\f");
                        break;
                    case '\n':
                        sb.Append("\\n");
                        break;
                    case '\r':
                        sb.Append("\\r");
                        break;
                    case '\t':
                        sb.Append("\\t");
                        break;
                    default:
                        sb.Append(c[i].ToString());
                        break;
                }
                StringBuilder show = sb;
            }
            return sb.ToString();
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}