using CallCenter.Utility;
using CallCenterApi.Interface.Controllers.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CallCenterApi.Interface.Controllers
{
    public class CallOutOptController : BaseController
    {
        //外呼判断本地外地以及加前缀返回
        public ActionResult GetCallOutprefix(string phone)
        {
            string phone1 = RequestString.ToDBC(RequestString.RemoveNotNumber(WebHelper.NoHtml(phone)));
            string tophone = phone1;
            string zipcode = ""; string bfix = ""; string wfix = ""; string fix = "";
            string userseatgroupid = CurrentUser.UserData.F_SeartGroupID.ToString();
            if (userseatgroupid != "" && userseatgroupid != "0")
            {
                Model.T_Sys_SeatGroup smodel = new BLL.T_Sys_SeatGroup().GetModel(int.Parse(userseatgroupid));
                if (smodel != null)
                {
                    zipcode = smodel.F_ZXAtt.Trim();
                    bfix = smodel.F_WHBDKey.Trim();
                    wfix = smodel.F_WHWDKey.Trim();
                }
            }
            int zip = GetZipCodeByPhone(phone1, zipcode);
            #region 
            if (zip == 1)
            {//手机本地号码
                fix = bfix;
            }
            else if (zip == 2)
            {//手机外地号码
                fix = wfix;
            }
            else
            {
                tophone = phone1.TrimStart('0');
                if (zip == 3)
                {//本地固话去0加9 比如：988888517，937188888517
                    fix = bfix;
                }
                else if (zip == 4)
                {//外地固话前加9 比如：9037188888517
                    fix = wfix;
                }
            }
            #endregion
            var obj = new
            {
                phone = fix + tophone,
                fix = fix
            };

            return Success("外呼号码加前缀", obj);
        }

        /// <summary>
        /// 根据号码和区号判断号码是否为归属地号码
        /// 返回0为分机号或特殊号码
        /// 返回1为手机本地号码
        /// 返回2为手机外地号码
        /// 返回3为固定电话本地号码
        /// 返回4为固定电话外地号码
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="zipcode"></param>
        /// <returns></returns>
        [NonAction]
        public int GetZipCodeByPhone(string phone, string zipcode)
        {
            int res = 0;
            if (phone.Trim().Length >= 7)
            {
                //7位及7位以上是固定电话或手机
                //判断是否手机
                if (phone.Trim().Length == 11 && phone[0] == '1')
                {//号码为11位，首位是1，为手机号
                    BLL.T_Sys_MobileData mobile_Bll = new BLL.T_Sys_MobileData();
                    Model.T_Sys_MobileData mobileModel = mobile_Bll.GetModelList("F_MobileNum='"+ phone.Substring(0, 7) + "'").FirstOrDefault();
                    if (mobileModel != null)
                    {
                        if (mobileModel.F_ZipCode.Equals(zipcode))
                        {
                            res = 1;
                        }
                        else
                        {
                            res = 2;
                        }
                    }
                }
                else
                {
                    if (phone.Trim().Length == 11 && phone.Substring(0, 3).Equals(zipcode))
                    {//号码为11位
                        //截取前三位区号判断是否本地
                        bool resbd3 = phone.Substring(0, 3).Equals(zipcode);
                        //截取前四位区号判断是否本地
                        bool resbd4 = phone.Substring(0, 4).Equals(zipcode);
                        if (resbd3 || resbd4)
                        {
                            res = 3;
                        }
                        else
                        {
                            res = 4;
                        }
                    }
                    else if (phone.Trim().Length < 11)
                    {//号码小于11位，为本地
                        res = 3;
                    }
                    else if (phone.Trim().Length > 11 && phone.Substring(0, 4).Equals(zipcode))
                    {//号码大于11位，截取前四位区号判断是否本地
                        res = 3;
                    }
                    else
                    {
                        res = 4;
                    }
                }
            }
            return res;
        }
    }
}