using CallCenter.Utility;
using CallCenterApi.DB;
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.tel
{
    
    [Authority]
    public class VideoRecordsController : BaseController
    {
        //获取通话记录列表
        public ActionResult GetList(string phone, string usercode, int isdc = 0)
        {
            string sql = "";
            DataTable dt = new DataTable();

            //  int roleid = User.F_RoleId;
            string callstate = HttpUtility.UrlDecode(RequestString.GetQueryString("callstate"));
            string calltype = HttpUtility.UrlDecode(RequestString.GetQueryString("calltype"));
            string actiontype = HttpUtility.UrlDecode(RequestString.GetQueryString("actiontype"));
            string starttime = HttpUtility.UrlDecode(RequestString.GetQueryString("starttime"));
            string endtime = HttpUtility.UrlDecode(RequestString.GetQueryString("endtime"));
            string tasktype = HttpUtility.UrlDecode(RequestString.GetQueryString("tasktype"));
            string extnumber = HttpUtility.UrlDecode(RequestString.GetQueryString("extnumber"));
            string PhoneType = HttpUtility.UrlDecode(RequestString.GetQueryString("phoneType"));
            int type = RequestString.GetInt("type", 0);
            int islike = RequestString.GetInt("islike", 1);
            string CallbackCallid = HttpUtility.UrlDecode(RequestString.GetQueryString("callbackCallid"));
            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;
            if (type != 0)
            {
                sql += " and F_CallInType = '" + type + "' ";
            }
            if (usercode != null && usercode.Trim() != "")
            {
                sql += " and UserCode in(" + usercode + ")";
            }
            if (PhoneType != null && PhoneType.Trim() != "")
            {
                sql += " and PhoneType='" + PhoneType + "'";
            }

            if (islike > 0)
            {
                if (phone != null && phone.Trim() != "")
                {
                    sql += " and CallNumber like '%" + phone + "%'";
                }
            }
            else
            {
                if (phone != null && phone.Trim() != "")
                {
                    sql += " and CallNumber = '" + phone + "'";
                }
            }
            if (!string.IsNullOrEmpty(CallbackCallid))
            {
                sql += "and CallbackCallid='" + CallbackCallid + "'";
            }
            if (callstate.Trim() != "")
            {
                if (callstate.Trim() == "0")
                {
                    sql += " and CallState=0 and isnull(UserCode,'')!='' ";
                }
                else if (callstate.Trim() == "3")
                {
                    sql += " and CallState=0 and CallType=0 and isnull(UserCode,'')='' ";
                }
                else
                {
                    sql += " and CallState='" + callstate + "'";
                }
            }
            if (calltype.Trim() != "")
            {
                if (calltype == "3")
                {
                    sql += " and CallType='1'";
                    sql += " and CallRecordsId in(select F_CallRecordId from T_Bus_VisitResult WITH(NOLOCK) where F_CallRecordId !='' and F_CallRecordId is not null )";
                }
                else
                    sql += " and CallType='" + calltype + "'";
            }
            if (tasktype.Trim() != "")
            {
                if (tasktype.Trim() == "2")
                {
                    sql += " and TaskType='" + tasktype + "'";
                }
                else if (tasktype.Trim() == "0")
                {
                    sql += " and (TaskType is null or TaskType!='2')";
                }
            }
            if (actiontype.Trim() != "")
            {
                sql += " and ActionType=" + actiontype.Trim();
            }
            if (starttime.Trim() != "")
            {
                sql += " and BeginTime>='" + starttime + "' ";
            }
            if (endtime.Trim() != "")
            {
                sql += " and BeginTime<='" + endtime + "' ";
            }
            if (extnumber.Trim() != "")
            {
                sql += " and isnull(ExtNumber,'')='" + extnumber.Trim() + "'";
            }

            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            if (isdc > 0)
            {
                var dtdc = DbHelperSQL.Query(" select   ROW_NUMBER()  OVER(ORDER BY CallRecordsId desc) 编号, CallNumber 电话号码,(case CallType when 0 then '呼入' else '呼出' end ) 呼叫方向,(case CallState when 0 then ( case  when CallType=0 and isnull(UserCode,'')='' then  '主动放弃' else '未接通'   end    ) else '已接通' end ) 呼叫状态,UserCode 坐席工号,dbo.GetUserName(UserCode) as 坐席姓名, "
                    + "BeginTime 开始时间,TalkStartTime 通话开始时间 ,TalkEndTime " +
                    " 通话结束时间 ,EndTime 结束时间 ,LongTime 通话时长 ,PhoneType " +
                    "电话类别"
                   + " from T_Call_VideoRecords  a WITH(NOLOCK)  where 1=1 " + sql + "ORDER BY CallRecordsId desc").Tables[0];
                var msg = new NPOIHelper().ExportToExcel("通话记录", dtdc);
                if (msg == "")
                {
                    return Success("导出成功");
                }
                else
                {
                    return Error("导出失败");
                }
            }
            else
            {
                int recordCount = 0;
                dt = BLL.PagerBLL.GetListPager(
                    "T_Call_VideoRecords WITH(NOLOCK)",
                    "CallRecordsId",
                    "*,dbo.GetUserName(UserCode) as UserName, WorkOrderId,dbo.GetDictionaryName(F_CallInType) as TypeName",
                    sql,
                    "ORDER BY CallRecordsId desc",
                    pagesize,
                    pageindex,
                    true,
                    out recordCount);

                var config = new BLL.T_Sys_SystemConfig().GetModelList(" F_ParamCode='VideoPath' ").FirstOrDefault();

                foreach (DataRow dr in dt.Rows)
                {
                    string path = dr["FilePath"] != null ? dr["FilePath"].ToString() : "";
                    if (path != "" && config != null && !string.IsNullOrEmpty(config.F_ParamValue))
                    {
                        var ym = config.F_ParamValue;
                        if (ym.Substring(ym.Length - 1) == "/")
                        {
                            ym = ym.Substring(0, ym.Length - 1);
                        }

                        string lujing = path.Substring(path.IndexOf(':') + 1).Replace('\\', '/');
                        string wlpath = ym + lujing;

                        dr["FilePath"] = wlpath;
                    }
                }

                var obj = new
                {
                    state = "success",
                    message = "成功",
                    rows = dt,
                    total = recordCount
                };

                return Content(obj.ToJson());
            }
        }
    }

}
