wesmiler 2 лет назад
Родитель
Сommit
c45c4d1007

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

@@ -256,6 +256,29 @@ 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++;
+            if(!RedisService::get('caches:task:lock:goods_category_loaded')){
+                $taskData = [
+                    'taskName' => 'UpdateGoodsCategory',
+                    'name' => "更新商品分类数据",
+                    'date' => date('Y-m-d'),
+                ];
+                $res = $serv->task($taskData);
+                RedisService::set('caches:task:lock:goods_category_loaded', true, rand(3,5));
+                echo "[Task UpdateGoodsCategory {$date}] 更新商品分类数据:{$res}\n";
+            }else{
+                echo "[Task UpdateGoodsCategory {$date}] 间隔时间调用\n";
+            }
+
+        });
+
         // TODO 更新商品SKU数据
         \swoole_timer_tick(120000, function ($timer) use ($serv, &$time) { // 启用定时器,每120秒执行一次
             $date = date('Y-m-d H:i:s');
@@ -388,6 +411,23 @@ class SwooleTask extends Command
                     echo "[Task {$taskName} {$date}] 更新商品SKu数据结果:{$error}\n";
                 }
                 break;
+            case 'UpdateGoodsCategory': // 更新商品分类数据
+                // 时间限制
+                if(date('H:i') >= '00:00' && date('H:i') <= '04:00'){
+                    echo "[Task {$taskName} {$date}] 不在运行时间段内\n";
+                    return false;
+                }
+
+                // 调用处理
+                if($res = GoodsService::make()->updateGoodsCategory()){
+                    $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 'GrantScore':
                 // 时间限制
                 if(date('H:i') >= '09:00'){

+ 25 - 0
app/Models/GoodsCategoryModel.php

@@ -0,0 +1,25 @@
+<?php
+// +----------------------------------------------------------------------
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 LARAVEL研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: laravel开发员 <laravel.qq.com>
+// +----------------------------------------------------------------------
+
+namespace App\Models;
+
+/**
+ * 商品分类-模型
+ * @author laravel开发员
+ * @since 2020/11/11
+ * @package App\Models
+ */
+class GoodsCategoryModel extends BaseModel
+{
+    // 设置数据表
+    protected $table = 'goods_category';
+
+}

+ 24 - 0
app/Models/GoodsModel.php

@@ -22,4 +22,28 @@ class GoodsModel extends BaseModel
     // 设置数据表
     protected $table = 'goods';
 
+    public function sku_list()
+{
+
+}
+
+    /**
+     * 商品规格
+     * @return \Illuminate\Database\Eloquent\Relations\HasMany
+     */
+    public function skuList()
+    {
+        return $this->hasMany(GoodsSkuModel::class, 'goods_id','goods_id')
+            ->where(['status'=>1,'mark'=>1]);
+    }
+
+    /**
+     * 商品规格(所有)
+     * @return \Illuminate\Database\Eloquent\Relations\HasMany
+     */
+    public function skuAll()
+    {
+        return $this->hasMany(GoodsSkuModel::class, 'goods_id','goods_id')
+            ->where(['mark'=>1]);
+    }
 }

+ 25 - 0
app/Models/GoodsSkuModel.php

@@ -0,0 +1,25 @@
+<?php
+// +----------------------------------------------------------------------
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 LARAVEL研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: laravel开发员 <laravel.qq.com>
+// +----------------------------------------------------------------------
+
+namespace App\Models;
+
+/**
+ * 商品规格-模型
+ * @author laravel开发员
+ * @since 2020/11/11
+ * @package App\Models
+ */
+class GoodsSkuModel extends BaseModel
+{
+    // 设置数据表
+    protected $table = 'goods_sku';
+
+}

+ 50 - 0
app/Models/OrderModel.php

@@ -0,0 +1,50 @@
+<?php
+// +----------------------------------------------------------------------
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 LARAVEL研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: laravel开发员 <laravel.qq.com>
+// +----------------------------------------------------------------------
+
+namespace App\Models;
+
+/**
+ * 订单管理-模型
+ * @author laravel开发员
+ * @since 2020/11/11
+ * Class OrderModel
+ * @package App\Models
+ */
+class OrderModel extends BaseModel
+{
+    // 设置数据表
+    protected $table = 'orders';
+
+    /**
+     * 订单商品
+     * @return \Illuminate\Database\Eloquent\Relations\HasMany
+     */
+    public function goods()
+    {
+        return $this->hasMany(OrdersGoodsModel::class, 'order_no','order_no')
+            ->where(['mark'=>1]);
+    }
+
+    /**
+     * 获取信息
+     * @param int $id
+     * @return array|string
+     * @author laravel开发员
+     * @since 2020/11/11
+     */
+    public function getInfo($id)
+    {
+        $info = parent::getInfo($id); // TODO: Change the autogenerated stub
+        return $info;
+    }
+
+
+}

+ 35 - 0
app/Models/OrdersGoodsModel.php

@@ -0,0 +1,35 @@
+<?php
+// +----------------------------------------------------------------------
+// | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
+// +----------------------------------------------------------------------
+// | 版权所有 2017~2021 LARAVEL研发中心
+// +----------------------------------------------------------------------
+// | 官方网站: http://www.laravel.cn
+// +----------------------------------------------------------------------
+// | Author: laravel开发员 <laravel.qq.com>
+// +----------------------------------------------------------------------
+
+namespace App\Models;
+
+/**
+ * 商品管理-模型
+ * @author laravel开发员
+ * @since 2020/11/11
+ * Class GoodsModel
+ * @package App\Models
+ */
+class OrdersGoodsModel extends BaseModel
+{
+    // 设置数据表
+    protected $table = 'orders_goods';
+
+    /**
+     * 封面
+     * @param $value
+     * @return string|\图片地址
+     */
+    public function getThumbAttribute($value)
+    {
+        return $value? get_image_url($value) : '';
+    }
+}

+ 201 - 123
app/Services/Api/GoodsService.php

@@ -12,7 +12,9 @@
 namespace App\Services\Api;
 
 use App\Models\AgentModel;
+use App\Models\GoodsCategoryModel;
 use App\Models\GoodsModel;
+use App\Models\GoodsSkuModel;
 use App\Models\MemberModel;
 use App\Models\MerchantModel;
 use App\Models\OrderModel;
@@ -68,54 +70,54 @@ class GoodsService extends BaseService
      * @param int $userId
      * @return array
      */
-    public function getDataList($params, $pageSize=12, $userId=0)
+    public function getDataList($params, $pageSize = 12, $userId = 0)
     {
-        $list = $this->model->from('goods as a')
-            ->where(['a.status'=>1,'a.mark'=>1])
-            ->where('a.cost_price','>',0)
-            ->where(function ($query) use($params){
-                $supplyType = isset($params['supply_type'])? intval($params['supply_type']) : 0;
-                if($supplyType>0){
-                    $query->where('a.supply_type',$supplyType);
+        $list = $this->model->with(['skuList'])->from('goods as a')
+            ->where(['a.status' => 1, 'a.mark' => 1])
+            ->where('a.cost_price', '>', 0)
+            ->where(function ($query) use ($params) {
+                $supplyType = isset($params['supply_type']) ? intval($params['supply_type']) : 0;
+                if ($supplyType > 0) {
+                    $query->where('a.supply_type', $supplyType);
                 }
 
             })
-            ->where(function ($query) use($params){
-                $keyword = isset($params['kw'])? $params['kw'] : '';
-                if($keyword){
-                    $query->where('a.goods_name','like',"%{$keyword}%")
-                        ->orWhere('a.spu_name','like',"%{$keyword}%")
-                        ->orWhere('a.tag','like',"%{$keyword}%");
+            ->where(function ($query) use ($params) {
+                $keyword = isset($params['kw']) ? $params['kw'] : '';
+                if ($keyword) {
+                    $query->where('a.goods_name', 'like', "%{$keyword}%")
+                        ->orWhere('a.spu_name', 'like', "%{$keyword}%")
+                        ->orWhere('a.tag', 'like', "%{$keyword}%");
                 }
 
             })
             ->select(['a.*'])
-            ->orderBy('a.sales','desc')
-            ->orderBy('a.create_time','desc')
+            ->orderBy('a.sales', 'desc')
+            ->orderBy('a.create_time', 'desc')
             ->paginate($pageSize > 0 ? $pageSize : 9999999);
-        $list = $list? $list->toArray() :[];
-        if($list) {
+        $list = $list ? $list->toArray() : [];
+        if ($list) {
             $locale = RedisService::get("caches:locale:lang_{$userId}");
             $locale = $locale ? $locale : session('locale_lang');
             $locale = $locale ? $locale : 'zh-cn';
             $supplyList = config('goods.supplyList');
             $usdtPrice = WalletService::make()->getBianRatePrice();
-            $xdPrice = ConfigService::make()->getConfigByCode('xd_price',100);
-            $xdPrice = $xdPrice>0 && $xdPrice<=10000? $xdPrice : 100;
+            $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
+            $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
             foreach ($list['data'] as &$item) {
-                $item['supply_name'] = isset($supplyList[$item['supply_type']])? $supplyList[$item['supply_type']] : '';
+                $item['supply_name'] = isset($supplyList[$item['supply_type']]) ? $supplyList[$item['supply_type']] : '';
                 $item['original_price'] = $item['cost_price'];
-                $item['cost_price'] = $usdtPrice>0? moneyFormat($item['cost_price']/$usdtPrice*$xdPrice, 2) : $item['cost_price'];
+                $item['cost_price'] = $usdtPrice > 0 ? moneyFormat($item['cost_price'] / $usdtPrice * $xdPrice, 2) : $item['cost_price'];
             }
             unset($item);
-        }else{
+        } else {
             $this->updateGoods();
         }
 
         return [
-            'total'=> isset($list['total'])? $list['total'] : 9,
-            'pageSize'=> $pageSize,
-            'list'=> isset($list['data'])? $list['data'] : [],
+            'total' => isset($list['total']) ? $list['total'] : 9,
+            'pageSize' => $pageSize,
+            'list' => isset($list['data']) ? $list['data'] : [],
         ];
     }
 
@@ -125,139 +127,215 @@ class GoodsService extends BaseService
      * @param $params 参数
      * @return array|false
      */
-    public function updateGoodsSku($pageSize=50,$params=[])
+    public function updateGoodsSku($pageSize = 100, $params = [])
     {
         $cacheKey = "caches:supply:goods_sku_update_{$pageSize}";
-        if(RedisService::get($cacheKey)){
+        if (RedisService::get($cacheKey)) {
             $this->error = 1047;
             return false;
         }
 
-        return false;
-
-        $lastTime = $this->model->where(['mark'=>1])->orderBy('create_time','desc')->value('create_time');
+        $page = RedisService::get($cacheKey . '_page');
+        $page = $page ? $page + 1 : 1;
+        $lastDate = GoodsSkuModel::where(['mark' => 1])->orderBy('last_update_at', 'desc')->value('last_update_at');
         $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 : '',  // 开始时间
+            'limit' => $pageSize > 0 ? $pageSize : 50,
+            'page' => $page,
+            'date' => $lastDate ? $lastDate : '',  // 开始时间
         ];
         $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++;
+        $error = 0;
+        $datas = SupplyService::make()->getApiData('getSkuUpdate', $params);
+        if ($datas && $datas['list']) {
+            foreach ($datas['list'] as &$item) {
+                $goodsId = isset($item['goods_id']) ? $item['goods_id'] : 0;
+                $goodsSkuSn = isset($item['sku_sn']) ? $item['sku_sn'] : '';
+                if ($goodsId && $goodsSkuSn) {
+
+                    $skuInfo = SupplyService::make()->getApiData('getSkuDetail', ['sku_sn' => $goodsSkuSn]);
+                    if ($goodsSkuSn) {
+                        $updateData = ['update_time' => time(), 'last_update_at' => $item['time']];
+                        if (isset($item['sku_name']) && $item['sku_name']) {
+                            $updateData['sku_name'] = $item['sku_name'];
+                        }
+                        if (isset($item['main_img']) && $item['main_img']) {
+                            $updateData['main_img'] = $item['main_img'];
+                        }
+                        if (isset($item['name']) && $item['name']) {
+                            $updateData['name'] = $item['name'];
+                        }
+                        if (isset($item['val']) && $item['val']) {
+                            $updateData['val'] = $item['val'];
+                        }
+                        if (isset($item['status']) && $item['status']) {
+                            $updateData['status'] = intval($item['status']);
+                        }
+                        if (isset($item['retail_price']) && $item['retail_price']) {
+                            $updateData['retail_price'] = floatval($item['retail_price']);
+                        }
+                        if (isset($item['plat_price']) && $item['plat_price']) {
+                            $updateData['plat_price'] = floatval($item['plat_price']);
+                        }
+                        if (isset($item['detail_img']) && $item['detail_img']) {
+                            $updateData['detail_img'] = json_encode($item['detail_img'], 256);
+                        }
+                        if (isset($item['attr']) && $item['attr']) {
+                            $updateData['attr'] = json_encode($item['attr'], 256);
+                        }
+                        $this->model->where(['goods_id' => $goodsId, 'mark' => 1])->update($updateData);
+                        $updated++;
+                    } else {
+                        $error++;
+                    }
+                } else {
+                    $error++;
                 }
             }
             unset($item);
         }
 
-        if($goods){
-            RedisService::set($cacheKey, $goods, rand(5, 10));
-            $this->model->insert($goods);
-        }
-
-        return ['count'=> count($goods),'updated'=> $updated];
+        return ['count' => count($goods), 'updated' => $updated, 'errorCount' => $error];
     }
 
     /**
-     *
+     * 更新商品
      * @param int $pageSize
      * @param $params 参数
      * @return array|false
      */
-    public function updateGoods($pageSize=50,$params=[])
+    public function updateGoods($pageSize = 50, $params = [])
     {
         set_time_limit(0);
         $cacheKey = "caches:supply:goods_list_update_{$pageSize}";
-        if(RedisService::get($cacheKey)){
+        if (RedisService::get($cacheKey)) {
             $this->error = 1047;
             return false;
         }
 
-        $page = RedisService::get($cacheKey.'_page');
-        $page = $page? $page+1 : 1;
-        $lastTime = $this->model->where(['mark'=>1])->orderBy('create_time','desc')->value('create_time');
+        $page = RedisService::get($cacheKey . '_page');
+        $page = $page ? $page + 1 : 1;
+        $lastTime = $this->model->where(['mark' => 1])->orderBy('create_time', 'desc')->value('create_time');
         $params = [
-            'limit'=> $pageSize>0?$pageSize : 50,
-            'page'=> $page,
-            '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 : '',  // 开始时间
+            'limit' => $pageSize > 0 ? $pageSize : 50,
+            'page' => $page,
+            '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;
-        $error =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)){
+        $error = 0;
+        $datas = SupplyService::make()->getApiData('getGoodsCategory', $params);
+        if ($datas && $datas['list']) {
+            foreach ($datas['list'] as &$item) {
+                $goodsId = isset($item['goods_id']) ? $item['goods_id'] : 0;
+                if ($goodsId && !$this->checkGoods($goodsId)) {
 
                     $info = $this->getApiInfo($goodsId);
-                    if($info){
+                    if ($info) {
                         $goods[] = [
-                            'goods_id'=> $goodsId,
-                            'supply_type'=> isset($item['supply_type'])? $item['supply_type'] : 0,
-                            'spu_sn'=> isset($item['spu_sn'])? $item['spu_sn'] : '',
-                            'spu_name'=> isset($info['spu_name'])? $info['spu_name'] : '',
-                            'main_img'=> isset($info['main_img'])? $info['main_img'] : '',
-                            'detail_img'=> isset($info['detail_img'])? json_encode($info['detail_img'], 256) : '',
-                            'goods_name'=> isset($item['goods_name'])? $item['goods_name'] : '',
-                            'brand_name'=> isset($info['brand_name'])? $info['brand_name'] : '',
-                            'limit_num'=> isset($info['limit_num'])? intval($info['limit_num']) : 0,
-                            'lowest_num'=> isset($info['lowest_num'])? intval($info['lowest_num']) : 1,
-                            'cost_price'=> isset($info['cost_price'])? floatval($info['cost_price']) : 0,
-                            'retail_price'=> isset($info['retail_price'])? floatval($info['retail_price']) : 0,
-                            'profit'=> isset($info['profit'])? floatval($info['profit']) : 0,
-                            'sku_list'=> isset($info['sku_list'])? json_encode($info['sku_list'], 256) : '',
-                            'sku_total'=> isset($info['sku_total'])? intval($info['sku_total']) : 0,
-                            'tag'=> isset($item['tag'])? json_encode($item['tag'],256) : '',
-                            'status'=> isset($info['status'])? intval($info['status']) : 1,
-                            'cate_id'=> isset($item['cate_id'])? intval($item['cate_id']) : 0,
-                            'last_update_at'=> isset($info['update_time'])? $info['update_time'] : (isset($item['time']) && $item['time']? $item['time'] : date('Y-m-d H:i:s')),
-                            'create_time'=> time(),
+                            'goods_id' => $goodsId,
+                            'supply_type' => isset($item['supply_type']) ? $item['supply_type'] : 0,
+                            'spu_sn' => isset($item['spu_sn']) ? $item['spu_sn'] : '',
+                            'spu_name' => isset($info['spu_name']) ? $info['spu_name'] : '',
+                            'main_img' => isset($info['main_img']) ? $info['main_img'] : '',
+                            'detail_img' => isset($info['detail_img']) ? json_encode($info['detail_img'], 256) : '',
+                            'goods_name' => isset($item['goods_name']) ? $item['goods_name'] : '',
+                            'brand_name' => isset($info['brand_name']) ? $info['brand_name'] : '',
+                            'limit_num' => isset($info['limit_num']) ? intval($info['limit_num']) : 0,
+                            'lowest_num' => isset($info['lowest_num']) ? intval($info['lowest_num']) : 1,
+                            'cost_price' => isset($info['cost_price']) ? floatval($info['cost_price']) : 0,
+                            'retail_price' => isset($info['retail_price']) ? floatval($info['retail_price']) : 0,
+                            'profit' => isset($info['profit']) ? floatval($info['profit']) : 0,
+                            'sku_list' => isset($info['sku_list']) ? json_encode($info['sku_list'], 256) : '',
+                            'sku_total' => isset($info['sku_total']) ? intval($info['sku_total']) : 0,
+                            'tag' => isset($item['tag']) ? json_encode($item['tag'], 256) : '',
+                            'status' => isset($info['status']) ? intval($info['status']) : 1,
+                            'cate_id' => isset($item['cate_id']) ? intval($item['cate_id']) : 0,
+                            'last_update_at' => isset($info['update_time']) ? $info['update_time'] : (isset($item['time']) && $item['time'] ? $item['time'] : date('Y-m-d H:i:s')),
+                            'create_time' => time(),
                         ];
-                    }else{
+                        $updated++;
+                    } else {
                         $error++;
                     }
 
-                }else{
-                    $updated++;
+                } else {
+                    $error++;
                 }
             }
             unset($item);
-        }else {
-            RedisService::set($cacheKey.'_page', 1, rand(300,600));
+        } else {
+            RedisService::set($cacheKey . '_page', 1, rand(300, 600));
         }
 
-        if($goods){
-            RedisService::set($cacheKey.'_page', $page, rand(300,600));
+        if ($goods) {
+            RedisService::set($cacheKey . '_page', $page, rand(300, 600));
             RedisService::set($cacheKey, $goods, rand(5, 10));
             $this->model->insertAll($goods);
         }
 
-        return ['count'=> count($goods),'updated'=> $updated,'errorCount'=> $error];
+
+        return ['count' => count($goods), 'updated' => $updated, 'errorCount' => $error];
+    }
+
+    /**
+     * 更新商品分类
+     * @param int $pageSize
+     * @param $params 参数
+     * @return array|false
+     */
+    public function updateGoodsCategory($pageSize = 100, $params = [])
+    {
+        set_time_limit(0);
+        $cacheKey = "caches:supply:goods_category_update_{$pageSize}";
+        if (RedisService::get($cacheKey)) {
+            $this->error = 1047;
+            return false;
+        }
+
+        $page = RedisService::get($cacheKey . '_page');
+        $page = $page ? $page + 1 : 1;
+        $params = [
+            'limit' => $pageSize > 0 ? $pageSize : 50,
+            'page' => $page,
+            'pid' => isset($params['pid']) ? $params['pid'] : 0, // 上级ID
+        ];
+        $categorys = [];
+        $updated = 0;
+        $error = 0;
+        $datas = SupplyService::make()->getApiData('getGoodsCategory', $params);
+        if ($datas && $datas['data']) {
+            foreach ($datas['data'] as &$item) {
+                $cateId = isset($item['id']) ? $item['id'] : 0;
+                if ($cateId && !$this->checkCategory($cateId)) {
+                    $categorys[] = [
+                        'cate_id' => $cateId,
+                        'name' => isset($info['name']) ? $info['name'] : '',
+                        'pid' => isset($info['pid']) ? intval($info['pid']) : 0,
+                        'create_time' => time(),
+                    ];
+                    $updated++;
+                } else {
+                    $error++;
+                }
+            }
+            unset($item);
+        } else {
+            RedisService::set($cacheKey . '_page', 1, rand(300, 600));
+        }
+
+        if ($categorys) {
+            RedisService::set($cacheKey . '_page', $page, rand(300, 600));
+            RedisService::set($cacheKey, $categorys, rand(5, 10));
+            GoodsCategoryModel::insertAll($categorys);
+        }
+
+
+        return ['count' => count($categorys), 'updated' => $updated, 'errorCount' => $error];
     }
 
     /**
@@ -267,13 +345,13 @@ class GoodsService extends BaseService
      */
     public function checkGoods($goodsId)
     {
-        $cacheKey ="caches:goods:check_{$goodsId}";
-        if(RedisService::get($cacheKey) || RedisService::exists($cacheKey)){
+        $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));
+        $data = $this->model->where(['goods_id' => $goodsId, 'mark' => 1])->value('id');
+        RedisService::set($cacheKey, $data, rand(30, 60));
         return $data;
     }
 
@@ -284,19 +362,19 @@ class GoodsService extends BaseService
      * @param int $type 数据类型:0-详情,1-仅SKU数据
      * @return array|false|mixed|string
      */
-    public function getApiInfo($goodsId, $isReal=0, $type=0)
+    public function getApiInfo($goodsId, $isReal = 0, $type = 0)
     {
-        $cacheKey ="caches:goods:detail_{$goodsId}_{$isReal}_{$type}";
+        $cacheKey = "caches:goods:detail_{$goodsId}_{$isReal}_{$type}";
         $info = RedisService::get($cacheKey);
-        if(empty($info)){
+        if (empty($info)) {
             $params = [
-                'goods_id'=> $goodsId,
-                'is_real'=> $isReal,
-                'type'=> $type
+                'goods_id' => $goodsId,
+                'is_real' => $isReal,
+                'type' => $type
             ];
-            $info = SupplyService::make()->getApiData('getGoodsDetail',$params);
-            if($info){
-                RedisService::set($cacheKey, $info, rand(5,10));
+            $info = SupplyService::make()->getApiData('getGoodsDetail', $params);
+            if ($info) {
+                RedisService::set($cacheKey, $info, rand(5, 10));
             }
         }
         return $info;