using CallCenter.Utility;
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 = "";
                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_MobileNum",
                        "*",
                        sql,
                        "ORDER BY F_MobileNum 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(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.GetModel(mobileNum.Trim());
                        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)
        {
            ActionResult res = NoToken("未知错误，请重新登录");
            if (Request.IsAuthenticated)
            {
                
                    BLL.T_Sys_MobileData dBLL = new BLL.T_Sys_MobileData();
                    Model.T_Sys_MobileData dModel = dBLL.GetModel(mobileNum);
                    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 = Success("编辑失败");
                        }
                    }

                    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();
                        }
                        bool b = new BLL.T_Sys_MobileData().Add(dModel);
                        if (b)
                        {
                            res = Success("添加成功");
                        }
                        else
                        {
                            res = Success("添加失败");
                        }
                    }
             
            }
            return res;
        }

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