spec_attr.php 9.2 KB

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