transport_data.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 15/11/2
  6. * Time: 下午2:59
  7. */
  8. class ModelOperator
  9. {
  10. private $mod_goods_tmp;
  11. private $mod_spec_ex;
  12. private $mod_spec;
  13. private $mod_spec_value;
  14. private $mod_good_class;
  15. private $mod_item_props;
  16. private $mod_item_prop_values;
  17. private $mod_type;
  18. private $mod_type_spec;
  19. private $mod_spvid_tvid; //spec value id => tmall props vid
  20. private $mod_attribute;
  21. private $mod_attr_tcpid;
  22. private $mod_attribute_value;
  23. public function __construct()
  24. {
  25. $this->mod_goods_tmp = Model('goods_tmp');
  26. $this->mod_spec_ex = Model('spec_ex');
  27. $this->mod_spec = Model('spec');
  28. $this->mod_good_class = Model('goods_class');
  29. $this->mod_item_props = Model('item_props');
  30. $this->mod_item_prop_values = Model('item_prop_values');
  31. $this->mod_spec_value = Model('spec_value');
  32. $this->mod_type = Model('type');
  33. $this->mod_type_spec = Model('type_spec');
  34. $this->mod_spvid_tvid = Model('spvid_tvid');
  35. $this->mod_attribute = Model('attribute');
  36. $this->mod_attr_tcpid = Model('attr_tcpid');
  37. $this->mod_attribute_value = Model('attribute_value');
  38. }
  39. public function __call($method,$args)
  40. {
  41. //$num_iid = $args[0];
  42. $body = &$args[1];
  43. $response = json_decode($body, true);
  44. $methods = array('spec');
  45. if(in_array(strtolower($method),$methods) == false) {
  46. return;
  47. }
  48. if($method == 'spec')
  49. {
  50. $item = &$response['item_get_response']['item'];
  51. if($item['approve_status'] != 'onsale') {
  52. return;
  53. }
  54. $cid = $item['cid'];
  55. $num_iid = $item['num_iid'];
  56. $title = $item['title'];
  57. $skus = &$item['skus'];
  58. foreach($skus as $skuex)
  59. {
  60. foreach($skuex as $sku) {
  61. $val = array();
  62. $val['cid'] = $cid;
  63. $val['num_iid'] = $num_iid;
  64. $val['sp_name'] = $sku['properties_name'];
  65. $val['sku_id'] = $sku['sku_id'];
  66. $val['outer_id'] = $sku['outer_id'];
  67. $props = $this->parse_props($sku['properties']);
  68. $val['props'] = empty($props) ? "" : $props[0];
  69. $val['props_val'] = $sku['properties_name'];
  70. $val['title'] = $title;
  71. $val['body'] = $body;
  72. $this->mod_spec_ex->insert($val);
  73. }
  74. }
  75. //把数据添加到商品临时表里面
  76. $this->mod_goods_tmp->insert(array("num_iid" => $num_iid,'body' => $body,'cid' => $cid,'product_id' =>$item['product_id'],'title' => $title));
  77. }
  78. }
  79. function parse_props($props)
  80. {
  81. $data = preg_split("/[:]+/", $props);
  82. $ret = array();
  83. $count = count($data);
  84. for ($i = 0; $i < $count; $i += 2) {
  85. array_push($ret, $data[$i]);
  86. }
  87. return $ret;
  88. }
  89. //根据天猫的商品规格,对应生成规格,并做好分类,类型,规格的映射关系
  90. public function genspec()
  91. {
  92. $datas = $this->mod_spec_ex->group('props')->order('props')->limit(false)->select(); //获取所有独立的规格
  93. $pids = array();
  94. foreach($datas as $item) {
  95. $cid = $item['cid'];
  96. $pid = $item['props'];
  97. $spec_name = $this->get_spec_name($cid, $pid);
  98. //向数据库中添加spec
  99. $val = array('sp_id' => $pid, 'sp_name' => $spec_name);
  100. $this->mod_spec->insert($val);
  101. array_push($pids,$pid);
  102. }
  103. $this->add_spec_val(); //添加规格值,由于规格值是按照分类走的
  104. $this->add_type($pids); //建立类型,以及 规格与类型 的关系
  105. }
  106. private function add_type($pids)
  107. {
  108. //通过分类直接建立类型
  109. $items = $this->mod_spec_ex->field('cid')->group('cid')->limit(false)->select();
  110. foreach($items as $item) {
  111. $cid = $item['cid']; // $cid => $typeid 以分类建立类型
  112. $cnmae = $this->get_class_name($cid);
  113. $this->mod_type->insert(array("type_id" => $cid, "type_name" => $cnmae));
  114. $this->mod_good_class->where(array("gc_id" => $cid))->update(array("type_id" => $cid, "type_name" => $cnmae));
  115. }
  116. foreach ($pids as $pid)
  117. {
  118. //按照分类建立类型
  119. $specexs = $this->mod_spec_ex->group('cid')->where(array('props' => $pid))->limit(false)->select();
  120. foreach($specexs as $specex) {
  121. $cid = $specex['cid']; // $cid => $typeid 以分类建立类型
  122. $this->mod_type_spec->insert(array("type_id" => $cid,"sp_id" => $pid));
  123. }
  124. }
  125. }
  126. private function add_spec_val()
  127. {
  128. $specex = $this->mod_spec_ex->field('cid,props')->group('cid,props')->limit(false)->select();
  129. foreach ($specex as $sp) {
  130. $cid = $sp["cid"];
  131. $pid = $sp["props"];
  132. if($cid == 50013794 && $pid ==1627207) {
  133. $x = 100;
  134. }
  135. $items = $this->get_prop_values($cid,$pid);
  136. foreach($items as $item) {
  137. $spec_val = array();
  138. $spec_val['sp_id'] = $pid;
  139. //$spec_val['sp_value_id'] = $item['vid']; //此处不能赋值,因为sp_value_id 是自索引的
  140. $spec_val['sp_value_name'] = $item['name'];
  141. $spec_val['store_id'] = fetch_config::store_id;
  142. $spec_val['sp_value_color'] = '#ddd9c3';
  143. $spec_val['gc_id'] = $cid;
  144. $spec_val['sp_value_sort'] = 0;
  145. $tm_vid = $item['vid'];
  146. $specval_id = $this->mod_spec_value->insert($spec_val);
  147. if($specval_id) {
  148. $this->mod_spvid_tvid->insert(array('spvid' => $specval_id,'tpvid' => $tm_vid));
  149. }
  150. }
  151. }
  152. }
  153. private function get_class_name($cid)
  154. {
  155. $item = $this->mod_good_class->where(array('gc_id' => $cid))->limit(false)->select();
  156. if(empty($item)) {
  157. return NULL;
  158. } else {
  159. return $item[0]['gc_name'];
  160. }
  161. }
  162. //根据cid 和 pid 取到规格名称。
  163. private function get_spec_name($cid,$pid)
  164. {
  165. $item = $this->mod_item_props->where(array('cid' => $cid,'pid' => $pid))->limit(false)->select();
  166. if(empty($item)) {
  167. return NULL;
  168. } else {
  169. return $item[0]['name'];
  170. }
  171. }
  172. private function get_prop_values($cid,$pid)
  173. {
  174. $items = $this->mod_item_prop_values->where(array('cid' => $cid,'pid' => $pid))->limit(false)->select();
  175. return $items;
  176. }
  177. private function get_expids()
  178. {
  179. $items = $this->mod_spec->field('sp_id')->group('sp_id')->limit(false)->select();
  180. $expids = array();
  181. foreach($items as $item) {
  182. array_push($expids,$item['sp_id']);
  183. }
  184. return $expids;
  185. }
  186. private function get_cids()
  187. {
  188. $items = $this->mod_spec_ex->group('cid')->order('cid')->limit(false)->select();
  189. $cids = array();
  190. foreach ($items as $item) {
  191. array_push($cids,$item['cid']);
  192. }
  193. return $cids;
  194. }
  195. private function get_attrid($cid,$pid)
  196. {
  197. $item = $this->mod_attr_tcpid->where(array("cid" => $cid,"pid" => $pid))->select();
  198. return $item[0]['attr_id'];
  199. }
  200. //需要从天猫的数据表里面排除掉 规格 =》 从而得到属性
  201. public function add_attribute()
  202. {
  203. $expids = $this->get_expids();
  204. $cids = $this->get_cids();
  205. foreach($cids as $cid)
  206. {
  207. $items =$this->mod_item_props->where(array('cid' => $cid))->select();
  208. $pids = array();
  209. foreach($items as $item) {
  210. $pid = $item['pid'];
  211. if(in_array($pid,$expids)) continue;
  212. array_push($pids,$pid);
  213. $name = $item['name'];
  214. $multi = strtolower($item['multi']) == 'true' ? true : false;
  215. $attr_multi = $multi ? "TRUE" : "FALSE";
  216. $attrid = $this->mod_attribute->table('attribute')->insert(array( "attr_name" => $name,"type_id" => $cid,"attr_multi" => $attr_multi,"attr_show" => 1));
  217. if($attrid) { // 建立shopnc attribute id 与 天猫数据 pid + cid 的对应关系
  218. $this->mod_attr_tcpid->insert(array("attr_id" => $attrid, "cid" => $cid, "pid" => $pid));
  219. } else {
  220. // add log...
  221. }
  222. }
  223. foreach($pids as $pid)
  224. {
  225. $tm_valus = $this->mod_item_prop_values->where(array('pid' => $pid,'cid'=>$cid))->select();
  226. foreach($tm_valus as $val)
  227. {
  228. $type_id = $cid;
  229. $attrid = $this->get_attrid($cid,$pid);
  230. $attr_value_name = $val['name'];
  231. $vid = $val['vid'];
  232. $attr_vid = $this->mod_attribute_value->insert(array("attr_id" => $attrid,"type_id" => $type_id, "attr_value_name" => $attr_value_name));
  233. if($attr_vid) {
  234. $this->mod_spvid_tvid->insert(array('spvid' =>$attr_vid,"tpvid" => $vid));
  235. } else {
  236. throw(new Exception("cannot add attribute value."));
  237. }
  238. }
  239. }
  240. }
  241. }
  242. }