category.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 15/11/3
  6. * Time: 下午7:22
  7. */
  8. class category
  9. {
  10. private $tm_cats;
  11. private $goods_class;
  12. private $tm_goods;
  13. public function __construct()
  14. {
  15. $this->tm_cats = Model('tm_cats');
  16. $this->goods_class = Model('goods_class');
  17. $this->tm_goods = Model('tm_goods');
  18. }
  19. private function insert($catdatas)
  20. {
  21. foreach($catdatas as &$val) {
  22. $item = array();
  23. $item['gc_id'] = $val['cid'];
  24. $item['gc_name'] = $val['name'];
  25. $item['type_id'] = 37;
  26. $item['type_name'] = '';
  27. $item['gc_parent_id'] = $val['parent_cid'];
  28. $item['commis_rate'] = 0;
  29. $item['gc_sort'] = $val['sort_order'];
  30. $item['gc_virtual'] = 0;
  31. $this->goods_class->insert($item);
  32. $cids[] = (int)$val['cid'];
  33. }
  34. return $cids;
  35. }
  36. private function insert_all($pcids)
  37. {
  38. if(empty($pcids)) return;
  39. foreach ($pcids as $pcid) {
  40. $datas = $this->tm_cats->field('*')->limit(false)->where(array('parent_cid'=> $pcid))->select();
  41. $cids = $this->insert($datas);
  42. $this->insert_all($cids);
  43. }
  44. }
  45. private function get_cids()
  46. {
  47. $items = $this->tm_goods->group('cid')->field('cid')->limit(false)->select();
  48. $cids = array();
  49. foreach($items as $val) {
  50. array_push($cids,$val['cid']);
  51. }
  52. return $cids;
  53. }
  54. public function proc()
  55. {
  56. $cids = $this->get_cids();
  57. foreach($cids as $cid) {
  58. $this->import_leaf($cid);
  59. }
  60. }
  61. private function get_goods_cidinfo($cid)
  62. {
  63. $items = $this->goods_class->field('*')->where(array('gc_id' => $cid))->limit(false)->select();
  64. if(empty($items)) {
  65. return NULL;
  66. } else {
  67. return $items[0];
  68. }
  69. }
  70. private function get_tm_cidinfo($cid)
  71. {
  72. $items = $this->tm_cats->field('*')->where(array('cid' => $cid))->limit(false)->select();
  73. if(empty($items)) {
  74. return NULL;
  75. } else {
  76. return $items[0];
  77. }
  78. }
  79. private function import_leaf($cid)
  80. {
  81. if($cid == 0 || !empty($this->get_goods_cidinfo($cid))) {
  82. return;
  83. }
  84. else
  85. {
  86. $val = $this->get_tm_cidinfo($cid);
  87. $item['gc_id'] = $val['cid'];
  88. $item['gc_name'] = $val['name'];
  89. $item['gc_parent_id'] = $val['parent_cid'];
  90. $item['commis_rate'] = 0;
  91. $item['gc_sort'] = $val['sort_order'];
  92. $item['gc_virtual'] = 0;
  93. $id = $this->goods_class->insert($item);
  94. if($id == false) {
  95. Log::record("insert cid = {$cid} error.",Log::ERR);
  96. }
  97. $this->import_leaf($item['gc_parent_id']);
  98. }
  99. }
  100. public function procex()
  101. {
  102. if(empty($this->topcats)) return;
  103. foreach ($this->topcats as $cid) {
  104. $datas = $this->tm_cats->field('*')->limit(false)->where(array('cid'=>$cid,'parent_cid'=> 0))->select();
  105. $this->insert($datas);
  106. }
  107. $this->insert_all($this->topcats);
  108. }
  109. }