|
@@ -0,0 +1,427 @@
|
|
|
+<?php
|
|
|
+/**
|
|
|
+ * Created by PhpStorm.
|
|
|
+ * User: stanley-king
|
|
|
+ * Date: 15/11/2
|
|
|
+ * Time: 下午2:59
|
|
|
+ */
|
|
|
+
|
|
|
+require_once(BASE_DATA_PATH . '/model/goods.model.php');
|
|
|
+
|
|
|
+class product_importer
|
|
|
+{
|
|
|
+ private $tm_goods;
|
|
|
+ private $mod_spec;
|
|
|
+ private $mod_spec_value;
|
|
|
+
|
|
|
+ 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;
|
|
|
+
|
|
|
+ private $mod_goods_common;
|
|
|
+ private $mod_goods;
|
|
|
+ private $mod_goods_images;
|
|
|
+
|
|
|
+ private $cls_tree;
|
|
|
+ private $down_path;
|
|
|
+
|
|
|
+ const store_name = "美宝莲";
|
|
|
+ const store_id = 1;
|
|
|
+
|
|
|
+ public function __construct()
|
|
|
+ {
|
|
|
+ $this->tm_goods = Model('tm_goods');
|
|
|
+
|
|
|
+ $this->mod_spec = Model('spec');
|
|
|
+ $this->mod_good_class = Model('goods_class');
|
|
|
+ $this->mod_spec_value = Model('spec_value');
|
|
|
+
|
|
|
+ $this->mod_type = Model('type');
|
|
|
+ $this->mod_type_spec = Model('type_spec');
|
|
|
+ $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');
|
|
|
+ $this->mod_goods_images = Model('goods_images');
|
|
|
+
|
|
|
+ $this->mod_goods = new goodsModel();
|
|
|
+ $this->mod_goods_common = Model('goods_common');
|
|
|
+ $this->cls_tree = new class_tree();
|
|
|
+
|
|
|
+ $this->down_path = BASE_DATA_PATH . '/Download';
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_tmgoods()
|
|
|
+ {
|
|
|
+ //->where(array('num_iid' => 14454708036))
|
|
|
+ $bodys = $this->tm_goods->field('body,description')->limit(false)->order('num_iid')->select();
|
|
|
+ $result = array();
|
|
|
+
|
|
|
+ foreach($bodys as $body) {
|
|
|
+ $item = json_decode($body['body'],true);
|
|
|
+ $product = array();
|
|
|
+ $product['body'] = $item['item_seller_get_response']['item'];
|
|
|
+ $product['desc'] = $body['description'];
|
|
|
+
|
|
|
+ array_push($result,$product);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function fill_class($cid,&$general,&$name)
|
|
|
+ {
|
|
|
+ $this->cls_tree->get_info($cid,$cid_1,$cid_2,$cid_3,$name);
|
|
|
+ $general['gc_id'] = $cid;
|
|
|
+ $general['gc_id_1'] = $cid_1;
|
|
|
+ $general['gc_id_2'] = $cid_2;
|
|
|
+ $general['gc_id_3'] = $cid_3;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_sp_name($sp_id)
|
|
|
+ {
|
|
|
+ $items = $this->mod_spec->where(array('sp_id' => $sp_id))->limit(false)->select();
|
|
|
+ foreach($items as $item) {
|
|
|
+ $name = $item['sp_name'];
|
|
|
+ return $name;
|
|
|
+ }
|
|
|
+
|
|
|
+ return '';
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_specar($typeid)
|
|
|
+ {
|
|
|
+ $items = $this->mod_type_spec->where(array("type_id" => $typeid))->limit(false)->select();
|
|
|
+
|
|
|
+ $ret = array();
|
|
|
+ foreach($items as $item)
|
|
|
+ {
|
|
|
+ $sp_id = $item['sp_id'];
|
|
|
+ $sp_name = $this->get_sp_name($sp_id);
|
|
|
+ $ret[$sp_id] = $sp_name;
|
|
|
+ }
|
|
|
+
|
|
|
+ return $ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_spec_name($sp_id)
|
|
|
+ {
|
|
|
+ $items = $this->mod_spec->field('sp_name')->where(array('sp_id' => $sp_id))->limit(false)->select();
|
|
|
+ if(empty($items)) {
|
|
|
+ return '';
|
|
|
+ } else {
|
|
|
+ return $items[0]['sp_name'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param $spid
|
|
|
+ * @param $spvid
|
|
|
+ * @return mixed
|
|
|
+ */
|
|
|
+ private function get_specval_name($spid, $spvid)
|
|
|
+ {
|
|
|
+ $items = $this->mod_spec_value->field('sp_value_name')->where(array('sp_id' => $spid, 'sp_value_id' => $spvid))->limit(false)->select();
|
|
|
+ if(empty($items)) {
|
|
|
+ return '';
|
|
|
+ } else {
|
|
|
+ return $items[0]['sp_value_name'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_spvid($cid,$pid,$vid)
|
|
|
+ {
|
|
|
+ $items = $this->tm_spvid_tpvid->field('spvid')->where(array('tpcid' => $cid,'tppid' => $pid,'tpvid' => $vid))->limit(false)->select();
|
|
|
+ if(empty($items)) {
|
|
|
+ return '';
|
|
|
+ } else {
|
|
|
+ return $items[0]['spvid'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_attrid($cid,$pid)
|
|
|
+ {
|
|
|
+ $items = $this->tm_cpid_attrid->field('attr_id')->where(array('cid' => $cid,'pid' => $pid))->limit(false)->select();
|
|
|
+ if(empty($items)) {
|
|
|
+ return '';
|
|
|
+ } else {
|
|
|
+ return $items[0]['attr_id'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_attrvid($cid,$pid,$vid)
|
|
|
+ {
|
|
|
+ $items = $this->tm_tvid_attrvid->field('attr_vid')->where(array('tcid' => $cid,'tpid' => $pid,'tvid' => $vid))->limit(false)->select();
|
|
|
+ if(empty($items)) {
|
|
|
+ return '';
|
|
|
+ } else {
|
|
|
+ return $items[0]['attr_vid'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function parse_sku_properties($cid,$props,&$spec_name,&$spec_val)
|
|
|
+ {
|
|
|
+ $spec_vals = preg_split("/[;]+/", $props);
|
|
|
+
|
|
|
+ foreach($spec_vals as $sv)
|
|
|
+ {
|
|
|
+ $data = preg_split("/[:]+/", $sv);
|
|
|
+ if(!empty($data)) {
|
|
|
+ $tmpid = $data[0];
|
|
|
+ $tmpvid = $data[1];
|
|
|
+
|
|
|
+ $spvid = $this->get_spvid($cid,$tmpid,$tmpvid);
|
|
|
+
|
|
|
+ $spid = $tmpid;
|
|
|
+ $sp_name = $this->get_spec_name($spid);
|
|
|
+ $spval_name = $this->get_specval_name($spid, $spvid);
|
|
|
+
|
|
|
+ $spec_val[$spid][$spvid] = $spval_name;
|
|
|
+ $spec_name[$spid] = $sp_name;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_attr_name($attrid)
|
|
|
+ {
|
|
|
+ $items = $this->mod_attribute->table('attribute')->field('attr_name')->where(array('attr_id' => $attrid))->limit(false)->select();
|
|
|
+ if(empty($items)) {
|
|
|
+ return NULL;
|
|
|
+ } else {
|
|
|
+ return $items[0]['attr_name'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_attr_vname($typeid,$attrid,$attrvid)
|
|
|
+ {
|
|
|
+ $items = $this->mod_attribute_value->table('attribute_value')->field('attr_value_name')->where(array('attr_id' => $attrid,'type_id' => $typeid,'attr_value_id' => $attrvid))->limit(false)->select();
|
|
|
+ if(empty($items)) {
|
|
|
+ return NULL;
|
|
|
+ } else {
|
|
|
+ return $items[0]['attr_value_name'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_common_attr($cid,$props)
|
|
|
+ {
|
|
|
+ $ar_attr= array();
|
|
|
+
|
|
|
+ $spec_vals = preg_split("/[;]+/", $props);
|
|
|
+ foreach($spec_vals as $sv)
|
|
|
+ {
|
|
|
+ $data = preg_split("/[:]+/", $sv);
|
|
|
+ if(!empty($data)) {
|
|
|
+ $tmpid = $data[0];
|
|
|
+ $tmvid = $data[1];
|
|
|
+
|
|
|
+ $attr_id = $this->get_attrid($cid,$tmpid);
|
|
|
+ $attr_name = $this->get_attr_name($attr_id);
|
|
|
+ $ar_attr[$attr_id]['name'] = $attr_name;
|
|
|
+
|
|
|
+ $attr_vid = $this->get_attrvid($cid,$tmpid,$tmvid);
|
|
|
+
|
|
|
+ $type_id = $cid;
|
|
|
+
|
|
|
+ $attr_vname = $this->get_attr_vname($type_id,$attr_id,$attr_vid);
|
|
|
+ $ar_attr[$attr_id][$attr_vid] = $attr_vname;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return serialize($ar_attr);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_common_spec_nameval($cid,$skus,&$spec_name,&$spec_val)
|
|
|
+ {
|
|
|
+ $ar_spec_name = array();
|
|
|
+ $ar_spec_val = array();
|
|
|
+
|
|
|
+ foreach($skus as $skuex)
|
|
|
+ {
|
|
|
+ foreach($skuex as $sku) {
|
|
|
+ $properties = $sku['properties'];
|
|
|
+ $this->parse_sku_properties($cid,$properties,$ar_spec_name,$ar_spec_val);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $spec_name = serialize($ar_spec_name);
|
|
|
+ $spec_val = serialize($ar_spec_val);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function get_goods_spec($cid,$properties,&$goods_spec)
|
|
|
+ {
|
|
|
+ $ar_spec_name = array();
|
|
|
+ $ar_spec_val = array();
|
|
|
+
|
|
|
+ $this->parse_sku_properties($cid,$properties,$ar_spec_name,$ar_spec_val);
|
|
|
+
|
|
|
+ $goods_spec = serialize($ar_spec_name);
|
|
|
+ $spec_val = serialize($ar_spec_val);
|
|
|
+ }
|
|
|
+
|
|
|
+ private function fill_general($cid,$item, &$general,&$name)
|
|
|
+ {
|
|
|
+ $this->fill_class($cid, $general,$name);
|
|
|
+
|
|
|
+ $general['store_id'] = self::store_id;
|
|
|
+ $general['store_name'] = self::store_name;
|
|
|
+ $general['goods_name'] = $item['title'];
|
|
|
+ $general['goods_jingle'] = empty($item['sell_point']) ? '' : $item['sell_point'];
|
|
|
+ $general['goods_price'] = $item['price'];
|
|
|
+ $general['goods_image'] = $item['pic_url'];
|
|
|
+ $general['goods_state'] = 1;
|
|
|
+ $general['goods_verify'] = 1;
|
|
|
+ $general['goods_commend'] = strtolower($item['has_showcase']) ? 1 : 0;
|
|
|
+ $general['goods_vat'] = strtolower($item['has_invoice']) ? 1 : 0;
|
|
|
+ $general['is_virtual'] = strtolower($item['is_virtual']) ? 1 : 0;
|
|
|
+ $general['store_name'] = $item['nick'];
|
|
|
+ $general['goods_serial'] = $item['outer_id'];
|
|
|
+ $general['goods_addtime'] = $item['created'];
|
|
|
+ }
|
|
|
+
|
|
|
+ private function upload_img($file)
|
|
|
+ {
|
|
|
+ $pic_name = '';
|
|
|
+ $upload = new upload_file();
|
|
|
+ $uploaddir = ATTACH_PATH.DS.'store_joinin'.DS;
|
|
|
+ $upload->set('default_dir',$uploaddir);
|
|
|
+ $upload->set('allow_type',array('jpg','jpeg','gif','png'));
|
|
|
+ if (!empty($_FILES[$file]['name'])){
|
|
|
+ $result = $upload->upfile($file);
|
|
|
+ if ($result){
|
|
|
+ $pic_name = $upload->file_name;
|
|
|
+ $upload->file_name = '';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $pic_name;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function reset_imgs($imges)
|
|
|
+ {
|
|
|
+ if(empty($imges)) return '';
|
|
|
+
|
|
|
+ $ret = array();
|
|
|
+
|
|
|
+ foreach($imges as $img) {
|
|
|
+ $ret[$img['position']]['url'] = $img['url'];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $ret;
|
|
|
+ }
|
|
|
+
|
|
|
+ private function upload_imges(&$imges,$storeid)
|
|
|
+ {
|
|
|
+ foreach($imges as &$img)
|
|
|
+ {
|
|
|
+ $url = $img['url'];
|
|
|
+ $info = pathinfo($url);
|
|
|
+ $src_name = $this->down_path . '/' . md5($url) . '.' . $info['extension'];
|
|
|
+ $upctl = new upload_control();
|
|
|
+ $upctl->set_store($storeid);
|
|
|
+ $up_name = $upctl->upload_img($src_name);
|
|
|
+ $img['upname'] = $up_name;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private function import_item($item,$desc)
|
|
|
+ {
|
|
|
+ $cid = $item['cid'];
|
|
|
+
|
|
|
+ $general = array();
|
|
|
+ $this->fill_general($cid,$item,$general,$name);
|
|
|
+
|
|
|
+
|
|
|
+ //属于SKU 信息中
|
|
|
+ $skus = &$item['skus'];
|
|
|
+ $typeid = $cid;
|
|
|
+ $goods_common = $general;
|
|
|
+ {
|
|
|
+ $goods_common['type_id'] = $typeid;
|
|
|
+ $goods_common['goods_body'] = $desc;
|
|
|
+ $goods_common['mobile_body'] = $desc;
|
|
|
+ $goods_common['gc_name'] = $name;
|
|
|
+ $goods_common['goods_selltime'] = $item['list_time'];
|
|
|
+ $goods_common['goods_costprice'] = $item['price'];
|
|
|
+ $goods_common['goods_attr'] = $this->get_common_attr($cid,$item['props']);
|
|
|
+
|
|
|
+ if(empty($skus)) {
|
|
|
+ $spec_name = 'N';
|
|
|
+ } else {
|
|
|
+ $this->get_common_spec_nameval($cid,$skus,$spec_name,$spec_val);
|
|
|
+ }
|
|
|
+
|
|
|
+ $goods_common['spec_name'] = $spec_name;
|
|
|
+ $goods_common['spec_value'] = $spec_val;
|
|
|
+
|
|
|
+ $common_id = $this->mod_goods->addGoodsCommon($goods_common);
|
|
|
+ $imgs = $this->reset_imgs($item['item_imgs']['item_img']);
|
|
|
+ $this->upload_imges($imgs,1);
|
|
|
+
|
|
|
+ foreach($imgs as $pos => $img)
|
|
|
+ {
|
|
|
+ $upname = $img['upname'];
|
|
|
+ if(empty($main_pic)) {
|
|
|
+ $main_pic = $upname;
|
|
|
+ $default = 1;
|
|
|
+ } else {
|
|
|
+ $default = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->mod_goods_images->insert(array('goods_commonid' => $common_id,'store_id' => self::store_id,'goods_image' => $upname,'goods_image_sort' => $pos,'is_default' => $default));
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->mod_goods_common->where(array('goods_commonid' => $common_id))->update(array('goods_image' => $main_pic));
|
|
|
+ }
|
|
|
+
|
|
|
+ if(empty($skus))
|
|
|
+ {
|
|
|
+ $goods = $general;
|
|
|
+
|
|
|
+ $goods['goods_edittime'] = $item['modified'];
|
|
|
+ $goods['goods_serial'] = $item['outer_id'];
|
|
|
+ $goods['goods_price'] = $item['price'];
|
|
|
+ $goods['goods_addtime'] = $item['created'];
|
|
|
+ $goods['goods_storage'] = $item['quantity'];
|
|
|
+ $goods['goods_commonid'] = $common_id;
|
|
|
+
|
|
|
+ $goods['goods_spec'] = 'N';
|
|
|
+ $goods['goods_image'] = $main_pic;
|
|
|
+
|
|
|
+ $this->mod_goods->addGoods($goods);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ foreach($skus as $skuex)
|
|
|
+ {
|
|
|
+ foreach($skuex as $sku) {
|
|
|
+ $goods = $general;
|
|
|
+
|
|
|
+ $goods['goods_edittime'] = $sku['modified'];
|
|
|
+ $goods['goods_serial'] = $sku['outer_id'];
|
|
|
+ $goods['goods_price'] = $sku['price'];
|
|
|
+ $goods['goods_addtime'] = $sku['created'];
|
|
|
+ $goods['goods_storage'] = $sku['quantity'];
|
|
|
+ $goods['goods_commonid'] = $common_id;
|
|
|
+ $this->get_goods_spec($cid,$sku['properties'],$goods_spec);
|
|
|
+ $goods['goods_spec'] = $goods_spec;
|
|
|
+ $goods['goods_image'] = $main_pic;
|
|
|
+
|
|
|
+ $this->mod_goods->addGoods($goods);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public function proc()
|
|
|
+ {
|
|
|
+ $items = $this->get_tmgoods();
|
|
|
+ foreach($items as $item) {
|
|
|
+ $this->import_item($item['body'],$item['desc']);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|