using CallCenter.Utility;
using CallCenterApi.Common;
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 CallrecordsController : BaseController
    {
        //获取通话记录列表
        public ActionResult GetList(string phone, string usercode)
        {
            string sql = "";
            DataTable dt = new DataTable();

            int roleid = Utils.StrToInt(User.UserData["F_RoleID"], 0);
            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"));
            int type = RequestString.GetInt("type", 0);

            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;

            if (roleid != 0)
            {
                if (roleid == 1)
                {
                    sql += " and UserCode='" + User.UserData["F_WorkNumber"] + "' ";
                }
            }
            if (type != 0)
            {
                sql += " and F_CallInType = '" + type + "' ";
            }
            if (usercode != null && usercode.Trim() != "")
            {
                sql += " and UserCode='" + usercode + "'";
            }

            if (phone != null && phone.Trim() != "")
            {
                sql += " and CallNumber like '%" + phone + "%'";
            }
            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() != "")
            {
                sql += " and CallType='" + calltype + "'";
            }
            if (actiontype.Trim() != "")
            {
                sql += " and ActionType=" + actiontype.Trim();
            }
            if (starttime.Trim() != "")
            {
                sql += " and BeginTime>='" + starttime + "' ";
            }
            if (endtime.Trim() != "")
            { 
                sql += " and BeginTime<='" + endtime + "' ";
            }

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

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Call_CallRecords",
                "CallRecordsId",
                "*",
                sql,
                "ORDER BY CallRecordsId desc",
                pagesize,
                pageindex,
                true,
                out recordCount);

            var config = new BLL.T_Sys_SystemConfig().GetModelList(" F_ParamCode='PlayPath' ").FirstOrDefault();
            var users = new BLL.T_Sys_UserAccount().GetModelList("");
            dt.Columns.Add("TypeName",typeof( string));
            dt.Columns.Add("WorkOrderId", typeof(string));
            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);
                    }
                    dr["FilePath"] = ym + path.Substring(path.IndexOf(':') + 1).Replace('\\', '/');
                }
                string ucode = dr["UserCode"] != null ? dr["UserCode"].ToString() : "";
                if (ucode != "")
                {
                    var uu = users.Where(u => u.F_UserCode.Equals(ucode)).FirstOrDefault();
                    if (uu != null)
                        dr["UserName"] = uu.F_UserName;
                }

                if (dr["F_CallInType"] != null&& dr["F_CallInType"].ToString()!="")
                {
                    int tid = Int32.Parse(dr["F_CallInType"].ToString());
                    var tn = new BLL.T_Sys_DictionaryValue().GetModel(tid);
                    if (tn != null)
                    {
                        dr["TypeName"] = tn.F_Value;
                    }
                }
                
                var wo = new BLL.T_Bus_WorkOrder().GetModelList(" F_CallRecordId='" + dr["CallRecordsId"].ToString() + "' ").FirstOrDefault();
                if (wo != null)
                {
                    dr["WorkOrderId"] = wo.F_WorkOrderId;
                }
            }

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

            return Content(obj.ToJson());


        }

        //获取未接来电
        public ActionResult GetListWJ(string phone)
        {

            string sql = "";
            DataTable dt = new DataTable();

            string starttime = HttpUtility.UrlDecode(RequestString.GetQueryString("starttime"));
            string endtime = HttpUtility.UrlDecode(RequestString.GetQueryString("endtime"));
            string ishf = HttpUtility.UrlDecode(RequestString.GetQueryString("ishf"));//是否回复 0否1是
            string ishfSuccess = HttpUtility.UrlDecode(RequestString.GetQueryString("ishfsuc"));//是否回复成功 0否1是

            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;


            if (phone != null && phone.Trim() != "")
            {
                sql += " and CallNumber like '%" + phone + "%'";
            }

            if (starttime.Trim() != "")
            {
                sql += " and datediff(day,BeginTime,'" + starttime.Trim() + "')<=0 ";
            }
            if (endtime.Trim() != "")
            {
                sql += " and datediff(day,BeginTime,'" + endtime.Trim() + "')>=0 ";
            }
            if (ishf.Trim() != "")
            {
                sql += " and F_wjishf=" + ishf.Trim() + " ";
            }
            if (ishfSuccess.Trim() != "")
            {
                sql += " and F_wjishfSuccess=" + ishfSuccess.Trim() + " ";
            }
            sql += " and CallState='0'";
            sql += " and CallType='0'";
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Call_CallRecords",
                "CallRecordsId",
                "*",
                sql,
                "ORDER BY CallRecordsId desc",
                pagesize,
                pageindex,
                true,
                out recordCount);

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

            return Content(obj.ToJson());

        }

        //获取转移或三方通话，均为手机号
        //rep_transfer只有转移的数据
        public ActionResult GetListZY(string phone, string usercode)
        {
            string sql = "";
            DataTable dt = new DataTable();

            string callstate = HttpUtility.UrlDecode(RequestString.GetQueryString("callstate"));
            string starttime = HttpUtility.UrlDecode(RequestString.GetQueryString("starttime"));
            string endtime = HttpUtility.UrlDecode(RequestString.GetQueryString("endtime"));

            string strpageindex = RequestString.GetQueryString("page");
            int pageindex = 1;
            string strpagesize = RequestString.GetQueryString("pagesize");
            int pagesize = 10;

            sql += " and ActionType>1";//1正常呼入呼出、2转移、3三方通话

            if (usercode != null && usercode.Trim() != "")
            {
                sql += " and UserCode='" + usercode + "'";
            }

            if (phone != null && phone.Trim() != "")
            {
                sql += " and CallNumber like '%" + phone + "%'";
            }
            if (callstate.Trim() != "")
            {
                sql += " and CallState='" + callstate + "'";
            }

            if (starttime.Trim() != "")
            {
                sql += " and datediff(day,BeginTime,'" + starttime + "')<=0 ";
            }
            if (endtime.Trim() != "")
            {
                sql += " and datediff(day,BeginTime,'" + endtime + "')>=0 ";
            }

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

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Call_CallRecords",
                "CallRecordsId",
                "*",
                sql,
                "ORDER BY CallRecordsId desc",
                pagesize,
                pageindex,
                true,
                out recordCount);

            List<Model.T_Call_CallRecords> callList = new BLL.T_Call_CallRecords().DataTableToList(dt);

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

            return Content(obj.ToJson());

        }

        //删除通话记录
        public ActionResult DelCallRecord(string[] ids)
        {
            if (ids != null && ids.Length > 0)
            {
                string idd = " ";
                foreach (string str in ids)
                {
                    idd += str + ",";
                }
                if (new BLL.T_Call_CallRecords().DeleteList(idd.TrimEnd(',')))
                {
                    return Success("删除成功");
                }
                else
                    return Error("删除失败");
            }
            else
            {
                return Error("请选择要删除的记录");
            }

        }
    }
}