using Newtonsoft.Json;
using System;
using System.Data;
using System.Web;
using System.Web.Mvc;
using ZXDT.CallCenter.Model;
using ZXDT.CallCenter.MVCWeb.Models;

namespace ZXDT.CallCenter.MVCWeb.Controllers
{

    public class QualityManagementController : BaseController
    {
        //
        // GET: /QualityManagement/
        //通话记录
        BLL.T_Call_CallRecords_All recordBLL = new BLL.T_Call_CallRecords_All();
        //用户列表
        BLL.T_Sys_UserAccount userBLL = new BLL.T_Sys_UserAccount();
        //指标分类
        BLL.T_QC_IndexCategory qcindexBLL = new BLL.T_QC_IndexCategory();
        //列表分类
        BLL.T_QC_IndexBase qcindexbaseBLL = new BLL.T_QC_IndexBase();
        /// <summary>
        /// 初始化列表
        /// </summary>
        /// <returns></returns>
        /// 
         
          //指标分类
        public ActionResult TargetManagementList()
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            return View(model);
        }
        public ActionResult QCIndexEdit()
        {

            WorkOrderMyModel model = new WorkOrderMyModel();
            return View(model);
        }
        //指标列表
        public ActionResult QCIndexList()
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            return View(model);
        }
        //质检评分
        public ActionResult QualityTestScore()
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            Model.T_Sys_UserAccount viewModel = new Model.T_Sys_UserAccount();
            //获取用户列表
            viewModel.UserList = userBLL.GetModelList("F_RemindFlag=0");
            return View(viewModel);
        }
 
        //质检结果
    public ActionResult QcResultList()
        {
            WorkOrderMyModel model = new WorkOrderMyModel();
            return View(model);
        }

        //
        // GET: /QualityManagement/Details/5
        /// <summary>
        /// 获取通话记录
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("CallRecordData")]
        [HttpGet]
        public string CallRecordData(DateTime? NowDateTime, int page, int limit, string callnumber, string usercode, string callType, string callStatus,string userid)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();

            string sql = "  and FilePath<>''";

            if (!string.IsNullOrEmpty(callnumber))
            {
                sql += " and CallNumber like '%" + callnumber + "%'";
            }
            
            if (!string.IsNullOrEmpty(usercode))
            {
                sql += " and userid ='" + usercode + "'";
            }
            if (!string.IsNullOrEmpty(userid))
            {
                sql += " and userid ='" + userid + "'";
            }
            if (!string.IsNullOrEmpty(callType))
            {
                sql += " and CallType =" + callType;
            }
            if (!string.IsNullOrEmpty(callStatus))
            {
                sql += " and CallState =" + callStatus;
            }
            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Call_CallRecords_All> pageModel = new Model.PageData<Model.T_Call_CallRecords_All>();
            dt = BLL.PagerBLL.GetListPager(
                "T_Call_CallRecords",
                "CallRecordsId",
                @"*,CONVERT(varchar,BeginTime, 120 ) as BeginTimeNew,CONVERT(varchar,TalkStartTime, 120 ) as TalkStartTimeNew,CONVERT(varchar,TalkEndTime, 120 ) as TalkEndTimeNew,CONVERT(varchar,TalkStartTime, 120 ) as TalkStartTimeNew,CONVERT(varchar,TalkEndTime, 120 ) as TalkEndTimeNew, replace(replace(FilePath,'\','/'),'"+GetSysconfig("newFilePath") +"','"+ GetSysconfig("newPlayPath") + "') as fileurl",
                sql,
                "ORDER BY CallRecordsId desc",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;
            string json = JsonConvert.SerializeObject(dataModel);
            return json;
        }
        // GET: /QualityManagement/Details/5
        /// <summary>
        /// 获取质检结果
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <param name="sqlWhere">查询条件</param>
        /// <returns></returns>
        [ActionName("QCResultCallRecordData")]
        [HttpGet]
        public string QCResultCallRecordData(DateTime? NowDateTime, int page, int limit, string callnumber, string usercode, string callType, string callStatus, string userid)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();

            string sql = " and  F_QCState<>0 and FilePath<>''";

            if (!string.IsNullOrEmpty(callnumber))
            {
                sql += " and CallNumber like '%" + callnumber + "%'";
            }

            if (!string.IsNullOrEmpty(usercode))
            {
                sql += " and userid ='" + usercode + "'";
            }
            if (!string.IsNullOrEmpty(userid))
            {
                sql += " and userid ='" + userid + "'";
            }
            if (!string.IsNullOrEmpty(callType))
            {
                sql += " and CallType =" + callType;
            }
            if (!string.IsNullOrEmpty(callStatus))
            {
                sql += " and CallState =" + callStatus;
            }
            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Call_CallRecords_All> pageModel = new Model.PageData<Model.T_Call_CallRecords_All>();
            dt = BLL.PagerBLL.GetListPager(
                "T_Call_CallRecords",
                "CallRecordsId",
                @"*,CONVERT(varchar,BeginTime, 120 ) as BeginTimeNew,CONVERT(varchar,TalkStartTime, 120 ) as TalkStartTimeNew,CONVERT(varchar,TalkEndTime, 120 ) as TalkEndTimeNew,CONVERT(varchar,TalkStartTime, 120 ) as TalkStartTimeNew,CONVERT(varchar,TalkEndTime, 120 ) as TalkEndTimeNew , replace(replace(FilePath,'\','/'),'" + GetSysconfig("newFilePath") + "','" + GetSysconfig("newPlayPath") + "') as fileurl ",
                sql,
                "ORDER BY CallRecordsId desc",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;
            string json = JsonConvert.SerializeObject(dataModel);
            return json;
        }

        /// <summary>
        /// 获取分类数据
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <returns></returns>
        [ActionName("QCindexDate")]
        [HttpGet]
        public string QCindexDate(DateTime? NowDateTime, int page, int limit, int? parentId)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();
            string sql = "";

            if (parentId != null)
            {
                sql += " and F_ParentId=" + parentId;
            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                "T_QC_IndexCategory",
                "F_CategoryId",
                "* ,(CASE F_DeleteFlag WHEN 1 THEN '启用' WHEN 0 THEN '禁用' ELSE NULL END ) as F_DeleteFlag",
                sql,
                "ORDER BY F_CategoryId   ",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }

        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <returns></returns>
        [ActionName("QCindexbaseDate")]
        [HttpGet]
        public string QCindexbaseDate(  int page, int limit, int? parentId)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();
            string sql = "";

            if (parentId != null)
            {
                sql += " and F_ParentId=" + parentId;
            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                "T_QC_IndexBase",
                "F_IndexId",
                "* ,(CASE F_DeleteFlag WHEN 1 THEN '启用' WHEN 0 THEN '禁用' ELSE NULL END ) as F_DeleteFlag",
                sql,
                "ORDER BY F_IndexId   ",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }

       
        #region 获取指标树
        BLL.T_Sys_RoleInfo roleBLL = new BLL.T_Sys_RoleInfo();
        public class DicSeaarchModel
        {
            public string RootCode
            {
                get;
                set;
            }
        }
        /// <summary>
        /// 初始化字典结构
        /// </summary>
        /// <param name="rootCode"></param>
        /// <returns></returns>
        public ActionResult MenuTree(string rootCode)
        {
            DicSeaarchModel model = new DicSeaarchModel();
            model.RootCode = rootCode;
            return View(model);
        }
        ///通过部门父级节点获取数据
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public string QCGetMenuJsonModel(int parentId)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(qcindexBLL.GetMenuJsonModel(parentId));
        }
        ///通过部门父级节点获取数据
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public string QCindexGetMenuJsonModel(int parentId)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(qcindexbaseBLL.GetMenuJsonModel(parentId));
        }

        #endregion
        /// <summary>
        ///指标分类编辑
        /// </summary>
        /// <param name="editType">类型1、新增 2、修改</param>
        /// /// <returns></returns>
        public ActionResult TargetManagementEdit(int? categoryId  ,int? IndexCategoryId, int editType  )
        {
            try
            {
                Model.T_QC_IndexCategory viewModel = new Model.T_QC_IndexCategory();
                //当前对象实体1新增，2编辑
                if (editType == 1)
                {
                    viewModel.F_ParentId = IndexCategoryId;
                    viewModel.F_Sort = 1;
                  

                }
                else
                {
                    Model.T_QC_IndexCategory callModel = qcindexBLL.GetModel(int.Parse(categoryId.ToString()));
                    viewModel = callModel;

                    }
                //获取指标分类
                viewModel.IndexCategoryList = qcindexBLL.GetModelList("F_ParentId=0");
                return View(viewModel);
                
            }
            catch
            {
                return View();
            }
        }
        /// <summary>
        /// 保存编辑
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public bool SaveCategoryData(T_QC_IndexCategory qcaModel, string Fl_CategoryId, string F_ParentId )
        {

            //当前对象实体
            if (qcaModel.F_CategoryId > 0)
            {
                Model.T_QC_IndexCategory qccateModel = qcindexBLL.GetModel(qcaModel.F_CategoryId);
              
                if (Convert.ToInt32(F_ParentId) > 0)
                {
                    qccateModel.F_ParentId = Convert.ToInt32(Fl_CategoryId);

                }
                else
                {
                    qccateModel.F_ParentId = 0;

                }

                //qccateModel.F_CategoryName = F_CategoryName;
                //qccateModel.F_Sort= Convert.ToInt32( F_Sort);
                return qcindexBLL.Update(qccateModel);
            }
            else
            {
                return qcindexBLL.Add(qcaModel) > 0;
            }

        }
        /// <summary>
        /// 删除功能
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public bool DeleteIndexCateData(int categoryId)
        {
            return qcindexBLL.Delete(categoryId);

        }

        /// <summary>
        /// 保存编辑
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public bool SaveQuaData(T_Call_CallRecords_All quaModel,string F_QCScore,string F_QCRemark)
        {
            
            //当前对象实体
            if (quaModel.CallRecordsId  > 0)
            {
                Model.T_Call_CallRecords_All qcModel = recordBLL.GetModel(quaModel.CallRecordsId);
                switch (F_QCRemark)
                {

                    case "1":
                        qcModel.F_QCRemark ="非常满意";
                        qcModel.F_QCState = 1;
                        break;
                    case "2":
                        qcModel.F_QCRemark = "基本满意";
                        qcModel.F_QCState = 2;
                        break;
                    case "3":
                        qcModel.F_QCRemark = "不满意";
                        qcModel.F_QCState = 3;
                        break;
                    default:
                        qcModel.F_QCRemark = " ";
                        qcModel.F_QCState = 0;
                        break;
                }
                if (Convert.ToInt32(F_QCScore)>0)
                { 
                qcModel.F_QCScore = Convert.ToInt32( F_QCScore);

                }
              else
                {
                    qcModel.F_QCScore = 0;

                }

                qcModel.F_QCTime = DateTime.Now;
                return recordBLL.Update(qcModel);
            }
            else
            {
                return recordBLL.Add(quaModel) > 0;
            }
                      
        }
        


        /// <summary>
        /// 听取录音
        /// </summary>
        /// <returns></returns>
        public ActionResult QCVoicePlay(string type, string callid, int callrecordsid)
        {
            Models.QCVoicePlayModel model = new QCVoicePlayModel();
            model.playpath = "";
            model.downpath = ""; 
            try
            {
                if (callrecordsid != 0)
                {
                    Model.T_Call_CallRecords_All callModel = recordBLL.GetModel(callrecordsid);
                    model.CallRecordsId = callModel.CallRecordsId;
                    model.CallId = callModel.CallId;
                    model.CallNumber = callModel.CallNumber;
                    model.UserId = callModel.UserId;
                    model.UserCode = callModel.UserCode;
                    model.UserName = callModel.UserName;
                    model.TalkLongTime =  callModel.TalkLongTime;
                    model.TalkStartTime = callModel.TalkStartTime;
                    model.TalkEndTime = callModel.TalkEndTime;
                    model.F_QCRemark = callModel.F_QCRemark;
                    model.F_QCScore = Convert.ToInt32( callModel.F_QCScore);
                    model.F_QCState = callModel.F_QCState;
                }
                
                model.playpath = QCGetSrc(type, callid);
                if (type == "0")
                {
                    model.downpath = model.playpath.Replace("playbyappid", "downbyappid");
                   
                }
                else
                {
                    model.downpath = model.playpath.Replace("playvoicemail", "downvoicemail");
                }
            }
            catch
            { }
            
            return View(model);

        }

        /// <summary>
        /// 听取录音
        /// </summary>
        /// <returns></returns>
        public ActionResult QCVoicePlayNew(string type, string callid, int callrecordsid)
        {
            Models.QCVoicePlayModel model = new QCVoicePlayModel();
            model.playpath = "";
            model.downpath = "";
            try
            {
                if (callrecordsid != 0)
                {
                    Model.T_Call_CallRecords_All callModel = recordBLL.GetModel(callrecordsid);
                    model.CallRecordsId = callModel.CallRecordsId;
                    model.CallId = callModel.CallId;
                    model.CallNumber = callModel.CallNumber;
                    model.UserId = callModel.UserId;
                    model.UserCode = callModel.UserCode;
                    model.UserName = callModel.UserName;
                    model.TalkLongTime = callModel.TalkLongTime;
                    model.TalkStartTime = callModel.TalkStartTime;
                    model.TalkEndTime = callModel.TalkEndTime;
                    model.F_QCRemark = callModel.F_QCRemark;
                    model.F_QCScore = Convert.ToInt32(callModel.F_QCScore);
                    model.F_QCState = callModel.F_QCState;
                }

                model.playpath = callid;
                model.downpath = model.playpath;
            }
            catch
            { }

            return View(model);

        }
        /// <summary>
        /// 质检结果
        /// </summary>
        /// <returns></returns>
        public ActionResult QCShowVoicePlay(string type, string callid, int callrecordsid)
        {
            Models.QCVoicePlayModel model = new QCVoicePlayModel();
            model.playpath = "";
            model.downpath = "";
            try
            {
                if (callrecordsid != 0)
                {
                    Model.T_Call_CallRecords_All callModel = recordBLL.GetModel(callrecordsid);
                    model.CallRecordsId = callModel.CallRecordsId;
                    model.CallId = callModel.CallId;
                    model.CallNumber = callModel.CallNumber;
                    model.UserId = callModel.UserId;
                    model.UserCode = callModel.UserCode;
                    model.UserName = callModel.UserName;
                    model.TalkLongTime = callModel.TalkLongTime;
                    model.TalkStartTime = callModel.TalkStartTime;
                    model.TalkEndTime = callModel.TalkEndTime;
                    model.F_QCRemark = callModel.F_QCRemark;
                    model.F_QCScore = Convert.ToInt32(callModel.F_QCScore);
                    model.F_QCState = callModel.F_QCState;
                }

                model.playpath = QCGetSrc(type, callid);
                if (type == "0")
                {
                    model.downpath = model.playpath.Replace("playbyappid", "downbyappid");

                }
                else
                {
                    model.downpath = model.playpath.Replace("playvoicemail", "downvoicemail");
                }
            }
            catch
            { }

            return View(model);

        }
        /// <summary>
        /// 新质检结果
        /// </summary>
        /// <returns></returns>
        public ActionResult QCShowVoicePlayNew(string type, string callid, int callrecordsid)
        {
            Models.QCVoicePlayModel model = new QCVoicePlayModel();
            model.playpath = "";
            model.downpath = "";
            try
            {
                if (callrecordsid != 0)
                {
                    Model.T_Call_CallRecords_All callModel = recordBLL.GetModel(callrecordsid);
                    model.CallRecordsId = callModel.CallRecordsId;
                    model.CallId = callModel.CallId;
                    model.CallNumber = callModel.CallNumber;
                    model.UserId = callModel.UserId;
                    model.UserCode = callModel.UserCode;
                    model.UserName = callModel.UserName;
                    model.TalkLongTime = callModel.TalkLongTime;
                    model.TalkStartTime = callModel.TalkStartTime;
                    model.TalkEndTime = callModel.TalkEndTime;
                    model.F_QCRemark = callModel.F_QCRemark;
                    model.F_QCScore = Convert.ToInt32(callModel.F_QCScore);
                    model.F_QCState = callModel.F_QCState;
                }
                //replace(replace(FilePath, '\',' / '),'" + GetSysconfig("newFilePath") + "','" + GetSysconfig("newPlayPath") + "')"
                model.playpath = callid;
                model.downpath = model.playpath;

            }
            catch
            { }

            return View(model);

        }




        public string QCGetSrc(string type, string callid)
        {
            string res = "";
            try
            {
                string para = "PlayPath";
                if (type == "0")
                {
                    para = "PlayPath";
                }
                else
                {
                    para = "PlayLeaveVoice";
                }
                BLL.T_Sys_SystemConfig bll_T_Sys_SystemConfig = new BLL.T_Sys_SystemConfig();
                string CTIserverIP = bll_T_Sys_SystemConfig.GetParamValueByParamCode(para);//CTIIP

                string path = callid;
                if (path.IndexOf(":") < 0)
                {
                    res = CTIserverIP + path;
                }
                else
                {
                    res = path.Replace("D:", CTIserverIP);
                }
                res = HttpUtility.HtmlEncode(res);
            }
            catch
            {
            }
            return res;
        }

       

    }

    }

