spec_attr_parser.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. $cid = $item['cid'];
  58. if(is_excids($cid)) continue;
  59. $pid = $item['props'];
  60. $spec_name = $this->get_tm_props_name($cid, $pid);
  61. $sp_id = $this->mod_spec->insert(array('sp_id' => $pid, 'sp_name' => $spec_name));
  62. if($sp_id != $pid) {
  63. Log::record(__FUNCTION__ . ':' . __LINE__ . " cannt find prop values from {$cid} {$pid}",Log::ERR);
  64. }
  65. }
  66. }
  67. private function add_type()
  68. {
  69. //通过分类直接建立类型
  70. $items = $this->tm_spec->field('cid')->group('cid')->limit(false)->select();
  71. foreach($items as $item) {
  72. $cid = $item['cid']; // $cid => $typeid 以分类建立类型
  73. if(is_excids($cid)) continue;
  74. $cnmae = $this->get_class_name($cid);
  75. $this->mod_type->insert(array("type_id" => $cid, "type_name" => $cnmae));
  76. $this->mod_good_class->where(array("gc_id" => $cid))->update(array("type_id" => $cid, "type_name" => $cnmae));
  77. }
  78. }
  79. private function type_spec_exist($type_id,$sp_id)
  80. {
  81. $items = $this->mod_type_spec->where(array('type_id' => $type_id,'sp_id' => $sp_id))->limit(false)->select();
  82. return (!empty($items));
  83. }
  84. private function add_type_spec()
  85. {
  86. //按照分类建立类型
  87. $items = $this->tm_spec->field('cid,props')->group('cid,props')->limit(false)->select();
  88. foreach($items as $item)
  89. {
  90. $cid = $item['cid']; // $cid => $typeid 以分类建立类型
  91. if(is_excids($cid)) continue;
  92. $pid = $item['props'];
  93. if($this->type_spec_exist($cid,$pid)) continue;
  94. $this->mod_type_spec->insert(array("type_id" => $cid,"sp_id" => $pid));
  95. }
  96. }
  97. private function get_tmgoods()
  98. {
  99. $bodys = $this->tm_goods->field('num_iid,body')->where(array('imported' => 0))->limit(false)->order('num_iid')->select();
  100. $result = array();
  101. foreach($bodys as $body) {
  102. $item = json_decode($body['body'],true);
  103. if(empty($item)) {
  104. Log::record('Cannot decode num_iid='.$body['num_iid'].' goods.');
  105. continue;
  106. }
  107. $product = array();
  108. $product['body'] = $item['item_seller_get_response']['item'];
  109. $product['num_iid'] = $body['num_iid'];
  110. $cid = $product['body']['cid'];
  111. if(is_excids($cid)) continue;
  112. array_push($result,$product);
  113. }
  114. return $result;
  115. }
  116. private function parse_properties_name($props)
  117. {
  118. $spec_vals = preg_split("/[;]+/", $props);
  119. $ret = array();
  120. foreach($spec_vals as $sv)
  121. {
  122. $data = preg_split("/[:]+/", $sv);
  123. if(!empty($data)) {
  124. $item = array();
  125. $item['pid'] = $data[0];
  126. $item['vid'] = $data[1];
  127. $item['pname'] = $data[2];
  128. $item['vname'] = $data[3];
  129. if(empty($item['pid']) || empty($item['vid']) || empty($item['pname']) || empty($item['vname'])) {
  130. continue;
  131. } else {
  132. array_push($ret,$item);
  133. }
  134. }
  135. }
  136. return $ret;
  137. }
  138. private function specval_exist($props,$cid)
  139. {
  140. $pid = $props['pid'];
  141. $vid = $props['vid'];
  142. $results = $this->tm_spvid_tpvid->where(array('tppid' => $pid,'tpvid' => $vid, 'tpcid' => $cid))->select();
  143. return (!empty($results));
  144. }
  145. private function add_spval($props,$cid)
  146. {
  147. foreach($props as $spvals)
  148. {
  149. if($this->specval_exist($spvals,$cid)) continue;
  150. $spec_val = array();
  151. $spec_val['sp_id'] = $spvals['pid'];
  152. $spec_val['sp_value_name'] = $spvals['vname'];
  153. $spec_val['store_id'] = 1;
  154. $spec_val['sp_value_color'] = '#ddd9c3';
  155. $spec_val['gc_id'] = $cid;
  156. $spec_val['sp_value_sort'] = 0;
  157. $tm_vid = $spvals['vid'];
  158. $specval_id = $this->mod_spec_value->insert($spec_val);
  159. if($specval_id)
  160. {
  161. $ret = $this->tm_spvid_tpvid->insert(array('spvid' => $specval_id,'tpcid' => $cid, 'tppid' => $spvals['pid'],'tpvid' => $tm_vid));
  162. if(empty($ret)) {
  163. Log::record(__FUNCTION__ . ':' . __LINE__,Log::ERR);
  164. }
  165. }
  166. else
  167. {
  168. Log::record(__FUNCTION__ . ':' . __LINE__,Log::ERR);
  169. }
  170. }
  171. }
  172. private function add_spec_val()
  173. {
  174. $tmgoods = $this->get_tmgoods();
  175. foreach ($tmgoods as $goods)
  176. {
  177. $cid = $goods['body']['cid'];
  178. if(is_excids($cid)) continue;
  179. $skus = $goods['body']['skus'];
  180. if(empty($skus)) continue;
  181. $skus = $skus['sku'];
  182. foreach($skus as $sku) {
  183. $prpos_name = $this->parse_properties_name($sku['properties_name']);
  184. $this->add_spval($prpos_name,$cid);
  185. }
  186. }
  187. }
  188. private function get_class_name($cid)
  189. {
  190. $item = $this->mod_good_class->where(array('gc_id' => $cid))->limit(false)->select();
  191. if(empty($item)) {
  192. return NULL;
  193. } else {
  194. return $item[0]['gc_name'];
  195. }
  196. }
  197. //根据cid 和 pid 取到规格名称。
  198. private function get_tm_props_name($cid,$pid)
  199. {
  200. $item = $this->tm_props->where(array('cid' => $cid,'pid' => $pid))->limit(false)->select();
  201. if(empty($item)) {
  202. return NULL;
  203. } else {
  204. return $item[0]['name'];
  205. }
  206. }
  207. private function attrval_exist($props,$cid)
  208. {
  209. $pid = $props['pid'];
  210. $vid = $props['vid'];
  211. $results = $this->tm_tvid_attrvid->field('attr_vid')->where(array('tpid' => $pid,'tvid' => $vid, 'tcid' => $cid))->select();
  212. if(empty($results)) {
  213. return false;
  214. } else {
  215. return $results[0]['attr_vid'];
  216. }
  217. }
  218. private function attr_exist($props,$cid)
  219. {
  220. $pid = $props['pid'];
  221. $results = $this->tm_cpid_attrid->field('attr_id')->where(array('pid' => $pid, 'cid' => $cid))->select();
  222. if(empty($results)) {
  223. return false;
  224. } else {
  225. return $results[0]['attr_id'];
  226. }
  227. }
  228. private function is_private_attr($pid,$cid)
  229. {
  230. if($pid == 20000) return false;
  231. $items = $this->tm_props->field('id')->where(array('pid' => $pid,'cid' => $cid))->select();
  232. return empty($items);
  233. }
  234. //需要从天猫的数据表里面排除掉 规格 =》 从而得到属性
  235. public function add_attribute()
  236. {
  237. $tmgoods = $this->get_tmgoods();
  238. foreach ($tmgoods as $goods)
  239. {
  240. $cid = $goods['body']['cid'];
  241. if(is_excids($cid)) continue;
  242. $props = $this->parse_properties_name($goods['body']['props_name']);
  243. $expids = $this->get_specids($cid);
  244. foreach($props as $attr)
  245. {
  246. $pid = $attr['pid'];
  247. if(in_array($pid,$expids)) continue;
  248. if($this->is_private_attr($pid,$cid)) continue;
  249. $attrid = $this->attr_exist($attr,$cid);
  250. if($attrid == false) {
  251. $row = array("attr_name" => $attr['pname'],"type_id" => $cid,"attr_show" => 1);
  252. $attrid = $this->mod_attribute->table('attribute')->insert($row);
  253. $this->tm_cpid_attrid->insert(array("attr_id" => $attrid, "cid" => $cid, "pid" => $pid));
  254. }
  255. if($this->attrval_exist($attr,$cid)) continue;
  256. $attr_vid = $this->mod_attribute_value->insert(array("attr_id" => $attrid,"type_id" => $cid, "attr_value_name" => $attr['vname']));
  257. if($attr_vid)
  258. {
  259. $rid = $this->tm_tvid_attrvid->insert(array('attr_vid' =>$attr_vid,'tcid' => $cid,'tpid' => $pid,'tvid' => $attr['vid']));
  260. if(empty($rid)) {
  261. throw new Exception('insert tm_tvid_attrvid error');
  262. }
  263. }
  264. else
  265. {
  266. throw new Exception('insert attribute_value error');
  267. }
  268. }
  269. }
  270. }
  271. private function get_specids($typeid)
  272. {
  273. $ret = array();
  274. $items = $this->mod_type_spec->field('type_id,sp_id')->where(array('type_id' => $typeid))->limit(false)->select();
  275. foreach($items as $item) {
  276. array_push($ret,(int)$item['sp_id']);
  277. }
  278. return $ret;
  279. }
  280. private function attr_value($type_id,$attr_id)
  281. {
  282. $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();
  283. $ret = array();
  284. foreach($items as $item) {
  285. array_push($ret,$item['attr_value_name']);
  286. }
  287. return implode(',',$ret);
  288. }
  289. private function update_attrval()
  290. {
  291. $items = $this->mod_attribute->table('attribute')->field('attr_id,type_id')->limit(false)->select();
  292. foreach($items as $item) {
  293. $attr_id = $item['attr_id'];
  294. $type_id = $item['type_id'];
  295. $attr_value = $this->attr_value($type_id,$attr_id);
  296. $this->mod_attribute->table('attribute')->where(array('attr_id' => $attr_id))->update(array('attr_value' => $attr_value));
  297. }
  298. }
  299. }