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
    {
        // GET: CallOutOpt

        //外呼判断本地外地以及加前缀返回
        [Authority]
        public ActionResult GetCallOutprefix(string phone)
        {
            string phone1 = RequestString.ToDBC(RequestString.RemoveNotNumber(WebHelper.NoHtml(phone)));

            string fix = ""; string bfix = ""; string wfix = "";
            string zipcode = Configs.GetValue("CallOutZipCode");
            bfix = Configs.GetValue("CallOutBPre");
            wfix = Configs.GetValue("CallOutWPre");
            int zip = GetZipCodeByPhone(phone1, zipcode);
            if (zip == 1)
            {
                fix = bfix;
            }
            else if (zip == 2)
            {
                fix = wfix;
            }

            var obj = new
            {
                phone = fix + phone1,
                fix = fix
            };

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

        }

        /// <summary>
        /// 根据号码和区号判断号码是否为归属地号码
        /// 返回0为分机号或特殊号码
        /// 返回1为本地号码
        /// 返回2为外地号码
        /// </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 = 1;
                        }
                    }
                    else if (phone.Trim().Length < 11)
                    {//号码小于11位，为本地
                        res = 1;
                    }
                    else if (phone.Trim().Length > 11 && phone.Substring(0, 4).Equals(zipcode))
                    {//号码大于11位，截取前四位区号判断是否本地
                        res = 1;
                    }
                    else
                    {
                        res = 2;
                    }
                }
            }
            return res;
        }
    }
}