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
{
    public class MobiledataController : BaseController
    {
        //获取号码归属地列表
        [Authority]
        public ActionResult GetList(string strtelnum)
        {
          
                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
                };

                return Content(obj.ToJson());

           
        }

        [Authority]
        public ActionResult GetMobiledata(int id)
        {
            BLL.T_Sys_MobileData dBLL = new BLL.T_Sys_MobileData();
            Model.T_Sys_MobileData dModel = dBLL.GetModel(id);
            return Success("获取号码归属地成功", dModel);
          
        }

        //获取号码归属地
        [Authority]
        public ActionResult GetMobiledataByNum(string mobileNum)
        {
           
            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)
                {
                    return Success("获取号码归属地成功", dModel);
                }
                else
                {
                    return Error("获取号码归属地失败");
                }
            }
            else
            {
                return Error("获取参数失败");
            }

        }

        //添加/编辑号码归属地
        [Authority]
        public ActionResult AddMobiledata(string mobileNum, string zipCode, string cityDes, string cardDes, int id = 0)
        {
           
                
                
                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)
                        {
                            return Success("编辑成功");
                        }
                        else
                        {
                            return Error("编辑失败");
                        }
                    }
                    else
                    {
                        return 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 = User.F_UserCode;
                    dModel.F_CreateTime = DateTime.Now;
                    int n = new BLL.T_Sys_MobileData().Add(dModel);
                    if (n > 0)
                    {
                        return Success("添加成功", n);
                    }
                    else
                    {
                        return Error("添加失败");
                    }
                }

           
        }

        //删除号码归属地记录
        [Authority]
        public ActionResult DelMobiledata(string[] nums)
        {
            
            
            
            if (nums != null && nums.Length > 0)
            {
                string idd = " ";
                foreach (string str in nums)
                {
                    idd += "'" + str + "',";
                }
                if (new BLL.T_Sys_MobileData().DeleteList(idd.TrimEnd(','), User.F_UserCode))
                {
                    return Success("删除成功");
                }
                else
                    return Error("删除失败");
            }
            else
            {
                return Error("请选择要删除的记录");
            }
        }
    }
}