| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270 |
- <?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\GoodsCollectModel;
- use App\Models\GoodsModel;
- use App\Models\GoodsSkuModel;
- use App\Services\BaseService;
- use App\Services\RedisService;
- /**
- * 商品管理-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Api
- */
- class GoodsService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- */
- public function __construct()
- {
- $this->model = new GoodsModel();
- }
- /**
- * 静态入口
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 列表数据
- * @param $params
- * @param int $pageSize
- * @return array
- */
- public function getDataList($params, $pageSize = 15)
- {
- $cacheKey = "caches:goods:list_{$pageSize}_" . ($params ? md5(json_encode($params)) : 0);
- $datas = RedisService::get($cacheKey);
- if (empty($datas)) {
- $query = $this->getQuery($params)
- ->orderBy('a.create_time', 'desc')
- ->orderBy('a.id', 'desc');
- $field = ["a.*"];
- $list = $query->select($field)
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list ? $list->toArray() : [];
- if ($list) {
- $datas = [
- 'pageSize' => $pageSize,
- 'total' => isset($list['total']) ? $list['total'] : 0,
- 'list' => isset($list['data']) ? $list['data'] : []
- ];
- RedisService::set($cacheKey, $datas, rand(3, 5));
- }
- }
- return $datas;
- }
- /**
- * 查询条件
- * @param $params
- * @return mixed
- */
- public function getQuery($params)
- {
- $where = ['a.status' => 1, 'a.mark' => 1];
- $status = isset($params['status']) ? $params['status'] : 1;
- if ($status > 0) {
- $where['a.status'] = $status;
- } else {
- unset($where['a.status']);
- }
- $model = $this->model->with(['store','category'])
- ->from('goods as a')
- ->where($where)
- ->where(function ($query) use ($params) {
- // 分类
- $categoryId = isset($params['category_id'])? intval($params['category_id']) : 0;
- if($categoryId>0){
- $query->where('a.category_id', $categoryId);
- }
- // 店铺
- $storeId = isset($params['store_id'])? intval($params['store_id']) : 0;
- if($storeId>0){
- $query->where('a.store_id', $storeId);
- }
- $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
- if ($keyword) {
- $query->where(function ($query) use ($keyword) {
- $query->where('a.goods_name', 'like', "%{$keyword}%")
- ->orWhere('a.tags','like',"%{$keyword}%");
- });
- }
- });
- return $model;
- }
- /**
- * 分类
- * @return array|mixed
- */
- public function getCategoryList()
- {
- $cacheKey = "caches:goods:categoryList";
- $datas = RedisService::get($cacheKey);
- if($datas){
- return $datas;
- }
- $datas = GoodsCategoryModel::where(['pid'=>0,'status'=>1,'mark'=>1])
- ->select(['id','name','icon','pid','sort'])
- ->orderBy('sort','desc')
- ->orderBy('id','desc')
- ->get();
- $datas = $datas? $datas->toArray() : [];
- if($datas){
- RedisService::set($cacheKey, $datas, rand(300,600));
- }
- return $datas;
- }
- /**
- * 详情信息
- * @param $id
- * @return mixed
- */
- public function getInfo($id,$userId=0)
- {
- $cacheKey = "caches:goods:info_{$id}_{$userId}";
- $info = RedisService::get($cacheKey);
- if ($info) {
- return $info;
- }
- $info = $this->model->with(['store','category','skus'])->where(['id' => $id])->first();
- $info = $info ? $info->toArray() : [];
- if ($info) {
- $checkId = GoodsCollectModel::where(['user_id'=>$userId,'goods_id'=>$id])->value('id');
- $info['is_collect'] = $checkId? 1 : 0;
- RedisService::set($cacheKey, $info, rand(10, 20));
- }
- return $info;
- }
- /**
- * 收藏
- * @param $userId
- * @param $goodsId
- * @return array|false
- */
- public function collect($userId, $goodsId)
- {
- $info = $this->model->where(['id' => $goodsId,'status'=>1,'mark'=>1])->first();
- $info = $info ? $info->toArray() : [];
- if(empty($info)){
- $this->error = '商品已下架';
- return false;
- }
- if($id = GoodsCollectModel::where(['user_id'=>$userId,'goods_id'=>$goodsId,'mark'=>1])->value('id')){
- GoodsCollectModel::where(['id'=>$id])->update(['mark'=>0,'update_time'=>time()]);
- $this->error = '取消收藏';
- RedisService::clear("caches:goods:info_{$id}_{$userId}");
- return ['id'=>$id,'is_collect'=>0];
- }else{
- if(!$id = GoodsCollectModel::insertGetId(['user_id'=>$userId,'goods_id'=>$goodsId,'status'=>1,'mark'=>1,'create_time'=>time(),'update_time'=>time()])){
- $this->error = '收藏失败';
- return false;
- }
- $this->error = '收藏成功';
- RedisService::clear("caches:goods:info_{$id}_{$userId}");
- return ['id'=>$id,'is_collect'=>1];
- }
- }
- /**
- * @param $ids
- * @param $goods
- * @param $orderNo
- * @return array|false
- */
- public function getOrderGoods($ids, $goods, $orderNo, $userId=0)
- {
- $list = $this->model->whereIn('id', $ids)
- ->where(['status'=>1,'mark'=>1])
- ->select(['id as goods_id','goods_name','category_id','store_id','price','stock','unit','weight','thumb','sku_type'])
- ->get();
- $list = $list? $list->toArray() : [];
- if($list){
- $skus = GoodsSkuModel::whereIn('goods_id', $ids)->select(['id','price','stock'])->get()->keyBy('id');
- $skus = $skus?$skus->toArray() :[];
- $result = ['store_id'=>0,'total'=>0,'count'=>0,'goods'=>[]];
- // var_dump($skus);
- // var_dump($list);
- // var_dump($goods);
- foreach ($list as $item){
- $item['order_no'] = $orderNo;
- $id = isset($item['goods_id'])?$item['goods_id']:0;
- $params = isset($goods['id_'.$id])? $goods['id_'.$id]:[];
- $goodsId = isset($params['id'])?$params['id']:0;
- $storeId = isset($item['store_id'])?$item['store_id']:0;
- $num = isset($params['num'])?$params['num']:0;
- $skuId = isset($params['sku_id'])?$params['sku_id']:0;
- $skuType = isset($item['sku_type'])?$item['sku_type']: 1;
- $skuData = isset($skus[$skuId])? $skus[$skuId]:[];
- $skuPrice = isset($skuData['price'])?$skuData['price']:0;
- $price = $skuType==2 ? $skuPrice : $item['price'];
- unset($item['skus']);
- if($result['store_id'] && $storeId != $result['store_id']){
- $this->error = '一次只能购买同一个商家的商品,请核对后重试~';
- return false;
- }
- if($num>0 && $goodsId == $id && $price>0){
- $result['store_id'] = $storeId;
- $item['user_id'] = $userId;
- $item['sku_id'] = $skuId;
- $item['price'] = $price;
- $item['total'] = $price;
- $item['num'] = $num;
- $total = round($price * $num,2);
- $item['total'] = $total;
- $item['thumb'] = get_image_path($item['thumb']);
- $result['goods'][] = $item;
- $result['total'] += $total;
- $result['count']++;
- }
- }
- return $result;
- }
- return false;
- }
- }
|