GoodsService.php 17 KB

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