|
@@ -0,0 +1,321 @@
|
|
|
|
|
+<?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\Models\MemberModel;
|
|
|
|
|
+use App\Services\BaseService;
|
|
|
|
|
+use App\Services\RedisService;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 商品管理-服务类
|
|
|
|
|
+ * @author laravel开发员
|
|
|
|
|
+ * @since 2020/11/11
|
|
|
|
|
+ * @package App\Services\Api
|
|
|
|
|
+ */
|
|
|
|
|
+class GoodsCategoryService extends BaseService
|
|
|
|
|
+{
|
|
|
|
|
+// 静态对象
|
|
|
|
|
+ protected static $instance = null;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 构造函数
|
|
|
|
|
+ * @author laravel开发员
|
|
|
|
|
+ * @since 2020/11/11
|
|
|
|
|
+ */
|
|
|
|
|
+ public function __construct()
|
|
|
|
|
+ {
|
|
|
|
|
+ $this->model = new GoodsCategoryModel();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 静态入口
|
|
|
|
|
+ */
|
|
|
|
|
+ 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:cateList_{$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.is_show'=>1,'a.is_menu'=>1,'a.status' => 1, 'a.mark' => 1];
|
|
|
|
|
+ $status = isset($params['status']) ? $params['status'] : 1;
|
|
|
|
|
+ $isShow = isset($params['is_show']) ? $params['is_show'] : 0;
|
|
|
|
|
+ $isMenu = isset($params['is_menu']) ? $params['is_menu'] : 0;
|
|
|
|
|
+ if ($status > 0) {
|
|
|
|
|
+ $where['a.status'] = $status;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ unset($where['a.status']);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($isShow>0) {
|
|
|
|
|
+ $where['a.is_show'] = $isShow;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ unset($where['a.is_show']);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if ($isMenu) {
|
|
|
|
|
+ $where['a.is_menu'] = $isMenu;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ unset($where['a.is_menu']);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $model = $this->model
|
|
|
|
|
+ ->from('goods_categorys as a')
|
|
|
|
|
+ ->where($where)
|
|
|
|
|
+ ->where(function ($query) use ($params) {
|
|
|
|
|
+ $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
|
|
|
|
|
+ if ($keyword) {
|
|
|
|
|
+ $query->where(function ($query) use ($keyword) {
|
|
|
|
|
+ $query->where('a.name', 'like', "%{$keyword}%");
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ return $model;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 分类
|
|
|
|
|
+ * @return array|mixed
|
|
|
|
|
+ */
|
|
|
|
|
+ public function getCategoryList()
|
|
|
|
|
+ {
|
|
|
|
|
+ $cacheKey = "caches:goods:categoryList";
|
|
|
|
|
+ $datas = RedisService::get($cacheKey);
|
|
|
|
|
+ if($datas){
|
|
|
|
|
+ return $datas;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $datas = $this->model->where(['pid'=>0,'status'=>1,'mark'=>1])
|
|
|
|
|
+ ->select(['id','name','icon','pid','sort'])
|
|
|
|
|
+ ->orderBy('sort','desc')
|
|
|
|
|
+ ->orderBy('id','asc')
|
|
|
|
|
+ ->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) {
|
|
|
|
|
+ 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 $userId
|
|
|
|
|
+ * @param $orderNo 订单号
|
|
|
|
|
+ * @return array|false
|
|
|
|
|
+ */
|
|
|
|
|
+ public function getOrderGoods($ids, $goods, $userId, $orderNo='', $discountPoint=0)
|
|
|
|
|
+ {
|
|
|
|
|
+ if(empty($ids) || empty($goods)){
|
|
|
|
|
+ $this->error = '请选择商品';
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 用户信息
|
|
|
|
|
+ if(empty($orderNo)){
|
|
|
|
|
+ $userInfo = MemberModel::where(['id' => $userId, 'mark' => 1])
|
|
|
|
|
+ ->select(['id','openid', 'discount_point', 'status'])
|
|
|
|
|
+ ->first();
|
|
|
|
|
+ $status = isset($userInfo['status']) ? $userInfo['status'] : 0;
|
|
|
|
|
+ $openid = isset($userInfo['openid']) ? $userInfo['openid'] : 0;
|
|
|
|
|
+ $discountPoint = isset($userInfo['discount_point']) ? $userInfo['discount_point'] : 0; // 折扣
|
|
|
|
|
+ if (empty($userInfo) || $status != 1) {
|
|
|
|
|
+ $this->error = 1045;
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (empty($openid)) {
|
|
|
|
|
+ $this->error = 1042;
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ $list = $this->model->whereIn('id', $ids)
|
|
|
|
|
+ ->where(['status'=>1,'mark'=>1])
|
|
|
|
|
+ ->select(['id as goods_id','goods_name','category_id','store_id','delivery_fee','sku_type','price','stock','unit','weight','thumb','sku_type'])
|
|
|
|
|
+ ->get();
|
|
|
|
|
+ $list = $list? $list->toArray() : [];
|
|
|
|
|
+ if($list){
|
|
|
|
|
+ $skus = GoodsSkuModel::whereIn('goods_id', $ids)->select(['id','sku_name','price','stock'])->get()->keyBy('id');
|
|
|
|
|
+ $skus = $skus?$skus->toArray() :[];
|
|
|
|
|
+ $result = ['discount_point'=>$discountPoint,'store_id'=>0,'discount_total'=>0.00,'delivery_fee'=>0.00,'goods_total'=>0,'order_total'=>0,'count'=>0,'goods'=>[]];
|
|
|
|
|
+ foreach ($list as $item){
|
|
|
|
|
+ $item['order_no'] = $orderNo;
|
|
|
|
|
+ $id = isset($item['goods_id'])?$item['goods_id']:0;
|
|
|
|
|
+ $goodsName = isset($item['goods_name'])?$item['goods_name']:'';
|
|
|
|
|
+ $params = isset($goods['id_'.$id])? $goods['id_'.$id]:[];
|
|
|
|
|
+ $goodsId = isset($params['id'])?$params['id']:0;
|
|
|
|
|
+ $storeId = isset($item['store_id'])?$item['store_id']:0;
|
|
|
|
|
+ $deliveryFee = isset($item['delivery_fee'])?$item['delivery_fee']:0;
|
|
|
|
|
+ $stock = isset($item['stock'])?$item['stock']: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;
|
|
|
|
|
+ $skuStock = isset($skuData['stock'])?$skuData['stock']:0;
|
|
|
|
|
+ $skuName = isset($skuData['sku_name'])?$skuData['sku_name']:'';
|
|
|
|
|
+ $price = $skuType==2 ? $skuPrice : $item['price'];
|
|
|
|
|
+ unset($item['skus']);
|
|
|
|
|
+ if($result['store_id'] && $storeId != $result['store_id']){
|
|
|
|
|
+ $this->error = '一次只能购买同一个商家的商品,请核对后重试~';
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if($stock<=0 || $num>$stock){
|
|
|
|
|
+ $this->error = $skuId? "商品[{$goodsName}]规格[{$skuName}]库存不足~" : "商品[{$goodsName}]库存不足~";
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if($skuType==2 && ($skuStock<=0 || $num>$skuStock)){
|
|
|
|
|
+ $this->error = "商品[{$goodsName}]规格[{$skuName}]库存不足~";
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if($num>0 && $goodsId == $id && $price>0){
|
|
|
|
|
+ $result['store_id'] = $storeId;
|
|
|
|
|
+ $result['delivery_fee'] = max($deliveryFee,$result['delivery_fee']);
|
|
|
|
|
+ $item['user_id'] = $userId;
|
|
|
|
|
+ $item['sku_id'] = $skuId;
|
|
|
|
|
+ if(empty($orderNo)){
|
|
|
|
|
+ $item['sku'] = $skuData;
|
|
|
|
|
+ }
|
|
|
|
|
+ $item['price'] = $price;
|
|
|
|
|
+ $item['total'] = $price;
|
|
|
|
|
+ $item['num'] = $num;
|
|
|
|
|
+ $total = round($price * $num,2);
|
|
|
|
|
+ $item['total'] = $total;
|
|
|
|
|
+ $item['thumb'] = $orderNo?get_image_path($item['thumb']):$item['thumb'];
|
|
|
|
|
+ $result['goods'][] = $item;
|
|
|
|
|
+ $result['goods_total'] += $total;
|
|
|
|
|
+ $result['order_total'] += $total;
|
|
|
|
|
+ $result['count']++;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 会员折扣
|
|
|
|
|
+ $orderTotal = $result['order_total'];
|
|
|
|
|
+ if($discountPoint>0 && $discountPoint<1){
|
|
|
|
|
+ $result['order_total'] = moneyFormat((1-$discountPoint) * $result['order_total'],2);
|
|
|
|
|
+ $result['discount_total'] = moneyFormat($orderTotal - $result['order_total'],2);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $result['pay_total'] = moneyFormat($result['order_total'] + $result['delivery_fee'],2);
|
|
|
|
|
+ return $result;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|