Browse Source

Merge branch '20241029_fl_order'

15313670163 2 months ago
parent
commit
255f498a39
3 changed files with 177 additions and 1 deletions
  1. 124 1
      app/JsonRpc/OrderService.php
  2. 26 0
      app/JsonRpc/OrderServiceInterface.php
  3. 27 0
      app/Model/ShoppingCart.php

+ 124 - 1
app/JsonRpc/OrderService.php

@@ -6,6 +6,7 @@ use App\Model\AdSize;
 use App\Model\AdPlace;
 use App\Model\Order;
 use App\Model\OrderAd;
+use App\Model\ShoppingCart;
 use App\Model\Website;
 use App\Tools\Result;
 use Carbon\Carbon;
@@ -647,7 +648,6 @@ class OrderService implements OrderServiceInterface
         if (empty($rep)) {
             return Result::error("您暂时还没有下单");
         } else {
-
             $data = [
                 'rows' => $rep,
                 'count' => $count,
@@ -744,5 +744,128 @@ class OrderService implements OrderServiceInterface
         }
         return Result::success($order);
     }
+    /**
+     * 创建购物车
+     * @param
+     * @return void
+     */
+    public function addShoppingCart(array $data): array
+    {
+        $shop = ShoppingCart::where('user_id', $data['user_id'])->get()->all();
+        $time = time();
+        $timestamp = date('YmdHis', $time);
+        $randomNumber = mt_rand(100, 999);
+        $shopping_id = $randomNumber . $timestamp; // 时间戳与随机数拼接   
+        Db::beginTransaction();
+        try {
+            if (!empty($shop)) {
+                $del_shop = ShoppingCart::where('user_id', $data['user_id'])->delete();
+                if (empty($del_shop)) {
+                    return Result::error("删除失败");
+                }
+            }
+            $shop = [
+                'user_id' => $data['user_id'],
+                'shopping_id' => $shopping_id,
+                'created_at' => date('Y-m-d H:i:s', time()),
+                'updated_at' => date('Y-m-d H:i:s', time())
+            ];
+            $result = ShoppingCart::insert($shop);
+            Db::commit();
+        } catch (\Exception $e) {
+            Db::rollBack();
+            return Result::error($e->getMessage());
+        }
+        if(empty($result)){
+            return Result::error("创建失败");
+        }else{
+            return Result::success($shopping_id);
+        }
+
+    }
+    /**
+     * 获取购物车中的广告位
+     * @param
+     * @return void
+     */
+    public function getShoppingCartAD(array $data): array
+    {
+        $shopcart = ShoppingCart::where('shopping_id', $data['shopping_id'])
+        ->where('user_id',$data['user_id'])
+        ->orderBy('pid')
+        ->pluck('pid')
+        ->toArray();
+    
+        $result['pid'] = $shopcart;
+    
+        if (empty($result)) {
+            return Result::error("购物车id错误");
+        }else{
+            return Result::success($result);
+        }
+    }
+    /**
+     * 添加购物车中的广告位
+     * @param
+     * @return void
+     */
+    public function addShoppingCartAD(array $data): array
+    {
+        $shop = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id',$data['user_id'])->first();
+        if (empty($shop)) {
+            return Result::error("购物车id错误");
+        }else{
+            $ad = AdPlace::where('ad_place.id', $data['pid'])
+            ->leftJoin('website','ad_place.website_id','website.id')
+            ->select('ad_place.id as pid','website.id as website_id')
+            ->first();
+            if(empty($ad)) {
+                return Result::error("广告位id错误"); 
+            }
+            if(empty($shop['pid']) && empty($shop['website_id'])){
+                $result = ShoppingCart::where('shopping_id', $shop['shopping_id'])->where('user_id',$data['user_id'])->update(['pid' => $ad['pid'],'website_id' => $ad['website_id']]);
+            }else{
+                if($data['pid'] == $shop['pid']){
+                    return Result::error("购物车中已存在该广告位"); 
+                }
+                $shop_ad = [
+                    'pid' => $ad['pid'],
+                    'website_id' => $ad['website_id'],
+                    'shopping_id' => $shop['shopping_id'],
+                    'user_id' => $data['user_id']
+                ];
+                $result = ShoppingCart::insertGetId($shop_ad);
+            }
+        }
+        if(empty($result)){
+            return Result::error("添加失败"); 
+        }else{
+            return Result::success($result);
+        }
 
+    }
+    /**
+     * 删除购物车中的广告位
+     * @param
+     * @return void
+     */
+    public function delShoppingCartAD(array $data): array
+    {
+        $shop = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id',$data['user_id'])->where('pid',$data['pid'])->first();
+        if (empty($shop)) {
+            return Result::error("不存在此条记录!(参数错误)"); 
+        }else{
+            $count = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id',$data['user_id'])->count();
+            if($count == 1){
+                $result = ShoppingCart::where('shopping_id', $data['shopping_id'])->where('user_id',$data['user_id'])->update(['pid' => null, 'website_id' => null]);; 
+            }else{
+                $result = ShoppingCart::where('shopping_id', $shop['shopping_id'])->where('user_id',$data['user_id'])->where('pid',$data['pid'])->delete();
+            } 
+        }
+        if(empty($result)){
+            return Result::error("删除失败"); 
+        }else{
+            return Result::success($result);
+         }
+    }
 }

+ 26 - 0
app/JsonRpc/OrderServiceInterface.php

@@ -81,4 +81,30 @@ interface OrderServiceInterface
      */
     public function delOrderAD(array $data): array;
 
+
+
+     /**
+     * @param array $data
+     *  @return array
+     */
+    public function addShoppingCart(array $data): array;
+
+     /**
+     * @param array $data
+     *  @return array
+     */
+    public function getShoppingCartAD(array $data): array;
+
+     /**
+     * @param array $data
+     *  @return array
+     */
+    public function addShoppingCartAD(array $data): array;
+
+     /**
+     * @param array $data
+     *  @return array
+     */
+    public function delShoppingCartAD(array $data): array;
+
 }

+ 27 - 0
app/Model/ShoppingCart.php

@@ -0,0 +1,27 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Model;
+
+use Hyperf\DbConnection\Model\Model;
+
+/**
+ */
+class ShoppingCart extends Model
+{
+    /**
+     * The table associated with the model.
+     */
+    protected ?string $table = 'order_shopping_cart';
+
+    /**
+     * The attributes that are mass assignable.
+     */
+    protected array $fillable = [];
+
+    /**
+     * The attributes that should be cast to native types.
+     */
+    protected array $casts = [];
+}