wesmiler 2 rokov pred
rodič
commit
ce5c8fd24a

+ 135 - 14
app/Console/Commands/SwooleTask.php

@@ -2,6 +2,8 @@
 
 namespace App\Console\Commands;
 
+use App\Services\Api\FinanceService;
+use App\Services\Api\GoodsService;
 use App\Services\RedisService;
 use Illuminate\Console\Command;
 use Illuminate\Support\Facades\DB;
@@ -231,24 +233,94 @@ class SwooleTask extends Command
             file_put_contents($this->options['log_file'],"Task {$date}:清空日志\n");
         }
 
-        \swoole_timer_tick(300000, function ($timer) use ($serv, &$time) { // 启用定时器,每300秒执行一次
+        // 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++;
-            if(!RedisService::get('caches:task:lock:music_loaded')){
+            if(!RedisService::get('caches:task:lock:goods_loaded')){
                 $taskData = [
-                    'taskName' => 'bgmMusic',
-                    'name' => "背景音乐写表处理",
+                    'taskName' => 'UpdateGoods',
+                    'name' => "更新商品数据",
                     'date' => date('Y-m-d'),
                 ];
                 $res = $serv->task($taskData);
-                RedisService::set('caches:task:lock:music_loaded', true, rand(3,5));
-                echo "[Task bgmMusic {$date}] 背景音乐写表处理:{$res}\n";
+                RedisService::set('caches:task:lock:goods_loaded', true, rand(3,5));
+                echo "[Task UpdateGoods {$date}] 更新商品数据:{$res}\n";
             }else{
-                echo "[Task bgmMusic {$date}] 间隔时间调用\n";
+                echo "[Task UpdateGoods {$date}] 间隔时间调用\n";
+            }
+
+        });
+
+        // TODO 更新商品SKU数据
+        \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++;
+            if(!RedisService::get('caches:task:lock:goods_sku_loaded')){
+                $taskData = [
+                    'taskName' => 'UpdateGoodsSku',
+                    'name' => "更新商品SKU数据",
+                    'date' => date('Y-m-d'),
+                ];
+                $res = $serv->task($taskData);
+                RedisService::set('caches:task:lock:goods_sku_loaded', true, rand(3,5));
+                echo "[Task UpdateGoodsSku {$date}] 更新商品SKU数据:{$res}\n";
+            }else{
+                echo "[Task UpdateGoodsSku {$date}] 间隔时间调用\n";
+            }
+
+        });
+
+        // TODO 发放积分
+        \swoole_timer_tick(1200000, function ($timer) use ($serv, &$time) { // 启用定时器,每1200秒执行一次
+            $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++;
+            if(!RedisService::get('caches:task:lock:grant_score_loaded')){
+                $taskData = [
+                    'taskName' => 'GrantScore',
+                    'name' => "每日发放积分",
+                    'date' => date('Y-m-d'),
+                ];
+                $res = $serv->task($taskData);
+                RedisService::set('caches:task:lock:grant_score_loaded', true, rand(3,5));
+                echo "[Task GrantScore {$date}] 每日发放积分:{$res}\n";
+            }else{
+                echo "[Task GrantScore {$date}] 间隔时间调用\n";
+            }
+
+        });
+
+        // TODO 待返积分返还
+        \swoole_timer_tick(1200000, function ($timer) use ($serv, &$time) { // 启用定时器,每1200秒执行一次
+            $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++;
+            if(!RedisService::get('caches:task:lock:waitt_score_loaded')){
+                $taskData = [
+                    'taskName' => 'ReturnWaitScore',
+                    'name' => "待返积分每日返还",
+                    'date' => date('Y-m-d'),
+                ];
+                $res = $serv->task($taskData);
+                RedisService::set('caches:task:lock:waitt_score_loaded', true, rand(3,5));
+                echo "[Task ReturnWaitScore {$date}] 待返积分每日返还:{$res}\n";
+            }else{
+                echo "[Task ReturnWaitScore {$date}] 间隔时间调用\n";
             }
 
         });
