<?php
/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 15/11/6
 * Time: 下午9:52
 */

class pretreat_tmdata
{
    private $tm_goods;
    private $tm_spec;
    private $tm_props;
    private $tm_prop_values;
    private $goods_class;
    private $cls_tree;

    public function __construct()
    {
        $this->tm_goods = Model('tm_goods');
        $this->tm_spec = Model('tm_spec');
        $this->tm_props = Model('tm_props');
        $this->tm_prop_values = Model('tm_prop_values');
        $this->goods_class = Model('goods_class');
        $this->cls_tree = new class_tree();
    }

    public function proc()
    {
        $this->tm_spec->where(array('num_iid > 0'))->delete(); //->where(array('imported' => 0))
        $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'];

            $cid = $item['cid'];
            if(is_excids($cid)) continue;

            $this->parse_tm_product($item);

            //下载图片，并确认图片数量，用于验证后面商品是否成功导入
            $picnum = 0;
            $this->parse_tm_pic($item,$picnum);
            $this->tm_goods->where(array('num_iid' => $num_iid))->update(array('picnum' => $picnum));
        }
    }

    private function parse_propnames($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;
    }

    //根据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_value_name($cid,$pid,$vid)
    {
        $items = $this->tm_prop_values->where(array('cid' => $cid,'pid' => $pid,'vid' => $vid))->limit(false)->select();
        if(empty($items)) {
            return NULL;
        } else {
            return $items[0]['name'];
        }
    }

    private function add_props($cid,$prposname)
    {
        $prop_names = $this->parse_propnames($prposname);
        foreach($prop_names as $key => $arval)
        {
            $pid = $key;
            $pname = $this->get_tm_props_name($cid,$pid);
            if(empty($pname)) {
                $pname = $arval['name'];
            }
            foreach($arval['val'] as $vid => $vname)
            {
                $name = $this->get_prop_value_name($cid,$pid,$vid);
                if(empty($name)) {
                    $this->tm_prop_values->insert(array('cid' => $cid,'vid' => $vid, 'pid' => $pid, 'name' => $vname));
                }
            }
        }
    }

    private function get_class_name($cid)
    {
        $ret = $this->goods_class->field('gc_name')->where(array('gc_id' => $cid))->find();
        return $ret['gc_name'];
    }


    private function download($url)
    {
        static $pngext = array('png');

        $info = pathinfo($url);
        $name = md5($url) . '.' . $info['extension'];

        $path = BASE_DATA_PATH . '/Download';
        $path = $path . '/' . $name;
        if(!file_exists($path)) {
            $pic_url = $info['dirname'] . '/' . urlencode($info['basename']);
            exec("wget -O $path $pic_url");
        }

        if(file_exists($path))
        {
            $fh = fopen($path, "rb");
            $head = fread($fh, 8);
            fclose($fh);

            if(empty($head)) {
                unlink($path);
                Log::record("{$path} file is empty.",Log::ERR);
                return false;
            }

            if(!in_array($info['extension'],$pngext))
            {
                $arr = unpack("C4", $head);
                if($arr[1] == 137 &&  $arr[2] == 80 && $arr[3] == 78 && $arr[4] == 71) {
                    Log::record("{$path} is a png file",Log::ERR);
                }
            }

            return true;
        } else {
            return false;
        }
    }

    private function parse_tm_pic($item,&$npics)
    {
        $pics = array();
        $main_pic = $item['pic_url'];
        if(!empty($main_pic)) {
            array_push($pics,$main_pic);
        }

        $item_imgs = $item['item_imgs'];
        $item_img = $item_imgs['item_img'];

        foreach ($item_img as $img) {
            $url = $img['url'];
            array_push($pics,$url);
        }

        $prop_imgs = $item['prop_imgs']['prop_img'];
        if(!empty($prop_imgs))
        {
            foreach($prop_imgs as $img) {
                $url = $img['url'];
                array_push($pics,$url);
            }
        }

        $arpic = array();
        foreach($pics as $pic)
        {
            $name = md5($pic);
            if(array_key_exists($name,$arpic) == false)
            {
                if($this->download($pic)) {
                    $arpic[$name] = 1;
                } else {
                    Log::record("download pic {$pic}.",Log::ERR);
                }
            } else {
                //$arpic[$name] += 1;
            }
        }

        foreach($arpic as $key => $val) {
            $npics += $val;
        }
    }

    private function parse_tm_product($item)
    {
        $cid = $item['cid'];
        $num_iid = $item['num_iid'];
        $title = $item['title'];
        $outer_id = $item['outer_id'];
        $sku_id = $item['sku_id'];
        $this->add_props($cid,$item['props_name']);

        $skus = $item['skus'];
        if(empty($skus))
        {
            $val = array();
            $val['cid'] = $cid;
            $val['num_iid'] = $num_iid;
            $val['sku_id'] = $sku_id;
            $val['outer_id'] = $outer_id;
            $val['title'] = $title;
            $val['class_name'] = $this->get_class_name($cid);
            $val['detail_url'] = $item['detail_url'];
            $this->tm_spec->insert($val);
        }
        else
        {
            foreach ($skus as $skuex)
            {
                foreach ($skuex as $sku)
                {
                    $val = array();
                    $val['cid'] = $cid;
                    $val['num_iid'] = $num_iid;
                    $val['props_val_name'] = $sku['properties_name'];

                    $val['sku_id'] = empty($sku['sku_id']) ? $sku_id : $sku['sku_id'];
                    $val['outer_id'] = empty($sku['outer_id']) ? $item['outer_id'] : $sku['outer_id'];

                    //$props = $this->parse_props($sku['properties']);
                    $prop_names = $this->parse_propnames($sku['properties_name']);
                    foreach($prop_names as $key => $arval)
                    {
                        $pid = $key;
                        $pname = $this->get_tm_props_name($cid,$pid);

                        foreach($arval['val'] as $vid => $vname)
                        {
                            $name = $this->get_prop_value_name($cid,$pid,$vid);
                            if(empty($name)) {
                                $this->tm_prop_values->insert(array('cid' => $cid,'vid' => $vid, 'pid' => $pid, 'name' => $vname));
                            }
                        }
                    }
                    $val['sp_name'] = $pname;
                    $val['props'] = $pid;
                    $val['props_val'] = $vid;
                    $val['title'] = $title;
                    $val['class_name'] = $this->get_class_name($cid);
                    $val['detail_url'] = $item['detail_url'];

                    $this->tm_spec->insert($val);
                }
            }
        }
    }
}