GoodsService.php 17 KB

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