GoodsService.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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\CartsModel;
  14. use App\Models\GoodsCategoryModel;
  15. use App\Models\GoodsModel;
  16. use App\Models\GoodsSkuModel;
  17. use App\Models\MemberModel;
  18. use App\Models\MerchantModel;
  19. use App\Models\OrderModel;
  20. use App\Models\ShopModel;
  21. use App\Models\TradeModel;
  22. use App\Services\BaseService;
  23. use App\Services\ConfigService;
  24. use App\Services\RedisService;
  25. use App\Services\SupplyService;
  26. use App\Services\WalletService;
  27. use BN\Red;
  28. use Illuminate\Support\Facades\DB;
  29. /**
  30. * 商品管理-服务类
  31. * @author laravel开发员
  32. * @since 2020/11/11
  33. * Class GoodsService
  34. * @package App\Services\Api
  35. */
  36. class GoodsService extends BaseService
  37. {
  38. // 静态对象
  39. protected static $instance = null;
  40. /**
  41. * 构造函数
  42. * @author laravel开发员
  43. * @since 2020/11/11
  44. * GoodsService constructor.
  45. */
  46. public function __construct()
  47. {
  48. $this->model = new GoodsModel();
  49. }
  50. /**
  51. * 静态入口
  52. * @return static|null
  53. */
  54. public static function make()
  55. {
  56. if (!self::$instance) {
  57. self::$instance = (new static());
  58. }
  59. return self::$instance;
  60. }
  61. /**
  62. * 商品列表
  63. * @param $params
  64. * @param int $pageSize
  65. * @param int $userId
  66. * @return array
  67. */
  68. public function getDataList($params, $pageSize = 12, $userId = 0)
  69. {
  70. $model = $this->model->with(['skuList'])->from('goods as a')
  71. ->where(['a.status' => 1, 'a.mark' => 1])
  72. ->where('a.cost_price', '>', 0)
  73. ->where(function ($query) use ($params) {
  74. $supplyType = isset($params['supply_type']) ? intval($params['supply_type']) : 0;
  75. if ($supplyType > 0) {
  76. $query->where('a.supply_type', $supplyType);
  77. }
  78. $cateId = isset($params['cate_id']) ? intval($params['cate_id']) : 0;
  79. if ($cateId > 0) {
  80. $subIds = GoodsCategoryModel::where(['pid'=> $cateId,'mark'=>1,'status'=>1])->pluck('cate_id');
  81. $query->where(function($query) use($cateId,$subIds){
  82. if($subIds){
  83. $query->whereIn('a.cate_id', $subIds)
  84. ->orWhere('a.cate_id', $cateId);
  85. }else{
  86. $query->where('a.cate_id', $cateId);
  87. }
  88. });
  89. }
  90. })
  91. ->where(function ($query) use ($params) {
  92. $keyword = isset($params['kw']) ? $params['kw'] : '';
  93. if ($keyword) {
  94. $query->where('a.goods_name', 'like', "%{$keyword}%")
  95. ->orWhere('a.spu_name', 'like', "%{$keyword}%")
  96. ->orWhere('a.tag', 'like', "%{$keyword}%");
  97. }
  98. })
  99. ->select(['a.*']);
  100. // 排序
  101. $sortType = isset($params['sort_type'])? $params['sort_type'] : 1;
  102. if ($sortType == 2){
  103. $model = $model->orderBy('a.is_recommend','asc')->orderBy('a.sales', 'desc');
  104. }
  105. $list = $model->orderBy('a.create_time', 'desc')
  106. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  107. $list = $list ? $list->toArray() : [];
  108. if ($list) {
  109. $locale = RedisService::get("caches:locale:lang_{$userId}");
  110. $locale = $locale ? $locale : session('locale_lang');
  111. $locale = $locale ? $locale : 'zh-cn';
  112. $supplyList = config('goods.supplyList');
  113. $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
  114. if($usdtPrice<=0){
  115. $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
  116. $usdtPrice = $usdtCnyPrice>0 && $usdtCnyPrice< 100? $usdtCnyPrice : 0;
  117. }
  118. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  119. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  120. foreach ($list['data'] as &$item) {
  121. $item['detail_img'] = isset($item['detail_img']) && $item['detail_img'] ? json_decode($item['detail_img'], true) : [];
  122. $item['supply_name'] = isset($supplyList[$item['supply_type']]) ? $supplyList[$item['supply_type']] : '';
  123. $item['usdt_price'] = $usdtPrice;
  124. $item['xd_price_rate'] = $xdPrice;
  125. $item['original_price'] = $item['cost_price'];
  126. $item['cost_price'] = $usdtPrice > 0 ? moneyFormat($item['cost_price'] / $usdtPrice * $xdPrice, 2) : $item['cost_price'];
  127. }
  128. unset($item);
  129. } else {
  130. $this->updateGoods();
  131. }
  132. return [
  133. 'total' => isset($list['total']) ? $list['total'] : 9,
  134. 'pageSize' => $pageSize,
  135. 'list' => isset($list['data']) ? $list['data'] : [],
  136. ];
  137. }
  138. /**
  139. * 详情
  140. * @param $id
  141. * @return array
  142. */
  143. public function getInfo($goodsId, $userId=0)
  144. {
  145. $field = ['a.*'];
  146. $info = $this->model->from('goods as a')->with(['category','skuList'])
  147. ->where(['a.goods_id'=> $goodsId,'a.status'=>1,'a.mark'=>1])
  148. ->select($field)
  149. ->first();
  150. $info = $info? $info->toArray() : [];
  151. if($info){
  152. if(isset($info['main_img'])){
  153. $info['main_img'] = $info['main_img']? get_image_url($info['main_img']) : '';
  154. }
  155. if(isset($info['detail_img'])){
  156. $info['detail_img'] = $info['detail_img']? json_decode($info['detail_img'], true) : [];
  157. }
  158. $supplyList = config('goods.supplyList');
  159. $info['supply_name'] = isset($supplyList[$info['supply_type']]) ? $supplyList[$info['supply_type']] : '';
  160. $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
  161. if($usdtPrice<=0){
  162. $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
  163. $usdtPrice = $usdtCnyPrice>0 && $usdtCnyPrice< 100? $usdtCnyPrice : 0;
  164. }
  165. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  166. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  167. $info['usdt_price_rate'] = $usdtPrice;
  168. $info['xd_price'] = $xdPrice;
  169. $info['custom_uid'] = ConfigService::make()->getConfigByCode('xl_custom_id',100001);
  170. if(isset($info['cost_price']) && $info['cost_price']){
  171. $info['cost_price'] = moneyFormat($info['cost_price']/$usdtPrice * $xdPrice,2);
  172. }
  173. if(isset($info['sku_list']) && $info['sku_list']){
  174. foreach ($info['sku_list'] as &$v){
  175. $v['detail_img'] = $v['detail_img']? json_decode($v['detail_img'], true) : [];
  176. $v['attr'] = $v['attr']? json_decode($v['attr'], true) : [];
  177. $v['main_img'] = $v['main_img']? get_image_url($v['main_img']) : '';
  178. $v['cost_price'] = $v['retail_price']? moneyFormat($v['retail_price']/$usdtPrice * $xdPrice,2) : $info['cost_price'];
  179. }
  180. unset($v);
  181. }
  182. $this->updateView($userId, $goodsId);
  183. }
  184. return $info;
  185. }
  186. /**
  187. * 添加/更新购物车
  188. * @param $userId
  189. * @param $goodsId
  190. * @param $params
  191. * @return array|false
  192. */
  193. public function updateCart($userId, $goodsId, $params)
  194. {
  195. $skuId = isset($params['sku_id'])? $params['sku_id'] : 0;
  196. $status = isset($params['status'])? $params['status'] : 0;
  197. $merchId = isset($params['merch_id'])? $params['merch_id'] : 0;
  198. $num = isset($params['num'])? $params['num'] : 1;
  199. if($skuId<=0 || $goodsId<=0 || $userId<=0 || $num<=0){
  200. $this->error = 2014;
  201. return false;
  202. }
  203. if(CartsModel::where(['user_id'=> $userId,'status'=>1,'mark'=>1])->count('id') > 20){
  204. $this->error = 1050;
  205. return false;
  206. }
  207. $cartId = CartsModel::where(['user_id'=> $userId,'goods_id'=> $goodsId,'sku_id'=>$skuId])->value('id');
  208. if($cartId){
  209. CartsModel::where(['id'=> $cartId])->update(['num'=> $num,'merch_id'=>$merchId,'status'=> $status,'mark'=>1,'update_time'=>time()]);
  210. RedisService::clear("caches:members:cartList:{$userId}");
  211. $count = $this->getCartCount($userId,true);
  212. return ['id'=> $cartId,'count'=> $count];
  213. }else{
  214. $cartId = CartsModel::insertGetId(['user_id'=> $userId,'goods_id'=>$goodsId,'merch_id'=>$merchId,'sku_id'=>$skuId,'num'=> $num,'status'=> $status,'mark'=>1,'create_time'=>time()]);
  215. RedisService::clear("caches:members:cartList:{$userId}");
  216. $count = $this->getCartCount($userId, true);
  217. return ['id'=> $cartId,'count'=>$count];
  218. }
  219. }
  220. /**
  221. * 购物车列表
  222. * @param $userId
  223. * @param int $pageSize
  224. * @return array|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|mixed
  225. */
  226. public function getCartList($userId, $pageSize = 30)
  227. {
  228. $cacheKey = "caches:members:cartList:{$userId}";
  229. $cacheCountKey = "caches:members:cartCount:{$userId}";
  230. $datas = RedisService::get($cacheKey);
  231. if($datas){
  232. return $datas;
  233. }
  234. $datas = CartsModel::with(['sku'])->from('carts as a')
  235. ->leftJoin('goods as b','b.goods_id','=','a.goods_id')
  236. ->leftJoin('goods_sku as c','c.sku_id','=','a.sku_id')
  237. ->where(['a.status' => 1, 'a.mark' => 1,'b.status'=>1,'b.mark'=>1])
  238. ->where('b.cost_price', '>', 0)
  239. ->select(['b.*','a.num','a.sku_id'])
  240. ->orderBy('a.create_time','desc')
  241. ->limit($pageSize)
  242. ->get();
  243. if($datas){
  244. // 价格等参数格式化
  245. $locale = RedisService::get("caches:locale:lang_{$userId}");
  246. $locale = $locale ? $locale : session('locale_lang');
  247. $locale = $locale ? $locale : 'zh-cn';
  248. $supplyList = config('goods.supplyList');
  249. $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
  250. if($usdtPrice<=0){
  251. $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
  252. $usdtPrice = $usdtCnyPrice>0 && $usdtCnyPrice< 100? $usdtCnyPrice : 0;
  253. }
  254. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  255. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  256. foreach ($datas as &$item) {
  257. $item['detail_img'] = isset($item['detail_img']) && $item['detail_img'] ? json_decode($item['detail_img'], true) : [];
  258. $item['supply_name'] = isset($supplyList[$item['supply_type']]) ? $supplyList[$item['supply_type']] : '';
  259. $item['usdt_price'] = $usdtPrice;
  260. $item['xd_price_rate'] = $xdPrice;
  261. $item['original_price'] = $item['cost_price'];
  262. $item['cost_price'] = $usdtPrice > 0 ? moneyFormat($item['cost_price'] / $usdtPrice * $xdPrice, 2) : $item['cost_price'];
  263. }
  264. unset($item);
  265. RedisService::set($cacheCountKey, count($datas), rand(300, 600));
  266. RedisService::set($cacheKey, $datas, rand(300, 600));
  267. }
  268. return $datas;
  269. }
  270. /**
  271. * 购物车中数量
  272. * @param $userId
  273. * @return array|mixed
  274. */
  275. public function getCartCount($userId, $refresh = false)
  276. {
  277. $cacheKey = "caches:member:cartCount:{$userId}";
  278. $data = RedisService::get($cacheKey);
  279. if($data>0 && !$refresh){
  280. return $data;
  281. }
  282. $data = CartsModel::from('carts as a')
  283. ->leftJoin('goods as b','b.goods_id','=','a.goods_id')
  284. ->where(['a.status' => 1, 'a.mark' => 1,'b.status'=>1,'b.mark'=>1])
  285. ->where('b.cost_price', '>', 0)
  286. ->count('a.id');
  287. if($data){
  288. RedisService::set($cacheKey, $data, rand(300, 600));
  289. }
  290. return $data;
  291. }
  292. public function getFreight($userId, $addressId,$skuList)
  293. {
  294. $cacheKey = "caches:goods:freight:{$userId}_{$addressId}_".md5(json_encode($skuList,256));
  295. $data = RedisService::get($cacheKey);
  296. if($data){
  297. return $data;
  298. }
  299. if(empty($addressId)){
  300. $address = MemberAddressService::make()->getBindInfo($userId);
  301. $addressId = isset($address['district_code'])? $address['district_code'] : '';
  302. }
  303. $result = SupplyService::make()->getApiData('getFreight',['address_id'=> intval($addressId),'sku_list'=> $skuList]);
  304. $freight = isset($result['freight'])? floatval($result['freight']) : -1;
  305. if($freight>=0){
  306. RedisService::set($cacheKey, ['freight'=>$freight], rand(5,10));
  307. return ['freight'=>$freight];
  308. }else{
  309. $this->error = 1052;
  310. return false;
  311. }
  312. }
  313. /**
  314. * 更新浏览量
  315. * @param $userId
  316. * @param $dynamicId
  317. * @return array|mixed
  318. */
  319. public function updateView($userId, $id)
  320. {
  321. $cacheKey = "caches:goods:views:u{$userId}_d{$id}";
  322. $data = RedisService::get($cacheKey);
  323. if($data){
  324. return false;
  325. }
  326. $data = $this->model->where(['goods_id'=> $id])->update(['views'=>DB::raw('views + 1'),'update_time'=>time()]);
  327. RedisService::set($cacheKey, $id, rand(1,3)*7200);
  328. return $data;
  329. }
  330. /**
  331. * 更新商品SKU数据到本地
  332. * @param int $pageSize
  333. * @param $params 参数
  334. * @return array|false
  335. */
  336. public function updateGoodsSku($pageSize = 100, $params = [])
  337. {
  338. $cacheKey = "caches:supply:goods_sku_update_{$pageSize}";
  339. if (RedisService::get($cacheKey)) {
  340. $this->error = 1047;
  341. return false;
  342. }
  343. $page = RedisService::get($cacheKey . '_page');
  344. $page = $page ? $page + 1 : 1;
  345. $lastDate = GoodsSkuModel::where(['mark' => 1])->orderBy('last_update_at', 'desc')->value('last_update_at');
  346. $params = [
  347. 'limit' => $pageSize > 0 ? $pageSize : 50,
  348. 'page' => $page,
  349. 'date' => $lastDate ? $lastDate : '', // 开始时间
  350. ];
  351. $goods = [];
  352. $updated = 0;
  353. $error = 0;
  354. $datas = SupplyService::make()->getApiData('getSkuUpdate', $params);
  355. if ($datas && $datas['list']) {
  356. foreach ($datas['list'] as &$item) {
  357. $goodsId = isset($item['goods_id']) ? $item['goods_id'] : 0;
  358. $goodsSkuSn = isset($item['sku_sn']) ? $item['sku_sn'] : '';
  359. $changeType = isset($item['change_type']) ? $item['change_type'] : '';
  360. if ($goodsId && $goodsSkuSn && $changeType) {
  361. $skuInfo = SupplyService::make()->getApiData('getSkuDetail', ['sku_sn' => $goodsSkuSn]);
  362. if ($skuInfo) {
  363. $updateData = ['goods_id'=>$goodsId,'sku_sn'=> $goodsSkuSn,'remark'=>'SKU更新','update_time' => time(),'mark'=>1, 'last_update_at' => $item['update_time']];
  364. if (isset($skuInfo['sku_name']) && $skuInfo['sku_name']) {
  365. $updateData['sku_name'] = $skuInfo['sku_name'];
  366. }
  367. if (isset($skuInfo['main_img']) && $skuInfo['main_img']) {
  368. $updateData['main_img'] = $skuInfo['main_img'];
  369. }
  370. if (isset($skuInfo['spu_sn']) && $skuInfo['spu_sn']) {
  371. $updateData['spu_sn'] = $skuInfo['spu_sn'];
  372. }
  373. if (isset($skuInfo['sku_id']) && $skuInfo['sku_id']) {
  374. $updateData['sku_id'] = intval($skuInfo['sku_id']);
  375. }
  376. if (isset($skuInfo['status']) && $skuInfo['status']) {
  377. $updateData['status'] = intval($skuInfo['status']);
  378. }
  379. if (isset($skuInfo['retail_price']) && $skuInfo['retail_price']) {
  380. $updateData['retail_price'] = floatval($skuInfo['retail_price']);
  381. }
  382. if (isset($skuInfo['plat_price']) && $skuInfo['plat_price']) {
  383. $updateData['plat_price'] = floatval($skuInfo['plat_price']);
  384. }
  385. if (isset($skuInfo['detail_img']) && $skuInfo['detail_img']) {
  386. $updateData['detail_img'] = json_encode($skuInfo['detail_img'], 256);
  387. }
  388. if (isset($skuInfo['attr']) && $skuInfo['attr']) {
  389. $updateData['attr'] = json_encode($skuInfo['attr'], 256);
  390. }
  391. if(GoodsSkuModel::where(['goods_id' => $goodsId])->value('id')){
  392. GoodsSkuModel::where(['goods_id' => $goodsId, 'mark' => 1])->update($updateData);
  393. $updated++;
  394. }else{
  395. $error++;
  396. }
  397. } else {
  398. $error++;
  399. }
  400. } else {
  401. $error++;
  402. }
  403. }
  404. unset($item);
  405. RedisService::set($cacheKey . '_page', $page, rand(300, 600));
  406. }else{
  407. RedisService::set($cacheKey . '_page', 0, rand(300, 600));
  408. }
  409. return ['count' => count($goods), 'updated' => $updated, 'errorCount' => $error,'page'=>$page];
  410. }
  411. /**
  412. * 更新商品
  413. * @param int $pageSize
  414. * @param $params 参数
  415. * @return array|false
  416. */
  417. public function updateGoods($pageSize = 100, $params = [])
  418. {
  419. set_time_limit(0);
  420. $cacheKey = "caches:supply:goods_list_update_{$pageSize}";
  421. if (RedisService::get($cacheKey)) {
  422. $this->error = 1047;
  423. return false;
  424. }
  425. $page = RedisService::get($cacheKey . '_page');
  426. $page = $page ? $page + 1 : 1;
  427. $lastTime = $this->model->where(['mark' => 1])->orderBy('create_time', 'desc')->value('create_time');
  428. $params = [
  429. 'limit' => $pageSize > 0 ? $pageSize : 50,
  430. 'page' => $page,
  431. 'status' => isset($params['status']) ? $params['status'] : 1, // 状态:0-全部,1-上架的,2-下架的
  432. 'supply_type' => isset($params['supply_type']) ? $params['supply_type'] : 0, // 渠道商
  433. 'title' => isset($params['title']) ? $params['title'] : '', // 标题关键词
  434. 'cate_id' => isset($params['cate_id']) ? $params['cate_id'] : '', // 分类ID
  435. 'begin_time' => $lastTime ? $lastTime : '', // 开始时间
  436. ];
  437. $goods = [];
  438. $skus = [];
  439. $updated = 0;
  440. $error = 0;
  441. $datas = SupplyService::make()->getApiData('getGoodsList', $params);
  442. if ($datas && $datas['list']) {
  443. foreach ($datas['list'] as &$item) {
  444. $goodsId = isset($item['goods_id']) ? $item['goods_id'] : 0;
  445. if ($goodsId && !$this->checkGoods($goodsId)) {
  446. $info = $this->getApiInfo($goodsId);
  447. if ($info) {
  448. $skuList = isset($info['sku_list']) ? $info['sku_list'] : [];
  449. $goods[] = [
  450. 'goods_id' => $goodsId,
  451. 'supply_type' => isset($item['supply_type']) ? $item['supply_type'] : 0,
  452. 'spu_sn' => isset($item['spu_sn']) ? $item['spu_sn'] : '',
  453. 'spu_name' => isset($info['spu_name']) ? $info['spu_name'] : '',
  454. 'main_img' => isset($info['main_img']) ? $info['main_img'] : '',
  455. 'detail_img' => isset($info['detail_img']) ? json_encode($info['detail_img'], 256) : '',
  456. 'goods_name' => isset($item['goods_name']) ? $item['goods_name'] : '',
  457. 'brand_name' => isset($info['brand_name']) ? $info['brand_name'] : '',
  458. 'limit_num' => isset($info['limit_num']) ? intval($info['limit_num']) : 0,
  459. 'lowest_num' => isset($info['lowest_num']) ? intval($info['lowest_num']) : 1,
  460. 'cost_price' => isset($info['cost_price']) ? floatval($info['cost_price']) : 0,
  461. 'retail_price' => isset($info['retail_price']) ? floatval($info['retail_price']) : 0,
  462. 'profit' => isset($info['profit']) ? floatval($info['profit']) : 0,
  463. 'sku_list' => $skuList? json_encode($skuList,256):'',
  464. 'sku_total' => isset($info['sku_total']) ? intval($info['sku_total']) : 0,
  465. 'tag' => isset($item['tag']) ? json_encode($item['tag'], 256) : '',
  466. 'status' => isset($info['status']) ? intval($info['status']) : 1,
  467. 'cate_id' => isset($item['cate_id']) ? intval($item['cate_id']) : 0,
  468. 'last_update_at' => isset($info['update_time']) ? $info['update_time'] : (isset($item['time']) && $item['time'] ? $item['time'] : date('Y-m-d H:i:s')),
  469. 'create_time' => time(),
  470. ];
  471. foreach($skuList as $v){
  472. $skus[] = [
  473. 'sku_id'=> isset($v['sku_id'])? $v['sku_id'] : 0,
  474. 'goods_id'=> $goodsId,
  475. 'spu_sn'=> isset($v['spu_sn'])? $v['spu_sn'] : '',
  476. 'sku_sn'=> isset($v['sku_sn'])? $v['sku_sn'] : '',
  477. 'sku_name'=> isset($v['sku_name'])? $v['sku_name'] : '',
  478. 'main_img'=> isset($v['main_img'])? $v['main_img'] : '',
  479. 'status'=> isset($v['status'])? $v['status'] : 1,
  480. 'source_type'=> isset($v['source_type'])? $v['source_type'] : 0,
  481. 'retail_price'=> isset($v['retail_price'])? floatval($v['retail_price']) : 0,
  482. 'plat_price'=> isset($v['plat_price'])? floatval($v['plat_price']) : 0,
  483. 'profit'=> isset($v['profit'])? floatval($v['profit']) : 0,
  484. 'last_update_at'=> isset($v['update_time'])? $v['update_time'] : date('Y-m-d H:i:s'),
  485. 'detail_img'=> isset($v['detail_img'])? json_encode($v['detail_img'],256) : '',
  486. 'attr'=> isset($v['attr'])? json_encode($v['attr'],256) : '',
  487. 'remark'=>'SKU同步创建',
  488. ];
  489. }
  490. $updated++;
  491. } else {
  492. $error++;
  493. }
  494. } else {
  495. $error++;
  496. }
  497. }
  498. unset($item);
  499. } else {
  500. RedisService::set($cacheKey . '_page', 0, rand(300, 600));
  501. }
  502. if ($goods) {
  503. RedisService::set($cacheKey . '_page', $page, rand(300, 600));
  504. RedisService::set($cacheKey, $goods, rand(5, 10));
  505. DB::beginTransaction();
  506. try {
  507. $this->model->insertAll($goods);
  508. if($skus){
  509. GoodsSkuModel::insert($skus);
  510. }
  511. DB::commit();
  512. }catch (\Exception $exception){
  513. DB::rollBack();
  514. }
  515. }
  516. return ['count' => count($goods), 'updated' => $updated, 'errorCount' => $error,'page'=>$page];
  517. }
  518. /**
  519. * 更新商品分类
  520. * @param int $pid 上级ID
  521. * @param int $pageSize
  522. * @param $params 参数
  523. * @return array|false
  524. */
  525. public function updateGoodsCategory($pid=0, $pageSize = 200, $params = [])
  526. {
  527. set_time_limit(0);
  528. $cacheKey = "caches:supply:goods_category_update_{$pid}_{$pageSize}";
  529. if (RedisService::get($cacheKey)) {
  530. $this->error = 1047;
  531. return false;
  532. }
  533. $params = [
  534. 'limit' => $pageSize > 0 ? $pageSize : 50,
  535. 'page' => 1,
  536. 'pid' => $pid, // 上级ID
  537. ];
  538. $categorys = [];
  539. $updated = 0;
  540. $error = 0;
  541. $datas = SupplyService::make()->getApiData('getGoodsCategory', $params);
  542. if ($datas && $datas['data']) {
  543. foreach ($datas['data'] as &$item) {
  544. $cateId = isset($item['id']) ? $item['id'] : 0;
  545. if ($cateId && !$this->checkCategory($cateId)) {
  546. $categorys[] = [
  547. 'cate_id' => $cateId,
  548. 'name' => isset($item['name']) ? $item['name'] : '',
  549. 'pid' => isset($item['pid']) ? intval($item['pid']) : 0,
  550. 'create_time' => time(),
  551. ];
  552. $updated++;
  553. } else {
  554. $error++;
  555. }
  556. }
  557. unset($item);
  558. }
  559. if ($categorys) {
  560. RedisService::set($cacheKey, $categorys, rand(5, 10));
  561. GoodsCategoryModel::insert($categorys);
  562. }
  563. return ['count' => count($categorys), 'updated' => $updated,'pid'=>$pid, 'errorCount' => $error];
  564. }
  565. /**
  566. * 验证
  567. * @param $goodsId
  568. * @return bool
  569. */
  570. public function checkGoods($goodsId)
  571. {
  572. $cacheKey = "caches:goods:check_{$goodsId}";
  573. if (RedisService::get($cacheKey) || RedisService::exists($cacheKey)) {
  574. return true;
  575. }
  576. $data = $this->model->where(['goods_id' => $goodsId, 'mark' => 1])->value('id');
  577. RedisService::set($cacheKey, $data, rand(30, 60));
  578. return $data;
  579. }
  580. /**
  581. * 验证分类
  582. * @param $cateId
  583. * @return bool
  584. */
  585. public function checkCategory($cateId)
  586. {
  587. $cacheKey = "caches:goods:category_check_{$cateId}";
  588. if (RedisService::get($cacheKey) || RedisService::exists($cacheKey)) {
  589. return true;
  590. }
  591. $data = GoodsCategoryModel::where(['cate_id' => $cateId, 'mark' => 1])->value('id');
  592. RedisService::set($cacheKey, $data, rand(30, 60));
  593. return $data;
  594. }
  595. /**
  596. * 接口商品详情
  597. * @param $goodsId 商品ID
  598. * @param int $isReal 是否实时数据,0-是,1-否
  599. * @param int $type 数据类型:0-详情,1-仅SKU数据
  600. * @return array|false|mixed|string
  601. */
  602. public function getApiInfo($goodsId, $isReal = 0, $type = 0)
  603. {
  604. $cacheKey = "caches:goods:detail_{$goodsId}_{$isReal}_{$type}";
  605. $info = RedisService::get($cacheKey);
  606. if (empty($info)) {
  607. $params = [
  608. 'goods_id' => $goodsId,
  609. 'is_real' => $isReal,
  610. 'type' => $type
  611. ];
  612. $info = SupplyService::make()->getApiData('getGoodsDetail', $params);
  613. if ($info) {
  614. RedisService::set($cacheKey, $info, rand(5, 10));
  615. }
  616. }
  617. return $info;
  618. }
  619. public function apiCategory($num)
  620. {
  621. }
  622. }