| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- namespace addons\QiJuhe\common\service;
- use addons\JuheShop\common\enums\JuheShopEnum;
- use addons\JuheShop\common\listeners\GoodsListener;
- use addons\QiJuhe\common\models\Category;
- use common\enums\RabbitMqEnum;
- use yii\base\BaseObject;
- use Yii;
- class CategoryService extends BaseObject
- {
- public $mall_id = 0;
- public $supply_id = 0;
- public $error = '未知错误';
- public function __construct (array $config = [])
- {
- parent::__construct($config);
- }
- // 初始化分类
- public function initCategory(){
- $this->getCategory(0);
- $cate_ids = Category::getAllCateId();
- foreach ($cate_ids as $id) {
- $this->getCategory($id);
- }
- }
- /**
- * 新增/更新 api接口的分类
- * @param int $parent_id 0 时获取全部一级分类,有子分类时异步任务去获取
- * @return bool
- */
- public function getCategory(int $parent_id = 0,$is_savedb = false)
- {
- $api_conn = ApiService::getConnect($this->mall_id,$this->supply_id);
- $this->supply_id = $api_conn->supply_id;
- $data = $api_conn->getCategory($parent_id);
- $is_savedb && $this->_handel_cate($data['list'],$parent_id);
- return $data;
- }
- // 异步任务获取子分类
- public static function getChildren(array $data):bool
- {
- $mall_id = $data['mall_id'] ?? 0;
- $parent_id = $data['parent_id'] ?? 0;
- $key = $data['key'] ?? 0;
- if($mall_id <= 0 ) return true;
- echo $key.PHP_EOL;
- $obj = new self(['mall_id'=>$mall_id]);
- $obj->getCategory($parent_id);
- return true;
- }
- public function task($mall_id,$supply_id,$parent_id = 0){
- Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE,[
- 'mall_id' => $mall_id,
- 'supply_id' => $supply_id,
- 'parent_id' => $parent_id,
- ], self::class, 'getChildren');
- return true;
- }
- // 处理分类
- private function _handel_cate(array $cates,int $parent_id = 0):bool
- {
- if (empty($cates)) return true;
- $cate_ids = Category::getAllCateId($parent_id);
- $insert = [];
- $children = [];
- foreach ($cates as $cate) {
- if($cate['hasChildrens']){
- $children[] = $cate['id'];
- }
- if(isset($cate_ids[$cate['id']])){
- // 更新
- Category::updateCate($cate);
- }else{
- // 新增
- $c = Category::filterField($cate);
- $c['supply_id'] = $this->supply_id;
- $c['category_id'] = $c['id'];
- $insert [] = $c;
- }
- }
- if(!empty($insert)){
- Category::batchAdd($insert);
- }
- if(!empty($children)){
- // 有子分类
- foreach ($children as $key=>$parent_id) {
- $this->task($this->mall_id,$this->supply_id,$parent_id);
- }
- }
- return true;
- }
- public function import($mall_id,$supply_id,$parent_id = 0){
- $key = "qi_juhe:category_import:{$mall_id}_{$supply_id}";
- $is_repeat = Yii::$app->redis->get($key);
- if(!empty($is_repeat)){
- $this->error = '10分钟内,不能重复导入,请稍后重试';
- return false;
- }
- // 队列去获取
- $this->task($mall_id,$supply_id,$parent_id);
- Yii::$app->redis->setex($key,600,time());
- return true;
- }
- /**
- * @param int $supply_id 来源0自营2京东6阿里巴巴7天猫
- * @param int $level
- * @param int $parent_id
- * time:2021-10-09-21:09
- * user: 林深时见鹿
- * title:
- * desc:
- *
- * @return array
- */
- public function getList($supply_id = 0, $level = 1, $parent_id = 0)
- {
- $model = Category::find();
- $model->andWhere([
- 'supply_id' => $supply_id,
- 'is_display' => 1,
- ]);
- // $parent_id <= 0 && $model->andWhere([
- // 'level' => $level,
- // ]);
- $parent_id > 0 && $model->andWhere([
- 'parent_id' => $parent_id,
- ]);
- $model->orderBy('sort desc,category_id asc');
- $list = $model->asArray()->all();
- return $list;
- }
- }
|