GoodsService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 $params
  60. * @param int $pageSize
  61. * @param int $userId
  62. * @return array
  63. */
  64. public function getDataList($params, $pageSize=12, $userId=0)
  65. {
  66. $list = $this->model->from('goods as a')
  67. ->where(['a.status'=>1,'a.mark'=>1])
  68. ->where('a.cost_price','>',0)
  69. ->where(function ($query) use($params){
  70. $supplyType = isset($params['supply_type'])? intval($params['supply_type']) : 0;
  71. if($supplyType>0){
  72. $query->where('a.supply_type',$supplyType);
  73. }
  74. })
  75. ->where(function ($query) use($params){
  76. $keyword = isset($params['kw'])? $params['kw'] : '';
  77. if($keyword){
  78. $query->where('a.goods_name','like',"%{$keyword}%")
  79. ->orWhere('a.spu_name','like',"%{$keyword}%")
  80. ->orWhere('a.tag','like',"%{$keyword}%");
  81. }
  82. })
  83. ->select(['a.*'])
  84. ->orderBy('a.sales','desc')
  85. ->orderBy('a.create_time','desc')
  86. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  87. $list = $list? $list->toArray() :[];
  88. if($list) {
  89. $locale = RedisService::get("caches:locale:lang_{$userId}");
  90. $locale = $locale ? $locale : session('locale_lang');
  91. $locale = $locale ? $locale : 'zh-cn';
  92. $supplyList = config('goods.supplyList');
  93. foreach ($list['data'] as &$item) {
  94. $item['supply_name'] = isset($supplyList[$item['supply_type']])? $supplyList[$item['supply_type']] : '';
  95. }
  96. unset($item);
  97. }else{
  98. $this->updateGoods();
  99. }
  100. return [
  101. 'total'=> isset($list['total'])? $list['total'] : 9,
  102. 'pageSize'=> $pageSize,
  103. 'list'=> isset($list['data'])? $list['data'] : [],
  104. ];
  105. }
  106. /**
  107. * 更新商品SKU数据到本地
  108. * @param int $pageSize
  109. * @param $params 参数
  110. * @return array|false
  111. */
  112. public function updateGoodsSku($pageSize=50,$params=[])
  113. {
  114. $cacheKey = "caches:supply:goods_sku_update_{$pageSize}";
  115. if(RedisService::get($cacheKey)){
  116. $this->error = 1047;
  117. return false;
  118. }
  119. return false;
  120. $lastTime = $this->model->where(['mark'=>1])->orderBy('create_time','desc')->value('create_time');
  121. $params = [
  122. 'limit'=> $pageSize>0?$pageSize : 50,
  123. 'page'=> 1,
  124. 'status'=> isset($params['status'])? $params['status'] : 1, // 状态:0-全部,1-上架的,2-下架的
  125. 'supply_type'=> isset($params['supply_type'])? $params['supply_type'] : 0, // 渠道商
  126. 'title'=> isset($params['title'])? $params['title'] : '', // 标题关键词
  127. 'cate_id'=> isset($params['cate_id'])? $params['cate_id'] : '', // 分类ID
  128. 'begin_time'=> $lastTime? $lastTime : '', // 开始时间
  129. ];
  130. $goods = [];
  131. $updated = 0;
  132. $datas = SupplyService::make()->getApiData('getGoodsList', $params);
  133. if($datas && $datas['list']){
  134. foreach ($datas['list'] as &$item){
  135. $goodsId = isset($item['goods_id'])? $item['goods_id'] : 0;
  136. if($goodsId && !$this->checkGoods($goodsId)){
  137. $goods[] = [
  138. 'goods_id'=> $goodsId,
  139. 'supply_type'=> isset($item['supply_type'])? $item['supply_type'] : 0,
  140. 'spu_sn'=> isset($item['spu_sn'])? $item['spu_sn'] : '',
  141. 'goods_name'=> isset($item['goods_name'])? $item['goods_name'] : '',
  142. 'tag'=> isset($item['tag'])? json_encode($item['tag'],256) : '',
  143. 'status'=> isset($item['status'])? intval($item['status']) : 1,
  144. 'cate_id'=> isset($item['cate_id'])? intval($item['cate_id']) : 0,
  145. 'last_update_at'=> isset($item['time'])? $item['time'] : date('Y-m-d H:i:s'),
  146. 'create_time'=> time(),
  147. ];
  148. }else{
  149. $updated++;
  150. }
  151. }
  152. unset($item);
  153. }
  154. if($goods){
  155. RedisService::set($cacheKey, $goods, rand(5, 10));
  156. $this->model->insert($goods);
  157. }
  158. return ['count'=> count($goods),'updated'=> $updated];
  159. }
  160. /**
  161. *
  162. * @param int $pageSize
  163. * @param $params 参数
  164. * @return array|false
  165. */
  166. public function updateGoods($pageSize=50,$params=[])
  167. {
  168. set_time_limit(0);
  169. $cacheKey = "caches:supply:goods_list_update_{$pageSize}";
  170. if(RedisService::get($cacheKey)){
  171. $this->error = 1047;
  172. return false;
  173. }
  174. $lastTime = $this->model->where(['mark'=>1])->orderBy('create_time','desc')->value('create_time');
  175. $params = [
  176. 'limit'=> $pageSize>0?$pageSize : 50,
  177. 'page'=> 1,
  178. 'status'=> isset($params['status'])? $params['status'] : 1, // 状态:0-全部,1-上架的,2-下架的
  179. 'supply_type'=> isset($params['supply_type'])? $params['supply_type'] : 0, // 渠道商
  180. 'title'=> isset($params['title'])? $params['title'] : '', // 标题关键词
  181. 'cate_id'=> isset($params['cate_id'])? $params['cate_id'] : '', // 分类ID
  182. 'begin_time'=> $lastTime? $lastTime : '', // 开始时间
  183. ];
  184. $goods = [];
  185. $updated = 0;
  186. $error =0;
  187. $datas = SupplyService::make()->getApiData('getGoodsList', $params);
  188. if($datas && $datas['list']){
  189. foreach ($datas['list'] as &$item){
  190. $goodsId = isset($item['goods_id'])? $item['goods_id'] : 0;
  191. if($goodsId && !$this->checkGoods($goodsId)){
  192. $info = $this->getApiInfo($goodsId);
  193. if($info){
  194. $goods[] = [
  195. 'goods_id'=> $goodsId,
  196. 'supply_type'=> isset($item['supply_type'])? $item['supply_type'] : 0,
  197. 'spu_sn'=> isset($item['spu_sn'])? $item['spu_sn'] : '',
  198. 'spu_name'=> isset($info['spu_name'])? $info['spu_name'] : '',
  199. 'main_img'=> isset($info['main_img'])? $info['main_img'] : '',
  200. 'detail_img'=> isset($info['detail_img'])? json_encode($info['detail_img'], 256) : '',
  201. 'goods_name'=> isset($item['goods_name'])? $item['goods_name'] : '',
  202. 'brand_name'=> isset($info['brand_name'])? $info['brand_name'] : '',
  203. 'limit_num'=> isset($info['limit_num'])? intval($info['limit_num']) : 0,
  204. 'lowest_num'=> isset($info['lowest_num'])? intval($info['lowest_num']) : 1,
  205. 'cost_price'=> isset($info['cost_price'])? floatval($info['cost_price']) : 0,
  206. 'retail_price'=> isset($info['retail_price'])? floatval($info['retail_price']) : 0,
  207. 'profit'=> isset($info['profit'])? floatval($info['profit']) : 0,
  208. 'sku_list'=> isset($info['sku_list'])? json_encode($info['sku_list'], 256) : '',
  209. 'sku_total'=> isset($info['sku_total'])? intval($info['sku_total']) : 0,
  210. 'tag'=> isset($item['tag'])? json_encode($item['tag'],256) : '',
  211. 'status'=> isset($info['status'])? intval($info['status']) : 1,
  212. 'cate_id'=> isset($item['cate_id'])? intval($item['cate_id']) : 0,
  213. 'last_update_at'=> isset($info['update_time'])? $info['update_time'] : (isset($item['time']) && $item['time']? $item['time'] : date('Y-m-d H:i:s')),
  214. 'create_time'=> time(),
  215. ];
  216. }else{
  217. $error++;
  218. }
  219. }else{
  220. $updated++;
  221. }
  222. }
  223. unset($item);
  224. }
  225. if($goods){
  226. RedisService::set($cacheKey, $goods, rand(5, 10));
  227. $this->model->insertAll($goods);
  228. }
  229. return ['count'=> count($goods),'updated'=> $updated,'errorCount'=> $error];
  230. }
  231. /**
  232. * 验证
  233. * @param $goodsId
  234. * @return bool
  235. */
  236. public function checkGoods($goodsId)
  237. {
  238. $cacheKey ="caches:goods:check_{$goodsId}";
  239. if(RedisService::get($cacheKey) || RedisService::exists($cacheKey)){
  240. return true;
  241. }
  242. $data = $this->model->where(['goods_id'=> $goodsId,'mark'=>1])->value('id');
  243. RedisService::set($cacheKey, $data, rand(30,60));
  244. return $data;
  245. }
  246. /**
  247. * 接口商品详情
  248. * @param $goodsId 商品ID
  249. * @param int $isReal 是否实时数据,0-是,1-否
  250. * @param int $type 数据类型:0-详情,1-仅SKU数据
  251. * @return array|false|mixed|string
  252. */
  253. public function getApiInfo($goodsId, $isReal=0, $type=0)
  254. {
  255. $cacheKey ="caches:goods:detail_{$goodsId}_{$isReal}_{$type}";
  256. $info = RedisService::get($cacheKey);
  257. if(empty($info)){
  258. $params = [
  259. 'goods_id'=> $goodsId,
  260. 'is_real'=> $isReal,
  261. 'type'=> $type
  262. ];
  263. $info = SupplyService::make()->getApiData('getGoodsDetail',$params);
  264. if($info){
  265. RedisService::set($cacheKey, $info, rand(5,10));
  266. }
  267. }
  268. return $info;
  269. }
  270. public function apiCategory($num)
  271. {
  272. }
  273. }