<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/3/16
 * Time: 下午8:54
 */


namespace area;

require_once(BASE_ROOT_PATH . '/helper/util_helper.php');

use util;

class upgrade
{
    private $mod_area;
    private $mod_region;
    public function __construct()
    {
        $this->mod_area = Model('area');
        $this->mod_region = Model('bs_region');
    }

    public function province()
    {
        $this->parent_match(0,1,1);
    }

    public function city()
    {
        $items = $this->mod_area->where(array('area_parent_id' => 0))->field('area_id,tm_region_id')->order('area_name ASC')->select();
        foreach ($items as $item)
        {
            $area_id = intval($item['area_id']);
            $region_id = intval($item['tm_region_id']);

            $this->parent_match($area_id,$region_id,2);
        }
    }
    public function country()
    {
        //得到所有的省
        $items = $this->mod_area->where(array('area_parent_id' => 0))->field('area_id,tm_region_id')->order('area_name ASC')->select();
        foreach ($items as $item)
        {
            $area_id = intval($item['area_id']);
            if($area_id >= 32) continue; //海外地区不处理了。

            $citys = $this->mod_area->where(array('area_parent_id' => $area_id))->field('area_id,tm_region_id')->order('area_name ASC')->select();
            foreach ($citys as $city)
            {
                $city_id = intval($city['area_id']);
                $region_id = intval($city['tm_region_id']);
                $this->parent_match($city_id,$region_id,3);
            }
        }
    }


    private function parent_match($area_parent,$tm_parent,$area_deep)
    {
        $areas   = $this->mod_area->where(array('area_parent_id' => $area_parent))->field('area_id,area_name,area_parent_id,area_deep')->order('area_name ASC')->select();
        $regions = $this->mod_region->where(array('parent_id' => $tm_parent)) ->field('county_name,region_id,parent_id')->order('county_name ASC')->select();
        $maps = $this->map_region($regions);
        $this->match($areas,$maps,$area_parent,$area_deep);
    }

    private function match($areas,$maps,$parent_id,$area_deep)
    {
        foreach ($areas as $area)
        {
            $name = $area['area_name'];
            if(array_key_exists($name,$maps)) {
                $maps[$name]['finded'] = true;
                $this->update($area,$maps[$name]);
            }
            else
            {
                foreach ($maps as $key => $region)
                {
                    if($region['finded'] == true) continue;

                    if(self::fuzzy_name($key,$name)) {
                        $maps[$name]['finded'] = true;
                        $this->update($area,$region);
                        break;
                    }
                }

                $this->nouse($area);
            }
        }
        foreach ($maps as $region)
        {
            if($region['finded'] != true) {
                $this->insert($parent_id,$area_deep,$region);
            }
        }
    }
    private function nouse($area)
    {
        $ret = $this->mod_area->where(array('area_id' => $area['area_id']))->update(array('enable' => 0));
    }

    private function update($area,$region)
    {
        $this->mod_area->where(array('area_id' => $area['area_id']))->update(array('tm_name' => $region['county_name'],'tm_region_id' => $region['region_id'],'tm_parent_id' => $region['parent_id']));
    }
    private function insert($parent_id,$area_deep,$region)
    {
        $ret = $this->mod_area->insert(array('area_name' => "",'area_parent_id' => $parent_id,'area_deep' => $area_deep,
            'tm_name' => $region['county_name'],'tm_region_id' => $region['region_id'],'tm_parent_id' => $region['parent_id']));
    }
    private static function short_name($area_name)
    {
        static $words = array('省','市','区','县','乡','镇','阁');
        $last = mb_substr($area_name, -1, 1);
        if(in_array($last,$words)) {
            return mb_substr($area_name, 0, mb_strlen($area_name) - 1);
        }
        else {
            return $area_name;
        }
    }

    private static function fuzzy_name($country_name,$area_name)
    {
        if (util::start_with($country_name, $area_name)) {
            return true;
        }

        $area_name = self::short_name($area_name);
        if (util::start_with($country_name, $area_name)) {
            return true;
        } else {
            return false;
        }
    }

    private function map_region($regions)
    {
        $maps = [];
        foreach ($regions as $region) {
            $name = trim($region['county_name']);
            $maps[$name] = $region;
        }
        return $maps;
    }
}