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_dx
{
    public class DXTelController : BaseController
    {
        //导入后获取
        public ActionResult GetList(int type,int? fileid)
        {
            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_insertUserID=" + userid.Trim();
                    
                }
                if (fileid != null)
                {
                    sql += " and F_Fileid=" + fileid;
                }
                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;
                string tbname = "";
                string tbfield = "";
                if (type == 0)
                {
                    tbname = "T_Vis_DXOTelphone";
                    tbfield = "F_DVoID";
                }
                else if (type == 1)
                {
                    tbname = "T_Vis_DXNTelphone";
                    tbfield = "F_DVNID";
                }

                dt = BLL.PagerBLL.GetListPager(
                    tbname,
                    tbfield,
                    "*",
                    " " + sql,
                    "ORDER BY "+ tbfield + " 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 hcstate,
             string hfstate, string starttime, string endtime, string jtstate, string hjjg,
             string zruser, int type, string hffl)
        {
            ActionResult res = Error("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string sql = "";
                DataTable dt = new DataTable();
                if (zruser.Trim() != "" && zruser.Trim() != "全部")
                {
                    sql += " and F_liableP='" + zruser.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 (hffl != null && hffl.Trim() != "")
                {
                    sql += " and F_VisType='" + hffl.Trim() + "' ";
                }
                if (key != null && key.Trim() != "")
                {
                    if (type == 0)
                    {
                        sql += " and (F_khMobile like '%" + key.Trim() + "%' or F_khPhone like '%" + key.Trim() + "%' ) ";
                    }
                    else if (type == 1)
                    {
                        sql += " and (F_Telphone like '%" + key.Trim() + "%' or F_MobilePhone like '%" + key.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;
                string tbname = "";
                string tbfield = "";
                if (type == 0)
                {
                    tbname = "T_Vis_DXOTelphone";
                    tbfield = "F_DVoID";
                }
                else if (type == 1)
                {
                    tbname = "T_Vis_DXNTelphone";
                    tbfield = "F_DVNID";
                }

                dt = BLL.PagerBLL.GetListPager(
                    tbname,
                    tbfield,
                    "*",
                    " " + sql,
                    "ORDER BY " + tbfield + " 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 hcstate,
             string hfstate, string starttime, string endtime, string jtstate, string hjjg, 
             string zruser, int type, string hffl)
        {
            ActionResult res = Error("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string sql = " 1=1 ";
                DataTable dt = new DataTable();
                if (zruser.Trim() != "" && zruser.Trim() != "全部")
                {
                    sql += " and F_liableP='" + zruser.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 (hffl != null && hffl.Trim() != "")
                {
                    sql += " and F_VisType='" + hffl.Trim() + "' ";
                }
                if (key != null && key.Trim() != "")
                {
                    if (type == 0)
                    {
                        sql += " and (F_khMobile like '%" + key.Trim() + "%' or F_khPhone like '%" + key.Trim() + "%' ) ";
                    }
                    else if (type == 1)
                    {
                        sql += " and (F_Telphone like '%" + key.Trim() + "%' or F_MobilePhone like '%" + key.Trim() + "%' ) ";
                    }
                }
                NPOIHelper npoi = new NPOIHelper();
                if (type == 0)
                    dt = new BLL.T_Vis_DXOTelphone().GetListExpt(sql).Tables[0];
                if(type==1)
                    dt = new BLL.T_Vis_DXNTelphone().GetListExpt(sql).Tables[0];
                if (npoi.ExportToExcel("回访结果数据", dt, col(type)) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }
            return res;
        }

        private string[] col(int type)
        {
            string[] ccc = {
            };
            if (type == 1)
            {
                string[] ccc1 ={ "序号","信息编号"," 车牌号","品牌","车系","车型","车主姓名","固定电话","移动电话",
                    "归属人","使用地","报备备注","客户提供信息","集团名称","客户类别","是否进店","进店意向程度",
                    "报备人","归属人","报备时间","信息完善率","信息状态","跟进时效",
                    "流失原因","处理方式","责任人","回访时间","回访内容","分类","回访人","导入时间","呼叫时间",
                    "呼叫状态","接通状态","回访结果"};
                ccc = ccc1;
            }
            if (type == 0)
            {
                string[] ccc1 ={ "办保养卡时间","保养卡类别","保养卡金额","电销维护","车辆常用地","等级","接车员","历史维修次",
                    "首次入厂日期","上次入厂里程","上次出厂日期","年份","承保公司","保险到期日","7月店面",
                    "系统车牌号","识别号","品牌","车型","联系人","客户手机","客户电话","预流失","工单","使用地","客户提供信息",
                    "流失原因","处理方式","责任人","回访时间","回访内容","分类","回访人","导入时间","呼叫时间",
                    "呼叫状态","接通状态","回访结果"};
                ccc = ccc1;
            }
            return ccc;
        }

        //获取我的回访任务
        public ActionResult GetMyList(string key,int type)
        {
            ActionResult res = Error("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string sql = "";
                DataTable dt = new DataTable();
                string username = CurrentUser.UserData.F_UserName;
                if (username.Trim() != "")
                {
                    sql += " and F_liableP='" + username.Trim() + "' ";
                }
                if (key != null && key.Trim() != "")
                {
                    if (type == 0)
                    {
                        sql += " and (F_khMobile like '%" + key.Trim() + "%' or F_khPhone like '%" + key.Trim() + "%' ) ";
                    }
                    else if (type == 1)
                    {
                        sql += " and (F_Telphone like '%" + key.Trim() + "%' or F_MobilePhone like '%" + key.Trim() + "%' ) ";
                    }
                }
                //sql += " and  isnull(F_ISCallRes,0)=0 "; //未接通
                //sql += " and isnull(F_CallRes,'')!='免回访' "; //免回访
                sql += " and isnull(F_ISVis,0)=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;
                string tbname = "";
                string tbfield = "";
                if (type == 0)
                {
                    tbname = "T_Vis_DXOTelphone";
                    tbfield = "F_DVoID";
                }
                else if (type == 1)
                {
                    tbname = "T_Vis_DXNTelphone";
                    tbfield = "F_DVNID";
                }

                dt = BLL.PagerBLL.GetListPager(
                    tbname,
                    tbfield,
                    "*",
                    " " + sql,
                    "ORDER BY " + tbfield + " 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, 
             int type, string hffl)
        {
            ActionResult res = Error("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string sql = "";
                DataTable dt = new DataTable();
                string username = CurrentUser.UserData.F_UserName;
                if (username != null && username.Trim() != "")
                {
                    sql += " and F_liableP='" + username.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 (hffl != null && hffl.Trim() != "")
                {
                    sql += " and F_VisType='" + hffl.Trim() + "' ";
                }
                if (key != null && key.Trim() != "")
                {
                    if (type == 0)
                    {
                        sql += " and (F_khMobile like '%" + key.Trim() + "%' or F_khPhone like '%" + key.Trim() + "%' ) ";
                    }
                    else if (type == 1)
                    {
                        sql += " and (F_Telphone like '%" + key.Trim() + "%' or F_MobilePhone like '%" + key.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;
                string tbname = "";
                string tbfield = "";
                if (type == 0)
                {
                    tbname = "T_Vis_DXOTelphone";
                    tbfield = "F_DVoID";
                }
                else if (type == 1)
                {
                    tbname = "T_Vis_DXNTelphone";
                    tbfield = "F_DVNID";
                }

                dt = BLL.PagerBLL.GetListPager(
                    tbname,
                    tbfield,
                    "*",
                    " " + sql,
                    "ORDER BY " + tbfield + " desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

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

                res = Content(obj.ToJson());
            }
            return res;
        }
        //我的回访结果导出
        public ActionResult GetMyResultExpt(string key, string hcstate,
             string hfstate, string starttime, string endtime, string jtstate, string hjjg, 
             int type, string hffl)
        {
            ActionResult res = Error("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string sql = " 1=1 ";
                DataTable dt = new DataTable();
                string username = CurrentUser.UserData.F_UserName;
                if (username != null && username.Trim() != "")
                {
                    sql += " and F_liableP='" + username.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 (hffl != null && hffl.Trim() != "")
                {
                    sql += " and F_VisType='" + hffl.Trim() + "' ";
                }
                if (key != null && key.Trim() != "")
                {
                    if (type == 0)
                    {
                        sql += " and (F_khMobile like '%" + key.Trim() + "%' or F_khPhone like '%" + key.Trim() + "%' ) ";
                    }
                    else if (type == 1)
                    {
                        sql += " and (F_Telphone like '%" + key.Trim() + "%' or F_MobilePhone like '%" + key.Trim() + "%' ) ";
                    }
                }
                NPOIHelper npoi = new NPOIHelper();
                if (type == 0)
                    dt = new BLL.T_Vis_DXOTelphone().GetListExpt(sql).Tables[0];
                if (type == 1)
                    dt = new BLL.T_Vis_DXNTelphone().GetListExpt(sql).Tables[0];
                if (npoi.ExportToExcel("我的回访结果数据", dt, col(type)) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }
            return res;
        }
    }
}