<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/3/23
 * Time: 下午12:09
 */
require_once(BASE_ROOT_PATH . '/helper/area/area_upgrade.php');
require_once(BASE_ROOT_PATH . '/helper/area/area_check.php');
require_once(BASE_ROOT_PATH . '/helper/area/area_validator.php');

class area_helper
{
    //让'其它区' 排到最后
    public static function uplast_country()
    {
        $items = Db::getAll("SELECT max(area_id) as max_id FROM lrlz_area;");
        $maxid = $items[0]['max_id'];
        $mod_area = Model('area');

        $success_count = 0;
        $fail_count = 0;
        if($maxid > 0)
        {
            $items = $mod_area->where(array('area_name' => array('like',"%其它区%")))->field('area_id')->limit(false)->select();
            foreach ($items as $item)
            {
                $maxid += 1;
                $area_id = $item['area_id'];
                $ret = $mod_area->where(array('area_id' => $area_id))->update(array('area_id' => $maxid));
                if($ret == false) {
                    $fail_count++;
                } else {
                    $success_count++;
                }
            }
        }
        Log::record("area_helper uplast_country total update success {$success_count},fail {$fail_count}");
        return true;
    }

    public static function fix_areaid()
    {
        $validator = new area\area_validator('area_origion');

        $mod_addr = Model('address');
        $addres = $mod_addr->where(array('handled' => 0))->field('address_id,area_id,city_id,area_info')->order('address_id asc')->limit(false)->select();

        foreach ($addres as $addr)
        {
            $address_id = $addr['address_id'];
            $area_info = trim($addr['area_info']);
            $area_info = str_replace(array(" "),array("\t"),$area_info);

            $areas = explode("\t",$area_info);
            $count = count($areas);

            if($count == 3)
            {
                $result_name = $validator->from_name($areas[0],$areas[1],$areas[2]);
                if($result_name != false)
                {
                    $city_id = $result_name['city']['area_id'];
                    $country_id = $result_name['country']['area_id'];
                    $narea_info = "{$result_name['province']['area_name']}\t{$result_name['city']['area_name']}\t{$result_name['country']['area_name']}";
                    $ret = $mod_addr->where(array('address_id' => $address_id))->update(array('city_id' => $city_id,
                        'area_id' => $country_id,
                        'handled' => 10,
                        'area_info' => $narea_info));
                    Log::record("fix_areaid address_id = {$address_id} handled",Log::DEBUG);

                }
            }
            elseif($count == 2) {
                Log::record("address_id = {$address_id} 信息不全",Log::ERR);
            }
            else {
                Log::record("address_id = {$address_id} 信息不全",Log::ERR);
            }
        }
    }

    public static function fix_areainfo()
    {
        $validator = new area\area_validator('area');

        $mod_addr = Model('address');
        $addres = $mod_addr->where(array('handled' => 10))->field('address_id,area_id,city_id,area_info')->order('address_id asc')->limit(false)->select();

        foreach ($addres as $addr)
        {
            $address_id = $addr['address_id'];
            $area_id = $addr['area_id'];
            $area = $validator->country($area_id);
            $area_info = "{$area['province']['area_name']}\t{$area['city']['area_name']}\t{$area['country']['area_name']}";
            $mod_addr->where(array('address_id' => $address_id))->update(array('handled' => 11,'area_info' => $area_info));

            Log::record("fix_areainfo address_id = {$address_id} handled",Log::DEBUG);

        }
    }
}