<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 16/1/20
 * Time: 下午4:53
 */

require_once(BASE_CRONTAB_PATH . '/crawl/class_tree.php');

class gcid_fill
{
    private $cls_tree;
    private $mod_goods;
    private $mod_goods_common;

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

    private function fill_class($cid,&$val,&$name)
    {
        $ret = $this->cls_tree->get_info($cid,$cid_1,$cid_2,$cid_3,$name);

        if($ret) {
            $val['gc_id'] = $cid;
            $val['gc_id_1'] = $cid_1;
            $val['gc_id_2'] = $cid_2;
            $val['gc_id_3'] = $cid_3;
            return true;
        } else {
            return false;
        }
    }

    public function proc()
    {
        $this->proc_goods_common();
        $this->proc_goods();
    }

    private function proc_goods()
    {
        $items = $this->mod_goods->field('gc_id')->group('gc_id')->select();
        foreach ($items as $item)
        {
            $cid = $item['gc_id'];
            $val = array();
            if ($this->fill_class($cid, $val,$name)) {
                $ret = $this->mod_goods->where(array('gc_id' => $cid))->update($val);
                if ($ret == false) {
                    Log::record("cannot update goods class id info where gc_id = {$cid}.", Log::DEBUG);
                }
            } else {
                Log::record("cannot find goods class info where gc_id = {$cid}.", Log::DEBUG);
            }
        }
    }

    private function proc_goods_common()
    {
        $items = $this->mod_goods_common->field('gc_id')->group('gc_id')->select();
        foreach ($items as $item) {
            $cid = $item['gc_id'];
            $val = array();
            if ($this->fill_class($cid, $val,$name)) {
                $val['gc_name'] = $name;
                //$val['type_id'] = $cid;
                $ret = $this->mod_goods_common->where(array('gc_id' => $cid))->update($val);
                if ($ret == false) {
                    Log::record("cannot update goods class id info where gc_id = {$cid}.", Log::DEBUG);
                }
            } else {
                Log::record("cannot find goods class info where gc_id = {$cid}.", Log::DEBUG);
            }
        }
    }
}