GoodsService.php 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  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\CartsModel;
  14. use App\Models\GoodsCategoryModel;
  15. use App\Models\GoodsModel;
  16. use App\Models\GoodsSkuModel;
  17. use App\Models\MemberModel;
  18. use App\Models\MerchantModel;
  19. use App\Models\OrderModel;
  20. use App\Models\ShopModel;
  21. use App\Models\TradeModel;
  22. use App\Services\BaseService;
  23. use App\Services\ConfigService;
  24. use App\Services\RedisService;
  25. use App\Services\SupplyService;
  26. use App\Services\WalletService;
  27. use BN\Red;
  28. use Illuminate\Support\Facades\DB;
  29. /**
  30. * 商品管理-服务类
  31. * @author laravel开发员
  32. * @since 2020/11/11
  33. * Class GoodsService
  34. * @package App\Services\Api
  35. */
  36. class GoodsService extends BaseService
  37. {
  38. // 静态对象
  39. protected static $instance = null;
  40. /**
  41. * 构造函数
  42. * @author laravel开发员
  43. * @since 2020/11/11
  44. * GoodsService constructor.
  45. */
  46. public function __construct()
  47. {
  48. $this->model = new GoodsModel();
  49. }
  50. /**
  51. * 静态入口
  52. * @return static|null
  53. */
  54. public static function make()
  55. {
  56. if (!self::$instance) {
  57. self::$instance = (new static());
  58. }
  59. return self::$instance;
  60. }
  61. /**
  62. * 商品列表
  63. * @param $params
  64. * @param int $pageSize
  65. * @param int $userId
  66. * @return array
  67. */
  68. public function getDataList($params, $pageSize = 12, $userId = 0)
  69. {
  70. $model = $this->model->with(['skuList'])->from('goods as a')
  71. ->where(['a.status' => 1, 'a.mark' => 1])
  72. ->where('a.retail_price', '>', 0)
  73. ->where(function ($query) use ($params) {
  74. $supplyType = isset($params['supply_type']) ? intval($params['supply_type']) : 0;
  75. if ($supplyType > 0) {
  76. $query->where('a.supply_type', $supplyType);
  77. }
  78. $cateId = isset($params['cate_id']) ? intval($params['cate_id']) : 0;
  79. if ($cateId > 0) {
  80. $subIds = GoodsCategoryModel::where(['pid' => $cateId, 'mark' => 1, 'status' => 1])->pluck('cate_id');
  81. $query->where(function ($query) use ($cateId, $subIds) {
  82. if ($subIds) {
  83. $query->whereIn('a.cate_id', $subIds)
  84. ->orWhere('a.cate_id', $cateId);
  85. } else {
  86. $query->where('a.cate_id', $cateId);
  87. }
  88. });
  89. }
  90. })
  91. ->where(function ($query) use ($params) {
  92. $keyword = isset($params['kw']) ? $params['kw'] : '';
  93. if ($keyword) {
  94. $query->where('a.goods_name', 'like', "%{$keyword}%")
  95. ->orWhere('a.spu_name', 'like', "%{$keyword}%")
  96. ->orWhere('a.tag', 'like', "%{$keyword}%");
  97. }
  98. })
  99. ->select(['a.*']);
  100. // 排序
  101. $sortType = isset($params['sort_type']) ? $params['sort_type'] : 1;
  102. if ($sortType == 2) {
  103. $model = $model->orderBy('a.is_recommend', 'asc')->orderBy('a.sales', 'desc');
  104. }
  105. $list = $model->orderBy('a.create_time', 'desc')
  106. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  107. $list = $list ? $list->toArray() : [];
  108. if ($list) {
  109. $locale = RedisService::get("caches:locale:lang_{$userId}");
  110. $locale = $locale ? $locale : session('locale_lang');
  111. $locale = $locale ? $locale : 'zh-cn';
  112. $supplyList = config('goods.supplyList');
  113. $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
  114. if ($usdtPrice <= 0) {
  115. $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
  116. $usdtPrice = $usdtCnyPrice > 0 && $usdtCnyPrice < 100 ? $usdtCnyPrice : 0;
  117. }
  118. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  119. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  120. $awardWaitRate = ConfigService::make()->getConfigByCode('shop_award_score_rate', 0);
  121. $awardWaitRate = $awardWaitRate > 0 && $awardWaitRate < 1000 ? $awardWaitRate : 0;
  122. $waitRate = ConfigService::make()->getConfigByCode('day_wait_score_rate', 0);
  123. $waitRate = $waitRate > 0 && $waitRate < 100 ? $waitRate : 0;
  124. foreach ($list['data'] as &$item) {
  125. $item['detail_img'] = isset($item['detail_img']) && $item['detail_img'] ? json_decode($item['detail_img'], true) : [];
  126. $item['supply_name'] = isset($supplyList[$item['supply_type']]) ? $supplyList[$item['supply_type']] : '';
  127. $item['usdt_price'] = $usdtPrice;
  128. $item['xd_price_rate'] = $xdPrice;
  129. $item['retail_price1'] = $item['retail_price'];
  130. $item['retail_price'] = $this->getRealSalePrice($item['cost_price']);
  131. $item['wait_score'] = moneyFormat($item['retail_price'] * $awardWaitRate / 100 * $waitRate / 100, 2);
  132. }
  133. unset($item);
  134. }
  135. return [
  136. 'total' => isset($list['total']) ? $list['total'] : 9,
  137. 'pageSize' => $pageSize,
  138. 'list' => isset($list['data']) ? $list['data'] : [],
  139. ];
  140. }
  141. /**
  142. * 详情
  143. * @param $id
  144. * @return array
  145. */
  146. public function getInfo($goodsId, $userId = 0, $updateView = true)
  147. {
  148. $field = ['a.*'];
  149. $info = $this->model->from('goods as a')->with(['category', 'skuList'])
  150. ->where(['a.goods_id' => $goodsId, 'a.status' => 1, 'a.mark' => 1])
  151. ->select($field)
  152. ->first();
  153. $info = $info ? $info->toArray() : [];
  154. if ($info) {
  155. if (isset($info['main_img'])) {
  156. $info['main_img'] = $info['main_img'] ? get_image_url($info['main_img']) : '';
  157. }
  158. if (isset($info['detail_img'])) {
  159. $info['detail_img'] = $info['detail_img'] ? json_decode($info['detail_img'], true) : [];
  160. $thumbs = [$info['main_img']];
  161. $info['detail_img'] = $info['detail_img']? array_merge($thumbs,$info['detail_img']) : $thumbs;
  162. }
  163. if (empty($info['detail_img'])) {
  164. $info['detail_img'] = [$info['main_img']];
  165. }
  166. $supplyList = config('goods.supplyList');
  167. $info['supply_name'] = isset($supplyList[$info['supply_type']]) ? $supplyList[$info['supply_type']] : '';
  168. $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
  169. if ($usdtPrice <= 0) {
  170. $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
  171. $usdtPrice = $usdtCnyPrice > 0 && $usdtCnyPrice < 100 ? $usdtCnyPrice : 0;
  172. }
  173. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  174. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  175. $info['usdt_price_rate'] = $usdtPrice;
  176. $info['xd_price'] = $xdPrice;
  177. $info['custom_uid'] = ConfigService::make()->getConfigByCode('xl_custom_id', 100001);
  178. $awardWaitRate = ConfigService::make()->getConfigByCode('shop_award_score_rate', 0);
  179. $awardWaitRate = $awardWaitRate > 0 && $awardWaitRate < 1000 ? $awardWaitRate : 0;
  180. $waitRate = ConfigService::make()->getConfigByCode('day_wait_score_rate', 0);
  181. $waitRate = $waitRate > 0 && $waitRate < 100 ? $waitRate : 0;
  182. if (isset($info['retail_price']) && $info['retail_price']) {
  183. $info['retail_price1'] = $info['retail_price'];
  184. $info['retail_price'] = $this->getRealSalePrice($info['cost_price']);
  185. $info['wait_score'] = moneyFormat($info['retail_price'] * $awardWaitRate / 100 * $waitRate / 100, 2);
  186. }
  187. if (isset($info['sku_list']) && $info['sku_list']) {
  188. foreach ($info['sku_list'] as &$v) {
  189. $v['detail_img'] = $v['detail_img'] ? json_decode($v['detail_img'], true) : [];
  190. $v['attr'] = $v['attr'] ? json_decode($v['attr'], true) : [];
  191. $v['main_img'] = $v['main_img'] ? get_image_url($v['main_img']) : '';
  192. $v['retail_price_1'] = $v['retail_price'];
  193. $v['retail_price'] = $this->getRealSalePrice($v['plat_price']);
  194. $v['wait_score'] = moneyFormat($v['retail_price'] * $awardWaitRate / 100 * $waitRate / 100, 2);
  195. }
  196. unset($v);
  197. }
  198. // 更新访问量
  199. if ($updateView) {
  200. $this->updateView($userId, $goodsId);
  201. }
  202. }
  203. return $info;
  204. }
  205. /**
  206. * 实际售价
  207. * @param $price 成本价或其他价格
  208. * @param bool $float 是否浮动转换,1-浮动转换,2-不浮动转换,3-浮动不转换
  209. * @return string
  210. */
  211. public function getRealSalePrice($price, $float = 1)
  212. {
  213. $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
  214. if ($usdtPrice <= 0) {
  215. $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
  216. $usdtPrice = $usdtCnyPrice > 0 && $usdtCnyPrice < 100 ? $usdtCnyPrice : 0;
  217. }
  218. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  219. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  220. $floatRate = ConfigService::make()->getConfigByCode('goods_price_float_rate', 0);
  221. $floatRate = $floatRate > 0 && $floatRate < 100 ? $floatRate : 0;
  222. $price = $float == 2 ? $price : floatval($price * (1 + ($floatRate / 100)));
  223. if ($float == 3) {
  224. return $price;
  225. }
  226. return $usdtPrice > 0 ? moneyFormat($price / $usdtPrice * $xdPrice, 2) : $price;
  227. }
  228. /**
  229. * 获取要购买或结算的商品列表
  230. * @param $userId
  231. * @param array $params
  232. * @return array|false|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
  233. */
  234. public function getBuyList($userId, $params = [])
  235. {
  236. $goodsId = isset($params['goods_id']) ? $params['goods_id'] : 0;
  237. $skuId = isset($params['sku_id']) ? $params['sku_id'] : 0;
  238. $num = isset($params['num']) ? $params['num'] : 0;
  239. $cartIds = isset($params['cart_ids']) ? $params['cart_ids'] : '';
  240. $cartIds = $cartIds ? explode('|', $cartIds) : [];
  241. if (empty($goodsId) && empty($cartIds)) {
  242. $this->error = 2901;
  243. return false;
  244. }
  245. if ($goodsId && (empty($skuId) || $num <= 0)) {
  246. $this->error = 2901;
  247. return false;
  248. }
  249. $cacheKey = "caches:goods:buyList:{$userId}_" . md5(json_encode($params, 256));
  250. $datas = RedisService::get($cacheKey);
  251. if ($datas) {
  252. return $datas;
  253. }
  254. $goods = [];
  255. $skuList = [];
  256. $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
  257. if ($usdtPrice <= 0) {
  258. $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
  259. $usdtPrice = $usdtCnyPrice > 0 && $usdtCnyPrice < 100 ? $usdtCnyPrice : 0;
  260. }
  261. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  262. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  263. if ($goodsId) {
  264. $info = $this->getInfo($goodsId, $userId, false);
  265. if ($info) {
  266. $info['num'] = $num;
  267. $info['sku_id'] = $skuId;
  268. $skuInfo = GoodsSkuModel::where(['goods_id' => $goodsId, 'sku_id' => $skuId, 'mark' => 1])->first();
  269. $price = isset($skuInfo['plat_price']) ? $skuInfo['plat_price'] : 0;
  270. if ($price) {
  271. $info['retail_price1'] = $info['retail_price'];
  272. $info['retail_price'] = $this->getRealSalePrice($price);
  273. }
  274. if (isset($skuInfo['attr']) && $skuInfo['attr']) {
  275. $skuInfo['attr'] = json_decode($skuInfo['attr'], true);
  276. }
  277. $skuList[$goodsId] = [
  278. 'sku_id' => $skuId,
  279. 'num' => $num
  280. ];
  281. $info['sku'] = $skuInfo;
  282. $goods[] = $info;
  283. }
  284. } else {
  285. $goods = CartsModel::with(['sku'])->from('carts as a')
  286. ->leftJoin('goods as b', 'b.goods_id', '=', 'a.goods_id')
  287. ->leftJoin('goods_sku as c', 'c.sku_id', '=', 'a.sku_id')
  288. ->whereIn('a.id', $cartIds)
  289. ->where(['a.status' => 1, 'a.mark' => 1, 'b.status' => 1, 'b.mark' => 1])
  290. ->where('b.retail_price', '>', 0)
  291. ->where('a.num', '>', 0)
  292. ->select(['b.goods_id', 'b.merch_id', 'b.goods_name', 'b.supply_type', 'b.main_img', 'b.cost_price', 'b.retail_price', 'b.limit_num', 'b.lowest_num', 'b.brand_name', 'a.num', 'a.sku_id'])
  293. ->get();
  294. if ($goods) {
  295. // 价格等参数格式化
  296. $locale = RedisService::get("caches:locale:lang_{$userId}");
  297. $locale = $locale ? $locale : session('locale_lang');
  298. $locale = $locale ? $locale : 'zh-cn';
  299. $supplyList = config('goods.supplyList');
  300. foreach ($goods as &$item) {
  301. $item['detail_img'] = isset($item['detail_img']) && $item['detail_img'] ? json_decode($item['detail_img'], true) : [];
  302. $item['supply_name'] = isset($supplyList[$item['supply_type']]) ? $supplyList[$item['supply_type']] : '';
  303. $item['usdt_price'] = $usdtPrice;
  304. $item['xd_price_rate'] = $xdPrice;
  305. $item['retail_price1'] = $item['retail_price'];
  306. $skuInfo = isset($item['sku']) ? $item['sku'] : [];
  307. if (isset($skuInfo['attr']) && $skuInfo['attr']) {
  308. $skuInfo['attr'] = json_decode($skuInfo['attr'], true);
  309. }
  310. $item['sku'] = $skuInfo;
  311. $price = isset($skuInfo['plat_price']) ? $skuInfo['plat_price'] : 0;
  312. if ($price) {
  313. $item['retail_price'] = $this->getRealSalePrice($price);
  314. } else {
  315. $item['retail_price'] = $this->getRealSalePrice($item['cost_price']);
  316. }
  317. $skuList[$item['goods_id']] = [
  318. 'sku_id' => $item['sku_id'],
  319. 'num' => $item['num']
  320. ];
  321. }
  322. unset($item);
  323. }
  324. }
  325. if (empty($goods)) {
  326. $this->error = 2901;
  327. return false;
  328. }
  329. RedisService::set($cacheKey, ['sku_list' => array_values($skuList), 'goods' => $goods], rand(2, 3));
  330. return ['sku_list' => array_values($skuList), 'goods' => $goods];
  331. }
  332. /**
  333. * 订单商品
  334. * @param $userId
  335. * @param $ids
  336. * @return array
  337. */
  338. public function getOrderGoods($userId, $ids)
  339. {
  340. $cacheKey = "caches:goodsOrder:{$userId}_" . md5(json_encode($ids, 256));
  341. $datas = RedisService::get($cacheKey);
  342. if ($datas) {
  343. return $datas;
  344. }
  345. $goods = $this->model->from('goods as a')
  346. ->whereIn('a.goods_id', $ids)
  347. ->where(['a.status' => 1, 'a.mark' => 1])
  348. ->where('a.retail_price', '>', 0)
  349. ->select(['a.goods_id', 'a.merch_id', 'a.goods_name', 'a.cate_id', 'a.supply_type', 'a.main_img', 'a.cost_price', 'a.retail_price'])
  350. ->get();
  351. $goods = $goods ? $goods->toArray() : [];
  352. if ($goods) {
  353. RedisService::set($cacheKey, $goods, rand(5, 10));
  354. }
  355. return $goods;
  356. }
  357. /**
  358. * 添加/更新购物车
  359. * @param $userId
  360. * @param $goodsId
  361. * @param $params
  362. * @return array|false
  363. */
  364. public function updateCart($userId, $goodsId, $params)
  365. {
  366. $skuId = isset($params['sku_id']) ? $params['sku_id'] : 0;
  367. $status = isset($params['status']) ? $params['status'] : 0;
  368. $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0;
  369. $num = isset($params['num']) ? $params['num'] : 1;
  370. if ($skuId <= 0 || $goodsId <= 0 || $userId <= 0 || $num <= 0) {
  371. $this->error = 2014;
  372. return false;
  373. }
  374. if (CartsModel::where(['user_id' => $userId, 'status' => 1, 'mark' => 1])->count('id') > 20) {
  375. $this->error = 1050;
  376. return false;
  377. }
  378. $cartId = CartsModel::where(['user_id' => $userId, 'goods_id' => $goodsId, 'sku_id' => $skuId])->value('id');
  379. if ($cartId) {
  380. CartsModel::where(['id' => $cartId])->update(['num' => $num, 'merch_id' => $merchId, 'status' => $status, 'mark' => 1, 'update_time' => time()]);
  381. RedisService::clear("caches:members:cartList:{$userId}");
  382. $count = $this->getCartCount($userId, true);
  383. return ['id' => $cartId, 'count' => $count];
  384. } else {
  385. $cartId = CartsModel::insertGetId(['user_id' => $userId, 'goods_id' => $goodsId, 'merch_id' => $merchId, 'sku_id' => $skuId, 'num' => $num, 'status' => $status, 'mark' => 1, 'create_time' => time()]);
  386. RedisService::clear("caches:members:cartList:{$userId}");
  387. $count = $this->getCartCount($userId, true);
  388. return ['id' => $cartId, 'count' => $count];
  389. }
  390. }
  391. /**
  392. * 删除购物车商品
  393. * @param $userId
  394. * @param $params
  395. * @return bool
  396. */
  397. public function deleteCart($userId, $params)
  398. {
  399. $ids = isset($params['ids']) ? $params['ids'] : [];
  400. if (empty($ids)) {
  401. $this->error = 2923;
  402. return false;
  403. }
  404. CartsModel::whereIn('id', $ids)->update(['status' => 2, 'update_time' => time()]);
  405. $this->error = 1002;
  406. RedisService::clear("caches:members:cartCount:{$userId}");
  407. RedisService::clear("caches:members:cartList:{$userId}");
  408. return true;
  409. }
  410. /**
  411. * 购物车列表
  412. * @param $userId
  413. * @param int $pageSize
  414. * @return array|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|mixed
  415. */
  416. public function getCartList($userId, $pageSize = 30)
  417. {
  418. $cacheKey = "caches:members:cartList:{$userId}";
  419. $cacheCountKey = "caches:members:cartCount:{$userId}";
  420. $datas = RedisService::get($cacheKey);
  421. if ($datas) {
  422. return $datas;
  423. }
  424. $datas = CartsModel::with(['sku'])->from('carts as a')
  425. ->leftJoin('goods as b', 'b.goods_id', '=', 'a.goods_id')
  426. ->leftJoin('goods_sku as c', 'c.sku_id', '=', 'a.sku_id')
  427. ->where(['a.status' => 1, 'a.mark' => 1, 'b.status' => 1, 'b.mark' => 1])
  428. ->where('b.cost_price', '>', 0)
  429. ->where('a.num', '>', 0)
  430. ->where('a.user_id', '=', $userId)
  431. ->select(['a.id as cart_id', 'b.goods_id', 'b.merch_id', 'b.goods_name', 'b.supply_type', 'b.main_img', 'b.cost_price', 'b.retail_price', 'b.limit_num', 'b.lowest_num', 'b.brand_name', 'a.num', 'a.sku_id'])
  432. ->orderBy('a.create_time', 'desc')
  433. ->limit($pageSize)
  434. ->get();
  435. $datas = $datas ? $datas->toArray() : [];
  436. if ($datas) {
  437. // 价格等参数格式化
  438. $locale = RedisService::get("caches:locale:lang_{$userId}");
  439. $locale = $locale ? $locale : session('locale_lang');
  440. $locale = $locale ? $locale : 'zh-cn';
  441. $supplyList = config('goods.supplyList');
  442. $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
  443. if ($usdtPrice <= 0) {
  444. $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
  445. $usdtPrice = $usdtCnyPrice > 0 && $usdtCnyPrice < 100 ? $usdtCnyPrice : 0;
  446. }
  447. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  448. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  449. foreach ($datas as &$item) {
  450. $item['detail_img'] = isset($item['detail_img']) && $item['detail_img'] ? json_decode($item['detail_img'], true) : [];
  451. $item['supply_name'] = isset($supplyList[$item['supply_type']]) ? $supplyList[$item['supply_type']] : '';
  452. $item['usdt_price'] = $usdtPrice;
  453. $item['xd_price_rate'] = $xdPrice;
  454. $item['retail_price1'] = $item['retail_price'];
  455. $skuInfo = isset($item['sku']) ? $item['sku'] : [];
  456. $skuInfo['attr'] = isset($skuInfo['attr']) && $skuInfo['attr'] ? json_decode($skuInfo['attr'], true) : [];
  457. $item['sku'] = $skuInfo;
  458. $price = isset($skuInfo['plat_price']) ? $skuInfo['plat_price'] : 0;
  459. if ($price) {
  460. $item['retail_price'] = $this->getRealSalePrice($price);
  461. } else {
  462. $item['retail_price'] = $this->getRealSalePrice($item['cost_price']);
  463. }
  464. }
  465. unset($item);
  466. RedisService::set($cacheCountKey, count($datas), rand(300, 600));
  467. RedisService::set($cacheKey, $datas, rand(300, 600));
  468. }
  469. return $datas;
  470. }
  471. /**
  472. * 购物车中数量
  473. * @param $userId
  474. * @return array|mixed
  475. */
  476. public function getCartCount($userId, $refresh = false)
  477. {
  478. $cacheKey = "caches:member:cartCount:{$userId}";
  479. $data = RedisService::get($cacheKey);
  480. if ($data > 0 && !$refresh) {
  481. return $data;
  482. }
  483. $data = CartsModel::from('carts as a')
  484. ->leftJoin('goods as b', 'b.goods_id', '=', 'a.goods_id')
  485. ->where(['a.status' => 1, 'a.mark' => 1, 'b.status' => 1, 'b.mark' => 1])
  486. ->where('a.num', '>', 0)
  487. ->where('b.cost_price', '>', 0)
  488. ->where('a.user_id',$userId)
  489. ->count('a.id');
  490. if ($data) {
  491. RedisService::set($cacheKey, $data, rand(300, 600));
  492. }
  493. return $data;
  494. }
  495. /**
  496. * 运费
  497. */
  498. public function getFreight($userId, $addressId, $skuList)
  499. {
  500. $cacheKey = "caches:goods:freight:{$userId}_{$addressId}_" . md5(json_encode($skuList, 256));
  501. $data = RedisService::get($cacheKey);
  502. if ($data) {
  503. return $data;
  504. }
  505. if (empty($addressId)) {
  506. $address = MemberAddressService::make()->getBindInfo($userId);
  507. $streetCode = isset($address['street_code']) ? $address['street_code'] : '';
  508. $addressId = $streetCode ? $streetCode : (isset($address['district_code']) ? $address['district_code'] : '');
  509. }
  510. $result = SupplyService::make()->getApiData('getFreight', ['address_id' => intval($addressId), 'sku_list' => $skuList]);
  511. $freight = isset($result['freight']) ? floatval($result['freight']) : -1;
  512. if ($freight >= 0) {
  513. // 价格参数
  514. $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
  515. if ($usdtPrice <= 0) {
  516. $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
  517. $usdtPrice = $usdtCnyPrice > 0 && $usdtCnyPrice < 100 ? $usdtCnyPrice : 0;
  518. }
  519. $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
  520. $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
  521. $xdFreight = $freight ? moneyFormat($freight / $usdtPrice * $xdPrice, 2) : 0;
  522. RedisService::set($cacheKey, ['freight' => $xdFreight, 'fee' => $freight], rand(5, 10));
  523. return ['freight' => $xdFreight, 'fee' => $freight];
  524. } else {
  525. $errorCode = SupplyService::make()->getError();
  526. $this->error = $errorCode ? $errorCode : 1052;
  527. return false;
  528. }
  529. }
  530. public function getSkuInfo($sakuId)
  531. {
  532. $cacheKey = "caches:goodsSku:{$sakuId}";
  533. $data = RedisService::get($cacheKey);
  534. if ($data) {
  535. return $data;
  536. }
  537. $data = GoodsSkuModel::where(['sku_id' => $sakuId, 'status' => 1, 'mark' => 1])
  538. ->select(['id', 'sku_id', 'goods_id', 'retail_price', 'plat_price', 'sku_sn', 'sku_name', 'main_img', 'attr'])
  539. ->first();
  540. $data = $data ? $data->toArray() : [];
  541. if ($data) {
  542. RedisService::set($cacheKey, $data, rand(3, 5));
  543. }
  544. return $data;
  545. }
  546. /**
  547. * 更新浏览量
  548. * @param $userId
  549. * @param $dynamicId
  550. * @return array|mixed
  551. */
  552. public function updateView($userId, $id)
  553. {
  554. $cacheKey = "caches:goods:views:u{$userId}_d{$id}";
  555. $data = RedisService::get($cacheKey);
  556. if ($data) {
  557. return false;
  558. }
  559. $data = $this->model->where(['goods_id' => $id])->update(['views' => DB::raw('views + 1'), 'update_time' => time()]);
  560. RedisService::set($cacheKey, $id, rand(1, 3) * 7200);
  561. return $data;
  562. }
  563. /**
  564. * 更新商品SKU数据到本地
  565. * @param int $pageSize
  566. * @param $params 参数
  567. * @return array|false
  568. */
  569. public function updateGoodsSku($pageSize = 100, $params = [])
  570. {
  571. $cacheKey = "caches:supply:goods_sku_update_{$pageSize}";
  572. if (RedisService::get($cacheKey)) {
  573. $this->error = 1047;
  574. return false;
  575. }
  576. $page = RedisService::get($cacheKey . '_page');
  577. $page = $page ? $page + 1 : 1;
  578. $lastDate = GoodsSkuModel::where(['mark' => 1])->orderBy('last_update_at', 'desc')->value('last_update_at');
  579. $params = [
  580. 'limit' => $pageSize > 0 ? $pageSize : 50,
  581. 'page' => $page,
  582. 'date' => $lastDate ? $lastDate : '', // 开始时间
  583. ];
  584. $goods = [];
  585. $updated = 0;
  586. $error = 0;
  587. $datas = SupplyService::make()->getApiData('getSkuUpdate', $params);
  588. if ($datas && $datas['list']) {
  589. foreach ($datas['list'] as &$item) {
  590. $goodsId = isset($item['goods_id']) ? $item['goods_id'] : 0;
  591. $goodsSkuSn = isset($item['sku_sn']) ? $item['sku_sn'] : '';
  592. $changeType = isset($item['change_type']) ? $item['change_type'] : '';
  593. if ($goodsId && $goodsSkuSn && $changeType) {
  594. $skuInfo = SupplyService::make()->getApiData('getSkuDetail', ['sku_sn' => $goodsSkuSn]);
  595. if ($skuInfo) {
  596. $updateData = ['goods_id' => $goodsId, 'sku_sn' => $goodsSkuSn, 'remark' => 'SKU更新', 'update_time' => time(), 'mark' => 1, 'last_update_at' => $item['update_time']];
  597. if (isset($skuInfo['sku_name']) && $skuInfo['sku_name']) {
  598. $updateData['sku_name'] = $skuInfo['sku_name'];
  599. }
  600. if (isset($skuInfo['main_img']) && $skuInfo['main_img']) {
  601. $updateData['main_img'] = $skuInfo['main_img'];
  602. }
  603. if (isset($skuInfo['spu_sn']) && $skuInfo['spu_sn']) {
  604. $updateData['spu_sn'] = $skuInfo['spu_sn'];
  605. }
  606. if (isset($skuInfo['sku_id']) && $skuInfo['sku_id']) {
  607. $updateData['sku_id'] = intval($skuInfo['sku_id']);
  608. }
  609. if (isset($skuInfo['status']) && $skuInfo['status']) {
  610. $updateData['status'] = intval($skuInfo['status']);
  611. }
  612. if (isset($skuInfo['retail_price']) && $skuInfo['retail_price']) {
  613. $updateData['retail_price'] = floatval($skuInfo['retail_price']);
  614. }
  615. if (isset($skuInfo['plat_price']) && $skuInfo['plat_price']) {
  616. $updateData['plat_price'] = floatval($skuInfo['plat_price']);
  617. }
  618. if (isset($skuInfo['detail_img']) && $skuInfo['detail_img']) {
  619. $updateData['detail_img'] = json_encode($skuInfo['detail_img'], 256);
  620. }
  621. if (isset($skuInfo['attr']) && $skuInfo['attr']) {
  622. $updateData['attr'] = json_encode($skuInfo['attr'], 256);
  623. }
  624. if(isset($skuInfo['status'])){
  625. $updateGoodsData = ['update_time'=>time()];
  626. // 下架
  627. if($skuInfo['status'] == 0){
  628. $updateGoodsData['status'] = 0;
  629. }
  630. // 删除
  631. else if(in_array(intval($skuInfo['status']),[2,3])){
  632. $updateGoodsData['status'] = 3;
  633. $updateGoodsData['mark'] = 0;
  634. }else if($skuInfo['status'] == 1){
  635. $updateGoodsData['status'] = 1;
  636. }
  637. // 更新商品状态
  638. GoodsModel::where(['goods_id' => $goodsId])->update($updateGoodsData);
  639. }
  640. // 更新商品SKU信息
  641. if (GoodsSkuModel::where(['goods_id' => $goodsId])->value('id')) {
  642. GoodsSkuModel::where(['goods_id' => $goodsId, 'mark' => 1])->update($updateData);
  643. $updated++;
  644. } else {
  645. $error++;
  646. }
  647. } else {
  648. $error++;
  649. }
  650. } else {
  651. $error++;
  652. }
  653. }
  654. unset($item);
  655. RedisService::set($cacheKey . '_page', $page, rand(300, 600));
  656. } else {
  657. RedisService::set($cacheKey . '_page', 0, rand(300, 600));
  658. }
  659. return ['count' => count($goods), 'updated' => $updated, 'errorCount' => $error, 'page' => $page];
  660. }
  661. /**
  662. * 更新商品
  663. * @param int $pageSize
  664. * @param $params 参数
  665. * @return array|false
  666. */
  667. public function updateGoods($pageSize = 0, $params = [])
  668. {
  669. set_time_limit(0);
  670. $cateId = isset($params['cate_id']) ? $params['cate_id'] : 0;
  671. $cacheKey = "caches:supply:goods_list_update_{$pageSize}_{$cateId}";
  672. if (RedisService::get($cacheKey)) {
  673. $this->error = 1047;
  674. return false;
  675. }
  676. $size = ConfigService::make()->getConfigByCode('goods_update_limit', 0);
  677. $pageSize = $pageSize? $pageSize : ($size > 10 && $size <= 5000 ? $size : 500);
  678. $page = RedisService::get($cacheKey . '_page');
  679. $page = $page ? $page + 1 : 1;
  680. // $lastTime = $this->model->where(['mark' => 1])->orderBy('create_time', 'desc')->value('create_time');
  681. // $lastTime = $lastTime? date('Y-m-d H:i:s', strtotime($lastTime)) : '';
  682. // $lastTime = date('Y-m-d H:i:s', time() - 60 * 86400);
  683. $lastTime = '';
  684. $params = [
  685. 'limit' => $pageSize > 0 ? $pageSize : 50,
  686. 'page' => $page,
  687. 'status' => isset($params['status']) ? $params['status'] : 1, // 状态:0-全部,1-上架的,2-下架的
  688. 'supply_type' => isset($params['supply_type']) ? $params['supply_type'] : 0, // 渠道商
  689. 'title' => isset($params['title']) ? $params['title'] : '', // 标题关键词
  690. 'cate_id' => isset($params['cate_id']) ? $params['cate_id'] : '', // 分类ID
  691. 'begin_time' => $lastTime ? $lastTime : '', // 开始时间
  692. ];
  693. $goods = [];
  694. $skus = [];
  695. $updated = 0;
  696. $error = 0;
  697. $infoError = 0;
  698. $datas = SupplyService::make()->getApiData('getGoodsList', $params);
  699. if ($datas && $datas['list']) {
  700. foreach ($datas['list'] as &$item) {
  701. $goodsId = isset($item['goods_id']) ? $item['goods_id'] : 0;
  702. if ($goodsId && !$this->checkGoods($goodsId)) {
  703. $info = $this->getApiInfo($goodsId);
  704. if ($info) {
  705. $skuList = isset($info['sku_list']) ? $info['sku_list'] : [];
  706. $goods[] = [
  707. 'goods_id' => $goodsId,
  708. 'supply_type' => isset($item['supply_type']) ? $item['supply_type'] : 0,
  709. 'spu_sn' => isset($item['spu_sn']) ? $item['spu_sn'] : '',
  710. 'spu_name' => isset($info['spu_name']) ? $info['spu_name'] : '',
  711. 'main_img' => isset($info['main_img']) ? $info['main_img'] : '',
  712. 'detail_img' => isset($info['detail_img']) ? json_encode($info['detail_img'], 256) : '',
  713. 'goods_name' => isset($item['goods_name']) ? $item['goods_name'] : '',
  714. 'brand_name' => isset($info['brand_name']) ? $info['brand_name'] : '',
  715. 'limit_num' => isset($info['limit_num']) ? intval($info['limit_num']) : 0,
  716. 'lowest_num' => isset($info['lowest_num']) ? intval($info['lowest_num']) : 1,
  717. 'cost_price' => isset($info['cost_price']) ? floatval($info['cost_price']) : 0,
  718. 'retail_price' => isset($info['retail_price']) ? floatval($info['retail_price']) : 0,
  719. 'profit' => isset($info['profit']) ? floatval($info['profit']) : 0,
  720. 'sku_list' => $skuList ? json_encode($skuList, 256) : '',
  721. 'sku_total' => isset($info['sku_total']) ? intval($info['sku_total']) : 0,
  722. 'tag' => isset($item['tag']) ? json_encode($item['tag'], 256) : '',
  723. 'status' => isset($info['status']) ? intval($info['status']) : 1,
  724. 'cate_id' => isset($item['cate_id']) ? intval($item['cate_id']) : 0,
  725. 'last_update_at' => isset($info['update_time']) ? $info['update_time'] : (isset($item['time']) && $item['time'] ? $item['time'] : date('Y-m-d H:i:s')),
  726. 'create_time' => time(),
  727. ];
  728. foreach ($skuList as $v) {
  729. $skus[] = [
  730. 'sku_id' => isset($v['sku_id']) ? $v['sku_id'] : 0,
  731. 'goods_id' => $goodsId,
  732. 'spu_sn' => isset($v['spu_sn']) ? $v['spu_sn'] : '',
  733. 'sku_sn' => isset($v['sku_sn']) ? $v['sku_sn'] : '',
  734. 'sku_name' => isset($v['sku_name']) ? $v['sku_name'] : '',
  735. 'main_img' => isset($v['main_img']) ? $v['main_img'] : '',
  736. 'status' => isset($v['status']) ? $v['status'] : 1,
  737. 'source_type' => isset($v['source_type']) ? $v['source_type'] : 0,
  738. 'retail_price' => isset($v['retail_price']) ? floatval($v['retail_price']) : 0,
  739. 'plat_price' => isset($v['plat_price']) ? floatval($v['plat_price']) : 0,
  740. 'profit' => isset($v['profit']) ? floatval($v['profit']) : 0,
  741. 'last_update_at' => isset($v['update_time']) ? $v['update_time'] : date('Y-m-d H:i:s'),
  742. 'detail_img' => isset($v['detail_img']) ? json_encode($v['detail_img'], 256) : '',
  743. 'attr' => isset($v['attr']) ? json_encode($v['attr'], 256) : '',
  744. 'remark' => 'SKU同步创建',
  745. ];
  746. }
  747. $updated++;
  748. } else {
  749. $infoError++;
  750. }
  751. } else {
  752. $error++;
  753. }
  754. }
  755. unset($item);
  756. } else {
  757. RedisService::set($cacheKey . '_page', 0, rand(300, 600));
  758. }
  759. if ($goods) {
  760. RedisService::set($cacheKey . '_page', $page, rand(300, 600));
  761. RedisService::set($cacheKey, $goods, rand(5, 10));
  762. DB::beginTransaction();
  763. try {
  764. $this->model->insertAll($goods);
  765. if ($skus) {
  766. GoodsSkuModel::insert($skus);
  767. }
  768. DB::commit();
  769. } catch (\Exception $exception) {
  770. RedisService::clear($cacheKey);
  771. DB::rollBack();
  772. }
  773. }
  774. return ['total'=>isset($datas['list'])? count($datas['list']) : 0,'all'=>isset($datas['total'])?$datas['total']:0,'count' => count($goods), 'updated' => $updated, 'errorCount' => $error,'info_error'=>$infoError, 'page' => $page];
  775. }
  776. /**
  777. * 更新商品分类
  778. * @param int $pid 上级ID
  779. * @param int $pageSize
  780. * @param $params 参数
  781. * @return array|false
  782. */
  783. public function updateGoodsCategory($pid = 0, $pageSize = 200, $params = [])
  784. {
  785. set_time_limit(0);
  786. $cacheKey = "caches:supply:goods_category_update_{$pid}_{$pageSize}";
  787. if (RedisService::get($cacheKey)) {
  788. $this->error = 1047;
  789. return false;
  790. }
  791. $params = [
  792. 'limit' => $pageSize > 0 ? $pageSize : 50,
  793. 'page' => 1,
  794. 'pid' => $pid, // 上级ID
  795. ];
  796. $categorys = [];
  797. $updated = 0;
  798. $error = 0;
  799. $datas = SupplyService::make()->getApiData('getGoodsCategory', $params);
  800. if ($datas && $datas['data']) {
  801. foreach ($datas['data'] as &$item) {
  802. $cateId = isset($item['id']) ? $item['id'] : 0;
  803. if ($cateId && !$this->checkCategory($cateId)) {
  804. $categorys[] = [
  805. 'cate_id' => $cateId,
  806. 'name' => isset($item['name']) ? $item['name'] : '',
  807. 'pid' => isset($item['pid']) ? intval($item['pid']) : 0,
  808. 'create_time' => time(),
  809. ];
  810. $updated++;
  811. } else {
  812. $error++;
  813. }
  814. }
  815. unset($item);
  816. }
  817. if ($categorys) {
  818. RedisService::set($cacheKey, $categorys, rand(5, 10));
  819. GoodsCategoryModel::insert($categorys);
  820. }
  821. return ['count' => count($categorys), 'updated' => $updated, 'pid' => $pid, 'errorCount' => $error];
  822. }
  823. /**
  824. * 验证
  825. * @param $goodsId
  826. * @return bool
  827. */
  828. public function checkGoods($goodsId)
  829. {
  830. $cacheKey = "caches:goods:check_{$goodsId}";
  831. if (RedisService::get($cacheKey) || RedisService::exists($cacheKey)) {
  832. return true;
  833. }
  834. $data = $this->model->where(['goods_id' => $goodsId, 'mark' => 1])->value('id');
  835. RedisService::set($cacheKey, $data, rand(30, 60));
  836. return $data;
  837. }
  838. /**
  839. * 验证分类
  840. * @param $cateId
  841. * @return bool
  842. */
  843. public function checkCategory($cateId)
  844. {
  845. $cacheKey = "caches:goods:category_check_{$cateId}";
  846. if (RedisService::get($cacheKey) || RedisService::exists($cacheKey)) {
  847. return true;
  848. }
  849. $data = GoodsCategoryModel::where(['cate_id' => $cateId, 'mark' => 1])->value('id');
  850. RedisService::set($cacheKey, $data, rand(30, 60));
  851. return $data;
  852. }
  853. /**
  854. * 接口商品详情
  855. * @param $goodsId 商品ID
  856. * @param int $isReal 是否实时数据,0-是,1-否
  857. * @param int $type 数据类型:0-详情,1-仅SKU数据
  858. * @return array|false|mixed|string
  859. */
  860. public function getApiInfo($goodsId, $isReal = 0, $type = 0)
  861. {
  862. $cacheKey = "caches:goods:detail_{$goodsId}_{$isReal}_{$type}";
  863. $info = RedisService::get($cacheKey);
  864. if (empty($info)) {
  865. $params = [
  866. 'goods_id' => $goodsId,
  867. 'is_real' => $isReal,
  868. 'type' => $type
  869. ];
  870. $info = SupplyService::make()->getApiData('getGoodsDetail', $params);
  871. if ($info) {
  872. RedisService::set($cacheKey, $info, rand(5, 10));
  873. }
  874. }
  875. return $info;
  876. }
  877. /**
  878. * 购物车商品数量
  879. * @param $userId
  880. * @param $params
  881. * @return bool
  882. */
  883. public function count($userId, $params)
  884. {
  885. $count = CartsModel::where(['user_id' => $userId,'status'=>1,'mark'=>1])->count();
  886. return ['count' => $count];
  887. }
  888. }