GoodsService.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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['supply_name'] = isset($supplyList[$item['supply_type']]) ? $supplyList[$item['supply_type']] : '';
  121. $item['usdt_price'] = $usdtPrice;
  122. $item['xd_price_rate'] = $xdPrice;
  123. $item['original_price'] = $item['cost_price'];
  124. $item['cost_price'] = $usdtPrice > 0 ? moneyFormat($item['cost_price'] / $usdtPrice * $xdPrice, 2) : $item['cost_price'];
  125. }
  126. unset($item);
  127. } else {
  128. $this->updateGoods();
  129. }
  130. return [
  131. 'total' => isset($list['total']) ? $list['total'] : 9,
  132. 'pageSize' => $pageSize,
  133. 'list' => isset($list['data']) ? $list['data'] : [],
  134. ];
  135. }
  136. /**
  137. * 更新商品SKU数据到本地
  138. * @param int $pageSize
  139. * @param $params 参数
  140. * @return array|false
  141. */
  142. public function updateGoodsSku($pageSize = 100, $params = [])
  143. {
  144. $cacheKey = "caches:supply:goods_sku_update_{$pageSize}";
  145. if (RedisService::get($cacheKey)) {
  146. $this->error = 1047;
  147. return false;
  148. }
  149. $page = RedisService::get($cacheKey . '_page');
  150. $page = $page ? $page + 1 : 1;
  151. $lastDate = GoodsSkuModel::where(['mark' => 1])->orderBy('last_update_at', 'desc')->value('last_update_at');
  152. $params = [
  153. 'limit' => $pageSize > 0 ? $pageSize : 50,
  154. 'page' => $page,
  155. 'date' => $lastDate ? $lastDate : '', // 开始时间
  156. ];
  157. $goods = [];
  158. $updated = 0;
  159. $error = 0;
  160. $datas = SupplyService::make()->getApiData('getSkuUpdate', $params);
  161. if ($datas && $datas['list']) {
  162. foreach ($datas['list'] as &$item) {
  163. $goodsId = isset($item['goods_id']) ? $item['goods_id'] : 0;
  164. $goodsSkuSn = isset($item['sku_sn']) ? $item['sku_sn'] : '';
  165. if ($goodsId && $goodsSkuSn) {
  166. $skuInfo = SupplyService::make()->getApiData('getSkuDetail', ['sku_sn' => $goodsSkuSn]);
  167. if ($skuInfo) {
  168. $updateData = ['sku_sn'=> $goodsSkuSn,'remark'=>'SKU更新','update_time' => time(),'mark'=>1, 'last_update_at' => $item['update_time']];
  169. if (isset($skuInfo['sku_name']) && $skuInfo['sku_name']) {
  170. $updateData['sku_name'] = $skuInfo['sku_name'];
  171. }
  172. if (isset($skuInfo['main_img']) && $skuInfo['main_img']) {
  173. $updateData['main_img'] = $skuInfo['main_img'];
  174. }
  175. if (isset($skuInfo['spu_sn']) && $skuInfo['spu_sn']) {
  176. $updateData['spu_sn'] = $skuInfo['spu_sn'];
  177. }
  178. if (isset($skuInfo['sku_id']) && $skuInfo['sku_id']) {
  179. $updateData['sku_id'] = intval($skuInfo['sku_id']);
  180. }
  181. if (isset($skuInfo['status']) && $skuInfo['status']) {
  182. $updateData['status'] = intval($skuInfo['status']);
  183. }
  184. if (isset($skuInfo['retail_price']) && $skuInfo['retail_price']) {
  185. $updateData['retail_price'] = floatval($skuInfo['retail_price']);
  186. }
  187. if (isset($skuInfo['plat_price']) && $skuInfo['plat_price']) {
  188. $updateData['plat_price'] = floatval($skuInfo['plat_price']);
  189. }
  190. if (isset($skuInfo['detail_img']) && $skuInfo['detail_img']) {
  191. $updateData['detail_img'] = json_encode($skuInfo['detail_img'], 256);
  192. }
  193. if (isset($skuInfo['attr']) && $skuInfo['attr']) {
  194. $updateData['attr'] = json_encode($skuInfo['attr'], 256);
  195. }
  196. if(GoodsSkuModel::where(['goods_id' => $goodsId])->value('id')){
  197. GoodsSkuModel::where(['goods_id' => $goodsId, 'mark' => 1])->update($updateData);$updated++;
  198. }else{
  199. $error++;
  200. }
  201. } else {
  202. $error++;
  203. }
  204. } else {
  205. $error++;
  206. }
  207. }
  208. unset($item);
  209. RedisService::set($cacheKey . '_page', $page, rand(300, 600));
  210. }else{
  211. RedisService::set($cacheKey . '_page', 0, rand(300, 600));
  212. }
  213. return ['count' => count($goods), 'updated' => $updated, 'errorCount' => $error,'page'=>$page];
  214. }
  215. /**
  216. * 更新商品
  217. * @param int $pageSize
  218. * @param $params 参数
  219. * @return array|false
  220. */
  221. public function updateGoods($pageSize = 50, $params = [])
  222. {
  223. set_time_limit(0);
  224. $cacheKey = "caches:supply:goods_list_update_{$pageSize}";
  225. if (RedisService::get($cacheKey)) {
  226. $this->error = 1047;
  227. return false;
  228. }
  229. $page = RedisService::get($cacheKey . '_page');
  230. $page = $page ? $page + 1 : 1;
  231. $lastTime = $this->model->where(['mark' => 1])->orderBy('create_time', 'desc')->value('create_time');
  232. $params = [
  233. 'limit' => $pageSize > 0 ? $pageSize : 50,
  234. 'page' => $page,
  235. 'status' => isset($params['status']) ? $params['status'] : 1, // 状态:0-全部,1-上架的,2-下架的
  236. 'supply_type' => isset($params['supply_type']) ? $params['supply_type'] : 0, // 渠道商
  237. 'title' => isset($params['title']) ? $params['title'] : '', // 标题关键词
  238. 'cate_id' => isset($params['cate_id']) ? $params['cate_id'] : '', // 分类ID
  239. 'begin_time' => $lastTime ? $lastTime : '', // 开始时间
  240. ];
  241. $goods = [];
  242. $skus = [];
  243. $updated = 0;
  244. $error = 0;
  245. $datas = SupplyService::make()->getApiData('getGoodsList', $params);
  246. if ($datas && $datas['list']) {
  247. foreach ($datas['list'] as &$item) {
  248. $goodsId = isset($item['goods_id']) ? $item['goods_id'] : 0;
  249. if ($goodsId && !$this->checkGoods($goodsId)) {
  250. $info = $this->getApiInfo($goodsId);
  251. if ($info) {
  252. $skuList = isset($info['sku_list']) ? $info['sku_list'] : [];
  253. $goods[] = [
  254. 'goods_id' => $goodsId,
  255. 'supply_type' => isset($item['supply_type']) ? $item['supply_type'] : 0,
  256. 'spu_sn' => isset($item['spu_sn']) ? $item['spu_sn'] : '',
  257. 'spu_name' => isset($info['spu_name']) ? $info['spu_name'] : '',
  258. 'main_img' => isset($info['main_img']) ? $info['main_img'] : '',
  259. 'detail_img' => isset($info['detail_img']) ? json_encode($info['detail_img'], 256) : '',
  260. 'goods_name' => isset($item['goods_name']) ? $item['goods_name'] : '',
  261. 'brand_name' => isset($info['brand_name']) ? $info['brand_name'] : '',
  262. 'limit_num' => isset($info['limit_num']) ? intval($info['limit_num']) : 0,
  263. 'lowest_num' => isset($info['lowest_num']) ? intval($info['lowest_num']) : 1,
  264. 'cost_price' => isset($info['cost_price']) ? floatval($info['cost_price']) : 0,
  265. 'retail_price' => isset($info['retail_price']) ? floatval($info['retail_price']) : 0,
  266. 'profit' => isset($info['profit']) ? floatval($info['profit']) : 0,
  267. 'sku_list' => $skuList? json_encode($skuList,256):'',
  268. 'sku_total' => isset($info['sku_total']) ? intval($info['sku_total']) : 0,
  269. 'tag' => isset($item['tag']) ? json_encode($item['tag'], 256) : '',
  270. 'status' => isset($info['status']) ? intval($info['status']) : 1,
  271. 'cate_id' => isset($item['cate_id']) ? intval($item['cate_id']) : 0,
  272. 'last_update_at' => isset($info['update_time']) ? $info['update_time'] : (isset($item['time']) && $item['time'] ? $item['time'] : date('Y-m-d H:i:s')),
  273. 'create_time' => time(),
  274. ];
  275. foreach($skuList as $v){
  276. $skus[] = [
  277. 'sku_id'=> isset($v['sku_id'])? $v['sku_id'] : 0,
  278. 'goods_id'=> isset($v['goods_id'])? $v['goods_id'] : 0,
  279. 'spu_sn'=> isset($v['spu_sn'])? $v['spu_sn'] : '',
  280. 'sku_sn'=> isset($v['sku_sn'])? $v['sku_sn'] : '',
  281. 'sku_name'=> isset($v['sku_name'])? $v['sku_name'] : '',
  282. 'main_img'=> isset($v['main_img'])? $v['main_img'] : '',
  283. 'status'=> isset($v['status'])? $v['status'] : 1,
  284. 'source_type'=> isset($v['source_type'])? $v['source_type'] : 0,
  285. 'retail_price'=> isset($v['retail_price'])? floatval($v['retail_price']) : 0,
  286. 'plat_price'=> isset($v['plat_price'])? floatval($v['plat_price']) : 0,
  287. 'profit'=> isset($v['profit'])? floatval($v['profit']) : 0,
  288. 'last_update_at'=> isset($v['update_time'])? $v['update_time'] : date('Y-m-d H:i:s'),
  289. 'detail_img'=> isset($v['detail_img'])? json_encode($v['detail_img'],256) : '',
  290. 'attr'=> isset($v['attr'])? json_encode($v['attr'],256) : '',
  291. 'remark'=>'SKU同步创建',
  292. ];
  293. }
  294. $updated++;
  295. } else {
  296. $error++;
  297. }
  298. } else {
  299. $error++;
  300. }
  301. }
  302. unset($item);
  303. } else {
  304. RedisService::set($cacheKey . '_page', 0, rand(300, 600));
  305. }
  306. if ($goods) {
  307. RedisService::set($cacheKey . '_page', $page, rand(300, 600));
  308. RedisService::set($cacheKey, $goods, rand(5, 10));
  309. DB::beginTransaction();
  310. try {
  311. $this->model->insertAll($goods);
  312. if($skus){
  313. GoodsSkuModel::insert($skus);
  314. }
  315. DB::commit();
  316. }catch (\Exception $exception){
  317. DB::rollBack();
  318. }
  319. }
  320. return ['count' => count($goods), 'updated' => $updated, 'errorCount' => $error,'page'=>$page];
  321. }
  322. /**
  323. * 更新商品分类
  324. * @param int $pid 上级ID
  325. * @param int $pageSize
  326. * @param $params 参数
  327. * @return array|false
  328. */
  329. public function updateGoodsCategory($pid=0, $pageSize = 200, $params = [])
  330. {
  331. set_time_limit(0);
  332. $cacheKey = "caches:supply:goods_category_update_{$pid}_{$pageSize}";
  333. if (RedisService::get($cacheKey)) {
  334. $this->error = 1047;
  335. return false;
  336. }
  337. $params = [
  338. 'limit' => $pageSize > 0 ? $pageSize : 50,
  339. 'page' => 1,
  340. 'pid' => $pid, // 上级ID
  341. ];
  342. $categorys = [];
  343. $updated = 0;
  344. $error = 0;
  345. $datas = SupplyService::make()->getApiData('getGoodsCategory', $params);
  346. if ($datas && $datas['data']) {
  347. foreach ($datas['data'] as &$item) {
  348. $cateId = isset($item['id']) ? $item['id'] : 0;
  349. if ($cateId && !$this->checkCategory($cateId)) {
  350. $categorys[] = [
  351. 'cate_id' => $cateId,
  352. 'name' => isset($item['name']) ? $item['name'] : '',
  353. 'pid' => isset($item['pid']) ? intval($item['pid']) : 0,
  354. 'create_time' => time(),
  355. ];
  356. $updated++;
  357. } else {
  358. $error++;
  359. }
  360. }
  361. unset($item);
  362. }
  363. if ($categorys) {
  364. RedisService::set($cacheKey, $categorys, rand(5, 10));
  365. GoodsCategoryModel::insert($categorys);
  366. }
  367. return ['count' => count($categorys), 'updated' => $updated,'pid'=>$pid, 'errorCount' => $error];
  368. }
  369. /**
  370. * 验证
  371. * @param $goodsId
  372. * @return bool
  373. */
  374. public function checkGoods($goodsId)
  375. {
  376. $cacheKey = "caches:goods:check_{$goodsId}";
  377. if (RedisService::get($cacheKey) || RedisService::exists($cacheKey)) {
  378. return true;
  379. }
  380. $data = $this->model->where(['goods_id' => $goodsId, 'mark' => 1])->value('id');
  381. RedisService::set($cacheKey, $data, rand(30, 60));
  382. return $data;
  383. }
  384. /**
  385. * 验证分类
  386. * @param $cateId
  387. * @return bool
  388. */
  389. public function checkCategory($cateId)
  390. {
  391. $cacheKey = "caches:goods:category_check_{$cateId}";
  392. if (RedisService::get($cacheKey) || RedisService::exists($cacheKey)) {
  393. return true;
  394. }
  395. $data = GoodsCategoryModel::where(['cate_id' => $cateId, 'mark' => 1])->value('id');
  396. RedisService::set($cacheKey, $data, rand(30, 60));
  397. return $data;
  398. }
  399. /**
  400. * 接口商品详情
  401. * @param $goodsId 商品ID
  402. * @param int $isReal 是否实时数据,0-是,1-否
  403. * @param int $type 数据类型:0-详情,1-仅SKU数据
  404. * @return array|false|mixed|string
  405. */
  406. public function getApiInfo($goodsId, $isReal = 0, $type = 0)
  407. {
  408. $cacheKey = "caches:goods:detail_{$goodsId}_{$isReal}_{$type}";
  409. $info = RedisService::get($cacheKey);
  410. if (empty($info)) {
  411. $params = [
  412. 'goods_id' => $goodsId,
  413. 'is_real' => $isReal,
  414. 'type' => $type
  415. ];
  416. $info = SupplyService::make()->getApiData('getGoodsDetail', $params);
  417. if ($info) {
  418. RedisService::set($cacheKey, $info, rand(5, 10));
  419. }
  420. }
  421. return $info;
  422. }
  423. public function apiCategory($num)
  424. {
  425. }
  426. }