wesmiler преди 2 години
родител
ревизия
568a93df49
променени са 3 файла, в които са добавени 64 реда и са изтрити 102 реда
  1. 47 0
      app/Console/Commands/SwooleTask.php
  2. 12 98
      app/Services/Api/GoodsCategoryService.php
  3. 5 4
      app/Services/Api/GoodsService.php

+ 47 - 0
app/Console/Commands/SwooleTask.php

@@ -3,6 +3,7 @@
 namespace App\Console\Commands;
 
 use App\Services\Api\FinanceService;
+use App\Services\Api\GoodsCategoryService;
 use App\Services\Api\GoodsService;
 use App\Services\RedisService;
 use App\Services\WalletService;
@@ -303,6 +304,34 @@ class SwooleTask extends Command
 
         });
 
+        // TODO 更新商品分类
+        \swoole_timer_tick(120000, function ($timer) use ($serv, &$time) { // 启用定时器,每120秒执行一次
+            $date = date('Y-m-d H:i:s');
+            if($time>3600 && file_exists($this->options['log_file'])){
+                $time = 0;
+                file_put_contents($this->options['log_file'],"Task {$date}:清空日志\n");
+            }
+            $time++;
+            $cateIds = GoodsCategoryService::make()->getCateIds();
+            foreach($cateIds as $pid){
+                if(!RedisService::get("caches:task:lock:goods_category_sub_loaded_{$pid}")){
+                    $taskData = [
+                        'taskName' => 'UpdateGoodsCategorySub',
+                        'name' => "更新商品分类【{$pid}】的子分类数据",
+                        'pid'=> $pid,
+                        'date' => date('Y-m-d'),
+                    ];
+                    $res = $serv->task($taskData);
+                    RedisService::set("caches:task:lock:goods_category_sub_loaded_{$pid}", true, rand(3,5));
+                    echo "[Task UpdateGoodsCategorySub {$date}] 更新商品分类【{$pid}】的子分类数据:{$res}\n";
+                }else{
+                    echo "[Task UpdateGoodsCategorySub {$date}] 间隔时间调用\n";
+                }
+            }
+
+
+        });
+
         // TODO 更新商品SKU数据
         \swoole_timer_tick(120000, function ($timer) use ($serv, &$time) { // 启用定时器,每120秒执行一次
             $date = date('Y-m-d H:i:s');
@@ -452,6 +481,24 @@ class SwooleTask extends Command
                     echo "[Task {$taskName} {$date}] 更新商品分类数据结果:{$error}\n";
                 }
                 break;
+            case 'UpdateGoodsCategorySub': // 更新商品分类数据
+                // 时间限制
+                if(date('H:i') >= '00:00' && date('H:i') <= '04:00'){
+                    echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
+                    return false;
+                }
+
+                // 调用处理
+                $pid = isset($data['pid'])? $data['pid'] : 0;
+                if($res = GoodsService::make()->updateGoodsCategory($pid)){
+                    $res = is_array($res) && $res? json_encode($res, 256) : 'success';
+                    echo "[Task {$taskName} {$date}] 更新商品分类数据结果:{$res}\n";
+                }else{
+                    $error = GoodsService::make()->getError();
+                    $error = $error? lang($error) : 'failed';
+                    echo "[Task {$taskName} {$date}] 更新商品分类数据结果:{$error}\n";
+                }
+                break;
             case 'UpdateUsdtPrice': // 更新USDT价格
                 // 时间限制
                 if(date('H:i') >= '01:00' && date('H:i') <= '04:00'){

+ 12 - 98
app/Services/Api/GoodsCategoryService.php

@@ -14,6 +14,7 @@ namespace App\Services\Api;
 use App\Models\GoodsCategoryModel;
 use App\Models\GoodsModel;
 use App\Services\BaseService;
+use App\Services\RedisService;
 
 /**
  * 商品分类管理-服务类
@@ -106,110 +107,23 @@ class GoodsCategoryService extends BaseService
     }
 
     /**
-     * 添加编辑
-     * @return array
-     * @since 2020/11/11
-     * @author laravel开发员
-     */
-    public function edit()
-    {
-        // 请求参数
-        $data = request()->all();
-        // 图标
-        $icon = isset($data['icon']) ? trim($data['icon']) : '';
-        if ($icon && strpos($icon, "temp")) {
-            $data['icon'] = save_image($icon, 'images');
-        } else if ($icon) {
-            $data['icon'] = str_replace(IMG_URL, "", $data['icon']);
-        }
-        return parent::edit($data); // TODO: Change the autogenerated stub
-    }
-
-
-
-
-    /**
-     * 封存/解封
-     * @param $goodsId
-     */
-    public function lock($goodsId)
-    {
-        $params = request()->all();
-        $status = isset($params['status']) ? $params['status'] : 2;
-        $goods  = $this->model->where(['id' => $goodsId, 'mark' => 1])->first();
-        if (empty($goods)) {
-            $this->error = 2061;
-            return false;
-        }
-
-        if ($this->model->where(['id' => $goodsId])->update(['status' => $status, 'update_time' => time(), 'remark' => '店长封存'])) {
-            $this->error = 1002;
-            return true;
-        }
-
-        return false;
-    }
-
-    /**
-     * 修改信息
-     * @param $goodsId
-     * @return bool
-     */
-    public function modify($id)
-    {
-        $params = request()->all();
-        $info  = $this->model->where(['id' => $id, 'mark' => 1])->first();
-        if (empty($info)) {
-            $this->error = 2061;
-            return false;
-        }
-
-        $data = [
-            'name'  => isset($params['name']) ? $params['name'] : '',
-            'type'  => isset($params['type']) ? $params['type'] : 0,
-            'status'  => isset($params['status']) ? $params['status'] : 0,
-            'update_time' => time(),
-        ];
-        $icon = isset($data['icon']) ? trim($data['icon']) : '';
-        if ($icon && strpos($icon, "temp")) {
-            $data['icon'] = save_image($icon, 'images');
-        } else if ($icon) {
-            $data['icon'] = str_replace(IMG_URL, "", $data['icon']);
-        }
-
-        if ($this->model->where(['id' => $id])->update($data)) {
-            $this->error = 1008;
-            return true;
-        }
-
-        $this->error = 1009;
-        return false;
-    }
-
-    /**
-     * 删除
-     * @return array
+     * 获取主分类ID
+     * @return array|mixed
      */
-    public function delete()
+    public function getCateIds()
     {
-        // 参数
-        $param = request()->all();
-        $ids   = getter($param, "id");
-        if (empty($ids)) {
-            return message("记录ID不能为空", false);
+        $cacheKey = "caches:goods:category_ids";
+        $datas = RedisService::get($cacheKey);
+        if($datas){
+            return $datas;
         }
 
-        $ids = is_array($ids) ? $ids : [$ids];
-
-        if(GoodsModel::whereIn('cate_id', $ids)->count()){
-            $this->error = 2541;
-            return false;
+        $datas = $this->model->where(['status'=>1,'pid'=>0,'mark'=>1])->orderBy('create_time','asc')->pluck('cate_id');
+        if($datas){
+            RedisService::set($cacheKey, $datas, rand(1200, 3600));
         }
 
-        $result = parent::delete(); // TODO: Change the autogenerated stub
-        $success = isset($result['success'])? $result['success'] : false;
-        return $success? true : false;
+        return $datas;
     }
 
-
 }

+ 5 - 4
app/Services/Api/GoodsService.php

@@ -333,14 +333,15 @@ class GoodsService extends BaseService
 
     /**
      * 更新商品分类
+     * @param int $pid 上级ID
      * @param int $pageSize
      * @param $params 参数
      * @return array|false
      */
-    public function updateGoodsCategory($pageSize = 200, $params = [])
+    public function updateGoodsCategory($pid=0, $pageSize = 200, $params = [])
     {
         set_time_limit(0);
-        $cacheKey = "caches:supply:goods_category_update_{$pageSize}";
+        $cacheKey = "caches:supply:goods_category_update_{$pid}_{$pageSize}";
         if (RedisService::get($cacheKey)) {
             $this->error = 1047;
             return false;
@@ -349,7 +350,7 @@ class GoodsService extends BaseService
         $params = [
             'limit' => $pageSize > 0 ? $pageSize : 50,
             'page' => 1,
-            'pid' => isset($params['pid']) ? $params['pid'] : 0, // 上级ID
+            'pid' => $pid, // 上级ID
         ];
         $categorys = [];
         $updated = 0;
@@ -379,7 +380,7 @@ class GoodsService extends BaseService
         }
 
 
-        return ['count' => count($categorys), 'updated' => $updated, 'errorCount' => $error];
+        return ['count' => count($categorys), 'updated' => $updated,'pid'=>$pid, 'errorCount' => $error];
     }
 
     /**