| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\AgentModel;
- use App\Models\GoodsModel;
- use App\Models\MemberModel;
- use App\Models\MerchantModel;
- use App\Models\OrderModel;
- use App\Models\ShopModel;
- use App\Models\TradeModel;
- use App\Services\BaseService;
- use App\Services\ConfigService;
- use App\Services\RedisService;
- use App\Services\SupplyService;
- use BN\Red;
- use Illuminate\Support\Facades\DB;
- /**
- * 商品管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * Class GoodsService
- * @package App\Services\Api
- */
- class GoodsService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * GoodsService constructor.
- */
- public function __construct()
- {
- $this->model = new GoodsModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 更新商品到本地
- * @param int $pageSize
- * @param $params 参数
- * @return array|false
- */
- public function updateGoods($pageSize=50,$params=[])
- {
- $cacheKey = "caches:supply:goods_list_update_{$pageSize}";
- if(RedisService::get($cacheKey)){
- $this->error = 1047;
- return false;
- }
- $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;
- }
- $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;
- }
- /**
- * 接口商品详情
- * @param $goodsId 商品ID
- * @param int $isReal 是否实时数据,0-是,1-否
- * @param int $type 数据类型:0-详情,1-仅SKU数据
- * @return array|false|mixed|string
- */
- public function getApiInfo($goodsId, $isReal=0, $type=0)
- {
- $cacheKey ="caches:goods:detail_{$goodsId}_{$isReal}_{$type}";
- $info = RedisService::get($cacheKey);
- if(empty($info)){
- $params = [
- 'goods_id'=> $goodsId,
- 'is_real'=> $isReal,
- 'type'=> $type
- ];
- $info = SupplyService::make()->getApiData('getGoodsDetail',$params);
- if($info){
- RedisService::set($cacheKey, $info, rand(5,10));
- }
- }
- return $info;
- }
- public function apiCategory($num)
- {
- }
- }
|