<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 15/11/3
 * Time: 下午7:22
 */

class category
{
    private $tm_cats;
    private $goods_class;
    private $tm_goods;

    public function __construct()
    {
        $this->tm_cats = Model('tm_cats');
        $this->goods_class = Model('goods_class');
        $this->tm_goods = Model('tm_goods');
    }

    private function insert($catdatas)
    {
        foreach($catdatas as &$val) {
            $item = array();
            $item['gc_id'] = $val['cid'];
            $item['gc_name'] = $val['name'];
            $item['type_id'] = 37;
            $item['type_name'] = '';
            $item['gc_parent_id'] = $val['parent_cid'];
            $item['commis_rate'] = 0;
            $item['gc_sort'] = $val['sort_order'];
            $item['gc_virtual'] = 0;

            $this->goods_class->insert($item);
            $cids[] = (int)$val['cid'];
        }

        return $cids;
    }

    private function insert_all($pcids)
    {
        if(empty($pcids)) return;

        foreach ($pcids as $pcid) {
            $datas = $this->tm_cats->field('*')->limit(false)->where(array('parent_cid'=> $pcid))->select();
            $cids = $this->insert($datas);

            $this->insert_all($cids);
        }
    }

    private function get_cids()
    {
        $items = $this->tm_goods->group('cid')->field('cid')->limit(false)->select();

        $cids = array();
        foreach($items as $val) {
            array_push($cids,$val['cid']);
        }
        return $cids;
    }
    public function proc()
    {
        $cids = $this->get_cids();
        foreach($cids as $cid) {
            $this->import_leaf($cid);
        }
    }

    private function get_goods_cidinfo($cid)
    {
        $items = $this->goods_class->field('*')->where(array('gc_id' => $cid))->limit(false)->select();

        if(empty($items)) {
            return NULL;
        } else {
            return $items[0];
        }
    }

    private function get_tm_cidinfo($cid)
    {
        $items = $this->tm_cats->field('*')->where(array('cid' => $cid))->limit(false)->select();

        if(empty($items)) {
            return NULL;
        } else {
            return $items[0];
        }
    }

    private function import_leaf($cid)
    {
        if($cid == 0 || !empty($this->get_goods_cidinfo($cid))) {
            return;
        }
        else
        {
            $val = $this->get_tm_cidinfo($cid);

            $item['gc_id'] = $val['cid'];
            $item['gc_name'] = $val['name'];
            $item['gc_parent_id'] = $val['parent_cid'];
            $item['commis_rate'] = 0;
            $item['gc_sort'] = $val['sort_order'];
            $item['gc_virtual'] = 0;

            $id = $this->goods_class->insert($item);
            if($id == false) {
                Log::record("insert cid = {$cid} error.",Log::ERR);
            }
            $this->import_leaf($item['gc_parent_id']);
        }
    }

    public function procex()
    {
        if(empty($this->topcats)) return;

        foreach ($this->topcats as $cid) {
            $datas = $this->tm_cats->field('*')->limit(false)->where(array('cid'=>$cid,'parent_cid'=> 0))->select();
            $this->insert($datas);
        }

        $this->insert_all($this->topcats);
    }
}
