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.report
{
    public class ZuoXiManYiDuController : BaseController
    {
        //// GET: ZuoXiManYiDu
        //public ActionResult Index()
        //{
        //    return View();
        //}

        //坐席满意度
        //获取表头
        public ActionResult GetColumnList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");

            String[] str = { "坐席姓名", "非常满意", "非常满意占比", "基本满意", "基本满意占比", "不满意", "不满意占比", "未评价", "未评价占比" };
            res = Success("获取坐席满意度评价情况报表表头成功", str);
            return res;
        }
        //获取数据
        public ActionResult GetDataList(string stime, string endtime)
        {
            ActionResult res = NoToken("未知错误，请重新登录");

            DataTable dtNew = new DataTable();
            if (string .IsNullOrEmpty (stime) && string.IsNullOrEmpty(endtime))
            {
                stime = (DateTime.Now.AddDays(1 - DateTime.Now.Day).Date).ToString("yyyy-MM-dd");
                endtime = DateTime.Now.ToString("yyyy-MM-dd");
            }
                dtNew = getData(stime, endtime);
            res = Success("获取坐席满意度评价情况报表数据成功", dtNew);
            return res;
        }

        //导出数据
        public ActionResult ExptList(string stime, string endtime)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                NPOIHelper npoi = new NPOIHelper();
                if (string.IsNullOrEmpty(stime) && string.IsNullOrEmpty(endtime))
                {
                    stime = (DateTime.Now.AddDays(1 - DateTime.Now.Day).Date).ToString("yyyy-MM-dd");
                    endtime = DateTime.Now.ToString("yyyy-MM-dd");
                }
                DataTable dt = getData(stime, endtime,1);
                if (npoi.ExportToExcel("坐席满意度评价情况", dt) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }
            return res;
        }

        private DataTable getData(string stime, string endtime,int type=0)
        {
            DataTable dtNew = new DataTable();
            #region
            DataColumn dc1 = new DataColumn("坐席姓名");
            DataColumn dc2 = new DataColumn("非常满意");
            DataColumn dc3 = new DataColumn("非常满意占比");
            DataColumn dc4 = new DataColumn("基本满意");
            DataColumn dc5 = new DataColumn("基本满意占比");
            DataColumn dc6 = new DataColumn("不满意");
            DataColumn dc7 = new DataColumn("不满意占比");
            DataColumn dc8 = new DataColumn("未评价");
            DataColumn dc9 = new DataColumn("未评价占比");

            dtNew.Columns.Add(dc1);
            dtNew.Columns.Add(dc2);
            dtNew.Columns.Add(dc3);
            dtNew.Columns.Add(dc4);
            dtNew.Columns.Add(dc5);
            dtNew.Columns.Add(dc6);
            dtNew.Columns.Add(dc7);
            dtNew.Columns.Add(dc8);
            dtNew.Columns.Add(dc9);
            #endregion

            string sqltimeCallRecords = "";
            string time = "";
            if (stime != null && stime.Trim() != "")
            {
                time += " and CONVERT(varchar , BeginTime, 120)>=CONVERT(varchar , '" + stime.Trim() + " 00:00:01', 120) ";
            }
            if (endtime != null && endtime.Trim() != "")
            {
                time += " and CONVERT(varchar , BeginTime, 120)<=CONVERT(varchar , '" + endtime.Trim() + " 23:59:59', 120) ";
            }
            //存储数据
            Dictionary<string, List<List<string>>> body = new Dictionary<string, List<List<string>>>();
            //坐席
            //  DataTable dtable = new BLL.T_Sys_UserAccount().GetList("F_RoleId in (1,4,28,29) and F_DeleteFlag=0").Tables[0];//客服坐席、班长坐席、售前坐席、投诉坐席
            DataTable dtable = new BLL.T_Sys_UserAccount().GetList("F_DeleteFlag=0 and F_SeatFlag=1   and  F_DeptId = 2").Tables[0];
            int allcount = dtable.Rows.Count;
            for (int i = 0; i < allcount; i++)
            {
                int userid = Convert.ToInt32(dtable.Rows[i]["F_UserId"].ToString());
                List<string> bodyitem = new List<string>();
                string[] UNDN = getRoleNameAndUserName(userid);
                bodyitem.Add(UNDN[2]+"("+ UNDN[1]+")");
                DataTable dt = new BLL.T_Call_CallRecords().GetList("UserId='" + userid + "'" + time + "and CallType=0 and CallState=1").Tables[0];
                //1非常满意、2基本满意、3不满意、4未评价
                for (int j = 1; j <= 4; j++)
                {
                    string[] cp = getMYDCountAndPercent(dt, j);
                    bodyitem.AddRange(cp);
                }
                if (body.ContainsKey(UNDN[1]))
                {
                    body[UNDN[1]].Add(bodyitem);
                }
                else
                {
                    List<List<string>> bodylist = new List<List<string>>();
                    bodylist.Add(bodyitem);
                    body.Add(UNDN[1], bodylist);
                }
            }
            if (type ==0)
            {
                foreach (string key in body.Keys)
                {
                    DataRow dr = dtNew.NewRow();
                    int count = body[key].Count;
                    foreach (List<string> listiteam in body[key])
                    {
                        dr["坐席姓名"] = listiteam[0].ToString();
                        dr["非常满意"] = listiteam[1].ToString();
                        dr["非常满意占比"] = listiteam[2].ToString();
                        dr["基本满意"] = listiteam[3].ToString();
                        dr["基本满意占比"] = listiteam[4].ToString();
                        dr["不满意"] = listiteam[5].ToString();
                        dr["不满意占比"] = listiteam[6].ToString();
                        dr["未评价"] = listiteam[7].ToString();
                        dr["未评价占比"] = listiteam[8].ToString();
                    }
                    dtNew.Rows.Add(dr);
                }
            }
            else
            {
                foreach (string key in body.Keys)
                {
                    DataRow dr = dtNew.NewRow();
                    int count = body[key].Count;
                    foreach (List<string> listiteam in body[key])
                    {
                        dr["坐席姓名"] = listiteam[0].ToString();
                        dr["非常满意"] = listiteam[1].ToString();
                        dr["非常满意占比"] = string.Format("{0:f2}%", double.Parse(listiteam[2]) * 100); 
                        dr["基本满意"] = listiteam[3].ToString();
                        dr["基本满意占比"] = string.Format("{0:f2}%", double.Parse(listiteam[4]) * 100);
                        dr["不满意"] = listiteam[5].ToString();
                        dr["不满意占比"] = string.Format("{0:f2}%", double.Parse(listiteam[6]) * 100);
                        dr["未评价"] = listiteam[7].ToString();
                        dr["未评价占比"] = string.Format("{0:f2}%", double.Parse(listiteam[8]) * 100);
                    }
                    dtNew.Rows.Add(dr);
                }
            }
         
           
            return dtNew;
        }

        /// <summary>
        /// 通过UserId得到角色和姓名
        /// </summary>
        /// <param name="userid"></param>
        /// <returns></returns>
        private string[] getRoleNameAndUserName(int userid)
        {
            var user = new BLL.T_Sys_UserAccount().GetModel(userid);
            string[] resarr = new string[3];
            if (user != null)
            {
                var role = new BLL.T_Sys_RoleInfo().GetModel(user.F_RoleId);
                if (role != null)
                {
                    resarr[0] = role.F_RoleName;
                }
                resarr[1] = user.F_UserCode;
                //姓名会重复 但后面要显示姓名
                resarr[2] = user.F_UserName;
            }
            return resarr;
        }
        /// <summary>
        /// 每种满意度的个数和占比
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        private string[] getMYDCountAndPercent(DataTable dt, int type)
        {
            int allcount = dt.Rows.Count;
            int typecount = 0;
            if (type == 4) typecount = dt.Select("MYD IS NULL or myd=0").Count();
            else typecount = dt.Select("MYD=" + type).Count();
            string percent = "0.00";
            if (allcount > 0) percent = (typecount * 1.0 / allcount).ToString("0.00");
            return new string[] { typecount.ToString(), percent };
        }
    }
}