| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\GoodsCategoryModel;
- use App\Models\GoodsModel;
- use App\Models\GoodsSkuModel;
- use App\Models\OrderModel;
- use App\Services\BaseService;
- use App\Services\ConfigService;
- use App\Services\RedisService;
- use App\Services\SupplyService;
- use Illuminate\Support\Facades\DB;
- /**
- * 商品管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * Class GoodsService
- * @package App\Services\Api
- */
- class GoodsService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- * GoodsService constructor.
- */
- public function __construct()
- {
- $this->model = new GoodsModel();
- }
- /**
- * 静态入口
- * @return static|null
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = (new static());
- }
- return self::$instance;
- }
- /**
- * 商品列表
- * @param $params
- * @param int $pageSize
- * @param int $userId
- * @return array
- */
- public function getDataList($params, $pageSize = 12, $userId = 0)
- {
- $model = $this->model->with(['skuList'])->from('goods as a')
- ->where(['a.status' => 1, 'a.mark' => 1])
- ->where('a.retail_price', '>', 0)
- ->where(function ($query) use ($params) {
- $supplyType = isset($params['supply_type']) ? intval($params['supply_type']) : 0;
- if ($supplyType > 0) {
- $query->where('a.supply_type', $supplyType);
- }
- $cateId = isset($params['cate_id']) ? intval($params['cate_id']) : 0;
- if ($cateId > 0) {
- $subIds = GoodsCategoryModel::where(['pid' => $cateId, 'mark' => 1, 'status' => 1])->pluck('cate_id');
- $query->where(function ($query) use ($cateId, $subIds) {
- if ($subIds) {
- $query->whereIn('a.cate_id', $subIds)
- ->orWhere('a.cate_id', $cateId);
- } else {
- $query->where('a.cate_id', $cateId);
- }
- });
- }
- })
- ->where(function ($query) use ($params) {
- $keyword = isset($params['kw']) ? $params['kw'] : '';
- if ($keyword) {
- $query->where('a.goods_name', 'like', "%{$keyword}%")
- ->orWhere('a.spu_name', 'like', "%{$keyword}%")
- ->orWhere('a.tag', 'like', "%{$keyword}%");
- }
- })
- ->select(['a.*']);
- // 排序
- $sortType = isset($params['sort_type']) ? $params['sort_type'] : 1;
- if ($sortType == 2) {
- $model = $model->orderBy('a.is_recommend', 'asc')->orderBy('a.sales', 'desc');
- }
- $list = $model->orderBy('a.create_time', 'desc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- if ($list) {
- $locale = RedisService::get("caches:locale:lang_{$userId}");
- $locale = $locale ? $locale : session('locale_lang');
- $locale = $locale ? $locale : 'zh-cn';
- $supplyList = config('goods.supplyList');
- $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
- if ($usdtPrice <= 0) {
- $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
- $usdtPrice = $usdtCnyPrice > 0 && $usdtCnyPrice < 100 ? $usdtCnyPrice : 0;
- }
- $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
- $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
- $awardWaitRate = ConfigService::make()->getConfigByCode('shop_award_score_rate', 0);
- $awardWaitRate = $awardWaitRate > 0 && $awardWaitRate < 1000 ? $awardWaitRate : 0;
- $waitRate = ConfigService::make()->getConfigByCode('day_wait_score_rate', 0);
- $waitRate = $waitRate > 0 && $waitRate < 100 ? $waitRate : 0;
- foreach ($list['data'] as &$item) {
- $item['detail_img'] = isset($item['detail_img']) && $item['detail_img'] ? json_decode($item['detail_img'], true) : [];
- $item['supply_name'] = isset($supplyList[$item['supply_type']]) ? $supplyList[$item['supply_type']] : '';
- $item['usdt_price'] = $usdtPrice;
- $item['xd_price_rate'] = $xdPrice;
- $item['retail_price1'] = $item['retail_price'];
- $item['retail_price'] = $this->getRealSalePrice($item['cost_price']);
- $item['wait_score'] = moneyFormat($item['retail_price'] * $awardWaitRate / 100 * $waitRate / 100, 2);
- }
- unset($item);
- }
- return [
- 'total' => isset($list['total']) ? $list['total'] : 9,
- 'pageSize' => $pageSize,
- 'list' => isset($list['data']) ? $list['data'] : [],
- ];
- }
- /**
- * 详情
- * @param $id
- * @return array
- */
- public function getInfo($goodsId, $userId = 0, $updateView = true)
- {
- $field = ['a.*'];
- $info = $this->model->from('goods as a')->with(['category', 'skuList'])
- ->where(['a.goods_id' => $goodsId, 'a.status' => 1, 'a.mark' => 1])
- ->select($field)
- ->first();
- $info = $info ? $info->toArray() : [];
- if ($info) {
- if (isset($info['main_img'])) {
- $info['main_img'] = $info['main_img'] ? get_image_url($info['main_img']) : '';
- }
- if (isset($info['detail_img'])) {
- $info['detail_img'] = $info['detail_img'] ? json_decode($info['detail_img'], true) : [];
- $thumbs = [$info['main_img']];
- $info['detail_img'] = $info['detail_img']? array_merge($thumbs,$info['detail_img']) : $thumbs;
- }
- if (empty($info['detail_img'])) {
- $info['detail_img'] = [$info['main_img']];
- }
- $supplyList = config('goods.supplyList');
- $info['supply_name'] = isset($supplyList[$info['supply_type']]) ? $supplyList[$info['supply_type']] : '';
- $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
- if ($usdtPrice <= 0) {
- $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
- $usdtPrice = $usdtCnyPrice > 0 && $usdtCnyPrice < 100 ? $usdtCnyPrice : 0;
- }
- $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
- $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
- $info['usdt_price_rate'] = $usdtPrice;
- $info['xd_price'] = $xdPrice;
- $info['custom_uid'] = ConfigService::make()->getConfigByCode('xl_custom_id', 100001);
- $awardWaitRate = ConfigService::make()->getConfigByCode('shop_award_score_rate', 0);
- $awardWaitRate = $awardWaitRate > 0 && $awardWaitRate < 1000 ? $awardWaitRate : 0;
- $waitRate = ConfigService::make()->getConfigByCode('day_wait_score_rate', 0);
- $waitRate = $waitRate > 0 && $waitRate < 100 ? $waitRate : 0;
- if (isset($info['retail_price']) && $info['retail_price']) {
- $info['retail_price1'] = $info['retail_price'];
- $info['retail_price'] = $this->getRealSalePrice($info['cost_price']);
- $info['wait_score'] = moneyFormat($info['retail_price'] * $awardWaitRate / 100 * $waitRate / 100, 2);
- }
- if (isset($info['sku_list']) && $info['sku_list']) {
- foreach ($info['sku_list'] as &$v) {
- $v['detail_img'] = $v['detail_img'] ? json_decode($v['detail_img'], true) : [];
- $v['attr'] = $v['attr'] ? json_decode($v['attr'], true) : [];
- $v['main_img'] = $v['main_img'] ? get_image_url($v['main_img']) : '';
- $v['retail_price_1'] = $v['retail_price'];
- $v['retail_price'] = $this->getRealSalePrice($v['plat_price']);
- $v['wait_score'] = moneyFormat($v['retail_price'] * $awardWaitRate / 100 * $waitRate / 100, 2);
- }
- unset($v);
- }
- // 更新访问量
- if ($updateView) {
- $this->updateView($userId, $goodsId);
- }
- }
- return $info;
- }
- /**
- * 实际售价
- * @param $price 成本价或其他价格
- * @param bool $float 是否浮动转换,1-浮动转换,2-不浮动转换,3-浮动不转换
- * @return string
- */
- public function getRealSalePrice($price, $float = 1)
- {
- $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
- if ($usdtPrice <= 0) {
- $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
- $usdtPrice = $usdtCnyPrice > 0 && $usdtCnyPrice < 100 ? $usdtCnyPrice : 0;
- }
- $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
- $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
- $floatRate = ConfigService::make()->getConfigByCode('goods_price_float_rate', 0);
- $floatRate = $floatRate > 0 && $floatRate < 100 ? $floatRate : 0;
- $price = $float == 2 ? $price : floatval($price * (1 + ($floatRate / 100)));
- if ($float == 3) {
- return $price;
- }
- return $usdtPrice > 0 ? moneyFormat($price / $usdtPrice * $xdPrice, 2) : $price;
- }
- /**
- * 获取要购买或结算的商品列表
- * @param $userId
- * @param array $params
- * @return array|false|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
- */
- public function getBuyList($userId, $params = [])
- {
- $goodsId = isset($params['goods_id']) ? $params['goods_id'] : 0;
- $skuId = isset($params['sku_id']) ? $params['sku_id'] : 0;
- $num = isset($params['num']) ? $params['num'] : 0;
- $cartIds = isset($params['cart_ids']) ? $params['cart_ids'] : '';
- $cartIds = $cartIds ? explode('|', $cartIds) : [];
- if (empty($goodsId) && empty($cartIds)) {
- $this->error = 2901;
- return false;
- }
- if ($goodsId && (empty($skuId) || $num <= 0)) {
- $this->error = 2901;
- return false;
- }
- $cacheKey = "caches:goods:buyList:{$userId}_" . md5(json_encode($params, 256));
- $datas = RedisService::get($cacheKey);
- if ($datas) {
- return $datas;
- }
- $goods = [];
- $skuList = [];
- $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
- if ($usdtPrice <= 0) {
- $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
- $usdtPrice = $usdtCnyPrice > 0 && $usdtCnyPrice < 100 ? $usdtCnyPrice : 0;
- }
- $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
- $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
- if ($goodsId) {
- $info = $this->getInfo($goodsId, $userId, false);
- if ($info) {
- $info['num'] = $num;
- $info['sku_id'] = $skuId;
- $skuInfo = GoodsSkuModel::where(['goods_id' => $goodsId, 'sku_id' => $skuId, 'mark' => 1])->first();
- $price = isset($skuInfo['plat_price']) ? $skuInfo['plat_price'] : 0;
- if ($price) {
- $info['retail_price1'] = $info['retail_price'];
- $info['retail_price'] = $this->getRealSalePrice($price);
- }
- if (isset($skuInfo['attr']) && $skuInfo['attr']) {
- $skuInfo['attr'] = json_decode($skuInfo['attr'], true);
- }
- $skuList[$goodsId] = [
- 'sku_id' => $skuId,
- 'num' => $num
- ];
- $info['sku'] = $skuInfo;
- $goods[] = $info;
- }
- } else {
- $goods = CartsModel::with(['sku'])->from('carts as a')
- ->leftJoin('goods as b', 'b.goods_id', '=', 'a.goods_id')
- ->leftJoin('goods_sku as c', 'c.sku_id', '=', 'a.sku_id')
- ->whereIn('a.id', $cartIds)
- ->where(['a.status' => 1, 'a.mark' => 1, 'b.status' => 1, 'b.mark' => 1])
- ->where('b.retail_price', '>', 0)
- ->where('a.num', '>', 0)
- ->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'])
- ->get();
- if ($goods) {
- // 价格等参数格式化
- $locale = RedisService::get("caches:locale:lang_{$userId}");
- $locale = $locale ? $locale : session('locale_lang');
- $locale = $locale ? $locale : 'zh-cn';
- $supplyList = config('goods.supplyList');
- foreach ($goods as &$item) {
- $item['detail_img'] = isset($item['detail_img']) && $item['detail_img'] ? json_decode($item['detail_img'], true) : [];
- $item['supply_name'] = isset($supplyList[$item['supply_type']]) ? $supplyList[$item['supply_type']] : '';
- $item['usdt_price'] = $usdtPrice;
- $item['xd_price_rate'] = $xdPrice;
- $item['retail_price1'] = $item['retail_price'];
- $skuInfo = isset($item['sku']) ? $item['sku'] : [];
- if (isset($skuInfo['attr']) && $skuInfo['attr']) {
- $skuInfo['attr'] = json_decode($skuInfo['attr'], true);
- }
- $item['sku'] = $skuInfo;
- $price = isset($skuInfo['plat_price']) ? $skuInfo['plat_price'] : 0;
- if ($price) {
- $item['retail_price'] = $this->getRealSalePrice($price);
- } else {
- $item['retail_price'] = $this->getRealSalePrice($item['cost_price']);
- }
- $skuList[$item['goods_id']] = [
- 'sku_id' => $item['sku_id'],
- 'num' => $item['num']
- ];
- }
- unset($item);
- }
- }
- if (empty($goods)) {
- $this->error = 2901;
- return false;
- }
- RedisService::set($cacheKey, ['sku_list' => array_values($skuList), 'goods' => $goods], rand(2, 3));
- return ['sku_list' => array_values($skuList), 'goods' => $goods];
- }
- /**
- * 订单商品
- * @param $userId
- * @param $ids
- * @return array
- */
- public function getOrderGoods($userId, $ids)
- {
- $cacheKey = "caches:goodsOrder:{$userId}_" . md5(json_encode($ids, 256));
- $datas = RedisService::get($cacheKey);
- if ($datas) {
- return $datas;
- }
- $goods = $this->model->from('goods as a')
- ->whereIn('a.goods_id', $ids)
- ->where(['a.status' => 1, 'a.mark' => 1])
- ->where('a.retail_price', '>', 0)
- ->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'])
- ->get();
- $goods = $goods ? $goods->toArray() : [];
- if ($goods) {
- RedisService::set($cacheKey, $goods, rand(5, 10));
- }
- return $goods;
- }
- /**
- * 添加/更新购物车
- * @param $userId
- * @param $goodsId
- * @param $params
- * @return array|false
- */
- public function updateCart($userId, $goodsId, $params)
- {
- $skuId = isset($params['sku_id']) ? $params['sku_id'] : 0;
- $status = isset($params['status']) ? $params['status'] : 0;
- $merchId = isset($params['merch_id']) ? $params['merch_id'] : 0;
- $num = isset($params['num']) ? $params['num'] : 1;
- if ($skuId <= 0 || $goodsId <= 0 || $userId <= 0 || $num <= 0) {
- $this->error = 2014;
- return false;
- }
- if (CartsModel::where(['user_id' => $userId, 'status' => 1, 'mark' => 1])->count('id') > 20) {
- $this->error = 1050;
- return false;
- }
- $cartId = CartsModel::where(['user_id' => $userId, 'goods_id' => $goodsId, 'sku_id' => $skuId])->value('id');
- if ($cartId) {
- CartsModel::where(['id' => $cartId])->update(['num' => $num, 'merch_id' => $merchId, 'status' => $status, 'mark' => 1, 'update_time' => time()]);
- RedisService::clear("caches:members:cartList:{$userId}");
- $count = $this->getCartCount($userId, true);
- return ['id' => $cartId, 'count' => $count];
- } else {
- $cartId = CartsModel::insertGetId(['user_id' => $userId, 'goods_id' => $goodsId, 'merch_id' => $merchId, 'sku_id' => $skuId, 'num' => $num, 'status' => $status, 'mark' => 1, 'create_time' => time()]);
- RedisService::clear("caches:members:cartList:{$userId}");
- $count = $this->getCartCount($userId, true);
- return ['id' => $cartId, 'count' => $count];
- }
- }
- /**
- * 删除购物车商品
- * @param $userId
- * @param $params
- * @return bool
- */
- public function deleteCart($userId, $params)
- {
- $ids = isset($params['ids']) ? $params['ids'] : [];
- if (empty($ids)) {
- $this->error = 2923;
- return false;
- }
- CartsModel::whereIn('id', $ids)->update(['status' => 2, 'update_time' => time()]);
- $this->error = 1002;
- RedisService::clear("caches:members:cartCount:{$userId}");
- RedisService::clear("caches:members:cartList:{$userId}");
- return true;
- }
- /**
- * 购物车列表
- * @param $userId
- * @param int $pageSize
- * @return array|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|mixed
- */
- public function getCartList($userId, $pageSize = 30)
- {
- $cacheKey = "caches:members:cartList:{$userId}";
- $cacheCountKey = "caches:members:cartCount:{$userId}";
- $datas = RedisService::get($cacheKey);
- if ($datas) {
- return $datas;
- }
- $datas = CartsModel::with(['sku'])->from('carts as a')
- ->leftJoin('goods as b', 'b.goods_id', '=', 'a.goods_id')
- ->leftJoin('goods_sku as c', 'c.sku_id', '=', 'a.sku_id')
- ->where(['a.status' => 1, 'a.mark' => 1, 'b.status' => 1, 'b.mark' => 1])
- ->where('b.cost_price', '>', 0)
- ->where('a.num', '>', 0)
- ->where('a.user_id', '=', $userId)
- ->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'])
- ->orderBy('a.create_time', 'desc')
- ->limit($pageSize)
- ->get();
- $datas = $datas ? $datas->toArray() : [];
- if ($datas) {
- // 价格等参数格式化
- $locale = RedisService::get("caches:locale:lang_{$userId}");
- $locale = $locale ? $locale : session('locale_lang');
- $locale = $locale ? $locale : 'zh-cn';
- $supplyList = config('goods.supplyList');
- $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
- if ($usdtPrice <= 0) {
- $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
- $usdtPrice = $usdtCnyPrice > 0 && $usdtCnyPrice < 100 ? $usdtCnyPrice : 0;
- }
- $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
- $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
- foreach ($datas as &$item) {
- $item['detail_img'] = isset($item['detail_img']) && $item['detail_img'] ? json_decode($item['detail_img'], true) : [];
- $item['supply_name'] = isset($supplyList[$item['supply_type']]) ? $supplyList[$item['supply_type']] : '';
- $item['usdt_price'] = $usdtPrice;
- $item['xd_price_rate'] = $xdPrice;
- $item['retail_price1'] = $item['retail_price'];
- $skuInfo = isset($item['sku']) ? $item['sku'] : [];
- $skuInfo['attr'] = isset($skuInfo['attr']) && $skuInfo['attr'] ? json_decode($skuInfo['attr'], true) : [];
- $item['sku'] = $skuInfo;
- $price = isset($skuInfo['plat_price']) ? $skuInfo['plat_price'] : 0;
- if ($price) {
- $item['retail_price'] = $this->getRealSalePrice($price);
- } else {
- $item['retail_price'] = $this->getRealSalePrice($item['cost_price']);
- }
- }
- unset($item);
- RedisService::set($cacheCountKey, count($datas), rand(300, 600));
- RedisService::set($cacheKey, $datas, rand(300, 600));
- }
- return $datas;
- }
- /**
- * 购物车中数量
- * @param $userId
- * @return array|mixed
- */
- public function getCartCount($userId, $refresh = false)
- {
- $cacheKey = "caches:member:cartCount:{$userId}";
- $data = RedisService::get($cacheKey);
- if ($data > 0 && !$refresh) {
- return $data;
- }
- $data = CartsModel::from('carts as a')
- ->leftJoin('goods as b', 'b.goods_id', '=', 'a.goods_id')
- ->where(['a.status' => 1, 'a.mark' => 1, 'b.status' => 1, 'b.mark' => 1])
- ->where('a.num', '>', 0)
- ->where('b.cost_price', '>', 0)
- ->where('a.user_id',$userId)
- ->count('a.id');
- if ($data) {
- RedisService::set($cacheKey, $data, rand(300, 600));
- }
- return $data;
- }
- /**
- * 运费
- */
- public function getFreight($userId, $addressId, $skuList)
- {
- $cacheKey = "caches:goods:freight:{$userId}_{$addressId}_" . md5(json_encode($skuList, 256));
- $data = RedisService::get($cacheKey);
- if ($data) {
- return $data;
- }
- if (empty($addressId)) {
- $address = MemberAddressService::make()->getBindInfo($userId);
- $streetCode = isset($address['street_code']) ? $address['street_code'] : '';
- $addressId = $streetCode ? $streetCode : (isset($address['district_code']) ? $address['district_code'] : '');
- }
- $result = SupplyService::make()->getApiData('getFreight', ['address_id' => intval($addressId), 'sku_list' => $skuList]);
- $freight = isset($result['freight']) ? floatval($result['freight']) : -1;
- if ($freight >= 0) {
- // 价格参数
- $usdtPrice = RedisService::get("caches:wallets:usdt_rate");
- if ($usdtPrice <= 0) {
- $usdtCnyPrice = ConfigService::make()->getConfigByCode('usdt_cny_price', 7.2);
- $usdtPrice = $usdtCnyPrice > 0 && $usdtCnyPrice < 100 ? $usdtCnyPrice : 0;
- }
- $xdPrice = ConfigService::make()->getConfigByCode('xd_price', 100);
- $xdPrice = $xdPrice > 0 && $xdPrice <= 10000 ? $xdPrice : 100;
- $xdFreight = $freight ? moneyFormat($freight / $usdtPrice * $xdPrice, 2) : 0;
- RedisService::set($cacheKey, ['freight' => $xdFreight, 'fee' => $freight], rand(5, 10));
- return ['freight' => $xdFreight, 'fee' => $freight];
- } else {
- $errorCode = SupplyService::make()->getError();
- $this->error = $errorCode ? $errorCode : 1052;
- return false;
- }
- }
- public function getSkuInfo($sakuId)
- {
- $cacheKey = "caches:goodsSku:{$sakuId}";
- $data = RedisService::get($cacheKey);
- if ($data) {
- return $data;
- }
- $data = GoodsSkuModel::where(['sku_id' => $sakuId, 'status' => 1, 'mark' => 1])
- ->select(['id', 'sku_id', 'goods_id', 'retail_price', 'plat_price', 'sku_sn', 'sku_name', 'main_img', 'attr'])
- ->first();
- $data = $data ? $data->toArray() : [];
- if ($data) {
- RedisService::set($cacheKey, $data, rand(3, 5));
- }
- return $data;
- }
- /**
- * 更新浏览量
- * @param $userId
- * @param $dynamicId
- * @return array|mixed
- */
- public function updateView($userId, $id)
- {
- $cacheKey = "caches:goods:views:u{$userId}_d{$id}";
- $data = RedisService::get($cacheKey);
- if ($data) {
- return false;
- }
- $data = $this->model->where(['goods_id' => $id])->update(['views' => DB::raw('views + 1'), 'update_time' => time()]);
- RedisService::set($cacheKey, $id, rand(1, 3) * 7200);
- return $data;
- }
- /**
- * 更新商品SKU数据到本地
- * @param int $pageSize
- * @param $params 参数
- * @return array|false
- */
- public function updateGoodsSku($pageSize = 100, $params = [])
- {
- $cacheKey = "caches:supply:goods_sku_update_{$pageSize}";
- if (RedisService::get($cacheKey)) {
- $this->error = 1047;
- return false;
- }
- $page = RedisService::get($cacheKey . '_page');
- $page = $page ? $page + 1 : 1;
- $lastDate = GoodsSkuModel::where(['mark' => 1])->orderBy('last_update_at', 'desc')->value('last_update_at');
- $params = [
- 'limit' => $pageSize > 0 ? $pageSize : 50,
- 'page' => $page,
- 'date' => $lastDate ? $lastDate : '', // 开始时间
- ];
- $goods = [];
- $updated = 0;
- $error = 0;
- $datas = SupplyService::make()->getApiData('getSkuUpdate', $params);
- if ($datas && $datas['list']) {
- foreach ($datas['list'] as &$item) {
- $goodsId = isset($item['goods_id']) ? $item['goods_id'] : 0;
- $goodsSkuSn = isset($item['sku_sn']) ? $item['sku_sn'] : '';
- $changeType = isset($item['change_type']) ? $item['change_type'] : '';
- if ($goodsId && $goodsSkuSn && $changeType) {
- $skuInfo = SupplyService::make()->getApiData('getSkuDetail', ['sku_sn' => $goodsSkuSn]);
- if ($skuInfo) {
- $updateData = ['goods_id' => $goodsId, 'sku_sn' => $goodsSkuSn, 'remark' => 'SKU更新', 'update_time' => time(), 'mark' => 1, 'last_update_at' => $item['update_time']];
- if (isset($skuInfo['sku_name']) && $skuInfo['sku_name']) {
- $updateData['sku_name'] = $skuInfo['sku_name'];
- }
- if (isset($skuInfo['main_img']) && $skuInfo['main_img']) {
- $updateData['main_img'] = $skuInfo['main_img'];
- }
- if (isset($skuInfo['spu_sn']) && $skuInfo['spu_sn']) {
- $updateData['spu_sn'] = $skuInfo['spu_sn'];
- }
- if (isset($skuInfo['sku_id']) && $skuInfo['sku_id']) {
- $updateData['sku_id'] = intval($skuInfo['sku_id']);
- }
- if (isset($skuInfo['status']) && $skuInfo['status']) {
- $updateData['status'] = intval($skuInfo['status']);
- }
- if (isset($skuInfo['retail_price']) && $skuInfo['retail_price']) {
- $updateData['retail_price'] = floatval($skuInfo['retail_price']);
- }
- if (isset($skuInfo['plat_price']) && $skuInfo['plat_price']) {
- $updateData['plat_price'] = floatval($skuInfo['plat_price']);
- }
- if (isset($skuInfo['detail_img']) && $skuInfo['detail_img']) {
- $updateData['detail_img'] = json_encode($skuInfo['detail_img'], 256);
- }
- if (isset($skuInfo['attr']) && $skuInfo['attr']) {
- $updateData['attr'] = json_encode($skuInfo['attr'], 256);
- }
- if(isset($skuInfo['status'])){
- $updateGoodsData = ['update_time'=>time()];
- // 下架
- if($skuInfo['status'] == 0){
- $updateGoodsData['status'] = 0;
- }
- // 删除
- else if(in_array(intval($skuInfo['status']),[2,3])){
- $updateGoodsData['status'] = 3;
- $updateGoodsData['mark'] = 0;
- }else if($skuInfo['status'] == 1){
- $updateGoodsData['status'] = 1;
- }
- // 更新商品状态
- GoodsModel::where(['goods_id' => $goodsId])->update($updateGoodsData);
- }
- // 更新商品SKU信息
- if (GoodsSkuModel::where(['goods_id' => $goodsId])->value('id')) {
- GoodsSkuModel::where(['goods_id' => $goodsId, 'mark' => 1])->update($updateData);
- $updated++;
- } else {
- $error++;
- }
- } else {
- $error++;
- }
- } else {
- $error++;
- }
- }
- unset($item);
- RedisService::set($cacheKey . '_page', $page, rand(300, 600));
- } else {
- RedisService::set($cacheKey . '_page', 0, rand(300, 600));
- }
- return ['count' => count($goods), 'updated' => $updated, 'errorCount' => $error, 'page' => $page];
- }
- /**
- * 更新商品
- * @param int $pageSize
- * @param $params 参数
- * @return array|false
- */
- public function updateGoods($pageSize = 0, $params = [])
- {
- set_time_limit(0);
- $cateId = isset($params['cate_id']) ? $params['cate_id'] : 0;
- $cacheKey = "caches:supply:goods_list_update_{$pageSize}_{$cateId}";
- if (RedisService::get($cacheKey)) {
- $this->error = 1047;
- return false;
- }
- $size = ConfigService::make()->getConfigByCode('goods_update_limit', 0);
- $pageSize = $pageSize? $pageSize : ($size > 10 && $size <= 5000 ? $size : 500);
- $page = RedisService::get($cacheKey . '_page');
- $page = $page ? $page + 1 : 1;
- // $lastTime = $this->model->where(['mark' => 1])->orderBy('create_time', 'desc')->value('create_time');
- // $lastTime = $lastTime? date('Y-m-d H:i:s', strtotime($lastTime)) : '';
- // $lastTime = date('Y-m-d H:i:s', time() - 60 * 86400);
- $lastTime = '';
- $params = [
- 'limit' => $pageSize > 0 ? $pageSize : 50,
- 'page' => $page,
- 'status' => isset($params['status']) ? $params['status'] : 1, // 状态:0-全部,1-上架的,2-下架的
- 'supply_type' => isset($params['supply_type']) ? $params['supply_type'] : 0, // 渠道商
- 'title' => isset($params['title']) ? $params['title'] : '', // 标题关键词
- 'cate_id' => isset($params['cate_id']) ? $params['cate_id'] : '', // 分类ID
- 'begin_time' => $lastTime ? $lastTime : '', // 开始时间
- ];
- $goods = [];
- $skus = [];
- $updated = 0;
- $error = 0;
- $infoError = 0;
- $datas = SupplyService::make()->getApiData('getGoodsList', $params);
- if ($datas && $datas['list']) {
- foreach ($datas['list'] as &$item) {
- $goodsId = isset($item['goods_id']) ? $item['goods_id'] : 0;
- if ($goodsId && !$this->checkGoods($goodsId)) {
- $info = $this->getApiInfo($goodsId);
- if ($info) {
- $skuList = isset($info['sku_list']) ? $info['sku_list'] : [];
- $goods[] = [
- 'goods_id' => $goodsId,
- 'supply_type' => isset($item['supply_type']) ? $item['supply_type'] : 0,
- 'spu_sn' => isset($item['spu_sn']) ? $item['spu_sn'] : '',
- 'spu_name' => isset($info['spu_name']) ? $info['spu_name'] : '',
- 'main_img' => isset($info['main_img']) ? $info['main_img'] : '',
- 'detail_img' => isset($info['detail_img']) ? json_encode($info['detail_img'], 256) : '',
- 'goods_name' => isset($item['goods_name']) ? $item['goods_name'] : '',
- 'brand_name' => isset($info['brand_name']) ? $info['brand_name'] : '',
- 'limit_num' => isset($info['limit_num']) ? intval($info['limit_num']) : 0,
- 'lowest_num' => isset($info['lowest_num']) ? intval($info['lowest_num']) : 1,
- 'cost_price' => isset($info['cost_price']) ? floatval($info['cost_price']) : 0,
- 'retail_price' => isset($info['retail_price']) ? floatval($info['retail_price']) : 0,
- 'profit' => isset($info['profit']) ? floatval($info['profit']) : 0,
- 'sku_list' => $skuList ? json_encode($skuList, 256) : '',
- 'sku_total' => isset($info['sku_total']) ? intval($info['sku_total']) : 0,
- 'tag' => isset($item['tag']) ? json_encode($item['tag'], 256) : '',
- 'status' => isset($info['status']) ? intval($info['status']) : 1,
- 'cate_id' => isset($item['cate_id']) ? intval($item['cate_id']) : 0,
- 'last_update_at' => isset($info['update_time']) ? $info['update_time'] : (isset($item['time']) && $item['time'] ? $item['time'] : date('Y-m-d H:i:s')),
- 'create_time' => time(),
- ];
- foreach ($skuList as $v) {
- $skus[] = [
- 'sku_id' => isset($v['sku_id']) ? $v['sku_id'] : 0,
- 'goods_id' => $goodsId,
- 'spu_sn' => isset($v['spu_sn']) ? $v['spu_sn'] : '',
- 'sku_sn' => isset($v['sku_sn']) ? $v['sku_sn'] : '',
- 'sku_name' => isset($v['sku_name']) ? $v['sku_name'] : '',
- 'main_img' => isset($v['main_img']) ? $v['main_img'] : '',
- 'status' => isset($v['status']) ? $v['status'] : 1,
- 'source_type' => isset($v['source_type']) ? $v['source_type'] : 0,
- 'retail_price' => isset($v['retail_price']) ? floatval($v['retail_price']) : 0,
- 'plat_price' => isset($v['plat_price']) ? floatval($v['plat_price']) : 0,
- 'profit' => isset($v['profit']) ? floatval($v['profit']) : 0,
- 'last_update_at' => isset($v['update_time']) ? $v['update_time'] : date('Y-m-d H:i:s'),
- 'detail_img' => isset($v['detail_img']) ? json_encode($v['detail_img'], 256) : '',
- 'attr' => isset($v['attr']) ? json_encode($v['attr'], 256) : '',
- 'remark' => 'SKU同步创建',
- ];
- }
- $updated++;
- } else {
- $infoError++;
- }
- } else {
- $error++;
- }
- }
- unset($item);
- } else {
- RedisService::set($cacheKey . '_page', 0, rand(300, 600));
- }
- if ($goods) {
- RedisService::set($cacheKey . '_page', $page, rand(300, 600));
- RedisService::set($cacheKey, $goods, rand(5, 10));
- DB::beginTransaction();
- try {
- $this->model->insertAll($goods);
- if ($skus) {
- GoodsSkuModel::insert($skus);
- }
- DB::commit();
- } catch (\Exception $exception) {
- RedisService::clear($cacheKey);
- DB::rollBack();
- }
- }
- 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];
- }
- /**
- * 更新商品分类
- * @param int $pid 上级ID
- * @param int $pageSize
- * @param $params 参数
- * @return array|false
- */
- public function updateGoodsCategory($pid = 0, $pageSize = 200, $params = [])
- {
- set_time_limit(0);
- $cacheKey = "caches:supply:goods_category_update_{$pid}_{$pageSize}";
- if (RedisService::get($cacheKey)) {
- $this->error = 1047;
- return false;
- }
- $params = [
- 'limit' => $pageSize > 0 ? $pageSize : 50,
- 'page' => 1,
- 'pid' => $pid, // 上级ID
- ];
- $categorys = [];
- $updated = 0;
- $error = 0;
- $datas = SupplyService::make()->getApiData('getGoodsCategory', $params);
- if ($datas && $datas['data']) {
- foreach ($datas['data'] as &$item) {
- $cateId = isset($item['id']) ? $item['id'] : 0;
- if ($cateId && !$this->checkCategory($cateId)) {
- $categorys[] = [
- 'cate_id' => $cateId,
- 'name' => isset($item['name']) ? $item['name'] : '',
- 'pid' => isset($item['pid']) ? intval($item['pid']) : 0,
- 'create_time' => time(),
- ];
- $updated++;
- } else {
- $error++;
- }
- }
- unset($item);
- }
- if ($categorys) {
- RedisService::set($cacheKey, $categorys, rand(5, 10));
- GoodsCategoryModel::insert($categorys);
- }
- return ['count' => count($categorys), 'updated' => $updated, 'pid' => $pid, 'errorCount' => $error];
- }
- /**
- * 验证
- * @param $goodsId
- * @return bool
- */
- public function checkGoods($goodsId)
- {
- $cacheKey = "caches:goods:check_{$goodsId}";
- if (RedisService::get($cacheKey) || RedisService::exists($cacheKey)) {
- return true;
- }
- $data = $this->model->where(['goods_id' => $goodsId, 'mark' => 1])->value('id');
- RedisService::set($cacheKey, $data, rand(30, 60));
- return $data;
- }
- /**
- * 验证分类
- * @param $cateId
- * @return bool
- */
- public function checkCategory($cateId)
- {
- $cacheKey = "caches:goods:category_check_{$cateId}";
- if (RedisService::get($cacheKey) || RedisService::exists($cacheKey)) {
- return true;
- }
- $data = GoodsCategoryModel::where(['cate_id' => $cateId, 'mark' => 1])->value('id');
- RedisService::set($cacheKey, $data, rand(30, 60));
- return $data;
- }
- /**
- * 接口商品详情
- * @param $goodsId 商品ID
- * @param int $isReal 是否实时数据,0-是,1-否
- * @param int $type 数据类型:0-详情,1-仅SKU数据
- * @return array|false|mixed|string
- */
- public function getApiInfo($goodsId, $isReal = 0, $type = 0)
- {
- $cacheKey = "caches:goods:detail_{$goodsId}_{$isReal}_{$type}";
- $info = RedisService::get($cacheKey);
- if (empty($info)) {
- $params = [
- 'goods_id' => $goodsId,
- 'is_real' => $isReal,
- 'type' => $type
- ];
- $info = SupplyService::make()->getApiData('getGoodsDetail', $params);
- if ($info) {
- RedisService::set($cacheKey, $info, rand(5, 10));
- }
- }
- return $info;
- }
- /**
- * 购物车商品数量
- * @param $userId
- * @param $params
- * @return bool
- */
- public function count($userId, $params)
- {
- $count = CartsModel::where(['user_id' => $userId,'status'=>1,'mark'=>1])->count();
- return ['count' => $count];
- }
- }
|