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 DXReportsController : BaseController
    {

        //获取报表
        public ActionResult GetList(string telphone, int? type, string starttime, string endtime)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                DataTable dt = getdata(type, telphone, starttime, endtime);
                

                res = Success("获取报表成功", dt);
            }
            return res;
        }
        //报表导出
        public ActionResult ExptDPList(string telphone, int? type, string starttime, string endtime)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                NPOIHelper npoi = new NPOIHelper();
                DataTable dt = getdata(type, telphone, starttime, endtime);
                if (npoi.ExportToExcel("报表数据", dt, col(type)) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }
            return res;
        }

        private DataTable getdata(int? type, string telphone, string starttime, string endtime)
        {
            DataTable dtNew = new DataTable();
            string sql = "";
            sql= getsql(type, telphone, starttime, endtime);
            if (type != null)
            {
                if (type == 0)
                {
                    dtNew = new BLL.T_Vis_DXTelRecords().GetListOExpt(sql).Tables[0];
                }
                if (type == 1)
                {
                    dtNew = new BLL.T_Vis_DXTelRecords().GetListNExpt(sql).Tables[0];
                }
            }

            return dtNew;
        }
        private string[] col(int? type)
        {
            if (type != null)
            {
                if (type == 0)
                {
                    string[] ccc1 = { "车牌", "品牌", "车型", "客户类别", "责任人", "回访情况", "分类", "回访时间", "接通状态", "通话时长" };
                    return ccc1;
                }
                if (type == 1)
                {
                    string[] ccc = { "车牌", "品牌", "车型", "客户类别", "责任人", "回访情况", "分类", "回访时间", "接通状态", "通话时长" };
                    return ccc;
                }
            }
            return null;
        }

        private string getsql(int? type, string telphone, string starttime, string endtime)
        {
            string sqlwhere = "";string sqlcallwhere = "";
            if (type != null)
            {
                sqlwhere += " and r.F_HFType=" + type;
            }
            if (telphone != null && telphone.Trim() != "")
            {
                sqlwhere += " and r.F_HFTelphone like '%" + telphone.Trim() + "%'";
                sqlcallwhere += " and c.CallNumber like '%" + telphone.Trim() + "%'";
            }
            if (starttime != null && starttime != "")
            {
                sqlwhere += " and DateDiff(day,r.F_HFTime,'" + starttime.Trim() + "')<=0";
                sqlcallwhere += " and DateDiff(day,c.BeginTime,'" + starttime.Trim() + "')<=0";
            }
            if (endtime != null && endtime != "")
            {
                sqlwhere += " and DateDiff(day,r.F_HFTime,'" + endtime.Trim() + "')>=0";
                sqlcallwhere += " and DateDiff(day,c.BeginTime,'" + endtime.Trim() + "')>=0";
            }
            return sqlwhere + sqlcallwhere;
        }
    }
}