spec_attr_parser.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 15/12/5
  6. * Time: 下午4:21
  7. */
  8. class spec_attr_parser
  9. {
  10. private $tm_goods;
  11. private $tm_spec;
  12. private $tm_props;
  13. private $tm_prop_values;
  14. private $mod_spec;
  15. private $mod_spec_value;
  16. private $mod_good_class;
  17. private $mod_type;
  18. private $mod_type_spec;
  19. private $tm_spvid_tpvid; //spec value id => tmall props vid
  20. private $mod_attribute;
  21. private $tm_cpid_attrid;
  22. private $mod_attribute_value;
  23. private $tm_tvid_attrvid;
  24. const store_name = "美宝莲";
  25. const store_id = 1;
  26. public function __construct()
  27. {
  28. $this->tm_spec = Model('tm_spec');
  29. $this->tm_props = Model('tm_props');
  30. $this->tm_prop_values = Model('tm_prop_values');
  31. $this->tm_goods = Model('tm_goods');
  32. $this->mod_good_class = Model('goods_class');
  33. $this->mod_spec = Model('spec');
  34. $this->mod_type = Model('type');
  35. $this->mod_type_spec = Model('type_spec');
  36. $this->mod_spec_value = Model('spec_value');
  37. $this->tm_spvid_tpvid = Model('tm_spvid_tpvid');
  38. $this->mod_attribute = Model('attribute');
  39. $this->tm_cpid_attrid = Model('tm_cpid_attrid');
  40. $this->mod_attribute_value = Model('attribute_value');
  41. $this->tm_tvid_attrvid = Model('tm_tvid_attrvid');
  42. }
  43. //根据天猫的商品规格,对应生成规格,并做好分类,类型,规格的映射关系
  44. public function proc()
  45. {
  46. $this->add_spec(); //建立规格
  47. $this->add_type(); //建立类型,以及 规格与类型 的关系
  48. $this->add_type_spec();
  49. $this->add_spec_val(); //添加规格值,由于规格值是按照分类走的
  50. $this->add_attribute();
  51. $this->update_attrval();
  52. }
  53. private function add_spec()
  54. {
  55. $datas = $this->tm_spec->group('props')->order('props')->limit(false)->select(); //获取所有独立的规格
  56. foreach ($datas as $item)
  57. {
  58. $cid = $item['cid'];
  59. if(is_excids($cid)) {
  60. Log::record("num_iid = {$item['num_iid']} has been excluded.");
  61. continue;
  62. }
  63. $pid = $item['props'];
  64. if(empty($pid)) {
  65. Log::record("num_iid = {$item['num_iid']} props is null.");
  66. continue;
  67. }
  68. $spec_name = $this->get_tm_props_name($cid, $pid);
  69. if(empty($spec_name)) {
  70. Log::record("num_iid = {$item['num_iid']} spec_nme is empty.");
  71. continue;
  72. }
  73. $this->mod_spec->insert(array('sp_id' => $pid, 'sp_name' => $spec_name));
  74. }
  75. }
  76. private function add_type()
  77. {
  78. Log::record(__FILE__ ." " . __FUNCTION__,Log::DEBUG);
  79. //通过分类直接建立类型
  80. $items = $this->tm_spec->field('cid')->group('cid')->limit(false)->select();
  81. foreach($items as $item)
  82. {
  83. $cid = $item['cid']; // $cid => $typeid 以分类建立类型
  84. if(is_excids($cid)) {
  85. Log::record("num_iid = {$item['num_iid']} has been excluded.");
  86. continue;
  87. }
  88. $cnmae = $this->get_class_name($cid);
  89. if(empty($cnmae)) {
  90. Log::record("num_iid = {$item['num_iid']} cannot find cid = {$cid}.");
  91. continue;
  92. }
  93. $this->mod_type->insert(array("type_id" => $cid, "type_name" => $cnmae));
  94. $this->mod_good_class->where(array("gc_id" => $cid))->update(array("type_id" => $cid, "type_name" => $cnmae));
  95. }
  96. }
  97. private function type_spec_exist($type_id,$sp_id)
  98. {
  99. $items = $this->mod_type_spec->where(array('type_id' => $type_id,'sp_id' => $sp_id))->limit(false)->select();
  100. return (!empty($items));
  101. }
  102. private function add_type_spec()
  103. {
  104. //按照分类建立类型
  105. $items = $this->tm_spec->field('cid,props,num_iid')->group('cid,props')->limit(false)->select();
  106. foreach($items as $item)
  107. {
  108. $cid = $item['cid']; // $cid => $typeid 以分类建立类型
  109. if(is_excids($cid)) {
  110. Log::record("num_iid = {$item['num_iid']} has been excluded.");
  111. continue;
  112. }
  113. $pid = $item['props'];
  114. if(empty($pid)) {
  115. Log::record("num_iid = {$item['num_iid']} pid is empty.");
  116. continue;
  117. }
  118. if($this->type_spec_exist($cid,$pid)) continue;
  119. $this->mod_type_spec->insert(array("type_id" => $cid,"sp_id" => $pid));
  120. }
  121. }
  122. private function get_tmgoods()
  123. {
  124. $bodys = $this->tm_goods->field('num_iid,body')->where(array('imported' => 0))->limit(false)->order('num_iid')->select();
  125. $result = array();
  126. foreach($bodys as $body)
  127. {
  128. $item = json_decode($body['body'],true);
  129. if(empty($item)) {
  130. Log::record("Cannot decode num_iid={$body['num_iid']} goods.");
  131. continue;
  132. }
  133. $product = array();
  134. $product['body'] = $item['item_seller_get_response']['item'];
  135. $product['num_iid'] = $body['num_iid'];
  136. $cid = $product['body']['cid'];
  137. if(is_excids($cid)) continue;
  138. array_push($result,$product);
  139. }
  140. return $result;
  141. }
  142. private function parse_properties_name($props)
  143. {
  144. $spec_vals = preg_split("/[;]+/", $props);
  145. $ret = array();
  146. foreach($spec_vals as $sv)
  147. {
  148. $data = preg_split("/[:]+/", $sv);
  149. if(!empty($data)) {
  150. $item = array();
  151. $item['pid'] = $data[0];
  152. $item['vid'] = $data[1];
  153. $item['pname'] = $data[2];
  154. $item['vname'] = $data[3];
  155. if(empty($item['pid']) || empty($item['vid']) || empty($item['pname']) || empty($item['vname'])) {
  156. continue;
  157. } else {
  158. array_push($ret,$item);
  159. }
  160. }
  161. }
  162. return $ret;
  163. }
  164. private function specval_exist($props,$cid)
  165. {
  166. $pid = $props['pid'];
  167. $vid = $props['vid'];
  168. $results = $this->tm_spvid_tpvid->where(array('tppid' => $pid,'tpvid' => $vid, 'tpcid' => $cid))->select();
  169. return (!empty($results));
  170. }
  171. private function add_spval($props,$cid)
  172. {
  173. foreach($props as $spvals)
  174. {
  175. if($this->specval_exist($spvals,$cid)) continue;
  176. $spec_val = array();
  177. $spec_val['sp_id'] = $spvals['pid'];
  178. $spec_val['sp_value_name'] = $spvals['vname'];
  179. $spec_val['store_id'] = fetch_config::store_id;
  180. $spec_val['sp_value_color'] = '#ddd9c3';
  181. $spec_val['gc_id'] = $cid;
  182. $spec_val['sp_value_sort'] = 0;
  183. $tm_vid = $spvals['vid'];
  184. $specval_id = $this->mod_spec_value->insert($spec_val);
  185. if($specval_id)
  186. {
  187. $ret = $this->tm_spvid_tpvid->insert(array('spvid' => $specval_id,'tpcid' => $cid, 'tppid' => $spvals['pid'],'tpvid' => $tm_vid));
  188. if(empty($ret)) {
  189. Log::record(__FUNCTION__ . ':' . __LINE__,Log::ERR);
  190. }
  191. }
  192. else
  193. {
  194. Log::record(__FUNCTION__ . ':' . __LINE__,Log::ERR);
  195. }
  196. }
  197. }
  198. private function add_spec_val()
  199. {
  200. $tmgoods = $this->get_tmgoods();
  201. foreach ($tmgoods as $goods)
  202. {
  203. $cid = $goods['body']['cid'];
  204. if(is_excids($cid)) {
  205. continue;
  206. }
  207. $skus = $goods['body']['skus'];
  208. if(empty($skus)) {
  209. continue;
  210. }
  211. $skus = $skus['sku'];
  212. if(empty($skus)) {
  213. continue;
  214. }
  215. foreach($skus as $sku) {
  216. $prpos_name = $this->parse_properties_name($sku['properties_name']);
  217. $this->add_spval($prpos_name,$cid);
  218. }
  219. }
  220. }
  221. private function get_class_name($cid)
  222. {
  223. $item = $this->mod_good_class->where(array('gc_id' => $cid))->limit(false)->select();
  224. if(empty($item)) {
  225. return NULL;
  226. } else {
  227. return $item[0]['gc_name'];
  228. }
  229. }
  230. //根据cid 和 pid 取到规格名称。
  231. private function get_tm_props_name($cid,$pid)
  232. {
  233. $item = $this->tm_props->where(array('cid' => $cid,'pid' => $pid))->limit(false)->select();
  234. if(empty($item)) {
  235. return NULL;
  236. } else {
  237. return $item[0]['name'];
  238. }
  239. }
  240. private function attrval_exist($props,$cid)
  241. {
  242. $pid = $props['pid'];
  243. $vid = $props['vid'];
  244. $results = $this->tm_tvid_attrvid->field('attr_vid')->where(array('tpid' => $pid,'tvid' => $vid, 'tcid' => $cid))->select();
  245. if(empty($results)) {
  246. return false;
  247. } else {
  248. return $results[0]['attr_vid'];
  249. }
  250. }
  251. private function attr_exist($props,$cid)
  252. {
  253. $pid = $props['pid'];
  254. $results = $this->tm_cpid_attrid->field('attr_id')->where(array('pid' => $pid, 'cid' => $cid))->select();
  255. if(empty($results)) {
  256. return false;
  257. } else {
  258. return $results[0]['attr_id'];
  259. }
  260. }
  261. private function is_private_attr($pid,$cid)
  262. {
  263. if($pid == 20000) return false;
  264. $items = $this->tm_props->field('id')->where(array('pid' => $pid,'cid' => $cid))->select();
  265. return empty($items);
  266. }
  267. //需要从天猫的数据表里面排除掉 规格 =》 从而得到属性
  268. public function add_attribute()
  269. {
  270. $tmgoods = $this->get_tmgoods();
  271. foreach ($tmgoods as $goods)
  272. {
  273. $cid = $goods['body']['cid'];
  274. if(is_excids($cid)) continue;
  275. $props = $this->parse_properties_name($goods['body']['props_name']);
  276. $expids = $this->get_specids($cid);
  277. foreach($props as $attr)
  278. {
  279. $pid = $attr['pid'];
  280. if(in_array($pid,$expids)) continue;
  281. if($this->is_private_attr($pid,$cid)) continue;
  282. $attrid = $this->attr_exist($attr,$cid);
  283. if($attrid == false) {
  284. $row = array("attr_name" => $attr['pname'],"type_id" => $cid,"attr_show" => 1);
  285. $attrid = $this->mod_attribute->table('attribute')->insert($row);
  286. $this->tm_cpid_attrid->insert(array("attr_id" => $attrid, "cid" => $cid, "pid" => $pid));
  287. }
  288. if($this->attrval_exist($attr,$cid)) continue;
  289. $attr_vid = $this->mod_attribute_value->insert(array("attr_id" => $attrid,"type_id" => $cid, "attr_value_name" => $attr['vname']));
  290. if($attr_vid)
  291. {
  292. $rid = $this->tm_tvid_attrvid->insert(array('attr_vid' =>$attr_vid,'tcid' => $cid,'tpid' => $pid,'tvid' => $attr['vid']));
  293. if(empty($rid)) {
  294. throw new Exception('insert tm_tvid_attrvid error');
  295. }
  296. }
  297. else
  298. {
  299. throw new Exception('insert attribute_value error');
  300. }
  301. }
  302. }
  303. }
  304. private function get_specids($typeid)
  305. {
  306. $ret = array();
  307. $items = $this->mod_type_spec->field('type_id,sp_id')->where(array('type_id' => $typeid))->limit(false)->select();
  308. foreach($items as $item) {
  309. array_push($ret,(int)$item['sp_id']);
  310. }
  311. return $ret;
  312. }
  313. private function attr_value($type_id,$attr_id)
  314. {
  315. $items = $this->mod_attribute_value->field('attr_value_id,attr_value_name')->where(array('attr_id' => $attr_id,'type_id' => $type_id))->order('attr_value_sort')->limit(false)->select();
  316. $ret = array();
  317. foreach($items as $item) {
  318. array_push($ret,$item['attr_value_name']);
  319. }
  320. return implode(',',$ret);
  321. }
  322. private function update_attrval()
  323. {
  324. $items = $this->mod_attribute->table('attribute')->field('attr_id,type_id')->limit(false)->select();
  325. foreach($items as $item) {
  326. $attr_id = $item['attr_id'];
  327. $type_id = $item['type_id'];
  328. $attr_value = $this->attr_value($type_id,$attr_id);
  329. $this->mod_attribute->table('attribute')->where(array('attr_id' => $attr_id))->update(array('attr_value' => $attr_value));
  330. }
  331. }
  332. }