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 VisitAnalysisController : BaseController
    {
        BLL.T_Vis_Telphone telBLL = new BLL.T_Vis_Telphone();
        BLL.T_Sys_DictionaryValue dbll = new BLL.T_Sys_DictionaryValue();
        
        #region 店面问题客户图形分析
        public ActionResult GetDPQReport(string starttime, string endtime)
        {
                string[] dm; List<object> wt;
                
                getDPQData(starttime, endtime,out dm,out wt);
                var obj = new
                {
                    questype = wt,
                    store = dm,
                };
                return Success("获取店面问题客户分析成功", obj);
        }
        private void getDPQData(string starttime, string endtime, out string[] dm, out List<object> wt)
        {
            DataTable dt;
            string sql = "";
            sql = getsql(starttime, endtime);
            dt = telBLL.GetListDQQ(" 1=1 " + sql).Tables[0];
            List<Model.T_Sys_DictionaryValue> qtlist = dbll.GetModelList(" F_DictionaryFlag='WTKHFL' ");
            List<Model.T_Sys_DictionaryValue> dlist = dbll.GetModelList(" F_DictionaryFlag='HHBBMD' order by F_DictionaryValueId ");
            wt = new List<object>(qtlist.Count);
            dm = new string[dlist.Count];
            for (int i = 0; i < qtlist.Count; i++)
            {
                string qtid = qtlist[i].F_DictionaryValueId.ToString();
                string[] con = new string[dlist.Count];
                for (int j = 0; j < dlist.Count; j++)
                {
                    string conn = "0";
                    if (dt != null)
                    {
                        for (int z = 0; z < dt.Rows.Count; z++)
                        {
                            if (dlist[j].F_Name.Equals(dt.Rows[z]["F_Store"].ToString()) && qtid.Equals(dt.Rows[z]["F_QuestionTypeID"].ToString()))
                            {
                                conn = dt.Rows[z]["yscont"].ToString();
                                break;
                            }
                        }
                    }

                    con[j] = conn;
                }

                var obj = new
                {
                    questype = qtlist[i].F_Name,
                    typecont = con
                };
                wt.Add(obj);
            }
            for (int j = 0; j < dlist.Count; j++)
            {
                dm[j] = dlist[j].F_Name;
            }
        }
        #endregion
        #region 店面回访得分图形分析
        //获取店面报表
        public ActionResult GetDPReport(string starttime, string endtime)
        {
                string[] df; string[] pjf;string[] dm;
                getDPData(starttime, endtime,out dm, out df,out pjf);
                var obj = new {
                    store=dm,
                    score = df,
                    avgscore = pjf
                };
                return Success("获取店面得分分析成功", obj);
        }

        private void getDPData(string starttime, string endtime,out string[] dm,out string[] df,out string[] pjf)
        {
            DataTable dt;
            string sql = "";
            sql = getsql(starttime, endtime);
            dt = GetDP(starttime, endtime);
            
            List<Model.T_Sys_DictionaryValue> dlist = dbll.GetModelList(" F_DictionaryFlag='HHBBMD' order by F_DictionaryValueId ");
            int count = dlist.Count; decimal scores= 0, avgs = 0;
            df = new string[count];
            pjf = new string[count];
            dm = new string[count];
            for (int i = 0; i < count; i++)
            {
                string score = "0";
                string yscont = "0";
                if (dt != null)
                {
                    for (int j = 0; j < dt.Rows.Count; j++)
                    {
                        if (dlist[i].F_Name.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);
                    }
                }
                scores += avg;
                dm[i]= dlist[i].F_Name;
                df[i]= avg.ToString("#0.00");
            }
            if (count > 0)
                avgs = scores / decimal.Parse(count.ToString());

            for (int i = 0; i < pjf.Length; i++)
            {
                pjf[i] = avgs.ToString("#0.00");
            }
        }
        #endregion
        //店面得分 
        private DataTable GetDP(string stime, string etime)
        {
            DataSet ds = null;
            string sql = getsql(stime, etime);

            ds = telBLL.GetListDMDF(sql);
            if (ds.Tables[0].Rows.Count > 0)
                return ds.Tables[0];
            else
                return null;
        }
        private string getsql(string stime, string etime)
        {
            string sql = "  ";
                if (stime != null && stime != "")
                    sql += " and DATEDIFF(day,F_VisDate,'" + stime + "')<=0 ";
                if (etime != null && etime != "")
                    sql += " and DATEDIFF(day,F_VisDate,'" + etime + "')>=0";
                sql += " and F_CallRes!='免回访' ";

            return sql;
        }

    }
}