@@ -282,24 +354,73 @@ class SwooleTask extends Command
         $date = date('Y-m-d H:i:s');
         $taskName = isset($data['taskName']) ? $data['taskName'] : '';
         switch ($taskName) {
-            case 'OrderSettle': // 订单结算
+            case 'UpdateGoods': // 更新商品
+                // 时间限制
+                if(date('H:i') >= '00:00' && date('H:i') <= '03:00'){
+                    echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
+                    return false;
+                }
+
+                // 调用处理
+                if($res = GoodsService::make()->updateGoods()){
+                    $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 'UpdateGoodsSku': // 更新商品SKu数据
+                // 时间限制
+                if(date('H:i') >= '00:00' && date('H:i') <= '03:00'){
+                    echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
+                    return false;
+                }
+
+                // 调用处理
+                if($res = GoodsService::make()->updateGoodsSku()){
+                    $res = is_array($res) && $res? json_encode($res, 256) : 'success';
+                    echo "[Task {$taskName} {$date}] 更新商品SKu数据结果:{$res}\n";
+                }else{
+                    $error = GoodsService::make()->getError();
+                    $error = $error? lang($error) : 'failed';
+                    echo "[Task {$taskName} {$date}] 更新商品SKu数据结果:{$error}\n";
+                }
+                break;
+            case 'GrantScore':
                 // 时间限制
-                if(date('H:i') >= env('SETTLE_TIME','06:00')){
+                if(date('H:i') >= '09:00'){
                     echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
                     return false;
                 }
 
                 // 调用处理
-                if($res = WalletService::make()->orderSettle()){
+                if($res = FinanceService::make()->grantScore()){
                     $res = is_array($res) && $res? json_encode($res, 256) : 'success';
-                    echo "[Task {$taskName} {$date}] 订单结算结果:{$res}\n";
+                    echo "[Task {$taskName} {$date}] 每日发放积分结果:{$res}\n";
                 }else{
-                    $error = BonusService::make()->getError();
+                    $error = FinanceService::make()->getError();
                     $error = $error? lang($error) : 'failed';
-                    echo "[Task {$taskName} {$date}] 订单结算结果:{$error}\n";
+                    echo "[Task {$taskName} {$date}] 每日发放积分结果:{$error}\n";
                 }
                 break;
-            case 'bgmMusic':
+            case 'ReturnWaitScore':
+                // 时间限制
+                if(date('H:i') >= '10:00'){
+                    echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
+                    return false;
+                }
+
+                // 调用处理
+                if($res = FinanceService::make()->returnWaitScore()){
+                    $res = is_array($res) && $res? json_encode($res, 256) : 'success';
+                    echo "[Task {$taskName} {$date}] 待返积分每日返还结果:{$res}\n";
+                }else{
+                    $error = FinanceService::make()->getError();
+                    $error = $error? lang($error) : 'failed';
+                    echo "[Task {$taskName} {$date}] 待返积分每日返还结果:{$error}\n";
+                }
                 break;
         }
 

+ 18 - 0
app/Services/Api/FinanceService.php

@@ -448,4 +448,22 @@ class FinanceService extends BaseService
 
     }
 
+
+    /**
+     * 每日平台发放积分
+     * @return bool
+     */
+    public function grantScore()
+    {
+        return true;
+    }
+
+    /**
+     * 每日返还待返积分
+     * @return bool
+     */
+    public function returnWaitScore()
+    {
+        return true;
+    }
 }

+ 125 - 33
app/Services/Api/GoodsService.php

@@ -60,45 +60,137 @@ class GoodsService extends BaseService
         return self::$instance;
     }
 
