category.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. echo("gc_id = $cid cannot get info from goods_class.\r\n");
  66. return NULL;
  67. } else {
  68. return $items[0];
  69. }
  70. }
  71. private function get_tm_cidinfo($cid)
  72. {
  73. $items = $this->tm_cats->field('*')->where(array('cid' => $cid))->limit(false)->select();
  74. if(empty($items)) {
  75. echo "cid = $cid cannot get info from tm_cats.\r\n";
  76. return NULL;
  77. } else {
  78. return $items[0];
  79. }
  80. }
  81. private function import_leaf($cid)
  82. {
  83. if($cid == 0 || !empty($this->get_goods_cidinfo($cid))) {
  84. return;
  85. }
  86. else
  87. {
  88. $val = $this->get_tm_cidinfo($cid);
  89. $item['gc_id'] = $val['cid'];
  90. $item['gc_name'] = $val['name'];
  91. $item['gc_parent_id'] = $val['parent_cid'];
  92. $item['commis_rate'] = 0;
  93. $item['gc_sort'] = $val['sort_order'];
  94. $item['gc_virtual'] = 0;
  95. $id = $this->goods_class->insert($item);
  96. if($id == false) {
  97. echo "insert cid = $cid error.";
  98. }
  99. $this->import_leaf($item['gc_parent_id']);
  100. }
  101. }
  102. public function procex()
  103. {
  104. if(empty($this->topcats)) return;
  105. foreach ($this->topcats as $cid) {
  106. $datas = $this->tm_cats->field('*')->limit(false)->where(array('cid'=>$cid,'parent_cid'=> 0))->select();
  107. $this->insert($datas);
  108. }
  109. $this->insert_all($this->topcats);
  110. }
  111. }