GoodsService.php 18 KB

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