using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;
using System.Collections;

public class Cls_WangkeMS
{
    //常用变量的定义
    private Cls_Operation Operation = new Cls_Operation();
    public ArrayList sMsglist = new ArrayList();

    //获取在线访客总数
    public string GetRecordCount()
    {
        int sCount = 1;
        Operation.OpenConn_Child();
        SqlCommand getCountCmd = new SqlCommand("Select Count(*) As Count From MemberVIP Where isguest='1'", Operation.Conn_Child);
        SqlDataReader getCountRdr = getCountCmd.ExecuteReader();
        getCountRdr.Read();
        sCount = Convert.ToInt32(getCountRdr["Count"]) + 1;
        getCountRdr.Close();
        Operation.CloseConn_Child();
        return sCount.ToString();
    }

    //获取客服人员列表
    public string GetKefulist(string sLanguage)
    {
        Operation.OpenConn_Child();
        SqlCommand cmd = new SqlCommand("select * from UserAdmin", Operation.Conn_Child);
        SqlDataReader rdr = cmd.ExecuteReader();
        rdr.Read();
        string userName = rdr["UserName"].ToString();
        string userDomainName = rdr["DomainNames"].ToString();
        rdr.Close();
        Operation.CloseConn_Child();
        string kefuliststr = "";
        Operation.OpenConn();
        SqlCommand getkefulistCmd = new SqlCommand("select * from MasterControlConpany where DomainNames = '" + userDomainName + "'", Operation.Conn);
        SqlDataReader Rdr = getkefulistCmd.ExecuteReader();
        while (Rdr.Read())
        {
            if (userName == Rdr["UserName"].ToString())
            {
                HttpContext.Current.Session["AdminID"] = Rdr["ID"].ToString();
            }
            string usersexPic = "";
            if (Rdr["UserStatus"].ToString() == "offline")
            {
                usersexPic = Rdr["UserSex"].ToString() == "0" ? "offline_male.png" : "offline_female.png";
            }
            else
            {
                usersexPic = Rdr["UserSex"].ToString() == "0" ? "male.png" : "female.png";
            }
            if (sLanguage != "english")
            {
                kefuliststr = string.Concat(new object[] { kefuliststr, "<img src=\"../wangkems/images/", usersexPic, "\" border=\"0\" /><a href=\"#\" onclick=\"openWin('wangkems/chat.aspx?id=", Rdr["ID"].ToString(), "', 684, 466);return false;\">", Rdr["linkpeaplo"].ToString(), "</a><br />", '\r' });
            }
            else
            {
                kefuliststr = string.Concat(new object[] { kefuliststr, "<img src=\"../wangkems/images/", usersexPic, "\" border=\"0\" /><a href=\"#\" onclick=\"openWin('wangkems/chat_en.aspx?id=", Rdr["ID"].ToString(), "', 684, 466);return false;\">", Rdr["linkpeaplo"].ToString(), "</a><br />", '\r' });
            }
        }
        Rdr.Close();
        Operation.CloseConn();
        return kefuliststr;
    }

    //获取客服信息
    public void GetKefuinfo()
    {
        Operation.OpenConn();
        SqlCommand cmd = new SqlCommand("Select * From MasterControlConpany Where ID = " + HttpContext.Current.Request.QueryString["id"], Operation.Conn);
        SqlDataReader rdr = cmd.ExecuteReader();
        rdr.Read();
        HttpContext.Current.Session["CompanyName"] = rdr["ConpanyName"].ToString();
        HttpContext.Current.Session["Telephone"] = rdr["UserTel"].ToString();
        HttpContext.Current.Session["Email"] = rdr["Email"].ToString();
        HttpContext.Current.Session["UserName"] = rdr["UserName"].ToString();
        HttpContext.Current.Session["UserNickName"] = rdr["linkpeaplo"].ToString();
        rdr.Close();
        Operation.CloseConn();
        string ipstr = "Your IP: " + Operation.GetIP();
        if (Operation.GetArea().Trim() != "")
        {
            ipstr += "<br />" + Operation.GetArea();
        }
        HttpContext.Current.Session["Ipaddress"] = ipstr;
    }

