Browse Source

前台功能已完成

dddmo 6 months ago
parent
commit
0709641a85
2 changed files with 275 additions and 44 deletions
  1. 250 43
      app/JsonRpc/OrderService.php
  2. 25 1
      app/JsonRpc/OrderServiceInterface.php

+ 250 - 43
app/JsonRpc/OrderService.php

@@ -7,6 +7,11 @@ use App\Model\Order;
 use App\Model\OrderAd;
 use App\Tools\Result;
 use Hyperf\RpcServer\Annotation\RpcService;
+use Carbon\Carbon;
+use Hamcrest\Arrays\IsArray;
+use DateTime;
+use DateInterval;
+
 
 #[RpcService(name: "OrderService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
 class OrderService implements OrderServiceInterface
@@ -19,63 +24,265 @@ class OrderService implements OrderServiceInterface
     */
     public function getAD(array $data): array
     {
+        // $ads = [];
+        // $orderads = [];
         $where = [
-            'width' => $data['width'],
-            'height' => $data['height']
+            'ad_place.width' => $data['width'],
+            'ad_place.height' => $data['height']
         ];
-        $start = $data['starttime'];
-        $end = $data['endtime'];
-        $rep=Ad::where($where)
-        ->where('fromtime','<',$start)
-        ->where('totime','>',$end)
-        ->orderBy('id')
-        ->limit($data['pageSize'])
-        ->offset(($data['page']-1)*$data['pageSize'])->get();
-        $count = Ad::where($where)->count();
-        $data = [
-            'rows'=>$rep->toArray(),
-            'count'=>$count
+        $start=Carbon::parse($data['starttime']);
+        $end=Carbon::parse($data['endtime']);
+        $status = [
+            0=>'1',
+            1=>'4',
+            2=>'6'
         ];
-        $ads = Ad::whereIn($data['id'])
-                ->leftJoin('ad_place','ad.pid','ad_place.id')
-                ->leftJoin("article_data","article.id","article_data.article_id")
-                ->select("ad_place.*","ad.*")
-                ->orderBy("ad.id","desc") 
-                ->limit($data['pageSize'])
-                ->offset(($data['page']-1)*$data['pageSize'])->get();
-        $count = Ad::whereIn($data['id'])->count();
+        $ads = Ad::where('fromtime','<=',$start)->where('totime','>=',$end)->select('pid')->get()->all();
+        $orderads = OrderAd::where('fromtime','<=',$start)->where('totime','>=',$end)->whereIn('status',$status)->select('pid')->get()->all();
+        $pids = array_merge($ads, $orderads);
+        $ad_pids = array_unique($pids);
+        // $ad_pid = [1,2,3];
+        if(!empty($ad_pids)){
+            $pid = [];
+            foreach($ad_pids as $val){
+                array_push($pid,$val['pid']);
+            }
+            $placeids = AdPlace::whereNotIn('id',$pid)->where($where)->select('id')->get()->all();
+            $place_id = [];
+            foreach($placeids as $val){
+                array_push($place_id,$val['id']);
+            }
+            $rep = AdPlace::whereIn('ad_place.id',$place_id)
+            ->leftJoin('website','ad_place.website_id','website.id')
+            ->select('ad_place.*','website.website_name','website.id')
+            ->selectSub('website.id', 'webid')
+            ->selectSub('ad_place.id', 'pid')
+            ->orderBy("website.id","asc") 
+            ->limit($data['pageSize'])
+            ->offset(($data['page']-1)*$data['pageSize'])
+            ->get();
+            
+        }else{
+            $rep = AdPlace::where($where)
+            ->leftJoin('website','ad_place.website_id','website.id')
+            ->select('ad_place.*','website.website_name','website.id')
+            ->selectSub('website.id', 'webid')
+            ->selectSub('ad_place.id', 'pid')
+            ->orderBy("website.id","asc") 
+            ->limit($data['pageSize'])
+            ->offset(($data['page']-1)*$data['pageSize'])
+            ->get();
+        }
+        
+        $count = count($rep);
         $data = [
-            'rows'=>$ads->toArray(),
+            'rows'=>$rep->toArray(),
             'count'=>$count
         ];
+        
         if(empty($rep)){
-            return Result::error("没有信息数据");
+            return Result::error("暂时没有符合您条件的广告位");
         }
-        return Result::success($data);
+        return Result::success($rep);
+    }
+    
+   /**
+     * 添加广告订单
+     * @param
+     * @return void
+    */
+    public function addAD(array $data): array
+    { 
+        date_default_timezone_set('Asia/Shanghai');
+        $time = time(); 
+        $timestamp = date('YmdHis',$time);
+        $catetime = date('Y-m-d H:i:s',$time);
+        $randomNumber = mt_rand(1000, 9999); 
+        $ordernum = $randomNumber . $timestamp; // 时间戳与随机数拼接
+        // var_dump(($time));
+        $order = [
+            'order_num' => $ordernum,
+            'sttime' => $data['starttime'],
+            'edtime' => $data['endtime'],
+            'user_id' => $data['user_id'],
+            'cttime' => $catetime,
+            'height' => $data['height'],
+            'width' => $data['width']
+
+        ];
+        $orderid = Order::insertGetId($order);
+        $adplace = $data['pid'];
+        if(is_array($data['pid'])){
+            $adplace = AdPlace::whereIn('id',$data['pid'])->select('website_id','id')->get();
+            foreach($adplace as $key => $ads){ 
+                $order_ad[$key] =  [
+                    'order_id' => $orderid,
+                    'order_num' => $ordernum,
+                    'name' => $data['name'],
+                    'fromtime' => $data['starttime'],
+                    'totime' => $data['endtime'],
+                    'image_src' => $data['imgsrc'],
+                    'image_url' => $data['imgurl'],
+                    'pid' => $ads['id'],
+                    'website_id' => $ads['website_id']
+                ]; 
+                // $log = [
+                //     'order_id' => $orderid,
+                //     'user_id' => $data['user_id'],
+                //     'pid' => $ads['id'],
+                //     'website_id' => $ads['website_id'],
+                //     'action' => '添加',
+                //     'time' => $catetime
+                // ];      
+            }
+        }else{
+            
+            $order_ad =  [
+                'order_id' => $orderid,
+                'order_num' => $ordernum,
+                'name' => $data['name'],
+                'website_id' => $adplace['website_id'],
+                'fromtime' => $data['starttime'],
+                'totime' => $data['endtime'],
+                'image_src' => $data['imgsrc'],
+                'image_url' => $data['imgurl'],
+                'pid' => $adplace
+            ]; 
+            // $log = [
+            //     'order_id' => $orderid,
+            //     'user_id' => $data['user_id'],
+            //     'pid' => $ads['id'],
+            //     'website_id' => $ads['website_id'],
+            //     'action' => '添加',
+            //     'time' => $catetime
+            // ];          
+        }
+        $orderad_id = OrderAd::insert($order_ad);
+        // $log = AdLog::insert($log);
+        if(empty($orderid) || !$orderad_id){
+            return Result::error("添加失败");
+        }
+        $result = [
+            'order_id' => $orderid,
+            'orderad_id' => $orderad_id,
+            'name' => $data['name'],
+            'name' => $data['name'],
+        ];
+        return Result::success($result);
     }
     /**
-    * 添加订单
-    * @param
-    * @return void
+     * 获取订单列表
+     * @param
+     * @return void
     */
-    public function addOrder(array $data): array
+    public function getOrderList(array $data): array
     {
-        $ads = Ad::whereIn($data['id'])
-                ->leftJoin('ad_place','ad.pid','ad_place.id')
-                ->leftJoin("article_data","article.id","article_data.article_id")
-                ->select("ad_place.*","ad.*")
-                ->orderBy("ad.id","desc") 
-                ->limit($data['pageSize'])
-                ->offset(($data['page']-1)*$data['pageSize'])->get();
-        $count = Ad::whereIn($data['id'])->count();
-        $data = [
-            'rows'=>$ads->toArray(),
-            'count'=>$count
+        $where = [
+            'user_id' => $data['user_id'],
+            'user_del' => 2
         ];
-        if(empty($rep)){
-            return Result::error("没有信息数据");
+        if(isset($data['status'])){
+            $where['status'] = $data['status'];
+        }
+        $orders = Order::where($where)
+        ->orderBy("id","asc") 
+        ->limit($data['pageSize'])
+        ->offset(($data['page']-1)*$data['pageSize'])
+        ->get();
+       
+        if(empty($orders)){
+            return Result::error("您暂时还没有下单");
+        }else{
+            foreach($orders as $key => $order){
+
+                $startTime = strtotime($order['sttime']); 
+                $endTime = strtotime($order['edtime']); 
+                $days = ($endTime - $startTime)/(24 * 60 * 60); 
+                $orders[$key]['days'] =  $days;
+            }
+            $count = count($orders);
+            $data = [
+                'rows'=>$orders->toArray(),
+                'count'=>$count
+            ];
         }
         return Result::success($data);
     }
-
+    /**
+     * 获取订单详情
+     * @param
+     * @return void
+    */
+    public function getOrderDetail(array $data): array
+    {   
+        $order = Order::where('id',$data['order_id'])->first();
+        $startTime = strtotime($order['sttime']); 
+        $endTime = strtotime($order['edtime']); 
+        $days = ($endTime - $startTime)/(24 * 60 * 60); 
+        $order['days'] = $days; 
+        $orderads = OrderAd::where('order_ad.order_id',$data['order_id'])
+        ->leftJoin('website','order_ad.website_id','website.id')
+        ->select('order_ad.*','website.website_name','website.id')
+        ->selectSub('website.id', 'webid')
+        ->selectSub('order_ad.id', 'oid')
+        ->orderBy("website.id","asc") 
+        ->limit($data['pageSize'])
+        ->offset(($data['page']-1)*$data['pageSize'])
+        ->get();
+        if(empty($order)){
+            return Result::error("订单id错误");
+        }
+        $result = [
+            'order' => $order,
+            'orderads' => $orderads
+        ];
+        return Result::success($result);
+    }
+    /**
+     * 撤回订单
+     * @param
+     * @return void
+    */
+    public function cancelOrder(array $data): array
+    {   
+        $order = Order::where('id',$data['order_id'])->where('status',6)->update(['status' => 3]);
+        $ads = OrderAd::where('order_id',$data['order_id'])->where('status',6)->update(['status' => 3]);
+        if(!$order || !$ads){
+            return Result::error("订单id错误");
+        }
+        $result = [
+            'order' => $order,
+            'ads' => $ads
+        ];
+        return Result::success($result);
+    }
+     /**
+     * 删除订单
+     * @param
+     * @return void
+    */
+    public function delOrderAD(array $data): array
+    {   
+        $data['status'] = [
+            0 => 2,
+            1 => 3
+        ];
+        date_default_timezone_set('Asia/Shanghai');
+        $time = time(); 
+        $timestamp = date('YmdHis',$time);
+        $where = [
+            ['user_id','=',$data['user_id']],
+            ['id','=',$data['order_id']]
+        ];
+        $time = [['edtime','<=',$timestamp]];
+        $order = Order::where($where)->where($time)->update(['user_del' => 1]);
+        if(empty($order)){
+           $order = Order::where($where)->whereIn('status',$data['status'])->update(['user_del' => 1]); 
+        }
+        // 
+        if($order == 0){
+            return Result::error("订单id错误");
+        }
+        return Result::success($order);
+    }
 }

+ 25 - 1
app/JsonRpc/OrderServiceInterface.php

@@ -8,5 +8,29 @@ interface OrderServiceInterface
      *  @return array
      */
     public function getAD(array $data): array;
-
+    /**
+     * @param array $data
+     *  @return array
+     */
+    public function addAD(array $data): array;
+    /**
+     * @param array $data
+     *  @return array
+     */
+    public function getOrderList(array $data): array;
+    /**
+     * @param array $data
+     *  @return array
+     */
+    public function getOrderDetail(array $data): array;
+    /**
+     * @param array $data
+     *  @return array
+     */
+    public function cancelOrder(array $data): array;
+    /**
+     * @param array $data
+     *  @return array
+     */
+    public function delOrderAD(array $data): array;
 }