spec_attr.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 15/11/7
  6. * Time: 下午1:46
  7. */
  8. class spec_attr
  9. {
  10. private $tm_spec;
  11. private $tm_props;
  12. private $tm_prop_values;
  13. private $mod_spec;
  14. private $mod_spec_value;
  15. private $mod_good_class;
  16. private $mod_type;
  17. private $mod_type_spec;
  18. private $tm_spvid_tpvid; //spec value id => tmall props vid
  19. private $mod_attribute;
  20. private $tm_cpid_attrid;
  21. private $mod_attribute_value;
  22. private $tm_tvid_attrvid;
  23. public function __construct()
  24. {
  25. $this->tm_spec = Model('tm_spec');
  26. $this->tm_props = Model('tm_props');
  27. $this->tm_prop_values = Model('tm_prop_values');
  28. $this->mod_good_class = Model('goods_class');
  29. $this->mod_spec = Model('spec');
  30. $this->mod_type = Model('type');
  31. $this->mod_type_spec = Model('type_spec');
  32. $this->mod_spec_value = Model('spec_value');
  33. $this->tm_spvid_tpvid = Model('tm_spvid_tpvid');
  34. $this->mod_attribute = Model('attribute');
  35. $this->tm_cpid_attrid = Model('tm_cpid_attrid');
  36. $this->mod_attribute_value = Model('attribute_value');
  37. $this->tm_tvid_attrvid = Model('tm_tvid_attrvid');
  38. }
  39. //根据天猫的商品规格,对应生成规格,并做好分类,类型,规格的映射关系
  40. public function proc()
  41. {
  42. $this->add_spec(); //建立规格
  43. $this->add_type(); //建立类型,以及 规格与类型 的关系
  44. $this->add_type_spec();
  45. $this->add_spec_val(); //添加规格值,由于规格值是按照分类走的
  46. $this->add_attribute();
  47. }
  48. private function add_spec()
  49. {
  50. $datas = $this->tm_spec->group('props')->order('props')->limit(false)->select(); //获取所有独立的规格
  51. $pids = array();
  52. foreach ($datas as $item) {
  53. $cid = $item['cid'];
  54. $pid = $item['props'];
  55. $spec_name = $this->get_tm_props_name($cid, $pid);
  56. $sp_id = $this->mod_spec->insert(array('sp_id' => $pid, 'sp_name' => $spec_name));
  57. if($sp_id != $pid) {
  58. Log::record(__FUNCTION__ . ':' . __LINE__ . " cannt find prop values from $cid $pid",Log::ERR);
  59. } else {
  60. array_push($pids, $pid);
  61. }
  62. }
  63. }
  64. private function add_type()
  65. {
  66. //通过分类直接建立类型
  67. $items = $this->tm_spec->field('cid')->group('cid')->limit(false)->select();
  68. foreach($items as $item) {
  69. $cid = $item['cid']; // $cid => $typeid 以分类建立类型
  70. $cnmae = $this->get_class_name($cid);
  71. $this->mod_type->insert(array("type_id" => $cid, "type_name" => $cnmae));
  72. $this->mod_good_class->where(array("gc_id" => $cid))->update(array("type_id" => $cid, "type_name" => $cnmae));
  73. }
  74. }
  75. private function add_type_spec()
  76. {
  77. //按照分类建立类型
  78. $items = $this->tm_spec->field('cid,props')->group('cid,props')->limit(false)->select();
  79. foreach($items as $item) {
  80. $cid = $item['cid']; // $cid => $typeid 以分类建立类型
  81. $pid = $item['props'];
  82. $this->mod_type_spec->insert(array("type_id" => $cid,"sp_id" => $pid));
  83. }
  84. }
  85. private function get_prop_name($cid,$pid,$vid)
  86. {
  87. $items = $this->tm_prop_values->where(array('cid' => $cid,'pid' => $pid,'vid' => $vid))->limit(false)->select();
  88. return $items[0]['name'];
  89. }
  90. private function add_spec_val()
  91. {
  92. $specex = $this->tm_spec->field('cid,props')->group('cid,props')->limit(false)->select();
  93. foreach ($specex as $sp)
  94. {
  95. $cid = $sp["cid"];
  96. $pid = $sp["props"];
  97. $items = $this->get_prop_values($cid,$pid);
  98. if(empty($items)) {
  99. Log::record(__FUNCTION__ . ':' . __LINE__ . " cannt find prop values from $cid $pid",Log::ERR);
  100. }
  101. else
  102. {
  103. foreach($items as $item)
  104. {
  105. $spec_val = array();
  106. $spec_val['sp_id'] = $pid;
  107. $spec_val['sp_value_name'] = $item['name'];
  108. $spec_val['store_id'] = fetch_config::store_id;
  109. $spec_val['sp_value_color'] = '#ddd9c3';
  110. $spec_val['gc_id'] = $cid;
  111. $spec_val['sp_value_sort'] = 0;
  112. $tm_vid = $item['vid'];
  113. $specval_id = $this->mod_spec_value->insert($spec_val);
  114. if($specval_id)
  115. {
  116. $ret = $this->tm_spvid_tpvid->insert(array('spvid' => $specval_id,'tpcid' => $cid, 'tppid' => $pid,'tpvid' => $tm_vid));
  117. if(empty($ret)) {
  118. Log::record(__FUNCTION__ . ':' . __LINE__,Log::ERR);
  119. }
  120. } else {
  121. Log::record(__FUNCTION__ . ':' . __LINE__,Log::ERR);
  122. }
  123. }
  124. }
  125. }
  126. }
  127. private function get_class_name($cid)
  128. {
  129. $item = $this->mod_good_class->where(array('gc_id' => $cid))->limit(false)->select();
  130. if(empty($item)) {
  131. return NULL;
  132. } else {
  133. return $item[0]['gc_name'];
  134. }
  135. }
  136. //根据cid 和 pid 取到规格名称。
  137. private function get_tm_props_name($cid,$pid)
  138. {
  139. $item = $this->tm_props->where(array('cid' => $cid,'pid' => $pid))->limit(false)->select();
  140. if(empty($item)) {
  141. return NULL;
  142. } else {
  143. return $item[0]['name'];
  144. }
  145. }
  146. private function get_prop_values($cid,$pid)
  147. {
  148. $items = $this->tm_prop_values->where(array('cid' => $cid,'pid' => $pid))->limit(false)->select();
  149. return $items;
  150. }
  151. private function gen_attr_value($cid,$pid)
  152. {
  153. $items = $this->tm_prop_values->field('pid,name')->where(array('cid' => $cid,'pid' => $pid))->order('vid')->limit(false)->select();
  154. $ret = array();
  155. foreach($items as $item) {
  156. array_push($ret,$item['name']);
  157. }
  158. return implode(',',$ret);
  159. }
  160. //需要从天猫的数据表里面排除掉 规格 =》 从而得到属性
  161. public function add_attribute()
  162. {
  163. $cids = $this->get_cids();
  164. //$cids[] = 50013794;
  165. foreach($cids as $cid)
  166. {
  167. $expids = $this->get_specids($cid);
  168. $items =$this->tm_props->where(array('cid' => $cid))->limit(false)->select();
  169. $pids = array();
  170. foreach($items as $item)
  171. {
  172. $pid = $item['pid'];
  173. if(in_array($pid,$expids)) continue;
  174. if(empty($pid)) continue;
  175. array_push($pids,$pid);
  176. $name = $item['name'];
  177. $multi = strtolower($item['multi']) == 'true' ? true : false;
  178. $attr_multi = $multi ? 1 : 0;
  179. $attr_value = $this->gen_attr_value($cid,$pid);
  180. $attrid = $this->mod_attribute->table('attribute')->insert(array( "attr_name" => $name,"type_id" => $cid,
  181. 'attr_value' => $attr_value, "attr_multi" => $attr_multi,"attr_show" => 1));
  182. if($attrid)
  183. { // 建立shopnc attribute id 与 天猫数据 pid + cid 的对应关系
  184. $rid = $this->tm_cpid_attrid->insert(array("attr_id" => $attrid, "cid" => $cid, "pid" => $pid));
  185. if(empty($rid)) {
  186. throw new Exception('insert error');
  187. } else {
  188. $this->add_attr_value($pid,$cid,$attrid);
  189. }
  190. } else {
  191. // add log...
  192. }
  193. }
  194. }
  195. }
  196. private function add_attr_value($pid,$cid,$attrid)
  197. {
  198. Log::record("pid = $pid,cid=$cid,attrid = $attrid");
  199. $tm_valus = $this->tm_prop_values->field('name,vid')->where(array('pid' => $pid,'cid'=>$cid))->limit(false)->select();
  200. foreach($tm_valus as $val)
  201. {
  202. $type_id = $cid;
  203. $attr_value_name = $val['name'];
  204. $vid = $val['vid'];
  205. $attr_vid = $this->mod_attribute_value->insert(array("attr_id" => $attrid,"type_id" => $type_id, "attr_value_name" => $attr_value_name));
  206. if($attr_vid)
  207. {
  208. $rid = $this->tm_tvid_attrvid->insert(array('attr_vid' =>$attr_vid,'tcid' => $cid,'tpid' => $pid,'tvid' => $vid));
  209. if(empty($rid)) {
  210. //throw new Exception('insert tm_tvid_attrvid error');
  211. }
  212. } else {
  213. //throw(new Exception("cannot add attribute value."));
  214. }
  215. }
  216. }
  217. private function get_specids($typeid)
  218. {
  219. $ret = array();
  220. $items = $this->mod_type_spec->field('type_id,sp_id')->where(array('type_id' => $typeid))->limit(false)->select();
  221. foreach($items as $item) {
  222. array_push($ret,(int)$item['sp_id']);
  223. }
  224. return $ret;
  225. }
  226. private function get_cids()
  227. {
  228. $cids = array();
  229. $items = $this->tm_spec->group('cid')->order('cid')->limit(false)->select();
  230. foreach ($items as $item) {
  231. array_push($cids,$item['cid']);
  232. }
  233. return $cids;
  234. }
  235. }