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.tel
{
    public class MobiledataController : BaseController
    {
        //获取号码归属地列表
        public ActionResult GetList(string strtelnum)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string sql = " and F_IsDelete=0";
                DataTable dt = new DataTable();

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

                if (strtelnum != null && strtelnum.Trim() != "")
                {
                    sql += " and (F_MobileNum= '" + strtelnum.Trim() + "' or F_ZipCode like '%" + strtelnum.Trim()
                        + "%' or F_CityDes like '%" + strtelnum.Trim() + "%'or F_CardDes like '%" + strtelnum.Trim() + "%')";
                }

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

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

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

                res = Content(obj.ToJson());

            }
            return res;
        }
        public ActionResult GetMobiledata(int id)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                BLL.T_Sys_MobileData dBLL = new BLL.T_Sys_MobileData();
                Model.T_Sys_MobileData dModel = dBLL.GetModel(id);
                res = Success("获取号码归属地成功", dModel);
            }
            return res;
        }

        //获取号码归属地
        public ActionResult GetMobiledataByNum(string mobileNum)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {

                if (mobileNum != null && mobileNum.Trim() != "")
                {
                    BLL.T_Sys_MobileData dBLL = new BLL.T_Sys_MobileData();
                    Model.T_Sys_MobileData dModel = dBLL.GetModelList(" F_MobileNum='" + mobileNum.Trim() + "' ").FirstOrDefault();
                    if (dModel != null)
                    {
                        res = Success("获取号码归属地成功", dModel);
                    }
                    else
                    {
                        res = Error("获取号码归属地失败");
                    }
                }
                else
                {
                    res = Error("获取参数失败");
                }

            }
            return res;
        }

        //添加/编辑号码归属地
        public ActionResult AddMobiledata(string mobileNum, string zipCode, string cityDes, string cardDes, int id = 0)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                int userId = CurrentUser.UserData.F_UserId;
                Model.T_Sys_UserAccount ua = new BLL.T_Sys_UserAccount().GetModel(userId);
                BLL.T_Sys_MobileData dBLL = new BLL.T_Sys_MobileData();
                Model.T_Sys_MobileData dModel = new Model.T_Sys_MobileData();

                if (id != 0)
                {

                    dModel = dBLL.GetModel(id);
                    if (dModel != null)
                    {
                        if (!string.IsNullOrWhiteSpace(mobileNum.Trim()))
                        {
                            dModel.F_MobileNum = mobileNum.Trim();
                        }
                        if (!string.IsNullOrWhiteSpace(zipCode.Trim()))
                        {
                            dModel.F_ZipCode = zipCode.Trim();
                        }
                        if (!string.IsNullOrWhiteSpace(cityDes.Trim()))
                        {
                            dModel.F_CityDes = cityDes.Trim();
                        }
                        if (!string.IsNullOrWhiteSpace(cardDes.Trim()))
                        {
                            dModel.F_CardDes = cardDes.Trim();
                        }

                        bool b = dBLL.Update(dModel);
                        if (b)
                        {
                            res = Success("编辑成功");
                        }
                        else
                        {
                            res = Error("编辑失败");
                        }
                    }
                    else
                    {
                        res = Error("编辑失败");
                    }
                }
                else
                {
                    dModel = new Model.T_Sys_MobileData();

                    if (!string.IsNullOrWhiteSpace(mobileNum.Trim()))
                    {
                        dModel.F_MobileNum = mobileNum.Trim();
                    }
                    if (!string.IsNullOrWhiteSpace(zipCode.Trim()))
                    {
                        dModel.F_ZipCode = zipCode.Trim();
                    }
                    if (!string.IsNullOrWhiteSpace(cityDes.Trim()))
                    {
                        dModel.F_CityDes = cityDes.Trim();
                    }
                    if (!string.IsNullOrWhiteSpace(cardDes.Trim()))
                    {
                        dModel.F_CardDes = cardDes.Trim();
                    }
                    dModel.F_IsDelete = 0;
                    dModel.F_CreateUser = ua.F_UserCode;
                    dModel.F_CreateTime = DateTime.Now;
                    int n = new BLL.T_Sys_MobileData().Add(dModel);
                    if (n > 0)
                    {
                        res = Success("添加成功", n);
                    }
                    else
                    {
                        res = Error("添加失败");
                    }
                }

            }
            return res;
        }

        //删除号码归属地记录
        public ActionResult DelMobiledata(string[] nums)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                int userId = CurrentUser.UserData.F_UserId;
                Model.T_Sys_UserAccount ua = new BLL.T_Sys_UserAccount().GetModel(userId);
                if (nums != null && nums.Length > 0)
                {
                    string idd = " ";
                    foreach (string str in nums)
                    {
                        idd += "'" + str + "',";
                    }
                    if (new BLL.T_Sys_MobileData().DeleteList(idd.TrimEnd(','), ua.F_UserCode))
                    {
                        res = Success("删除成功");
                    }
                    else
                        res = Error("删除失败");
                }
                else
                {
                    res = Error("请选择要删除的记录");
                }

            }
            return res;
        }

        //IVR获取号码归属地
        public ActionResult IVRGetList(string strtelnum)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string sql = " and isDelete=0";
                DataTable dt = new DataTable();

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

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

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

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

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

                res = Content(obj.ToJson());

            }
            return res;
        }
        public ActionResult GetMobileUsersData(int id)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                BLL.telloc_users dBLL = new BLL.telloc_users();
                Model.telloc_users dModel = dBLL.GetModel(id);
                res = Success("获取号码归属地成功", dModel);
            }
            return res;
        }
        //获取IVR号码归属地坐席关联
        public ActionResult GetMobileUsersList()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                string sql = " and isDelete=0";
                DataTable dt = new DataTable();

                int userid = 0;
                userid = RequestString.GetQueryString("userid").ToString() == "" ? 0 : int.Parse(RequestString.GetQueryString("userid").ToString());
                string locstr = RequestString.GetQueryString("location");
                string strpageindex = RequestString.GetQueryString("page");
                int pageindex = 1;
                string strpagesize = RequestString.GetQueryString("pagesize");
                int pagesize = 10;

                List<int> telloclist = new List<int>();
                if (userid != 0)
                {
                    //sql += " and userid="+userid;
                    DataTable dttelusers = DbHelperSQL.Query(" select tellocid from telloc_users where isdelete=0 and userid=" + userid).Tables[0];
                    if (dttelusers.Rows.Count > 0)
                    {

                        foreach (DataRow dr in dttelusers.Rows)
                        {
                            telloclist.Add(int.Parse(dr[0].ToString()));
                        }
                    }
                }
                if (locstr.Trim() != "")
                {
                    sql += " and (province like '%" + locstr + "%' or city like '%" + locstr + "%')";
                }

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

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

                dt.Columns.Add("res");
                dt.Columns.Add("uid");
                if (userid == 0)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        dr["res"] = 0;
                        dr["uid"] = 0;
                    }
                }
                else
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        int tid = int.Parse(dr["id"].ToString());
                        if (telloclist.Contains(tid))
                        {
                            dr["res"] = 1;
                            dr["uid"] = userid;
                        }
                        else
                        {
                            dr["res"] = 0;
                            dr["uid"] = 0;
                        }
                    }
                }
                var obj = new
                {
                    state = "success",
                    message = "成功",
                    rows = dt,
                    total = recordCount
                };

                res = Content(obj.ToJson());
            }
            return res;
        }
        //添加/修改IVR号码归属地坐席关联
        public ActionResult AddMobileUsers(int tellocid, int userid, int type)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                int userId = CurrentUser.UserData.F_UserId;
                Model.T_Sys_UserAccount ua = new BLL.T_Sys_UserAccount().GetModel(userId);
                BLL.telloc_users dBLL = new BLL.telloc_users();
                Model.telloc_users dModel = new Model.telloc_users();

                //if (type == 1)//0为添加，1是解除
                //{
                //    dModel = dBLL.GetModel(userid,tellocid);
                //    if (dModel != null)
                //    {
                //        dModel.isdelete = 1;
                //        bool b = dBLL.Update(dModel);
                //        if (b)
                //        {
                //            res = Success("解除成功");
                //        }
                //        else
                //        {
                //            res = Error("解除失败");
                //        }
                //    }
                //    else
                //    {
                //        res = Error("解除失败");
                //    }
                //}
                //else
                //{
                //    dModel = new Model.telloc_users();
                //    if (!string.IsNullOrWhiteSpace(tellocid.ToString()))
                //    {
                //        dModel.tellocid = tellocid;
                //    }
                //    if (!string.IsNullOrWhiteSpace(userid.ToString()))
                //    {
                //        dModel.userid = userid;
                //    }
                //    dModel.isdelete = 0;
                //    int n = new BLL.telloc_users().Add(dModel);
                //    if (n > 0)
                //    {
                //        res = Success("添加成功", n);
                //    }
                //    else
                //    {
                //        res = Error("添加失败");
                //    }
                //}


                dModel = new Model.telloc_users();
                if (!string.IsNullOrWhiteSpace(tellocid.ToString()))
                {
                    dModel.tellocid = tellocid;
                }
                if (!string.IsNullOrWhiteSpace(userid.ToString()))
                {
                    dModel.userid = userid;
                }
                dModel = dBLL.GetModel(userid, tellocid);
                if (dModel != null)
                {
                    dModel.isdelete = type;
                    bool b = dBLL.Update(dModel);
                    var str = type == 1 ? "解除" : "添加";
                    if (b)
                    {
                        res = Success(str + "成功");
                    }
                    else
                    {
                        res = Error(str + "失败");
                    }
                }
                else
                {
                    dModel.isdelete = 0;
                    int n = new BLL.telloc_users().Add(dModel);
                    if (n > 0)
                    {
                        res = Success("添加成功", n);
                    }
                    else
                    {
                        res = Error("添加失败");
                    }
                }
            }
            return res;
        }
        //删除IVR号码归属地坐席关联
        public ActionResult DeleteMobileUsers(int id)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                BLL.telloc_users dBLL = new BLL.telloc_users();
                Model.telloc_users dModel = new Model.telloc_users();

                if (id != 0)//修改
                {
                    dModel = dBLL.GetModel(id);
                    if (dModel != null)
                    {
                        dModel.isdelete = 1;
                        bool b = dBLL.Update(dModel);
                        if (b)
                        {
                            res = Success("删除成功");
                        }
                        else
                        {
                            res = Error("删除失败");
                        }
                    }
                    else
                    {
                        res = Error("删除失败");
                    }
                }
                else
                {
                    res = Error("传入参数错误");
                }

            }
            return res;
        }

        public ActionResult GetSeatGroup()
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                BLL.T_Sys_SeatGroup dBLL = new BLL.T_Sys_SeatGroup();
                var seatgroups = dBLL.GetModelList("1=1").Select(n => new { n.F_ZXZID, n.F_ZXZName });
                res = Success("获取坐席组成功", seatgroups);
            }
            return res;
        }
    }
}