using CallCenter.Utility;
using CallCenterApi.Common;
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
{
    public class IVRWordsController : BaseController
    {
        BLL.T_Sys_IVRWords dBLL = new BLL.T_Sys_IVRWords();
        //获取IVR欢迎词列表
        public ActionResult GetList()
        {
            string sql = " and F_IsDel = 0 ";
            upstate();//更新过期的为失效状态
            DataTable dt = new DataTable();

            string key = HttpUtility.UrlDecode(RequestString.GetQueryString("key"));
            string strbtime = HttpUtility.UrlDecode(RequestString.GetQueryString("btime"));
            string stretime = HttpUtility.UrlDecode(RequestString.GetQueryString("etime"));

            string strybtime = HttpUtility.UrlDecode(RequestString.GetQueryString("bytime"));
            string stryetime = HttpUtility.UrlDecode(RequestString.GetQueryString("eytime"));
            string state = HttpUtility.UrlDecode(RequestString.GetQueryString("state"));

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

            if (key.Trim() != "" && key != "undefined")
            {
                sql += " and (F_Title like '%" + key.Trim() + "%' or F_Content like '%" + key.Trim() + "%' )";
            }
            if (strbtime.Trim() != "" && strbtime != "undefined")
            {
                sql += " and F_CreateTime >= '" + Convert.ToDateTime(strbtime.Trim()).ToString("yyyy-MM-dd 00:00:00") + "' ";
            }
            if (stretime.Trim() != "" && stretime != "undefined")
            {
                sql += " and F_CreateTime <= '" + Convert.ToDateTime(stretime.Trim()).ToString("yyyy-MM-dd 23:59:59") + "' ";
            }
            if (state.Trim() != "" && state != "undefined")
            {
                //0未启动，1生效中，2已失效
                sql += " and F_IsState=" + state;
            }
            #region 有效期添加
            var sqlwb = "";
            var sqlwe = "";
            if (strybtime.Trim() != "" && strybtime != "undefined")
            {
                sqlwb = "  ('" + strybtime.Trim() + "' between F_StartDate and F_EndDate) ";
            }
            if (stryetime.Trim() != "" && stryetime != "undefined")
            {
                sqlwe = "  ('" + stryetime.Trim() + "' between F_StartDate and F_EndDate) ";
            }

            if (sqlwb != "" && sqlwe != "")
            {
                sql += " and (" + sqlwb + " or " + sqlwe + ")";
            }
            else
            {
                if (sqlwb != "")
                {
                    sql += " and (" + sqlwb + ")";
                }
                else if (sqlwe != "")
                {
                    sql += " and (" + sqlwe + ")";
                }
            }
            #endregion

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

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

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

            return Content(obj.ToJson());

        }

        //获取IVR欢迎词
        public ActionResult GetIVRWords(string id)
        {
            if (id != null && id.Trim() != "")
            {

                Model.T_Sys_IVRWords dModel = dBLL.GetModel(int.Parse(id.Trim()));
                if (dModel != null)
                {
                    return Success("获取IVR欢迎词成功", dModel);
                }
                else
                {
                    return Error("获取IVR欢迎词失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }

        }

        //添加IVR欢迎词
        public ActionResult AddIVRWords(string id, string title, string content, string isstate, string sdate, string edate, string remark)
        {
            bool res = false;
            int iss = returnisadd(id, sdate, edate);
            if (iss <= 0)
            {

                if (id != null && id.Trim() != "")
                {
                    Model.T_Sys_IVRWords dModel = dBLL.GetModel(int.Parse(id.Trim()));
                    if (dModel != null)
                    {
                        dModel.F_Title = title.Trim();
                        dModel.F_Content = content.Trim();
                        dModel.F_IsState = Utils.StrToInt(isstate, 0);
                        dModel.F_StartDate = Utils.StrToDateTime(sdate, DateTime.Now);
                        dModel.F_EndDate = Utils.StrToDateTime(edate, DateTime.Now);
                        dModel.F_Remark = remark.Trim();

                        res = dBLL.Update(dModel);
                    }
                }
                else
                {
                    Model.T_Sys_IVRWords dModel = new Model.T_Sys_IVRWords();

                    dModel.F_Title = title.Trim();
                    dModel.F_Content = content.Trim();
                    dModel.F_IsState = Utils.StrToInt(isstate, 0);
                    dModel.F_StartDate = Utils.StrToDateTime(sdate, DateTime.Now);
                    dModel.F_EndDate = Utils.StrToDateTime(edate, DateTime.Now);
                    dModel.F_Remark = remark.Trim();
                    dModel.F_IsDel = 0;
                    dModel.F_CreateUser = User.UserData["F_UserCode"];
                    dModel.F_CreateTime = DateTime.Now;

                    int b = dBLL.Add(dModel);
                    res = b > 0;
                }
            }
            if (res)
            {
                return Success("保存IVR欢迎词成功");
            }
            else
            {
                if (iss > 0)
                {
                    return Error("存在已启动的IVR欢迎词在此有效期间，请查看记录后另外选择有效期！");
                }
                else
                {
                    return Error("保存IVR欢迎词失败");
                }
            }
        }

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

        }

        //获取ivr欢迎词是否有添加权限
        private int returnisadd(string id, string sdate, string edate)
        {

            DateTime ssdate = Utils.StrToDateTime(sdate, DateTime.Now);
            DateTime eedate = Utils.StrToDateTime(edate, DateTime.Now);
            string sql = " isnull(F_IsState,0)=1 and F_IsDel = 0 ";//生效中状态
            if (id != null && id != "")
            {
                sql += " and F_ID<>" + id;
            }
            sql += " and( "
                + "('" + ssdate.ToString("yyyy-MM-dd HH:mm:ss") + "' between F_StartDate and F_EndDate) "
                + "or ('" + eedate.ToString("yyyy-MM-dd HH:mm:ss") + "' between F_StartDate and F_EndDate) "
                + ")";
            return dBLL.GetRecordCount(sql);
        }

        private void upstate()
        {
            string sql = " update T_Sys_IVRWords set F_IsState=2 ";
            sql += " where isnull(F_IsState,0)=1 and F_IsDel = 0 ";//启动状态
            sql += " and F_EndDate<getdate()";
            DbHelperSQL.ExecuteSql(sql);
        }
    }
}