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'); } public function proc($create_brand) { $this->tm_spec->where(array('num_iid > 0'))->delete(); $results = $this->tm_goods->field('body,title,num_iid')->where(array('imported' => 0))->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)); } if($create_brand == true) { $this->create_brand(); } } private function create_brand() { $barnds = $this->tm_prop_values->field('name,cid')->where(array('pid' => 20000))->group('vid')->select(); $mod_brand = Model('brand'); foreach($barnds as $val) { $name = $val['name']; $cid = $val['cid']; $ret = $mod_brand->insert(array('brand_name' => $name,'brand_class' => $cid)); } } 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) { $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)) { 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 { //$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); } } } } }