    //保存来宾记录
    public void SaveVisitor()
    {
        string sRndnumber = Operation.GetRandNumber(3, "NUM") + GetRecordCount();
        string sVisitor = "Visitor" + sRndnumber;
        string sVisitorName = "访客" + sRndnumber;
        HttpContext.Current.Session["Visitor"] = sVisitor;
        HttpContext.Current.Session["VisitorName"] = sVisitorName;
        Operation.OpenConn_Child();
        try
        {
            SqlCommand ishaveVisitorCmd = new SqlCommand("Select Count(*) From MemberVIP Where vipusername='" + sVisitor + "'", Operation.Conn_Child);
            if (Convert.ToInt32(ishaveVisitorCmd.ExecuteScalar()) > 0)
            {
                Operation.CloseConn_Child();
            }
            else
            {
                SqlCommand saveVisitorCmd = new SqlCommand("Insert Into MemberVIP (vipname,vipusername,vipsex,loginIP,vipstatus,isguest) Values('访客" + sRndnumber + "','" + sVisitor + "', 'n','" + Operation.GetIP() + "','online','1')", Operation.Conn_Child);
                saveVisitorCmd.ExecuteNonQuery();
                Operation.CloseConn_Child();
            }
        }
        catch
        {
            Operation.CloseConn_Child();
        }
    }

    //删除来宾记录
    public void DelVisitor(string sVisitor)
    {
        Operation.OpenConn_Child();
        try
        {
            SqlCommand delVisitorCmd = new SqlCommand("Delete From MemberVIP Where vipusername='" + sVisitor + "'", Operation.Conn_Child);
            delVisitorCmd.ExecuteNonQuery();
            Operation.CloseConn_Child();
        }
        catch
        {
            Operation.CloseConn_Child();
        }
    }

    //发送消息
    public void SendMsg(string sUserName, string sVisitor, string sVisitorName, string sType, string sMsg)
    {
        switch (sType)
        {
            case "guest":
                Operation.ExecSql("Insert Into Chatrecord(MsgFormUser,MsgToUser,MsgFormStatus,MsgToStatus,MsgReceiveStatus,MsgContent,MsgSendDate,MsgType) Values('" + sVisitor + "', '" + sUserName + "', '1', '1', '1', '" + sMsg + "', '" + DateTime.Now.ToString() + "', '" + sType + "')");
                break;
            case "system":
                Operation.ExecSql("Insert Into Chatrecord(MsgFormUser,MsgToUser,MsgFormStatus,MsgToStatus,MsgReceiveStatus,MsgContent,MsgSendDate,MsgType) Values('system', '" + sUserName + "', '1', '1', '1', '" + sVisitorName + sMsg + "', '" + DateTime.Now.ToString() + "', '" + sType + "')");
                break;
            default:
                break;
        }
    }

    //获取消息列表
    public void GetMsglist(string sUserName, string sUserNickName, string sVisitor, string sVisitorName)
    {
        Operation.OpenConn();
        try
        {
            string sMsgContent;
            DateTime sSendDate = new DateTime();
            SqlCommand sCmd = new SqlCommand("Select * From Chatrecord Where (MsgFormUser = '" + sVisitor + "' and MsgToUser = '" + sUserName + "' and MsgReceiveStatus = '1') or (MsgFormUser = '" + sUserName + "' and MsgToUser = '" + sVisitor + "' and MsgReceiveStatus = '1')", Operation.Conn);
            SqlDataReader sRdr = sCmd.ExecuteReader();
            while (sRdr.Read())
            {
                sSendDate = Convert.ToDateTime(sRdr["MsgSendDate"].ToString());
                if (sRdr["MsgFormUser"].ToString() == sUserName)
                {
                    sMsgContent = "<font color==\"#0000FF\">" + sUserNickName + " " + sSendDate.ToLongTimeString() + "</font><br />" + sRdr["MsgContent"].ToString() + "<br />";
                }
                else
                {
                    sMsgContent = "<font color==\"#000800\">" + sVisitorName + " " + sSendDate.ToLongTimeString() + "</font><br />" + sRdr["MsgContent"].ToString() + "<br />";
                }
                sMsglist.Add(sMsgContent);
            }
            sRdr.Close();
            Operation.CloseConn();
            Operation.ExecSql("Update Chatrecord Set MsgReceiveStatus = '0' Where (MsgFormUser = '" + sUserName + "' and MsgToUser = '" + sVisitor + "') or (MsgFormUser = '" + sVisitor + "' and MsgToUser = '" + sUserName + "')");
        }
        catch
        {
            Operation.CloseConn();
        }
    }
}
