CategoryService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. namespace addons\QiJuhe\common\service;
  3. use addons\JuheShop\common\enums\JuheShopEnum;
  4. use addons\JuheShop\common\listeners\GoodsListener;
  5. use addons\QiJuhe\common\models\Category;
  6. use common\enums\RabbitMqEnum;
  7. use yii\base\BaseObject;
  8. use Yii;
  9. class CategoryService extends BaseObject
  10. {
  11. public $mall_id = 0;
  12. public $supply_id = 0;
  13. public $error = '未知错误';
  14. public function __construct (array $config = [])
  15. {
  16. parent::__construct($config);
  17. }
  18. // 初始化分类
  19. public function initCategory(){
  20. $this->getCategory(0);
  21. $cate_ids = Category::getAllCateId();
  22. foreach ($cate_ids as $id) {
  23. $this->getCategory($id);
  24. }
  25. }
  26. /**
  27. * 新增/更新 api接口的分类
  28. * @param int $parent_id 0 时获取全部一级分类,有子分类时异步任务去获取
  29. * @return bool
  30. */
  31. public function getCategory(int $parent_id = 0,$is_savedb = false)
  32. {
  33. $api_conn = ApiService::getConnect($this->mall_id,$this->supply_id);
  34. $this->supply_id = $api_conn->supply_id;
  35. $data = $api_conn->getCategory($parent_id);
  36. $is_savedb && $this->_handel_cate($data['list'],$parent_id);
  37. return $data;
  38. }
  39. // 异步任务获取子分类
  40. public static function getChildren(array $data):bool
  41. {
  42. $mall_id = $data['mall_id'] ?? 0;
  43. $parent_id = $data['parent_id'] ?? 0;
  44. $key = $data['key'] ?? 0;
  45. if($mall_id <= 0 ) return true;
  46. echo $key.PHP_EOL;
  47. $obj = new self(['mall_id'=>$mall_id]);
  48. $obj->getCategory($parent_id);
  49. return true;
  50. }
  51. public function task($mall_id,$supply_id,$parent_id = 0){
  52. Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE,[
  53. 'mall_id' => $mall_id,
  54. 'supply_id' => $supply_id,
  55. 'parent_id' => $parent_id,
  56. ], self::class, 'getChildren');
  57. return true;
  58. }
  59. // 处理分类
  60. private function _handel_cate(array $cates,int $parent_id = 0):bool
  61. {
  62. if (empty($cates)) return true;
  63. $cate_ids = Category::getAllCateId($parent_id);
  64. $insert = [];
  65. $children = [];
  66. foreach ($cates as $cate) {
  67. if($cate['hasChildrens']){
  68. $children[] = $cate['id'];
  69. }
  70. if(isset($cate_ids[$cate['id']])){
  71. // 更新
  72. Category::updateCate($cate);
  73. }else{
  74. // 新增
  75. $c = Category::filterField($cate);
  76. $c['supply_id'] = $this->supply_id;
  77. $c['category_id'] = $c['id'];
  78. $insert [] = $c;
  79. }
  80. }
  81. if(!empty($insert)){
  82. Category::batchAdd($insert);
  83. }
  84. if(!empty($children)){
  85. // 有子分类
  86. foreach ($children as $key=>$parent_id) {
  87. $this->task($this->mall_id,$this->supply_id,$parent_id);
  88. }
  89. }
  90. return true;
  91. }
  92. public function import($mall_id,$supply_id,$parent_id = 0){
  93. $key = "qi_juhe:category_import:{$mall_id}_{$supply_id}";
  94. $is_repeat = Yii::$app->redis->get($key);
  95. if(!empty($is_repeat)){
  96. $this->error = '10分钟内,不能重复导入,请稍后重试';
  97. return false;
  98. }
  99. // 队列去获取
  100. $this->task($mall_id,$supply_id,$parent_id);
  101. Yii::$app->redis->setex($key,600,time());
  102. return true;
  103. }
  104. /**
  105. * @param int $supply_id 来源0自营2京东6阿里巴巴7天猫
  106. * @param int $level
  107. * @param int $parent_id
  108. * time:2021-10-09-21:09
  109. * user: 林深时见鹿
  110. * title:
  111. * desc:
  112. *
  113. * @return array
  114. */
  115. public function getList($supply_id = 0, $level = 1, $parent_id = 0)
  116. {
  117. $model = Category::find();
  118. $model->andWhere([
  119. 'supply_id' => $supply_id,
  120. 'is_display' => 1,
  121. ]);
  122. // $parent_id <= 0 && $model->andWhere([
  123. // 'level' => $level,
  124. // ]);
  125. $parent_id > 0 && $model->andWhere([
  126. 'parent_id' => $parent_id,
  127. ]);
  128. $model->orderBy('sort desc,category_id asc');
  129. $list = $model->asArray()->all();
  130. return $list;
  131. }
  132. }