<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 15/11/7
 * Time: 下午1:46
 */
class spec_attr
{
    private $tm_spec;
    private $tm_props;
    private $tm_prop_values;

    private $mod_spec;
    private $mod_spec_value;
    private $mod_good_class;

    private $mod_type;
    private $mod_type_spec;
    private $tm_spvid_tpvid; //spec value id => tmall props vid

    private $mod_attribute;
    private $tm_cpid_attrid;
    private $mod_attribute_value;
    private $tm_tvid_attrvid;

    public function __construct()
    {
        $this->tm_spec = Model('tm_spec');
        $this->tm_props = Model('tm_props');
        $this->tm_prop_values = Model('tm_prop_values');

        $this->mod_good_class = Model('goods_class');
        $this->mod_spec = Model('spec');
        $this->mod_type = Model('type');
        $this->mod_type_spec = Model('type_spec');
        $this->mod_spec_value = Model('spec_value');
        $this->tm_spvid_tpvid = Model('tm_spvid_tpvid');

        $this->mod_attribute = Model('attribute');
        $this->tm_cpid_attrid = Model('tm_cpid_attrid');
        $this->mod_attribute_value = Model('attribute_value');
        $this->tm_tvid_attrvid = Model('tm_tvid_attrvid');
    }


    //根据天猫的商品规格，对应生成规格，并做好分类，类型，规格的映射关系
    public function proc()
    {
        $this->add_spec(); //建立规格
        $this->add_type(); //建立类型，以及 规格与类型 的关系
        $this->add_type_spec();
        $this->add_spec_val(); //添加规格值，由于规格值是按照分类走的
        $this->add_attribute();
    }

    private function add_spec()
    {
        $datas = $this->tm_spec->group('props')->order('props')->limit(false)->select(); //获取所有独立的规格
        $pids = array();
        foreach ($datas as $item) {
            $cid = $item['cid'];
            $pid = $item['props'];
            $spec_name = $this->get_tm_props_name($cid, $pid);
            $sp_id = $this->mod_spec->insert(array('sp_id' => $pid, 'sp_name' => $spec_name));
            if($sp_id != $pid) {
                Log::record(__FUNCTION__ . ':' . __LINE__ . "  cannt find prop values from $cid $pid",Log::ERR);
            } else {
                array_push($pids, $pid);
            }
        }
    }

    private function add_type()
    {
        //通过分类直接建立类型
        $items = $this->tm_spec->field('cid')->group('cid')->limit(false)->select();
        foreach($items as $item) {
            $cid = $item['cid']; // $cid => $typeid 以分类建立类型
            $cnmae = $this->get_class_name($cid);
            $this->mod_type->insert(array("type_id" => $cid, "type_name" => $cnmae));
            $this->mod_good_class->where(array("gc_id" => $cid))->update(array("type_id" => $cid, "type_name" => $cnmae));
        }
    }

    private function add_type_spec()
    {
        //按照分类建立类型
        $items = $this->tm_spec->field('cid,props')->group('cid,props')->limit(false)->select();
        foreach($items as $item) {
            $cid = $item['cid']; // $cid => $typeid 以分类建立类型
            $pid = $item['props'];
            $this->mod_type_spec->insert(array("type_id" => $cid,"sp_id" => $pid));
        }
    }

    private function get_prop_name($cid,$pid,$vid)
    {
        $items = $this->tm_prop_values->where(array('cid' => $cid,'pid' => $pid,'vid' => $vid))->limit(false)->select();
        return $items[0]['name'];
    }

    private function add_spec_val()
    {
        $specex = $this->tm_spec->field('cid,props')->group('cid,props')->limit(false)->select();
        foreach ($specex as $sp)
        {
            $cid = $sp["cid"];
            $pid = $sp["props"];

            $items = $this->get_prop_values($cid,$pid);
            if(empty($items)) {
                Log::record(__FUNCTION__ . ':' . __LINE__ . "  cannt find prop values from $cid $pid",Log::ERR);
            }
            else
            {
                foreach($items as $item)
                {
                    $spec_val = array();
                    $spec_val['sp_id'] = $pid;
                    $spec_val['sp_value_name'] = $item['name'];
                    $spec_val['store_id'] = fetch_config::store_id;
                    $spec_val['sp_value_color'] = '#ddd9c3';
                    $spec_val['gc_id'] = $cid;
                    $spec_val['sp_value_sort'] = 0;

                    $tm_vid = $item['vid'];
                    $specval_id = $this->mod_spec_value->insert($spec_val);

                    if($specval_id)
                    {
                        $ret = $this->tm_spvid_tpvid->insert(array('spvid' => $specval_id,'tpcid' => $cid, 'tppid' => $pid,'tpvid' => $tm_vid));
                        if(empty($ret)) {
                            Log::record(__FUNCTION__ . ':' . __LINE__,Log::ERR);
                        }

                    } else {
                        Log::record(__FUNCTION__ . ':' . __LINE__,Log::ERR);
                    }
                }
            }
        }
    }

