<?php


namespace App\Modules\Channel\Services;

use App\Libs\Ip2Region;
use App\Modules\Book\Services\ChapterService;
use App\Modules\Channel\Models\QappSendOrderContentShieldConfig;

class QappSendOrderContentShieldConfigService
{
    public static function doShied($sen_order_id,$ip)
    {
        $config = self::getShieldConfig($sen_order_id);
        if(!$config || (!$config->area && !$config->ip_text) || !$config->bid){
            return [];
        }
        $chapter_info = ChapterService::getChapterInfoByBidAndSeq($config->bid,1);
        if(!$chapter_info){
            return [];
        }
        if($config->area && !self::doShiedArea($config,$ip)){
            return [
                'bid'=>$config->bid,
                'cid'=>$chapter_info->id
            ];
        }
        if($config->ip_text && !self::doShiedAreaIp($config,$ip)){
            return [
                'bid'=>$config->bid,
                'cid'=>$chapter_info->id
            ];
        }
        return [];
    }


    private static function doShiedArea($config,$ip)
    {
        $area_list = explode(',',$config->area);
        $ip2Region = new Ip2Region();
        $area = $ip2Region->binarySearch($ip);
        $area_info = explode('|',$area['region']);
        if(!isset($area_info[2])  || !$area_info[2] || !isset($area_info[3]) || !$area_info[3]){
            return '';
        }
        $province = $area_info[2];
        $city = $area_info[3];
        $access = true;
        foreach ($area_list as $area_item){
            $area_item_list = explode('/',$area_item);
            $item = [
                'province'=>$area_item_list[0],
                'city'=>isset($area_item_list[1]) ? $area_item_list[1]:''
            ];
            $item_province = str_replace('省','',$item['province']);
            $item_province = str_replace('市','',$item_province);
            if($item_province == str_replace('省','',$province)){
                if(!$item['city']){$access = false;break;}
                if(str_replace('市','',$item['city']) == str_replace('市','',$city)){
                    $access = false;break;
                }
            }
        }
        return $access;
    }

    private static function doShiedAreaIp($config, $ip)
    {
        $ip_list = explode(',',$config->ip_text);
        $access = true;
        foreach ($ip_list as $item_ip){
            if(strstr($item_ip, '*') == "*"){
                $ip_format = trim(strstr($item_ip, '*',true),'.');
                if(stripos($ip,$ip_format) === 0){
                    $access = false;break;
                }
            }else{
                if($item_ip == $ip){
                    $access = false;
                    break;
                }
            }
        }
        return $access;
    }

    public static function getShieldConfig($send_order_id)
    {
        return QappSendOrderContentShieldConfig::join('qapp_send_orders','qapp_send_orders.account','=','qapp_send_order_content_shield_config.qapp_account')
            ->where('qapp_send_orders.send_order_id',$send_order_id)
            ->where('qapp_send_order_content_shield_config.is_enable',1)
            ->select('qapp_send_order_content_shield_config.area',
                'qapp_send_order_content_shield_config.ip_text',
                'qapp_send_order_content_shield_config.bid'
            )
            ->first();
    }
}