using CallCenter.Utility;
using CallCenterApi.Interface.Controllers.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers.visit
{
    public class VisitFPSetController : BaseController
    {
        //分配
        public ActionResult fpdata(string fpvalue, string[] arruser)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string fptype = "0";
                int userid = CurrentUser.UserData.F_UserId;
                string username = CurrentUser.UserData.F_UserName;
                #region 按数量和比例平均分配
                //按数量平均分配
                //按比例平均分配
                DataTable dt = new BLL.T_Vis_TelAllot().GetList(" F_state=0 ").Tables[0];
                List<Model.T_Sys_UserAccount> userlist = new BLL.T_Sys_UserAccount().GetModelList("");
                if (dt != null)
                {
                    int count = dt.Rows.Count;
                    int cqcount = 0;
                    //抽取类型
                    if (fptype.Trim() == "1")
                    {
                        //按比例平均分配
                        cqcount = Convert.ToInt32(Convert.ToInt32(fpvalue.Trim()) * count / (100 * arruser.Length));
                    }
                    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 < arruser.Length)
                                {
                                    Model.T_Sys_UserAccount usermodel = userlist.Where(u => u.F_UserCode == arruser[n].ToString()).FirstOrDefault();
                                    bool b = new BLL.T_Vis_TelAllot().UpdateFP(Convert.ToInt32(dt.Rows[i]["F_AllotID"].ToString()), usermodel.F_UserId, usermodel.F_UserName, userid, username);
                                    if (b)
                                    {
                                        bool a = new BLL.T_Vis_Telphone().UpdateFP(Convert.ToInt32(dt.Rows[i]["F_HFTelID"].ToString()), userid, usermodel.F_UserId, usermodel.F_UserName, username);
                                        if (a)
                                        {
                                            successcount++;
                                        }
                                    }
                                }
                                m++;
                            }
                            else
                            {
                                n++;
                                m = 0;
                                i--;
                            }
                            #endregion
                        }
                    }
                    res = Success("成功分配" + successcount.ToString() + "条数据");
                }
                #endregion
            }
            return res;
        }

        //获取未分配列表
        public ActionResult GetWFPList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                
                    DataTable dt = new DataTable();
                    string strpageindex = RequestString.GetQueryString("page");
                    int pageindex = 1;
                    string strpagesize = RequestString.GetQueryString("pagesize");
                    int pagesize = 10;

                    string sql = "";
                    sql += " and F_ISAllot=0 ";
                    sql += " and F_TelID in (select F_HFTelID from T_Vis_TelAllot where F_state=0 )";
                    if (strpageindex.Trim() != "")
                    {
                        pageindex = Convert.ToInt32(strpageindex);
                    }

                    if (strpagesize.Trim() != "")
                    {
                        pagesize = Convert.ToInt32(strpagesize);
                    }
                    int recordCount = 0;
                    dt = BLL.PagerBLL.GetListPager(
                        "T_Vis_Telphone",
                        "F_TelID",
                        "*",
                        " " + sql,
                        "ORDER BY F_TelID desc",
                        pagesize,
                        pageindex,
                        true,
                        out recordCount);

                    var obj = new
                    {
                        rows = dt,
                        total = recordCount
                    };

                    res = Content(obj.ToJson());
            
            }
            return res;
        }
    }
}