    private function get_class_name($cid)
    {
        $item = $this->mod_good_class->where(array('gc_id' => $cid))->limit(false)->select();
        if(empty($item)) {
            return NULL;
        } else {
            return $item[0]['gc_name'];
        }
    }

    //根据cid 和 pid 取到规格名称。
    private function get_tm_props_name($cid,$pid)
    {
        $item = $this->tm_props->where(array('cid' => $cid,'pid' => $pid))->limit(false)->select();
        if(empty($item)) {
            return NULL;
        } else {
            return $item[0]['name'];
        }
    }

    private function get_prop_values($cid,$pid)
    {
        $items = $this->tm_prop_values->where(array('cid' => $cid,'pid' => $pid))->limit(false)->select();
        return $items;
    }

    private function gen_attr_value($cid,$pid)
    {
        $items = $this->tm_prop_values->field('pid,name')->where(array('cid' => $cid,'pid' => $pid))->order('vid')->limit(false)->select();

        $ret = array();
        foreach($items as $item) {
            array_push($ret,$item['name']);
        }
        return implode(',',$ret);
    }

    //需要从天猫的数据表里面排除掉 规格 ＝》 从而得到属性
    public function add_attribute()
    {
        $cids = $this->get_cids();
        //$cids[] = 50013794;

        foreach($cids as $cid)
        {
            $expids = $this->get_specids($cid);
            $items =$this->tm_props->where(array('cid' => $cid))->limit(false)->select();

            $pids = array();
            foreach($items as $item)
            {
                $pid = $item['pid'];

                if(in_array($pid,$expids)) continue;
                if(empty($pid)) continue;

                array_push($pids,$pid);

                $name = $item['name'];
                $multi = strtolower($item['multi']) == 'true' ? true : false;
                $attr_multi = $multi ? 1 : 0;

                $attr_value = $this->gen_attr_value($cid,$pid);
                $attrid = $this->mod_attribute->table('attribute')->insert(array( "attr_name" => $name,"type_id" => $cid,
                    'attr_value' => $attr_value, "attr_multi" => $attr_multi,"attr_show" => 1));

                if($attrid)
                { // 建立shopnc attribute id 与 天猫数据 pid ＋ cid 的对应关系
                    $rid = $this->tm_cpid_attrid->insert(array("attr_id" => $attrid, "cid" => $cid, "pid" => $pid));
                    if(empty($rid)) {
                        throw new Exception('insert error');
                    } else {
                        $this->add_attr_value($pid,$cid,$attrid);
                    }
                } else {
                    // add log...
                }

            }
        }
    }

    private function add_attr_value($pid,$cid,$attrid)
    {
        Log::record("pid = $pid,cid=$cid,attrid = $attrid");

        $tm_valus = $this->tm_prop_values->field('name,vid')->where(array('pid' => $pid,'cid'=>$cid))->limit(false)->select();

        foreach($tm_valus as $val)
        {
            $type_id = $cid;
            $attr_value_name = $val['name'];
            $vid = $val['vid'];

            $attr_vid = $this->mod_attribute_value->insert(array("attr_id" => $attrid,"type_id" => $type_id, "attr_value_name" => $attr_value_name));
            if($attr_vid)
            {
                $rid = $this->tm_tvid_attrvid->insert(array('attr_vid' =>$attr_vid,'tcid' => $cid,'tpid' => $pid,'tvid' => $vid));
                if(empty($rid)) {
                    //throw new Exception('insert tm_tvid_attrvid error');
                }
            } else {
                //throw(new Exception("cannot add attribute value."));
            }
        }
    }

    private function get_specids($typeid)
    {
        $ret = array();
        $items = $this->mod_type_spec->field('type_id,sp_id')->where(array('type_id' => $typeid))->limit(false)->select();
        foreach($items as $item) {
            array_push($ret,(int)$item['sp_id']);
        }

        return $ret;
    }

    private function get_cids()
    {
        $cids = array();

        $items = $this->tm_spec->group('cid')->order('cid')->limit(false)->select();
        foreach ($items as $item) {
            array_push($cids,$item['cid']);
        }

        return $cids;
    }
}