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 CallblackController : BaseController
    {
        //获取黑名单列表
        public ActionResult GetList(string tel, int? type, string stime, string etime)
        {
            string sql = "";
            DataTable dt = new DataTable();

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

            if (tel != null && tel.Trim() != "")
            {
                sql += " and F_TelPhone like '%" + tel.Trim() + "%' ";
            }
            if (stime != null && stime.Trim() != "")
            {
                sql += " and F_SetTime > '" + Convert.ToDateTime(stime.Trim()) + "' ";
            }
            if (etime != null && etime.Trim() != "")
            {
                sql += " and F_SetTime < '" + Convert.ToDateTime(etime.Trim()) + "' ";
            }
            if (type != null)
            {
                sql += " and F_BlackType=" + type;
            }
            if (strpageindex.Trim() != "")
            {
                pageindex = Convert.ToInt32(strpageindex);
            }

            if (strpagesize.Trim() != "")
            {
                pagesize = Convert.ToInt32(strpagesize);
            }
            int recordCount = 0;
            dt = BLL.PagerBLL.GetListPager(
                "T_Call_Blacklist",
                "F_BlackId",
                "*",
                sql,
                "ORDER BY F_BlackId desc",
                pagesize,
                pageindex,
                true,
                out recordCount);
            List<Model.T_Call_Blacklist> msgList = new BLL.T_Call_Blacklist().DataTableToList(dt);
            List<Model.T_Sys_UserAccount> userlist = new BLL.T_Sys_UserAccount().GetModelList("");
            var obj = new
            {
                state = "success",
                message = "成功",
                rows = msgList.Select(m =>
                {
                    var sdate = ""; var edate = ""; var username = ""; var cdate = "";
                    if (m.F_SetTime != null)
                        sdate = m.F_SetTime.Value.ToString("yyyy-MM-dd");
                    if (m.F_RemoveTime != null)
                        edate = m.F_RemoveTime.Value.ToString("yyyy-MM-dd");
                    if (m.F_UserId != null)
                    {
                        Model.T_Sys_UserAccount usermodel = userlist.Where(u => u.F_UserId == m.F_UserId.Value).FirstOrDefault();
                        if (usermodel != null)
                            username = usermodel.F_UserName;
                    }
                    if (m.F_CreateTime != null)
                    {
                        cdate = m.F_CreateTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                    return new
                    {
                        F_BlackId = m.F_BlackId,
                        F_TelPhone = m.F_TelPhone,
                        F_BlackType = m.F_BlackType,
                        F_SetTime = sdate,
                        F_RemoveTime = edate,
                        F_Describe = m.F_Describe,
                        F_UserId = m.F_UserId,
                        F_UserName = username,
                        F_CreateTime = cdate
                    };
                }),
                total = recordCount
            };

            return Content(obj.ToJson());


        }

        //获取黑名单
        public ActionResult GetBlack(string blackid)
        {
            if (blackid != null && blackid.Trim() != "")
            {
                BLL.T_Call_Blacklist dBLL = new BLL.T_Call_Blacklist();
                Model.T_Call_Blacklist dModel = dBLL.GetModel(int.Parse(blackid.Trim()));
                if (dModel != null)
                {
                    var sdate = ""; var edate = "";
                    if (dModel.F_SetTime != null)
                        sdate = dModel.F_SetTime.Value.ToString("yyyy-MM-dd");
                    if (dModel.F_RemoveTime != null)
                        edate = dModel.F_RemoveTime.Value.ToString("yyyy-MM-dd");
                    var obj = new
                    {
                        F_BlackId = dModel.F_BlackId,
                        F_TelPhone = dModel.F_TelPhone,
                        F_BlackType = dModel.F_BlackType,
                        F_SetTime = sdate,
                        F_RemoveTime = edate,
                        F_Describe = dModel.F_Describe,
                        F_UserId = dModel.F_UserId,
                        F_CreateTime = dModel.F_CreateTime
                    };
                    return Success("获取黑名单成功", obj);
                }
                else
                {
                    return Error("获取黑名单失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }

        }

        //添加黑名单
        public ActionResult AddBlack(string blackid, string telphone, int? type, string settime, string removetime, string des)
        {
            int userId = Utils.StrToInt(User.UserData["F_UserID"], 0);
            Model.T_Sys_UserAccount ua = new BLL.T_Sys_UserAccount().GetModel(userId);
            var date = DateTime.Now;
            if (blackid != null && blackid.Trim() != "")
            {
                BLL.T_Call_Blacklist dBLL = new BLL.T_Call_Blacklist();
                Model.T_Call_Blacklist dModel = dBLL.GetModel(int.Parse(blackid.Trim()));
                if (dModel != null)
                {
                    if (telphone != null && telphone.Trim() != "")
                        dModel.F_TelPhone = telphone.Trim();
                    if (type != null)
                    {
                        dModel.F_BlackType = type;
                        if (type == 1)
                        {
                            if (settime != null && settime.Trim() != "")
                                dModel.F_SetTime = Convert.ToDateTime(settime.Trim());
                            else
                                dModel.F_SetTime = date;
                            if (removetime != null && removetime.Trim() != "")
                                dModel.F_RemoveTime = Convert.ToDateTime(removetime.Trim());
                            else
                                dModel.F_SetTime = date;
                        }
                    }
                    if (des != null && des.Trim() != "")
                        dModel.F_Describe = des.Trim();
                    dModel.F_InterceptNum += 1;
                    dModel.F_UserId = ua.F_UserId;
                    bool b = dBLL.Update(dModel);
                    if (b)
                    {
                        return Success("黑名单编辑成功", dModel);
                    }
                    else
                    {
                        return Error("黑名单编辑失败");
                    }
                }
                else
                {
                    return Error("黑名单编辑失败");
                }
            }
            else
            {
                Model.T_Call_Blacklist dModel = new Model.T_Call_Blacklist();
                dModel.F_CallId = "0";
                if (telphone != null && telphone.Trim() != "")
                    dModel.F_TelPhone = telphone.Trim();
                if (type != null)
                {
                    dModel.F_BlackType = type;
                    if (type == 1)
                    {
                        if (settime != null && settime.Trim() != "")
                            dModel.F_SetTime = Convert.ToDateTime(settime.Trim());
                        else
                            dModel.F_SetTime = date;
                        if (removetime != null && removetime.Trim() != "")
                            dModel.F_RemoveTime = Convert.ToDateTime(removetime.Trim());
                        else
                            dModel.F_RemoveTime = date;
                    }
                }
                if (des != null && des.Trim() != "")
                    dModel.F_Describe = des.Trim();
                dModel.F_InterceptNum = 1;
                dModel.F_UserId = ua.F_UserId;
                int b = new BLL.T_Call_Blacklist().Add(dModel);
                if (b > 0)
                {
                    return Success("黑名单添加成功", dModel);
                }
                else
                {
                    return Error("黑名单添加失败");
                }
            }

        }


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

        }
    }
}