using CallCenter.Utility;
using CallCenterApi.Common;
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 VisitTelphoneController : BaseController
    {
        //导入后获取
        public ActionResult GetList(int? fileid,string store)
        {
            ActionResult res = Error("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string sql = "";
                DataTable dt = new DataTable();
                string userid = CurrentUser.UserData.F_UserId.ToString();
                if (userid.Trim() != "")
                {
                    sql += " and F_InputUserID=" + userid.Trim();
                }
                if (fileid != null)
                {
                    sql += " and F_Fileid=" + fileid;
                }
                if (store != null && store.Trim() != "")
                {
                    sql += " and F_Store like '%" + store.Trim() + "%'";
                }
                //sql += " and F_InputDate=(select MAX(F_InputDate)from T_Vis_Telphone)";
                string strpageindex = RequestString.GetQueryString("page");
                int pageindex = 1;
                string strpagesize = RequestString.GetQueryString("pagesize");
                int pagesize = 10;
                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;
        }

        //回访结果查看
        public ActionResult GetResultList(string key, string userid, string fpstate, string hcstate,
             string hfstate, string starttime, string endtime, string jtstate, string hjjg, 
             string prosdate, string proedate,string qtype)
        {
            ActionResult res = Error("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string sql = "";
                DataTable dt = new DataTable();
                //F_Prodate 出厂日期
                if (jtstate != null && jtstate.Trim() != "")
                {
                    sql += " and F_ISCall=" + jtstate.Trim() + " ";
                }
                if (hjjg != null && hjjg.Trim() != "")
                {
                    sql += " and F_CallResID=" + hjjg.Trim() + " ";
                }
                if (starttime != null && starttime.Trim() != "")
                {
                    sql += " and datediff(day,F_VisDate,'" + starttime + "')<=0 ";
                }
                if (endtime != null && endtime.Trim() != "")
                {
                    sql += " and datediff(day,F_VisDate,'" + endtime + "')>=0 ";
                }
                if (fpstate != null && fpstate.Trim() != "")
                {
                    sql += " and F_ISAllot=" + fpstate.Trim() + " ";
                }
                if (hcstate != null && hcstate.Trim() != "")
                {
                    sql += " and F_ISCall=" + hcstate.Trim() + " ";
                }
                if (hfstate != null && hfstate.Trim() != "")
                {
                    sql += " and F_ISVis=" + hfstate.Trim() + " ";
                }
                if (userid != null && userid.Trim() != "")
                {
                    sql += " and F_AllotZXID=" + userid.Trim() + " ";
                }
                if (key != null && key.Trim() != "")
                {
                    sql += " and (F_CusTelphone like '%" + key.Trim() + "%' or F_CusPhone like '%" + key.Trim() + "%' ) ";
                }
                if (prosdate != null && prosdate.Trim() != "")
                {
                    sql += " and datediff(day,F_Prodate,'" + prosdate.Trim() + "')<=0";
                }
                if (proedate != null && proedate.Trim() != "")
                {
                    sql += " and datediff(day,F_Prodate,'" + proedate.Trim() + "')>=0";
                }
                if (qtype != null && qtype.Trim() != "")
                {
                    sql += " and F_QuestionTypeID=" + qtype.Trim();
                }
                string strpageindex = RequestString.GetQueryString("page");
                int pageindex = 1;
                string strpagesize = RequestString.GetQueryString("pagesize");
                int pagesize = 10;
                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
                {
                    state = "success",
                    message = "成功",
                    rows = dt,
                    total = recordCount
                };

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

        //回访结果导出
        public ActionResult GetResultExpt(string key, string userid, string fpstate, string hcstate,
             string hfstate, string starttime, string endtime, string jtstate, string hjjg, 
             string prosdate, string proedate, string qtype)
        {
            ActionResult res = Error("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string sql = " 1=1 ";
                DataTable dt = new DataTable();

                if (jtstate != null && jtstate.Trim() != "")
                {
                    sql += " and F_ISCall=" + jtstate.Trim() + " ";
                }
                if (hjjg != null && hjjg.Trim() != "")
                {
                    sql += " and F_CallResID=" + hjjg.Trim() + " ";
                }
                if (starttime != null && starttime.Trim() != "")
                {
                    sql += " and datediff(day,F_VisDate,'" + starttime + "')<=0 ";
                }
                if (endtime != null && endtime.Trim() != "")
                {
                    sql += " and datediff(day,F_VisDate,'" + endtime + "')>=0 ";
                }
                if (fpstate != null && fpstate.Trim() != "")
                {
                    sql += " and F_ISAllot=" + fpstate.Trim() + " ";
                }
                if (hcstate != null && hcstate.Trim() != "")
                {
                    sql += " and F_ISCall=" + hcstate.Trim() + " ";
                }
                if (hfstate != null && hfstate.Trim() != "")
                {
                    sql += " and F_ISVis=" + hfstate.Trim() + " ";
                }
                if (userid != null && userid.Trim() != "")
                {
                    sql += " and F_AllotZXID=" + userid.Trim() + " ";
                }
                if (key != null && key.Trim() != "")
                {
                    sql += " and (F_CusTelphone like '%" + key.Trim() + "%' or F_CusPhone like '%" + key.Trim() + "%' ) ";
                }
                if (prosdate != null && prosdate.Trim() != "")
                {
                    sql += " and datediff(day,F_Prodate,'" + prosdate.Trim() + "')<=0";
                }
                if (proedate != null && proedate.Trim() != "")
                {
                    sql += " and datediff(day,F_Prodate,'" + proedate.Trim() + "')>=0";
                }
                if (qtype != null && qtype.Trim() != "")
                {
                    sql += " and F_QuestionTypeID=" + qtype.Trim();
                }
                NPOIHelper npoi = new NPOIHelper();
                //dt = getData(stime, endtime, dpt, dayss);
                dt = new BLL.T_Vis_Telphone().GetListExpt(sql).Tables[0];
                if (npoi.ExportToExcel("回访结果数据", dt, col()) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }
            return res;
        }

        private string[] col()
        {
            string[] ccc = {
                  "大区", "店面", "出厂日期", "客户名称", "车牌号码", "品牌", "车型", "接车员","手机"
                  , "固定电话", "维修类别", "调查日期", "班组得分", "接车员得分", "环检", "整体", "合计"
                  , "备注", "回访结果", "详细内容", "是否成功", "维修项目", "班组", "机电", "钣金"
                , "喷漆", "维修工", "工单号", "备注", "分配坐席", "导入时间", "呼叫时间", "回访时间"
                ,"呼叫状态","接通状态","呼叫结果","回访状态"
            };
            return ccc;
        }

        //获取我的回访任务
        public ActionResult GetMyList(string key, string prosdate, string proedate, string qtype)
        {
            ActionResult res = Error("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string sql = "";
                DataTable dt = new DataTable();
                string userid = CurrentUser.UserData.F_UserId.ToString();
                if (userid.Trim() != "")
                {
                    sql += " and F_AllotZXID=" + userid.Trim();
                }
                if (key!=null && key.Trim() != "")
                {
                    sql += " and (F_CusTelphone like '%" + key.Trim() + "%' or F_CusPhone like '%" + key.Trim() + "%' ) ";
                }
                if(prosdate != null && prosdate.Trim()!="")
                {
                    sql += " and datediff(day,F_Prodate,'" + prosdate.Trim() + "')<=0";
                }
                if (proedate != null && proedate.Trim() != "")
                {
                    sql += " and datediff(day,F_Prodate,'" + proedate.Trim() + "')>=0";
                }
                if (qtype != null && qtype.Trim() != "")
                {
                    sql += " and F_QuestionTypeID=" + qtype.Trim();
                }
                sql += " and F_ISAllot=1 ";//已分配
                sql += " and  F_ISCallRes=0 "; //未接通
                sql += " and isnull(F_CallRes,'')!='免回访' "; //免回访
                sql += " and F_ISVis=0";//未回访
                string strpageindex = RequestString.GetQueryString("page");
                int pageindex = 1;
                string strpagesize = RequestString.GetQueryString("pagesize");
                int pagesize = 10;
                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;
        }

        //获取我的回访结果
        public ActionResult GetMyResultList(string key, string hcstate,
             string hfstate, string starttime, string endtime, string jtstate, string hjjg,
             string prosdate, string proedate, string qtype)
        {
            ActionResult res = Error("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string sql = "";
                DataTable dt = new DataTable();
                string userid = CurrentUser.UserData.F_UserId.ToString();
                if (userid.Trim() != "")
                {
                    sql += " and F_AllotZXID=" + userid.Trim();
                }
                if (key != null && key.Trim() != "")
                {
                    sql += " and (F_CusTelphone like '%" + key.Trim() + "%' or F_CusPhone like '%" + key.Trim() + "%' ) ";
                }
                if (jtstate != null && jtstate.Trim() != "")
                {
                    sql += " and F_ISCallRes=" + jtstate.Trim() + " ";
                }
                if (hjjg != null && hjjg.Trim() != "")
                {
                    sql += " and F_CallResID=" + hjjg.Trim() + " ";
                }
                if (starttime != null && starttime.Trim() != "")
                {
                    sql += " and datediff(day,F_VisDate,'" + starttime + "')<=0 ";
                }
                if (endtime != null && endtime.Trim() != "")
                {
                    sql += " and datediff(day,F_VisDate,'" + endtime + "')>=0 ";
                }
                if (hcstate != null && hcstate.Trim() != "")
                {
                    sql += " and F_ISCall=" + hcstate.Trim() + " ";
                }
                if (hfstate != null && hfstate.Trim() != "")
                {
                    sql += " and F_ISVis=" + hfstate.Trim() + " ";
                }
                if (prosdate != null && prosdate.Trim() != "")
                {
                    sql += " and datediff(day,F_Prodate,'" + prosdate.Trim() + "')<=0";
                }
                if (proedate != null && proedate.Trim() != "")
                {
                    sql += " and datediff(day,F_Prodate,'" + proedate.Trim() + "')>=0";
                }
                if (qtype != null && qtype.Trim() != "")
                {
                    sql += " and F_QuestionTypeID="+ qtype.Trim();
                }
                sql += " and F_ISAllot=1 ";
                string strpageindex = RequestString.GetQueryString("page");
                int pageindex = 1;
                string strpagesize = RequestString.GetQueryString("pagesize");
                int pagesize = 10;
                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;
        }

        //获取坐席列表
        public ActionResult GetSeatList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                DataTable dt = new DataTable();
                string sql = " ";
                try {
                    sql = " and f_seatflag=1 ";
                    int roleid = CurrentUser.UserData.F_RoleId;
                    int deptid = CurrentUser.UserData.F_DeptId;
                    if (roleid != 0)
                    {
                        if (roleid != 17)
                        {
                            if (deptid != 0)
                                sql += " and F_DeptId=" + deptid;
                        }
                    }
                    string strpageindex = RequestString.GetQueryString("page");
                    int pageindex = 1;
                    string strpagesize = RequestString.GetQueryString("pagesize");
                    int pagesize = 10;
                    if (strpageindex.Trim() != "")
                    {
                        pageindex = Convert.ToInt32(strpageindex);
                    }

                    if (strpagesize.Trim() != "")
                    {
                        pagesize = Convert.ToInt32(strpagesize);
                    }
                    int recordCount = 0;
                    dt = BLL.PagerBLL.GetListPager(
                                            "T_Sys_UserAccount",
                                            "F_UserId",
                                            "*",
                                            sql,
                                            "ORDER BY F_UserId desc",
                                            pagesize,
                                            pageindex,
                                            true,
                                            out recordCount);
                    DataTable dtCount = new BLL.T_Vis_TelAllot().GetList(" F_state=1 and F_HFTelID in (select F_TelID from T_Vis_Telphone where  F_ISAllot=1 and F_ISCallRes=0 ) ").Tables[0];
                    List<Model.UserAccount> modelList = new BLL.UserAccount().DataTableToList(dt);

                    var obj = new
                    {
                        rows = modelList.Select(x => new
                        {
                            UserCode = x.F_UserCode,
                            UserName = x.F_UserName,
                            WWCCount = dtCount.Select(" F_AllotZXID=" + x.F_UserId).Count()
                        }),
                        total = recordCount
                    };

                    res = Content(obj.ToJson());
                }
                catch (Exception ex) { }
                
            }
            return res;
        }
    }
}