<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/1/14
 * Time: 下午6:10
 */
class brand_operator
{
    private $tm_prop_values;
    private $cls_tree;
    private $mod_brand;
    private $tm_goods;
    private $mod_goods_common;
    private $mod_goods;

    const brand_pid = 20000;

    public function __construct()
    {
        $this->tm_goods = Model('tm_goods');
        $this->tm_prop_values = Model('tm_prop_values');
        $this->cls_tree = new class_tree();
        $this->mod_brand = Model('brand');
        $this->mod_goods_common = Model('goods_common');
        $this->mod_goods = Model('goods');
    }

    private function brand_exist($name)
    {
        $result = $this->mod_brand->field('brand_id')->where(array('brand_tm_name' => array('like',"%{$name}%")))->select();

        if(empty($result) || count($result) == 0) {
            return false;
        } else {
            return true;
        }
    }

    public function create()
    {
        $brands = $this->tm_prop_values->field('id,cid,name')->where(array('pid' => self::brand_pid))->group('name')->select();

        foreach($brands as $val)
        {
            $name = $val['name'];
            $cid = $val['cid'];
            if($this->brand_exist($name)) continue;

            if($this->cls_tree->get_info($cid,$c_1,$c_2,$c_3,$cls_name))
            {
                $ret = $this->mod_brand->insert(array('brand_name' => $name,'brand_tm_name' => $name,'brand_class' => $cls_name));
                if($ret == false) {
                    Log::record("insert brand error brand_name = brand_tm_name = {$name}",Log::ERR);
                } else {
                    Log::record("brand_name = {$name} brand_class = {$cls_name}.",Log::DEBUG);
                }
            }
        }
    }

    private function get_brand($propnames,&$brand_name)
    {
        $ret = $this->brand_name($propnames);
        if(!array_key_exists(self::brand_pid,$ret)) {
            return false;
        } else {
            foreach($ret[self::brand_pid]['val'] as $key => $val) {
                $brand_name = $val;
                break;
            }
        }
        if(isset($brand_name) && !empty($brand_name))
        {
            $result = $this->mod_brand->where(array('brand_tm_name' => array('like',"%{$brand_name}%")))->select();
            if(!empty($result) && count($result) > 0) {
                return $result[0]['brand_id'];
            }
        }

        return false;
    }

    public function update_brand()
    {
        $results = $this->tm_goods->field('body,title,num_iid')->limit(false)->select();
        foreach ($results as $result)
        {
            $num_iid = $result['num_iid'];
            $body = $result['body'];
            $response = json_decode($body, true);
            $item = $response['item_seller_get_response']['item'];
            $props_name = $item['props_name'];
            $brand_id = $this->get_brand($props_name,$brand_name);

            if($brand_id)
            {
                $ret = $this->mod_goods_common->where(array('num_iid' => $num_iid))->update(array('brand_id' => $brand_id,'brand_name' => $brand_name));
                if($ret == false) {
                    Log::record("update goods_common brand error: num_iid={$num_iid},brand_id={$brand_id},brand_name={$brand_name}");
                } else {
                    $ret = $this->mod_goods->where(array('num_iid' => $num_iid))->update(array('brand_id' => $brand_id));
                }
            } else {
                Log::record("update goods_common brand error: cannot find brand_id num_iid={$num_iid}");
            }
        }
    }

    /**
     * @param $propnames
     * @return array
     */
    private function brand_name($propnames)
    {
        $spec_vals = preg_split("/[;]+/", $propnames);

        $ret = array();
        foreach ($spec_vals as $sv)
        {
            $data = preg_split("/[:]+/", $sv);
            if (!empty($data)) {
                $tmpid = $data[0];
                $tmpvid = $data[1];
                $tmpname = $data[2];
                $tmvname = $data[3];

                $ret[$tmpid]['name'] = $tmpname;
                $ret[$tmpid]['val'][$tmpvid] = $tmvname;
            }
        }
        return $ret;
    }
}