GoodsService.php 18 KB

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