brand_operator.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/1/14
  6. * Time: 下午6:10
  7. */
  8. class brand_operator
  9. {
  10. private $tm_prop_values;
  11. private $cls_tree;
  12. private $mod_brand;
  13. private $tm_goods;
  14. private $mod_goods_common;
  15. private $mod_goods;
  16. const brand_pid = 20000;
  17. public function __construct()
  18. {
  19. $this->tm_goods = Model('tm_goods');
  20. $this->tm_prop_values = Model('tm_prop_values');
  21. $this->cls_tree = new class_tree();
  22. $this->mod_brand = Model('brand');
  23. $this->mod_goods_common = Model('goods_common');
  24. $this->mod_goods = Model('goods');
  25. }
  26. private function brand_exist($name)
  27. {
  28. $result = $this->mod_brand->field('brand_id')->where(array('brand_tm_name' => array('like',"%{$name}%")))->select();
  29. if(empty($result) || count($result) == 0) {
  30. return false;
  31. } else {
  32. return true;
  33. }
  34. }
  35. public function create()
  36. {
  37. $brands = $this->tm_prop_values->field('id,cid,name')->where(array('pid' => self::brand_pid))->group('name')->select();
  38. foreach($brands as $val)
  39. {
  40. $name = $val['name'];
  41. $cid = $val['cid'];
  42. if($this->brand_exist($name)) continue;
  43. if($this->cls_tree->get_info($cid,$c_1,$c_2,$c_3,$cls_name))
  44. {
  45. $ret = $this->mod_brand->insert(array('brand_name' => $name,'brand_tm_name' => $name,'brand_class' => $cls_name));
  46. if($ret == false) {
  47. Log::record("insert brand error brand_name = brand_tm_name = {$name}",Log::ERR);
  48. } else {
  49. Log::record("brand_name = {$name} brand_class = {$cls_name}.",Log::DEBUG);
  50. }
  51. }
  52. }
  53. }
  54. private function get_brand($propnames,&$brand_name)
  55. {
  56. $ret = $this->brand_name($propnames);
  57. if(!array_key_exists(self::brand_pid,$ret)) {
  58. return false;
  59. } else {
  60. foreach($ret[self::brand_pid]['val'] as $key => $val) {
  61. $brand_name = $val;
  62. break;
  63. }
  64. }
  65. if(isset($brand_name) && !empty($brand_name))
  66. {
  67. $result = $this->mod_brand->where(array('brand_tm_name' => array('like',"%{$brand_name}%")))->select();
  68. if(!empty($result) && count($result) > 0) {
  69. return $result[0]['brand_id'];
  70. }
  71. }
  72. return false;
  73. }
  74. public function update_brand()
  75. {
  76. $results = $this->tm_goods->field('body,title,num_iid')->limit(false)->select();
  77. foreach ($results as $result)
  78. {
  79. $num_iid = $result['num_iid'];
  80. $body = $result['body'];
  81. $response = json_decode($body, true);
  82. $item = $response['item_seller_get_response']['item'];
  83. $props_name = $item['props_name'];
  84. $brand_id = $this->get_brand($props_name,$brand_name);
  85. if($brand_id)
  86. {
  87. $ret = $this->mod_goods_common->where(array('num_iid' => $num_iid))->update(array('brand_id' => $brand_id,'brand_name' => $brand_name));
  88. if($ret == false) {
  89. Log::record("update goods_common brand error: num_iid={$num_iid},brand_id={$brand_id},brand_name={$brand_name}");
  90. } else {
  91. $ret = $this->mod_goods->where(array('num_iid' => $num_iid))->update(array('brand_id' => $brand_id));
  92. }
  93. } else {
  94. Log::record("update goods_common brand error: cannot find brand_id num_iid={$num_iid}");
  95. }
  96. }
  97. }
  98. /**
  99. * @param $propnames
  100. * @return array
  101. */
  102. private function brand_name($propnames)
  103. {
  104. $spec_vals = preg_split("/[;]+/", $propnames);
  105. $ret = array();
  106. foreach ($spec_vals as $sv)
  107. {
  108. $data = preg_split("/[:]+/", $sv);
  109. if (!empty($data)) {
  110. $tmpid = $data[0];
  111. $tmpvid = $data[1];
  112. $tmpname = $data[2];
  113. $tmvname = $data[3];
  114. $ret[$tmpid]['name'] = $tmpname;
  115. $ret[$tmpid]['val'][$tmpvid] = $tmvname;
  116. }
  117. }
  118. return $ret;
  119. }
  120. }