GoodsService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 ($goodsSkuSn) {
  146. $updateData = ['update_time' => time(), 'last_update_at' => $item['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['name']) && $item['name']) {
  154. $updateData['name'] = $item['name'];
  155. }
  156. if (isset($item['val']) && $item['val']) {
  157. $updateData['val'] = $item['val'];
  158. }
  159. if (isset($item['status']) && $item['status']) {
  160. $updateData['status'] = intval($item['status']);
  161. }
  162. if (isset($item['retail_price']) && $item['retail_price']) {
  163. $updateData['retail_price'] = floatval($item['retail_price']);
  164. }
  165. if (isset($item['plat_price']) && $item['plat_price']) {
  166. $updateData['plat_price'] = floatval($item['plat_price']);
  167. }
  168. if (isset($item['detail_img']) && $item['detail_img']) {
  169. $updateData['detail_img'] = json_encode($item['detail_img'], 256);
  170. }
  171. if (isset($item['attr']) && $item['attr']) {
  172. $updateData['attr'] = json_encode($item['attr'], 256);
  173. }
  174. $this->model->where(['goods_id' => $goodsId, 'mark' => 1])->update($updateData);
  175. $updated++;
  176. } else {
  177. $error++;
  178. }
  179. } else {
  180. $error++;
  181. }
  182. }
  183. unset($item);
  184. }
  185. return ['count' => count($goods), 'updated' => $updated, 'errorCount' => $error];
  186. }
  187. /**
  188. * 更新商品
  189. * @param int $pageSize
  190. * @param $params 参数
  191. * @return array|false
  192. */
  193. public function updateGoods($pageSize = 50, $params = [])
  194. {
  195. set_time_limit(0);
  196. $cacheKey = "caches:supply:goods_list_update_{$pageSize}";
  197. if (RedisService::get($cacheKey)) {
  198. $this->error = 1047;
  199. return false;
  200. }
  201. $page = RedisService::get($cacheKey . '_page');
  202. $page = $page ? $page + 1 : 1;
  203. $lastTime = $this->model->where(['mark' => 1])->orderBy('create_time', 'desc')->value('create_time');
  204. $params = [
  205. 'limit' => $pageSize > 0 ? $pageSize : 50,
  206. 'page' => $page,
  207. 'status' => isset($params['status']) ? $params['status'] : 1, // 状态:0-全部,1-上架的,2-下架的
  208. 'supply_type' => isset($params['supply_type']) ? $params['supply_type'] : 0, // 渠道商
  209. 'title' => isset($params['title']) ? $params['title'] : '', // 标题关键词
  210. 'cate_id' => isset($params['cate_id']) ? $params['cate_id'] : '', // 分类ID
  211. 'begin_time' => $lastTime ? $lastTime : '', // 开始时间
  212. ];
  213. $goods = [];
  214. $updated = 0;
  215. $error = 0;
  216. $datas = SupplyService::make()->getApiData('getGoodsCategory', $params);
  217. if ($datas && $datas['data']) {
  218. foreach ($datas['data'] as &$item) {
  219. $goodsId = isset($item['goods_id']) ? $item['goods_id'] : 0;
  220. if ($goodsId && !$this->checkGoods($goodsId)) {
  221. $info = $this->getApiInfo($goodsId);
  222. if ($info) {
  223. $goods[] = [
  224. 'goods_id' => $goodsId,
  225. 'supply_type' => isset($item['supply_type']) ? $item['supply_type'] : 0,
  226. 'spu_sn' => isset($item['spu_sn']) ? $item['spu_sn'] : '',
  227. 'spu_name' => isset($info['spu_name']) ? $info['spu_name'] : '',
  228. 'main_img' => isset($info['main_img']) ? $info['main_img'] : '',
  229. 'detail_img' => isset($info['detail_img']) ? json_encode($info['detail_img'], 256) : '',
  230. 'goods_name' => isset($item['goods_name']) ? $item['goods_name'] : '',
  231. 'brand_name' => isset($info['brand_name']) ? $info['brand_name'] : '',
  232. 'limit_num' => isset($info['limit_num']) ? intval($info['limit_num']) : 0,
  233. 'lowest_num' => isset($info['lowest_num']) ? intval($info['lowest_num']) : 1,
  234. 'cost_price' => isset($info['cost_price']) ? floatval($info['cost_price']) : 0,
  235. 'retail_price' => isset($info['retail_price']) ? floatval($info['retail_price']) : 0,
  236. 'profit' => isset($info['profit']) ? floatval($info['profit']) : 0,
  237. 'sku_list' => isset($info['sku_list']) ? json_encode($info['sku_list'], 256) : '',
  238. 'sku_total' => isset($info['sku_total']) ? intval($info['sku_total']) : 0,
  239. 'tag' => isset($item['tag']) ? json_encode($item['tag'], 256) : '',
  240. 'status' => isset($info['status']) ? intval($info['status']) : 1,
  241. 'cate_id' => isset($item['cate_id']) ? intval($item['cate_id']) : 0,
  242. 'last_update_at' => isset($info['update_time']) ? $info['update_time'] : (isset($item['time']) && $item['time'] ? $item['time'] : date('Y-m-d H:i:s')),
  243. 'create_time' => time(),
  244. ];
  245. $updated++;
  246. } else {
  247. $error++;
  248. }
  249. } else {
  250. $error++;
  251. }
  252. }
  253. unset($item);
  254. } else {
  255. RedisService::set($cacheKey . '_page', 1, rand(300, 600));
  256. }
  257. if ($goods) {
  258. RedisService::set($cacheKey . '_page', $page, rand(300, 600));
  259. RedisService::set($cacheKey, $goods, rand(5, 10));
  260. $this->model->insertAll($goods);
  261. }
  262. return ['count' => count($goods), 'updated' => $updated, 'errorCount' => $error];
  263. }
  264. /**
  265. * 更新商品分类
  266. * @param int $pageSize
  267. * @param $params 参数
  268. * @return array|false
  269. */
  270. public function updateGoodsCategory($pageSize = 100, $params = [])
  271. {
  272. set_time_limit(0);
  273. $cacheKey = "caches:supply:goods_category_update_{$pageSize}";
  274. if (RedisService::get($cacheKey)) {
  275. $this->error = 1047;
  276. return false;
  277. }
  278. $page = RedisService::get($cacheKey . '_page');
  279. $page = $page ? $page + 1 : 1;
  280. $params = [
  281. 'limit' => $pageSize > 0 ? $pageSize : 50,
  282. 'page' => $page,
  283. 'pid' => isset($params['pid']) ? $params['pid'] : 0, // 上级ID
  284. ];
  285. $categorys = [];
  286. $updated = 0;
  287. $error = 0;
  288. $datas = SupplyService::make()->getApiData('getGoodsCategory', $params);
  289. if ($datas && $datas['data']) {
  290. foreach ($datas['data'] as &$item) {
  291. $cateId = isset($item['id']) ? $item['id'] : 0;
  292. if ($cateId && !$this->checkCategory($cateId)) {
  293. $categorys[] = [
  294. 'cate_id' => $cateId,
  295. 'name' => isset($info['name']) ? $info['name'] : '',
  296. 'pid' => isset($info['pid']) ? intval($info['pid']) : 0,
  297. 'create_time' => time(),
  298. ];
  299. $updated++;
  300. } else {
  301. $error++;
  302. }
  303. }
  304. unset($item);
  305. } else {
  306. RedisService::set($cacheKey . '_page', 1, rand(300, 600));
  307. }
  308. if ($categorys) {
  309. RedisService::set($cacheKey . '_page', $page, rand(300, 600));
  310. RedisService::set($cacheKey, $categorys, rand(5, 10));
  311. GoodsCategoryModel::insertAll($categorys);
  312. }
  313. return ['count' => count($categorys), 'updated' => $updated, 'errorCount' => $error];
  314. }
  315. /**
  316. * 验证
  317. * @param $goodsId
  318. * @return bool
  319. */
  320. public function checkGoods($goodsId)
  321. {
  322. $cacheKey = "caches:goods:check_{$goodsId}";
  323. if (RedisService::get($cacheKey) || RedisService::exists($cacheKey)) {
  324. return true;
  325. }
  326. $data = $this->model->where(['goods_id' => $goodsId, 'mark' => 1])->value('id');
  327. RedisService::set($cacheKey, $data, rand(30, 60));
  328. return $data;
  329. }
  330. /**
  331. * 接口商品详情
  332. * @param $goodsId 商品ID
  333. * @param int $isReal 是否实时数据,0-是,1-否
  334. * @param int $type 数据类型:0-详情,1-仅SKU数据
  335. * @return array|false|mixed|string
  336. */
  337. public function getApiInfo($goodsId, $isReal = 0, $type = 0)
  338. {
  339. $cacheKey = "caches:goods:detail_{$goodsId}_{$isReal}_{$type}";
  340. $info = RedisService::get($cacheKey);
  341. if (empty($info)) {
  342. $params = [
  343. 'goods_id' => $goodsId,
  344. 'is_real' => $isReal,
  345. 'type' => $type
  346. ];
  347. $info = SupplyService::make()->getApiData('getGoodsDetail', $params);
  348. if ($info) {
  349. RedisService::set($cacheKey, $info, rand(5, 10));
  350. }
  351. }
  352. return $info;
  353. }
  354. public function apiCategory($num)
  355. {
  356. }
  357. }