pretreat_tmdata.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 15/11/6
  6. * Time: 下午9:52
  7. */
  8. class pretreat_tmdata
  9. {
  10. private $tm_goods;
  11. private $tm_spec;
  12. private $tm_props;
  13. private $tm_prop_values;
  14. private $goods_class;
  15. public function __construct()
  16. {
  17. $this->tm_goods = Model('tm_goods');
  18. $this->tm_spec = Model('tm_spec');
  19. $this->tm_props = Model('tm_props');
  20. $this->tm_prop_values = Model('tm_prop_values');
  21. $this->goods_class = Model('goods_class');
  22. }
  23. public function proc($create_brand)
  24. {
  25. $this->tm_spec->where(array('num_iid > 0'))->delete();
  26. $results = $this->tm_goods->field('body,title,num_iid')->where(array('imported' => 0))->limit(false)->select();
  27. foreach ($results as $result) {
  28. $num_iid = $result['num_iid'];
  29. $body = $result['body'];
  30. $response = json_decode($body, true);
  31. $item = $response['item_seller_get_response']['item'];
  32. $cid = $item['cid'];
  33. if(is_excids($cid)) continue;
  34. $this->parse_tm_product($item);
  35. //下载图片,并确认图片数量,用于验证后面商品是否成功导入
  36. $picnum = 0;
  37. $this->parse_tm_pic($item,$picnum);
  38. $this->tm_goods->where(array('num_iid' => $num_iid))->update(array('picnum' => $picnum));
  39. }
  40. if($create_brand == true) {
  41. $this->create_brand();
  42. }
  43. }
  44. private function create_brand()
  45. {
  46. $barnds = $this->tm_prop_values->field('name,cid')->where(array('pid' => 20000))->group('vid')->select();
  47. $mod_brand = Model('brand');
  48. foreach($barnds as $val) {
  49. $name = $val['name'];
  50. $cid = $val['cid'];
  51. $ret = $mod_brand->insert(array('brand_name' => $name,'brand_class' => $cid));
  52. }
  53. }
  54. private function parse_propnames($propnames)
  55. {
  56. $spec_vals = preg_split("/[;]+/", $propnames);
  57. $ret = array();
  58. foreach($spec_vals as $sv)
  59. {
  60. $data = preg_split("/[:]+/", $sv);
  61. if(!empty($data)) {
  62. $tmpid = $data[0];
  63. $tmpvid = $data[1];
  64. $tmpname = $data[2];
  65. $tmvname = $data[3];
  66. $ret[$tmpid]['name'] = $tmpname;
  67. $ret[$tmpid]['val'][$tmpvid] = $tmvname;
  68. }
  69. }
  70. return $ret;
  71. }
  72. //根据cid 和 pid 取到规格名称。
  73. private function get_tm_props_name($cid,$pid)
  74. {
  75. $item = $this->tm_props->where(array('cid' => $cid,'pid' => $pid))->limit(false)->select();
  76. if(empty($item)) {
  77. return NULL;
  78. } else {
  79. return $item[0]['name'];
  80. }
  81. }
  82. private function get_prop_value_name($cid,$pid,$vid)
  83. {
  84. $items = $this->tm_prop_values->where(array('cid' => $cid,'pid' => $pid,'vid' => $vid))->limit(false)->select();
  85. if(empty($items)) {
  86. return NULL;
  87. } else {
  88. return $items[0]['name'];
  89. }
  90. }
  91. private function add_props($cid,$prposname)
  92. {
  93. $prop_names = $this->parse_propnames($prposname);
  94. foreach($prop_names as $key => $arval)
  95. {
  96. $pid = $key;
  97. $pname = $this->get_tm_props_name($cid,$pid);
  98. if(empty($pname)) {
  99. $pname = $arval['name'];
  100. }
  101. foreach($arval['val'] as $vid => $vname)
  102. {
  103. $name = $this->get_prop_value_name($cid,$pid,$vid);
  104. if(empty($name)) {
  105. $this->tm_prop_values->insert(array('cid' => $cid,'vid' => $vid, 'pid' => $pid, 'name' => $vname));
  106. }
  107. }
  108. }
  109. }
  110. private function get_class_name($cid)
  111. {
  112. $ret = $this->goods_class->field('gc_name')->where(array('gc_id' => $cid))->find();
  113. return $ret['gc_name'];
  114. }
  115. private function download($url)
  116. {
  117. $info = pathinfo($url);
  118. $name = md5($url) . '.' . $info['extension'];
  119. $path = BASE_DATA_PATH . '/Download';
  120. $path = $path . '/' . $name;
  121. if(!file_exists($path)) {
  122. $pic_url = $info['dirname'] . '/' . urlencode($info['basename']);
  123. exec("wget -O $path $pic_url");
  124. }
  125. if(file_exists($path)) {
  126. return true;
  127. } else {
  128. return false;
  129. }
  130. }
  131. private function parse_tm_pic($item,&$npics)
  132. {
  133. $pics = array();
  134. $main_pic = $item['pic_url'];
  135. if(!empty($main_pic)) {
  136. array_push($pics,$main_pic);
  137. }
  138. $item_imgs = $item['item_imgs'];
  139. $item_img = $item_imgs['item_img'];
  140. foreach ($item_img as $img) {
  141. $url = $img['url'];
  142. array_push($pics,$url);
  143. }
  144. $prop_imgs = $item['prop_imgs']['prop_img'];
  145. if(!empty($prop_imgs))
  146. {
  147. foreach($prop_imgs as $img) {
  148. $url = $img['url'];
  149. array_push($pics,$url);
  150. }
  151. }
  152. $arpic = array();
  153. foreach($pics as $pic)
  154. {
  155. $name = md5($pic);
  156. if(array_key_exists($name,$arpic) == false)
  157. {
  158. if($this->download($pic)) {
  159. $arpic[$name] = 1;
  160. }
  161. } else {
  162. //$arpic[$name] += 1;
  163. }
  164. }
  165. foreach($arpic as $key => $val) {
  166. $npics += $val;
  167. }
  168. }
  169. private function parse_tm_product($item)
  170. {
  171. $cid = $item['cid'];
  172. $num_iid = $item['num_iid'];
  173. $title = $item['title'];
  174. $outer_id = $item['outer_id'];
  175. $sku_id = $item['sku_id'];
  176. $this->add_props($cid,$item['props_name']);
  177. $skus = $item['skus'];
  178. if(empty($skus))
  179. {
  180. $val = array();
  181. $val['cid'] = $cid;
  182. $val['num_iid'] = $num_iid;
  183. $val['sku_id'] = $sku_id;
  184. $val['outer_id'] = $outer_id;
  185. $val['title'] = $title;
  186. $val['class_name'] = $this->get_class_name($cid);
  187. $val['detail_url'] = $item['detail_url'];
  188. $this->tm_spec->insert($val);
  189. }
  190. else
  191. {
  192. foreach ($skus as $skuex)
  193. {
  194. foreach ($skuex as $sku)
  195. {
  196. $val = array();
  197. $val['cid'] = $cid;
  198. $val['num_iid'] = $num_iid;
  199. $val['props_val_name'] = $sku['properties_name'];
  200. $val['sku_id'] = empty($sku['sku_id']) ? $sku_id : $sku['sku_id'];
  201. $val['outer_id'] = empty($sku['outer_id']) ? $item['outer_id'] : $sku['outer_id'];
  202. //$props = $this->parse_props($sku['properties']);
  203. $prop_names = $this->parse_propnames($sku['properties_name']);
  204. foreach($prop_names as $key => $arval)
  205. {
  206. $pid = $key;
  207. $pname = $this->get_tm_props_name($cid,$pid);
  208. foreach($arval['val'] as $vid => $vname)
  209. {
  210. $name = $this->get_prop_value_name($cid,$pid,$vid);
  211. if(empty($name)) {
  212. $this->tm_prop_values->insert(array('cid' => $cid,'vid' => $vid, 'pid' => $pid, 'name' => $vname));
  213. }
  214. }
  215. }
  216. $val['sp_name'] = $pname;
  217. $val['props'] = $pid;
  218. $val['props_val'] = $vid;
  219. $val['title'] = $title;
  220. $val['class_name'] = $this->get_class_name($cid);
  221. $val['detail_url'] = $item['detail_url'];
  222. $this->tm_spec->insert($val);
  223. }
  224. }
  225. }
  226. }
  227. }