GoodsService.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\AgentModel;
  13. use App\Models\GoodsModel;
  14. use App\Models\MemberModel;
  15. use App\Models\MerchantModel;
  16. use App\Models\OrderModel;
  17. use App\Models\ShopModel;
  18. use App\Models\TradeModel;
  19. use App\Services\BaseService;
  20. use App\Services\ConfigService;
  21. use App\Services\RedisService;
  22. use App\Services\SupplyService;
  23. use BN\Red;
  24. use Illuminate\Support\Facades\DB;
  25. /**
  26. * 商品管理-服务类
  27. * @author laravel开发员
  28. * @since 2020/11/11
  29. * Class GoodsService
  30. * @package App\Services\Api
  31. */
  32. class GoodsService extends BaseService
  33. {
  34. // 静态对象
  35. protected static $instance = null;
  36. /**
  37. * 构造函数
  38. * @author laravel开发员
  39. * @since 2020/11/11
  40. * GoodsService constructor.
  41. */
  42. public function __construct()
  43. {
  44. $this->model = new GoodsModel();
  45. }
  46. /**
  47. * 静态入口
  48. * @return static|null
  49. */
  50. public static function make()
  51. {
  52. if (!self::$instance) {
  53. self::$instance = (new static());
  54. }
  55. return self::$instance;
  56. }
  57. /**
  58. * 更新商品到本地
  59. * @param int $pageSize
  60. * @param $params 参数
  61. * @return array|false
  62. */
  63. public function updateGoods($pageSize=50,$params=[])
  64. {
  65. $cacheKey = "caches:supply:goods_list_update_{$pageSize}";
  66. if(RedisService::get($cacheKey)){
  67. $this->error = 1047;
  68. return false;
  69. }
  70. $lastTime = $this->model->where(['mark'=>1])->orderBy('create_time','desc')->value('create_time');
  71. $params = [
  72. 'limit'=> $pageSize>0?$pageSize : 50,
  73. 'page'=> 1,
  74. 'status'=> isset($params['status'])? $params['status'] : 1, // 状态:0-全部,1-上架的,2-下架的
  75. 'supply_type'=> isset($params['supply_type'])? $params['supply_type'] : 0, // 渠道商
  76. 'title'=> isset($params['title'])? $params['title'] : '', // 标题关键词
  77. 'cate_id'=> isset($params['cate_id'])? $params['cate_id'] : '', // 分类ID
  78. 'begin_time'=> $lastTime? $lastTime : '', // 开始时间
  79. ];
  80. $goods = [];
  81. $updated = 0;
  82. $datas = SupplyService::make()->getApiData('getGoodsList', $params);
  83. if($datas && $datas['list']){
  84. foreach ($datas['list'] as &$item){
  85. $goodsId = isset($item['goods_id'])? $item['goods_id'] : 0;
  86. if($goodsId && !$this->checkGoods($goodsId)){
  87. $goods[] = [
  88. 'goods_id'=> $goodsId,
  89. 'supply_type'=> isset($item['supply_type'])? $item['supply_type'] : 0,
  90. 'spu_sn'=> isset($item['spu_sn'])? $item['spu_sn'] : '',
  91. 'goods_name'=> isset($item['goods_name'])? $item['goods_name'] : '',
  92. 'tag'=> isset($item['tag'])? json_encode($item['tag'],256) : '',
  93. 'status'=> isset($item['status'])? intval($item['status']) : 1,
  94. 'cate_id'=> isset($item['cate_id'])? intval($item['cate_id']) : 0,
  95. 'last_update_at'=> isset($item['time'])? $item['time'] : date('Y-m-d H:i:s'),
  96. 'create_time'=> time(),
  97. ];
  98. }else{
  99. $updated++;
  100. }
  101. }
  102. unset($item);
  103. }
  104. if($goods){
  105. RedisService::set($cacheKey, $goods, rand(5, 10));
  106. $this->model->insert($goods);
  107. }
  108. return ['count'=> count($goods),'updated'=> $updated];
  109. }
  110. /**
  111. * 更新商品SKU数据到本地
  112. * @param int $pageSize
  113. * @param $params 参数
  114. * @return array|false
  115. */
  116. public function updateGoodsSku($pageSize=50,$params=[])
  117. {
  118. $cacheKey = "caches:supply:goods_sku_update_{$pageSize}";
  119. if(RedisService::get($cacheKey)){
  120. $this->error = 1047;
  121. return false;
  122. }
  123. $lastTime = $this->model->where(['mark'=>1])->orderBy('create_time','desc')->value('create_time');
  124. $params = [
  125. 'limit'=> $pageSize>0?$pageSize : 50,
  126. 'page'=> 1,
  127. 'status'=> isset($params['status'])? $params['status'] : 1, // 状态:0-全部,1-上架的,2-下架的
  128. 'supply_type'=> isset($params['supply_type'])? $params['supply_type'] : 0, // 渠道商
  129. 'title'=> isset($params['title'])? $params['title'] : '', // 标题关键词
  130. 'cate_id'=> isset($params['cate_id'])? $params['cate_id'] : '', // 分类ID
  131. 'begin_time'=> $lastTime? $lastTime : '', // 开始时间
  132. ];
  133. $goods = [];
  134. $updated = 0;
  135. $datas = SupplyService::make()->getApiData('getGoodsList', $params);
  136. if($datas && $datas['list']){
  137. foreach ($datas['list'] as &$item){
  138. $goodsId = isset($item['goods_id'])? $item['goods_id'] : 0;
  139. if($goodsId && !$this->checkGoods($goodsId)){
  140. $goods[] = [
  141. 'goods_id'=> $goodsId,
  142. 'supply_type'=> isset($item['supply_type'])? $item['supply_type'] : 0,
  143. 'spu_sn'=> isset($item['spu_sn'])? $item['spu_sn'] : '',
  144. 'goods_name'=> isset($item['goods_name'])? $item['goods_name'] : '',
  145. 'tag'=> isset($item['tag'])? json_encode($item['tag'],256) : '',
  146. 'status'=> isset($item['status'])? intval($item['status']) : 1,
  147. 'cate_id'=> isset($item['cate_id'])? intval($item['cate_id']) : 0,
  148. 'last_update_at'=> isset($item['time'])? $item['time'] : date('Y-m-d H:i:s'),
  149. 'create_time'=> time(),
  150. ];
  151. }else{
  152. $updated++;
  153. }
  154. }
  155. unset($item);
  156. }
  157. if($goods){
  158. //RedisService::set($cacheKey, $goods, rand(5, 10));
  159. //$this->model->insert($goods);
  160. }
  161. return ['count'=> count($goods),'updated'=> $updated];
  162. }
  163. /**
  164. * 验证
  165. * @param $goodsId
  166. * @return bool
  167. */
  168. public function checkGoods($goodsId)
  169. {
  170. $cacheKey ="caches:goods:check_{$goodsId}";
  171. if(RedisService::get($cacheKey) || RedisService::exists($cacheKey)){
  172. return true;
  173. }
  174. $data = $this->model->where(['goods_id'=> $goodsId,'mark'=>1])->value('id');
  175. RedisService::set($cacheKey, $data, rand(30,60));
  176. return $data;
  177. }
  178. /**
  179. * 接口商品详情
  180. * @param $goodsId 商品ID
  181. * @param int $isReal 是否实时数据,0-是,1-否
  182. * @param int $type 数据类型:0-详情,1-仅SKU数据
  183. * @return array|false|mixed|string
  184. */
  185. public function getApiInfo($goodsId, $isReal=0, $type=0)
  186. {
  187. $cacheKey ="caches:goods:detail_{$goodsId}_{$isReal}_{$type}";
  188. $info = RedisService::get($cacheKey);
  189. if(empty($info)){
  190. $params = [
  191. 'goods_id'=> $goodsId,
  192. 'is_real'=> $isReal,
  193. 'type'=> $type
  194. ];
  195. $info = SupplyService::make()->getApiData('getGoodsDetail',$params);
  196. if($info){
  197. RedisService::set($cacheKey, $info, rand(5,10));
  198. }
  199. }
  200. return $info;
  201. }
  202. public function apiCategory($num)
  203. {
  204. }
  205. }