using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HySoft.Common;
using System.Data;

namespace HySoft.BaseCallCenter.Web.calloutmanage.ajax
{
    /// <summary>
    /// callplanfpset 的摘要说明
    /// </summary>
    public class callplanfpset : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = CommonRequest.GetQueryString("action");
            switch (action)
            {
                case "getuserlist":
                    context.Response.Write(getuserlist(context));
                    break;
                case "getfieldlist":
                    context.Response.Write(getfieldlist(context));
                    break;
                case "fpdata":
                    context.Response.Write(fpdata(context));
                    break;
            }
        }

        private string getfieldlist(HttpContext context)
        {
            string res = "";
            try
            {
                List<Model.T_Call_PageField> pageModel = new List<Model.T_Call_PageField>();
                if (context.Request.Params["taskid"] != null)
                    pageModel = new BLL.T_Call_PageField().GetModelList("F_TaskId='" + context.Request.Params["taskid"] + "'");
                System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(List<Model.T_Call_PageField>));

                using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
                {
                    //JSON序列化
                    serializer.WriteObject(stream, pageModel);
                    res = System.Text.Encoding.UTF8.GetString(stream.ToArray());
                }
            }
            catch (Exception err)
            {
                //res = err.ToString(); 
            }
            finally
            {

            }
            return res;
        }

        private string fpdata(HttpContext context)
        {
            string res = "";
            string type = "0";
            string sql = "";
            DataTable dt = new DataTable();
            try
            {
                string taskid = context.Request.Params["taskid"];
                string arruser = context.Request.Form["txtArrUser"].ToString();
                string arrname = context.Request.Form["txtArrName"].ToString();
                string arrphone = context.Request.Params["txtArrPhone"];
                string fptype = context.Request.Params["fptypepara"];
                string fpvalue = context.Request.Params["fpvalue"];
                string key = context.Request.Params["key"];
                if (fptype.Trim() == "2")
                {
                    //按选择平均分配

                    dt = new BLL.T_Call_TaskTelNum().GetList(" F_FPState=0 and F_Id in (" + arrphone + ") " + sql).Tables[0];

                    #region 按选择分配

                    if (dt != null)
                    {
                        arruser = arruser + "#@!";
                        arruser = arruser.Replace(",#@!", "").Replace("#@!", "");
                        int count = dt.Rows.Count;
                        int usercount = arruser.Split(',').Length;
                        int cqcount = 0;
                        //抽取类型
                        cqcount = Convert.ToInt32(count / usercount);

                        int successcount = 0;
                        if (cqcount > 0)
                        {
                            int m = 0;
                            int n = 0;
                            for (int i = 0; i < count; i++)
                            {
                                #region 更新分配数据
                                //更新
                                if (m < cqcount)
                                {
                                    if (n < usercount)
                                    {
                                        string uname = arrname.Split(',')[n].ToString();
                                        bool b = new BLL.T_Call_TaskTelNum().UpdateFP(Convert.ToInt32(dt.Rows[i]["F_Id"].ToString()), Convert.ToInt32(arruser.Split(',')[n].ToString()), uname);
                                        if (b)
                                        {
                                            successcount++;
                                        }
                                    }
                                    m++;
                                }
                                else
                                {
                                    n++;
                                    m = 0;
                                    i--;
                                }
                                #endregion
                            }
                        }
                        type = "1";
                        res = "成功分配" + successcount.ToString() + "条数据";
                    }

                    #endregion
                }
                else
                {
                    #region 按数量和比例平均分配
                    if (taskid.Trim() != "")
                    {
                        sql += " and F_TaskId=" + taskid + " ";
                    }
                    else
                    {
                        sql += " and 1=0 ";
                    }
                    if (key.Trim() != "")
                    {
                        sql += " and (F_Phone like '%" + key.Trim() + "%' or F_Customer like '%" + key.Trim() + "%' ) ";
                    }
                    //按数量平均分配
                    //按比例平均分配
                    dt = new BLL.T_Call_TaskTelNum().GetList(" F_FPState=0 " + sql).Tables[0];
                    if (dt != null)
                    {
                        arruser = arruser + "#@!";
                        arruser = arruser.Replace(",#@!", "").Replace("#@!", "");
                        int count = dt.Rows.Count;
                        int usercount = arruser.Split(',').Length;
                        int cqcount = 0;
                        //抽取类型
                        if (fptype.Trim() == "1")
                        {
                            //按比例平均分配
                            cqcount = Convert.ToInt32(Convert.ToInt32(fpvalue.Trim()) * count / (100 * usercount));
                        }
                        if (fptype.Trim() == "0")
                        {
                            //按数量平均分配
                            cqcount = Convert.ToInt32(fpvalue.Trim());
                        }
                        int successcount = 0;
                        if (cqcount > 0)
                        {
                            int m = 0;
                            int n = 0;
                            for (int i = 0; i < count; i++)
                            {
                                #region 更新分配数据
                                //更新
                                if (m < cqcount)
                                {
                                    if (n < usercount)
                                    {
                                        string uname=arrname.Split(',')[n].ToString();
                                        bool b = new BLL.T_Call_TaskTelNum().UpdateFP(Convert.ToInt32(dt.Rows[i]["F_Id"].ToString()), Convert.ToInt32(arruser.Split(',')[n].ToString()), uname);
                                        if (b)
                                        {
                                            successcount++;
                                        }
                                    }
                                    m++;
                                }
                                else
                                {
                                    n++;
                                    m = 0;
                                    i--;
                                }
                                #endregion
                            }
                        }
                        type = "1";
                        res = "成功分配" + successcount.ToString() + "条数据";
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                res = ex.ToString();
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }
            return "{msg:" + type + ",info:\"" + res + "\"}";
        }

        private string getuserlist(HttpContext context)
        {
            string strHtml = "";

            DataTable dt = new DataTable();
            try
            {
                dt = new BLL.T_Sys_UserAccount().GetList(" F_SeatFlag=1 order by F_UserId ").Tables[0];
                if (dt.Rows.Count > 0)
                {
                    strHtml += "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\" align='center'>";
                    strHtml += "<tr style=\"text-align: center;\">";
                    strHtml += "<td style=\"height: 15px; width:60px; background-color: #D5EDFE; border-left: 1px solid #CCCCCC;";
                    strHtml += "border-right: 1px solid #CCCCCC; border-top: 1px solid #CCCCCC; border-bottom: 1px solid #CCCCCC;";
                    strHtml += "padding: 1px; text-align: center;\">";
                    strHtml += "<input id=\"chkAll\" type=\"checkbox\" name=\"chkuserall\" onclick=\"SelectAll()\" /><label for=\"chkAll\">全选</label></td>";
                    strHtml += "<td style=\"height: 15px; width:90px; background-color: #D5EDFE; border-left: 1px solid #CCCCCC;";
                    strHtml += "border-right: 1px solid #CCCCCC; border-top: 1px solid #CCCCCC; border-bottom: 1px solid #CCCCCC;";
                    strHtml += "padding: 1px; text-align: center;\">用户工号</td>";
                    strHtml += "<td style=\"height: 15px; width:90px; background-color: #D5EDFE; border-left: 1px solid #CCCCCC;";
                    strHtml += "border-right: 1px solid #CCCCCC; border-top: 1px solid #CCCCCC; border-bottom: 1px solid #CCCCCC;";
                    strHtml += "padding: 1px; text-align: center;\">用户名称</td>";
                    strHtml += "<td style=\"height: 15px; width:90px; background-color: #D5EDFE; border-left: 1px solid #CCCCCC;";
                    strHtml += "border-right: 1px solid #CCCCCC; border-top: 1px solid #CCCCCC; border-bottom: 1px solid #CCCCCC;";
                    strHtml += "padding: 1px; text-align: center;\">未执行任务</td></tr>";
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        int wzxcount = 0;
                        string userid = dt.Rows[i]["F_UserId"].ToString();
                        try
                        {
                            wzxcount = new BLL.T_Call_TaskTelNum().GetList(" F_HCState=0 and F_UserId=" + userid + " ").Tables[0].Rows.Count;
                        }
                        catch
                        { 
                        }
                        strHtml += "<tr align=\"center\" style=\"text-align: center;\" valign=\"middle\" onmouseover=\"this.style.backgroundColor='#F6F6F6'\"";
                        strHtml += "onmouseout=\"this.style.backgroundColor='#ffffff'\" bgcolor=\"#ffffff\">";
                        strHtml += "<td style=\"vertical-align: middle;text-align: center; border-top: 0px solid #CCCCCC; border-bottom: 1px solid #CCCCCC;";
                        strHtml += "border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC; padding-left: 0px;";
                        strHtml += "height: 15px; padding-top: 1px; padding-bottom: 1px;\"><input id=\"chkuserid_" + userid + "\" type=\"checkbox\" name=\"chkuser\" /></td>";
                        strHtml += "<td style=\"vertical-align: middle;text-align: center; border-top: 0px solid #CCCCCC; border-bottom: 1px solid #CCCCCC;";
                        strHtml += "border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC; padding-left: 0px;";
                        strHtml += "height: 15px; padding-top: 1px; padding-bottom: 1px;\">&nbsp;" + dt.Rows[i]["F_UserCode"].ToString() + "</td>";
                        strHtml += "<td style=\"vertical-align: middle;text-align: center; border-top: 0px solid #CCCCCC; border-bottom: 1px solid #CCCCCC;";
                        strHtml += "border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC; padding-left: 0px;";
                        strHtml += "height: 15px; padding-top: 1px; padding-bottom: 1px;\">&nbsp;<span id='spanuser_" + userid + "'>" + dt.Rows[i]["F_UserName"].ToString().Trim() + "</span></td>";
                        strHtml += "<td style=\"vertical-align: middle;text-align: center; border-top: 0px solid #CCCCCC; border-bottom: 1px solid #CCCCCC;";
                        strHtml += "border-left: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC; padding-left: 0px;";
                        strHtml += "height: 15px; padding-top: 1px; padding-bottom: 1px;color:red;\">&nbsp;" + wzxcount.ToString() + "</td></tr>";
                    }
                    strHtml += "</table>";
                }
            }
            catch
            {
            }
            finally
            {
                dt.Clear();
                dt.Dispose();
            }


            return strHtml;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}