-    public function getApiDataList($params, $pageSize=12)
+    /**
+     * 更新商品到本地
+     * @param int $pageSize
+     * @param $params 参数
+     * @return array|false
+     */
+    public function updateGoods($pageSize=50,$params=[])
     {
-        $page = request()->post('page', 1);
-        $cacheKey = "caches:goods:list_{$page}_{$pageSize}".md5(json_encode($params));
-        $datas = RedisService::get($cacheKey);
-        if(empty($datas)){
-            $params = [
-                'limit'=> $pageSize>0?$pageSize : 12,
-                'page'=> $page,
-                'status'=> isset($params['status'])? $params['status'] : 1, // 状态:0-全部,1-上架的,2-下架的
-                'supply_type'=> isset($params['supply_type'])? $params['supply_type'] : 2,  // 渠道商
-                'title'=> isset($params['title'])? $params['title'] : '',  // 标题关键词
-                'cate_id'=> isset($params['cate_id'])? $params['cate_id'] : '',  // 分类ID
-            ];
-            $datas = SupplyService::make()->getApiData('getGoodsList', $params);
-            if($datas && $datas['list']){
-                $supplyList = config('goods.supplyList');
-                foreach ($datas['list'] as &$item){
-                    $supplyType = isset($item['supply_type'])? $item['supply_type'] : 0;
-                    $item['supply_name'] = isset($supplyList[$supplyType])? $supplyList[$supplyType] : '';
-
-                    $detail = $this->getApiInfo($item['goods_id']);
-                    $item['main_img'] = isset($detail['main_img'])? $detail['main_img'] : '';
-                    $item['cost_price'] = isset($detail['cost_price'])? $detail['cost_price'] : '0.00';
-                    $item['retail_price'] = isset($detail['retail_price'])? $detail['retail_price'] : '0.00';
-                    $item['brand_name'] = isset($detail['brand_name'])? $detail['brand_name'] : '';
-                    $item['status'] = isset($detail['status'])? $detail['status'] : 1;
-                }
-                unset($item);
+        $cacheKey = "caches:supply:goods_list_update_{$pageSize}";
+        if(RedisService::get($cacheKey)){
+            $this->error = 1047;
+            return false;
+        }
 
-                RedisService::set($cacheKey, $datas, rand(10,20));
+
+        $lastTime = $this->model->where(['mark'=>1])->orderBy('create_time','desc')->value('create_time');
+        $params = [
+            'limit'=> $pageSize>0?$pageSize : 50,
+            'page'=> 1,
+            'status'=> isset($params['status'])? $params['status'] : 1, // 状态:0-全部,1-上架的,2-下架的
+            'supply_type'=> isset($params['supply_type'])? $params['supply_type'] : 0,  // 渠道商
+            'title'=> isset($params['title'])? $params['title'] : '',  // 标题关键词
+            'cate_id'=> isset($params['cate_id'])? $params['cate_id'] : '',  // 分类ID
+            'begin_time'=> $lastTime? $lastTime : '',  // 开始时间
+        ];
+        $goods = [];
+        $updated = 0;
+        $datas = SupplyService::make()->getApiData('getGoodsList', $params);
+        if($datas && $datas['list']){
+            foreach ($datas['list'] as &$item){
+                $goodsId = isset($item['goods_id'])? $item['goods_id'] : 0;
+                if($goodsId && !$this->checkGoods($goodsId)){
+                    $goods[] = [
+                        'goods_id'=> $goodsId,
+                        'supply_type'=> isset($item['supply_type'])? $item['supply_type'] : 0,
+                        'spu_sn'=> isset($item['spu_sn'])? $item['spu_sn'] : '',
+                        'goods_name'=> isset($item['goods_name'])? $item['goods_name'] : '',
+                        'tag'=> isset($item['tag'])? json_encode($item['tag'],256) : '',
+                        'status'=> isset($item['status'])? intval($item['status']) : 1,
+                        'cate_id'=> isset($item['cate_id'])? intval($item['cate_id']) : 0,
+                        'last_update_at'=> isset($item['time'])? $item['time'] : date('Y-m-d H:i:s'),
+                        'create_time'=> time(),
+                    ];
+                }else{
+                    $updated++;
+                }
             }
+            unset($item);
+        }
+
+        if($goods){
+            RedisService::set($cacheKey, $goods, rand(5, 10));
+            $this->model->insert($goods);
+        }
 
+        return ['count'=> count($goods),'updated'=> $updated];
+    }
+
+    /**
+     * 更新商品SKU数据到本地
+     * @param int $pageSize
+     * @param $params 参数
+     * @return array|false
+     */
+    public function updateGoodsSku($pageSize=50,$params=[])
+    {
+        $cacheKey = "caches:supply:goods_sku_update_{$pageSize}";
+        if(RedisService::get($cacheKey)){
+            $this->error = 1047;
+            return false;
         }
-        return [
-            'total'=> isset($datas['total'])? $datas['total'] : 0,
-            'page'=> isset($datas['page'])? $datas['page'] : 1,
-            'list'=> isset($datas['list'])? $datas['list'] : [],
+
+
+        $lastTime = $this->model->where(['mark'=>1])->orderBy('create_time','desc')->value('create_time');
+        $params = [
+            'limit'=> $pageSize>0?$pageSize : 50,
+            'page'=> 1,
+            'status'=> isset($params['status'])? $params['status'] : 1, // 状态:0-全部,1-上架的,2-下架的
+            'supply_type'=> isset($params['supply_type'])? $params['supply_type'] : 0,  // 渠道商
+            'title'=> isset($params['title'])? $params['title'] : '',  // 标题关键词
+            'cate_id'=> isset($params['cate_id'])? $params['cate_id'] : '',  // 分类ID
+            'begin_time'=> $lastTime? $lastTime : '',  // 开始时间
         ];
+        $goods = [];
+        $updated = 0;
+        $datas = SupplyService::make()->getApiData('getGoodsList', $params);
+        if($datas && $datas['list']){
+            foreach ($datas['list'] as &$item){
+                $goodsId = isset($item['goods_id'])? $item['goods_id'] : 0;
+                if($goodsId && !$this->checkGoods($goodsId)){
+                    $goods[] = [
+                        'goods_id'=> $goodsId,
+                        'supply_type'=> isset($item['supply_type'])? $item['supply_type'] : 0,
+                        'spu_sn'=> isset($item['spu_sn'])? $item['spu_sn'] : '',
+                        'goods_name'=> isset($item['goods_name'])? $item['goods_name'] : '',
+                        'tag'=> isset($item['tag'])? json_encode($item['tag'],256) : '',
+                        'status'=> isset($item['status'])? intval($item['status']) : 1,
+                        'cate_id'=> isset($item['cate_id'])? intval($item['cate_id']) : 0,
+                        'last_update_at'=> isset($item['time'])? $item['time'] : date('Y-m-d H:i:s'),
+                        'create_time'=> time(),
+                    ];
+                }else{
+                    $updated++;
+                }
+            }
+            unset($item);
+        }
+
+        if($goods){
+            //RedisService::set($cacheKey, $goods, rand(5, 10));
+            //$this->model->insert($goods);
+        }
+
+        return ['count'=> count($goods),'updated'=> $updated];
+    }
+
+    /**
+     * 验证
+     * @param $goodsId
+     * @return bool
+     */
+    public function checkGoods($goodsId)
+    {
+        $cacheKey ="caches:goods:check_{$goodsId}";
+        if(RedisService::get($cacheKey) || RedisService::exists($cacheKey)){
+            return true;
+        }
+
+        $data = $this->model->where(['goods_id'=> $goodsId,'mark'=>1])->value('id');
+        RedisService::set($cacheKey, $data, rand(30,60));
+        return $data;
     }
 
     /**

+ 1 - 0
resources/lang/zh-cn/api.php

@@ -46,6 +46,7 @@ return [
     '1044' => '供应链接口未配置',
     '1045' => '供应链请求失败,请检查配置',
     '1046' => '供应链授权失败,请检查配置',
+    '1047' => '请求频繁',
 
     // 登录注册
     '2001'=> '账号非法或未注册',