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

        //获取店面报表
        public ActionResult GetDPReport(string starttime,string endtime)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
               
                    BLL.T_Vis_Telphone telBLL = new BLL.T_Vis_Telphone();
                    DataTable dtNew = new DataTable();
                    dtNew = getDPData(starttime, endtime);
                    #region
                    //#region 编辑表头
                    //DataColumn dc1 = new DataColumn("大区", Type.GetType("System.String"));
                    //DataColumn dc2 = new DataColumn("店面", Type.GetType("System.String"));
                    //DataColumn dc3 = new DataColumn("数据提供", Type.GetType("System.String"));
                    //DataColumn dc4 = new DataColumn("采集样本", Type.GetType("System.String"));
                    //DataColumn dc5 = new DataColumn("店面得分", Type.GetType("System.String"));

                    //dtNew.Columns.Add(dc1);
                    //dtNew.Columns.Add(dc2);
                    //dtNew.Columns.Add(dc3);
                    //dtNew.Columns.Add(dc4);
                    //dtNew.Columns.Add(dc5);
                    //#endregion

                    //DataTable dt1;
                    //DataTable dt;
                    //string sql = "";
                    //sql = getsql("1", starttime, endtime);
                    //dt = GetDP(starttime, endtime);
                    //dt1 = telBLL.GetListDQ(" 1=1 " + sql).Tables[0];
                    //for (int i = 0; i < dt1.Rows.Count; i++)
                    //{
                    //    DataRow drNew = dtNew.NewRow();
                    //    string score = "0";
                    //    string yscont = "0";
                    //    for (int j = 0; j < dt.Rows.Count; j++)
                    //    {
                    //        if (dt1.Rows[i]["F_Store"].ToString().Equals(dt.Rows[j]["F_Store"].ToString()))
                    //        {
                    //            score = dt.Rows[j]["totals"].ToString();
                    //            yscont = dt.Rows[j]["yscont"].ToString();
                    //            break;
                    //        }
                    //    }
                    //    decimal avg = 0;
                    //    if (yscont != "" && yscont != "0")
                    //    {
                    //        if (score != "")
                    //        {
                    //            avg = decimal.Parse(score) / decimal.Parse(yscont);
                    //        }
                    //    }
                    //    drNew["大区"] = dt1.Rows[i]["F_Sheng"];
                    //    drNew["店面"] = dt1.Rows[i]["F_Store"];
                    //    drNew["数据提供"] = dt1.Rows[i]["c"];
                    //    drNew["采集样本"] = yscont;
                    //    drNew["店面得分"] = avg.ToString("#0.00");
                    //    dtNew.Rows.Add(drNew);
                    //}
                    #endregion
                    res = Success("获取店面得分统计成功", dtNew);
              
            }
            return res;
        }
        //获取接车员报表
        public ActionResult GetJCYReport(string starttime, string endtime)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
               
                    BLL.T_Vis_Telphone telBLL = new BLL.T_Vis_Telphone();
                    DataTable dtNew = new DataTable();
                    dtNew = getJCYData(starttime, endtime);
                    #region
                    //#region 编辑表头
                    //DataColumn dc1 = new DataColumn("接车员", Type.GetType("System.String"));
                    //DataColumn dc2 = new DataColumn("数据提供", Type.GetType("System.String"));
                    //DataColumn dc3 = new DataColumn("采集样本", Type.GetType("System.String"));
                    //DataColumn dc4 = new DataColumn("接车员得分", Type.GetType("System.String"));

                    //dtNew.Columns.Add(dc1);
                    //dtNew.Columns.Add(dc2);
                    //dtNew.Columns.Add(dc3);
                    //dtNew.Columns.Add(dc4);
                    //#endregion

                    //DataTable dt1;
                    //DataTable dt;
                    //string sql = "";
                    //sql = getsql("2", starttime, endtime);
                    //dt1 = telBLL.GetListGW(" 1=1 " + sql).Tables[0];
                    //dt = GetGW(starttime, endtime);
                    //for (int i = 0; i < dt1.Rows.Count; i++)
                    //{
                    //    DataRow drNew = dtNew.NewRow();
                    //    string score = "0";
                    //    string yscont = "0";
                    //    if (dt1.Rows[i]["F_Reciver"].ToString() != "")
                    //    {
                    //        for (int j = 0; j < dt.Rows.Count; j++)
                    //        {
                    //            if (dt1.Rows[i]["F_Reciver"].ToString().Equals(dt.Rows[j]["F_Reciver"].ToString()))
                    //            {
                    //                score = dt.Rows[j]["totals"].ToString();
                    //                yscont = dt.Rows[j]["yscont"].ToString();
                    //                break;
                    //            }
                    //        }
                    //        decimal avg = 0;
                    //        if (yscont != "" && yscont != "0")
                    //        {
                    //            if (score != "")
                    //            {
                    //                avg = decimal.Parse(score) / decimal.Parse(yscont);
                    //            }
                    //        }
                    //        drNew["接车员"] = dt1.Rows[i]["F_Reciver"];
                    //        drNew["数据提供"] = dt1.Rows[i]["c"];
                    //        drNew["采集样本"] = yscont;
                    //        drNew["接车员得分"] = avg.ToString("#0.00");
                    //        dtNew.Rows.Add(drNew);

                    //    }
                    //}
                    #endregion
                    res = Success("获取店面得分统计成功", dtNew);
             
            }
            return res;
        }
        //获取班组报表
        public ActionResult GetBZReport(string starttime, string endtime)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
             
                    BLL.T_Vis_Telphone telBLL = new BLL.T_Vis_Telphone();
                    DataTable dtNew = new DataTable();
                    dtNew = getBZData(starttime, endtime);
                    #region
                    //#region 编辑表头
                    //DataColumn dc1 = new DataColumn("大区", Type.GetType("System.String"));
                    //DataColumn dc2 = new DataColumn("分店", Type.GetType("System.String"));
                    //DataColumn dc3 = new DataColumn("班组", Type.GetType("System.String"));
                    //DataColumn dc4 = new DataColumn("数据提供", Type.GetType("System.String"));
                    //DataColumn dc5 = new DataColumn("采集样本", Type.GetType("System.String"));
                    //DataColumn dc6 = new DataColumn("接车员得分", Type.GetType("System.String"));

                    //dtNew.Columns.Add(dc1);
                    //dtNew.Columns.Add(dc2);
                    //dtNew.Columns.Add(dc3);
                    //dtNew.Columns.Add(dc4);
                    //dtNew.Columns.Add(dc5);
                    //dtNew.Columns.Add(dc6);
                    //#endregion

                    //DataTable dt1;
                    //DataTable dt;
                    //string sql = "";
                    //sql = getsql("2", starttime, endtime);
                    //dt1 = telBLL.GetListBZ(" 1=1 " + sql).Tables[0];
                    //dt = GetBZ(starttime, endtime);
                    //for (int i = 0; i < dt1.Rows.Count; i++)
                    //{
                    //    DataRow drNew = dtNew.NewRow();
                    //    string score = "0";
                    //    string yscont = "0";
                    //    if (dt1.Rows[i]["F_Reciver"].ToString() != "")
                    //    {
                    //        for (int j = 0; j < dt.Rows.Count; j++)
                    //        {
                    //            if (dt1.Rows[i]["F_Leader"].ToString().Equals(dt.Rows[j]["F_Leader"].ToString()))
                    //            {
                    //                score = dt.Rows[j]["totals"].ToString();
                    //                yscont = dt.Rows[j]["yscont"].ToString();
                    //                break;
                    //            }
                    //        }
                    //        decimal avg = 0;
                    //        if (yscont != "" && yscont != "0")
                    //        {
                    //            if (score != "")
                    //            {
                    //                avg = decimal.Parse(score) / decimal.Parse(yscont);
                    //            }
                    //        }

                    //        drNew["大区"] = dt1.Rows[i]["F_Sheng"];
                    //        drNew["分店"] = dt1.Rows[i]["F_Store"];
                    //        drNew["班组"] = dt1.Rows[i]["F_Leader"];
                    //        drNew["数据提供"] = dt1.Rows[i]["c"];
                    //        drNew["采集样本"] = yscont;
                    //        drNew["接车员得分"] = avg.ToString("#0.00");
                    //        dtNew.Rows.Add(drNew);

                    //    }
                    //}
                    #endregion
                    res = Success("获取班组得分统计成功", dtNew);
            
            }
            return res;
        }


        //导出店面数据
        public ActionResult ExptDPList(string starttime, string endtime)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                NPOIHelper npoi = new NPOIHelper();
                DataTable dt = getDPData(starttime, endtime);
                if (npoi.ExportToExcel("店面得分数据", dt) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }
            return res;
        }
        //导出接车员数据
        public ActionResult ExptJCYList(string starttime, string endtime)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                NPOIHelper npoi = new NPOIHelper();
                DataTable dt = getJCYData(starttime, endtime);
                if (npoi.ExportToExcel("接车员得分数据", dt) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }
            return res;
        }
        //导出班组数据
        public ActionResult ExptBZList(string starttime, string endtime)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                NPOIHelper npoi = new NPOIHelper();
                DataTable dt = getBZData(starttime, endtime);
                if (npoi.ExportToExcel("班组得分数据", dt) == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }
            return res;
        }

        private DataTable getDPData(string starttime, string endtime)
        {
            DataTable dtNew = new DataTable();
            BLL.T_Vis_Telphone telBLL = new BLL.T_Vis_Telphone();
            #region 编辑表头
            DataColumn dc1 = new DataColumn("大区", Type.GetType("System.String"));
            DataColumn dc2 = new DataColumn("店面", Type.GetType("System.String"));
            DataColumn dc3 = new DataColumn("数据提供", Type.GetType("System.String"));
            DataColumn dc4 = new DataColumn("采集样本", Type.GetType("System.String"));
            DataColumn dc5 = new DataColumn("店面得分", Type.GetType("System.String"));

            dtNew.Columns.Add(dc1);
            dtNew.Columns.Add(dc2);
            dtNew.Columns.Add(dc3);
            dtNew.Columns.Add(dc4);
            dtNew.Columns.Add(dc5);
            #endregion

            DataTable dt1;
            DataTable dt;
            string sql = "";
            sql = getsql("1", starttime, endtime);
            dt = GetDP(starttime, endtime);
            dt1 = telBLL.GetListDQ(" 1=1 " + sql).Tables[0];
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                DataRow drNew = dtNew.NewRow();
                string score = "0";
                string yscont = "0";
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    if (dt1.Rows[i]["F_Store"].ToString().Equals(dt.Rows[j]["F_Store"].ToString()))
                    {
                        score = dt.Rows[j]["totals"].ToString();
                        yscont = dt.Rows[j]["yscont"].ToString();
                        break;
                    }
                }
                decimal avg = 0;
                if (yscont != "" && yscont != "0")
                {
                    if (score != "")
                    {
                        avg = decimal.Parse(score) / decimal.Parse(yscont);
                    }
                }
                drNew["大区"] = dt1.Rows[i]["F_Sheng"];
                drNew["店面"] = dt1.Rows[i]["F_Store"];
                drNew["数据提供"] = dt1.Rows[i]["c"];
                drNew["采集样本"] = yscont;
                drNew["店面得分"] = avg.ToString("#0.00");
                dtNew.Rows.Add(drNew);
            }
            return dtNew;
        }
        private DataTable getJCYData(string starttime, string endtime)
        {
            DataTable dtNew = new DataTable();
            BLL.T_Vis_Telphone telBLL = new BLL.T_Vis_Telphone();
            #region 编辑表头
            DataColumn dc1 = new DataColumn("接车员", Type.GetType("System.String"));
            DataColumn dc2 = new DataColumn("数据提供", Type.GetType("System.String"));
            DataColumn dc3 = new DataColumn("采集样本", Type.GetType("System.String"));
            DataColumn dc4 = new DataColumn("接车员得分", Type.GetType("System.String"));

            dtNew.Columns.Add(dc1);
            dtNew.Columns.Add(dc2);
            dtNew.Columns.Add(dc3);
            dtNew.Columns.Add(dc4);
            #endregion

            DataTable dt1;
            DataTable dt;
            string sql = "";
            sql = getsql("2", starttime, endtime);
            dt1 = telBLL.GetListGW(" 1=1 " + sql).Tables[0];
            dt = GetGW(starttime, endtime);
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                DataRow drNew = dtNew.NewRow();
                string score = "0";
                string yscont = "0";
                if (dt1.Rows[i]["F_Reciver"].ToString() != "")
                {
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        if (dt1.Rows[i]["F_Reciver"].ToString().Equals(dt.Rows[j]["F_Reciver"].ToString()))
                        {
                            score = dt.Rows[j]["totals"].ToString();
                            yscont = dt.Rows[j]["yscont"].ToString();
                            break;
                        }
                    }
                    decimal avg = 0;
                    if (yscont != "" && yscont != "0")
                    {
                        if (score != "")
                        {
                            avg = decimal.Parse(score) / decimal.Parse(yscont);
                        }
                    }
                    drNew["接车员"] = dt1.Rows[i]["F_Reciver"];
                    drNew["数据提供"] = dt1.Rows[i]["c"];
                    drNew["采集样本"] = yscont;
                    drNew["接车员得分"] = avg.ToString("#0.00");
                    dtNew.Rows.Add(drNew);

                }
            }
            return dtNew;
        }
        private DataTable getBZData(string starttime, string endtime)
        {
            DataTable dtNew = new DataTable();
            BLL.T_Vis_Telphone telBLL = new BLL.T_Vis_Telphone();
            #region 编辑表头
            DataColumn dc1 = new DataColumn("大区", Type.GetType("System.String"));
            DataColumn dc2 = new DataColumn("分店", Type.GetType("System.String"));
            DataColumn dc3 = new DataColumn("班组", Type.GetType("System.String"));
            DataColumn dc4 = new DataColumn("数据提供", Type.GetType("System.String"));
            DataColumn dc5 = new DataColumn("采集样本", Type.GetType("System.String"));
            DataColumn dc6 = new DataColumn("接车员得分", Type.GetType("System.String"));

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

            DataTable dt1;
            DataTable dt;
            string sql = "";
            sql = getsql("3", starttime, endtime);
            dt1 = telBLL.GetListBZ(" 1=1 " + sql).Tables[0];
            dt = GetBZ(starttime, endtime);
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                DataRow drNew = dtNew.NewRow();
                string score = "0";
                string yscont = "0";
                if (dt1.Rows[i]["F_Leader"].ToString() != "")
                {
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        if (dt1.Rows[i]["F_Leader"].ToString().Equals(dt.Rows[j]["F_Leader"].ToString()))
                        {
                            score = dt.Rows[j]["totals"].ToString();
                            yscont = dt.Rows[j]["yscont"].ToString();
                            break;
                        }
                    }
                    decimal avg = 0;
                    if (yscont != "" && yscont != "0")
                    {
                        if (score != "")
                        {
                            avg = decimal.Parse(score) / decimal.Parse(yscont);
                        }
                    }

                    drNew["大区"] = dt1.Rows[i]["F_Sheng"];
                    drNew["分店"] = dt1.Rows[i]["F_Store"];
                    drNew["班组"] = dt1.Rows[i]["F_Leader"];
                    drNew["数据提供"] = dt1.Rows[i]["c"];
                    drNew["采集样本"] = yscont;
                    drNew["接车员得分"] = avg.ToString("#0.00");
                    dtNew.Rows.Add(drNew);

                }
            }
            return dtNew;
        }

        //店面得分 
        private DataTable GetDP(string stime, string etime)
        {
            DataSet ds = null;
            string sql = getsql("1", stime, etime);

            ds = new BLL.T_Vis_Telphone().GetListDQYX(sql);
            if (ds.Tables[0].Rows.Count > 0)
                return ds.Tables[0];
            else
                return null;
        }
        //服务顾问得分
        private DataTable GetGW(string stime, string etime)
        {
            DataSet ds = null;
            string sql = getsql("2", stime, etime);

            ds = new BLL.T_Vis_Telphone().GetListGWYX(sql);
            if (ds.Tables[0].Rows.Count > 0)
                return ds.Tables[0];
            else
                return null;
        }
        //班组得分
        private DataTable GetBZ(string stime, string etime)
        {
            DataSet ds = null;
            //string sql = getsql("3", stime, etime);
            string sql = " 1=1 ";

            string sqlw = "  and F_CallRes!='免回访' ";
            if (stime != "")
                sqlw += " and DATEDIFF(day,F_VisDate,'" + stime + "')<=0 ";
            if (etime != "")
                sqlw += " and DATEDIFF(day,F_VisDate,'" + etime + "')>=0";
            sql += " and F_Telid in (select F_TelID from T_Vis_Telphone where F_ISVis = 1 " + sqlw + ")";

            ds = new BLL.T_Vis_Telphone().GetListBZYX(sql);
            if (ds.Tables[0].Rows.Count > 0)
                return ds.Tables[0];
            else
                return null;
        }

        private string getsql(string type, string stime, string etime)
        {
            string sql = "  ";
            if (type == "3")
            {
                string sqlw = " 1=1 and F_CallRes!='免回访' ";
                if (stime != "")
                    sqlw += " and DATEDIFF(day,F_VisDate,'" + stime + "')<=0 ";
                if (etime != "")
                    sqlw += " and DATEDIFF(day,F_VisDate,'" + etime + "')>=0";
                sql += " and F_Telid in (select F_TelID from T_Vis_Telphone where " + sqlw + ")";
            }
            else
            {
                if (stime != "")
                    sql += " and DATEDIFF(day,F_VisDate,'" + stime + "')<=0 ";
                if (etime != "")
                    sql += " and DATEDIFF(day,F_VisDate,'" + etime + "')>=0";
                sql += " and F_CallRes!='免回访' ";
            }

            return sql;
        }
    }
}