GoodsService.php 22 KB

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