
using RMYY_CallCenter_Api.Utility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RMYY_CallCenter_Api.Controllers.CallTel
{
    public class CallInScreenController : BaseController
    {
        /// <summary>
        /// 添加黑名单
        /// </summary>
        /// <returns></returns>
        public ActionResult AddBlack(string tel, string callid, int num = 0, int type = 0)
        {
            Model.T_Call_Blacklist dModel = new Bll.T_Call_Blacklist().GetModelList(" F_TelPhone='" + tel + "' ").FirstOrDefault();

            var date = DateTime.Now;
            var enddate = date;
            switch (type)
            {
                case 1: enddate = enddate.AddDays(num); break;
                case 2: enddate = enddate.AddHours(num); break;
                case 3: enddate = enddate.AddMinutes(num); break;
                case 4: enddate = DateTime.MaxValue; break;
            }
            if (dModel == null)
            {
                dModel = new Model.T_Call_Blacklist();
                dModel.F_CallId = callid;
                dModel.F_TelPhone = tel.Trim();
                dModel.F_SetTime = date;
                dModel.F_RemoveTime = enddate;
                dModel.F_BlackType = 1;
                dModel.F_InterceptNum = 1;
                dModel.F_UserId = User.F_UserId;
                int b = new Bll.T_Call_Blacklist().Add(dModel);
                if (b > 0)
                {
                    return Success("添加成功", dModel);
                }
                else
                {
                    return Error("添加失败");
                }
            }
            else
            {

                dModel.F_RemoveTime = enddate;
                dModel.F_BlackType = 1;
                if (new Bll.T_Call_Blacklist().Update(dModel))
                {
                    return Success("添加成功", dModel);
                }
                else
                {
                    return Error("修改失败");
                }
            }
        }

        /// <summary>
        /// 取消黑名单
        /// </summary>
        /// <returns></returns>
        public ActionResult DelBlack(string tel)
        {
            Model.T_Call_Blacklist dModel = new Bll.T_Call_Blacklist().GetModelList(" F_TelPhone='" + tel + "' ").FirstOrDefault();
            if (dModel != null)
            {
                bool bl = new Bll.T_Call_Blacklist().Delete(dModel.F_BlackId);
                if (bl)
                {
                    return Success("取消成功");
                }
                else
                {
                    return Error("取消失败");
                }
            }
            else
            {
                return Error("参数错误");
            }
        }

        ///// <summary>
        ///// 根据来电号码获取客户信息
        ///// </summary>
        ///// <returns></returns>
        //public ActionResult GetCustomerByTel(string tel)
        //{
        //    var dt = new Bll.T_Cus_CustomerBase().GetList(" F_Telephone like '%" + tel + "%' or F_Mobile like '%" + tel + "%' ").Tables[0];

        //    return Success("获取成功", dt);
        //}

        /// <summary>
        /// 根据来电号码获取最近的callid
        /// </summary>
        /// <returns></returns>
        public ActionResult GetCallIdByPhone(string tel)
        {
            return Success("加载成功", new Bll.T_Call_CallRecords().GetModelByTelphone(tel));
        }

        /// <summary>
        /// 根据callid获取最近的通话记录
        /// </summary>
        /// <returns></returns>
        public ActionResult GetTelRecordByCallid(string callid)
        {
            return Success("通话记录加载成功", new Bll.T_Call_CallRecords().GetModelByCallId(callid));
        }

        /// <summary>
        /// 来电归属地查询
        /// </summary>
        /// <returns></returns>
        public ActionResult GetPhoneLocation(string tel)
        {

            string location = "未知";
            if (!string.IsNullOrEmpty(tel))
            {
                if (tel.Trim().Length == 11 && tel.Substring(0, 1) != "0")
                {
                    Bll.T_Sys_MobileData mobile_Bll = new Bll.T_Sys_MobileData();
                    Model.T_Sys_MobileData mobileModel = mobile_Bll.GetModelList(" F_MobileNum = '" + tel.Substring(0, 7) + "'").FirstOrDefault();

                    if (mobileModel != null)
                    {
                        location = mobileModel.F_CityDes + mobileModel.F_CardDes;
                    }
                }
                else
                {
                    Bll.T_Sys_TelTitleData numbBll = new Bll.T_Sys_TelTitleData();
                    if (tel.Length > 4)
                    {
                        List<Model.T_Sys_TelTitleData> mobileModel = numbBll.GetModelList(" 1=1 and F_KeyPhoneNum='" + tel.Substring(0, 4) + "'");
                        if (mobileModel == null || mobileModel.Count <= 0)
                        {
                            mobileModel = numbBll.GetModelList(" 1=1 and F_KeyPhoneNum='" + tel.Substring(0, 3) + "'");
                        }
                        if (mobileModel.Count > 0)
                        {
                            location = mobileModel[0].F_TitleName;
                        }
                    }
                }
            }
            return Success("归属地加载成功", location);
        }

        /// <summary>
        /// 历史记录列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetOldList(string tel,  int page = 1, int pagesize = 10)
        {

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

            if (!string.IsNullOrEmpty(tel.Trim()) && tel != "undefined")
            {
                sql += " and CallNumber= '" + tel.Trim() + "' ";
            }

            int recordCount = 0;
            dt = Bll.PagerBll.GetListPager(
                "T_Call_CallRecords",
                "CallRecordsId",
                "*,dbo.GetUserName(UserCode) as UserName, dbo.GetDictionaryName(F_CallInType) as TypeName",
                sql,
                "ORDER BY CallRecordsId desc",
                pagesize,
                page,
                true,
                out recordCount);
            var config = new Bll.T_Sys_SystemConfig().GetModelList(" F_ParamCode='PlayPath' ").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());


        }

        /// <summary>
        /// 振铃事件
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateZL(string callid)
        {
            Model.T_Call_CallRecords model = new Model.T_Call_CallRecords();
            model.CallId = callid;
            model.UserId = User.F_UserId;
            model.UserCode = User.F_UserCode;
            model.UserName = User.F_UserName;
            model.ExtNumber = User.F_ExtensionNumber;

            model.DealType = 5;
            bool bl = new Bll.T_Call_CallRecords().UpdateCallInRingTelRecord(model);
            if (bl)
            {
                return Success("更新振铃状态成功");
            }
            else
            {
                return Success("更新振铃状态失败");
            }
        }

        /// <summary>
        /// 摘机事件
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateZJ(string callid)
        {

            Model.T_Call_CallRecords model = new Model.T_Call_CallRecords();
            model.CallId = callid;
            model.CallState = 1;
            model.DealType = 6;
            bool bl = new Bll.T_Call_CallRecords().UpdateCallInAnswerTelRecord(model);
            if (bl)
            {
                return Success("更新摘机状态成功");
            }
            else
            {
                return Success("更新摘机状态失败");//临时修改
            }

        }

        /// <summary>
        /// 呼入性质
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateCallInType(string callid, int tid = 0)
        {
            Model.T_Call_CallRecords vmodel = new Bll.T_Call_CallRecords().GetModelByCallId(callid);
            if (vmodel != null)
            {
                var n = DB.DbHelperSQL.ExecuteSql("update T_Call_CallRecords set F_CallInType=" + tid + " where CallRecordsId=" + vmodel.CallRecordsId);
                if (n > 0)
                {
                    return Success("操作成功");
                }
                else
                {
                    return Success("操作失败");
                }
            }
            else
            {
                return Success("操作失败");
            }
        }

        /// <summary>
        /// 呼出性质
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateCallOutType(string callid, int tid = 0)
        {
            Model.T_Call_CallRecords vmodel = new Bll.T_Call_CallRecords().GetModelByCallId(callid);
            if (vmodel != null)
            {
                var n = DB.DbHelperSQL.ExecuteSql("update T_Call_CallRecords set F_CallInType=" + tid + " where CallRecordsId=" + vmodel.CallRecordsId);
                if (n > 0)
                {
                    return Success("操作成功");
                }
                else
                {
                    return Success("操作失败");
                }
            }
            else
            {
                return Success("操作失败");
            }
        }

    }
}