Browse Source

预约候补下单

lizhen_gitee 8 months ago
parent
commit
475a4a827a
1 changed files with 141 additions and 14 deletions
  1. 141 14
      application/api/controller/Lesson.php

+ 141 - 14
application/api/controller/Lesson.php

@@ -219,11 +219,24 @@ class Lesson extends Api
                 $slot['num_remain'] = 0;
             }
 
+            //剩余候补数量
+            $slot['wait_remain'] = 0;
+            if($slot['waitnum_max'] > 0){
+                $wait_number = Db::name('lesson_order')->where('slot_id',$slot['id'])->where('jointype',2)->sum('usernumber');
+                $slot['wait_remain'] = $slot['waitnum_max'] - $wait_number;
+                if($slot['wait_remain'] < 0){
+                    $slot['wait_remain'] = 0;
+                }
+            }
+
             //预约按钮,1能预约,0不能
-            $slot['button_status'] = $slot['status'] == 0 ? 1 : 0;
+            $slot['button_status'] = 0;
 
-            if($slot['num_remain'] <= 0){
-                $slot['button_status'] = 0;//没有空位了,强制改
+            if($slot['status'] == 0 && $slot['num_remain'] > 0){
+                $slot['button_status'] = 1;//能预约
+            }
+            if($slot['status'] == 0 && $slot['wait_remain'] > 0){
+                $slot['button_status'] = 2;//能候补
             }
 
             //右上角备注
@@ -233,6 +246,9 @@ class Lesson extends Api
             }else{
 //                $slot['num_remark'] = __('剩N个名额',['number'=>$slot['num_remain']]);
                 $slot['num_remark'] = $pay_number.'/'.$slot['num_max'];
+                if($slot['waitnum_max'] > 0){
+                    $slot['num_remark'] .= '--'.$wait_number.'/'.$slot['waitnum_max'];
+                }
             }
         }
 
@@ -271,6 +287,16 @@ class Lesson extends Api
             $info['num_remain'] = 0;
         }
 
+        //剩余候补数量
+        $info['wait_remain'] = 0;
+        if($info['waitnum_max'] > 0){
+            $wait_number = Db::name('lesson_order')->where('slot_id',$info['id'])->where('jointype',2)->sum('usernumber');
+            $info['wait_remain'] = $info['waitnum_max'] - $wait_number;
+            if($info['wait_remain'] < 0){
+                $info['wait_remain'] = 0;
+            }
+        }
+
         //时间
         if($this->lang == 'en'){
             $info['slot_time'] = date('M d',$info['starttime']).'('.date('D',$info['starttime']).')'.','.date('H:i',$info['starttime']).'-'.date('H:i',$info['endtime']);
@@ -286,11 +312,21 @@ class Lesson extends Api
             ->find();
         $info['wait_buy_package'] = !empty($wait_buy_package) ? 1 : 0;
 
+
         //预约按钮,1能预约,0不能
-        /*$info['button_status'] = $info['status'] == 0 ? 1 : 0;
-        if($info['num_remain'] <= 0){
-            $info['button_status'] = 0;//没有空位了,强制改
-        }*/
+        $info['button_status'] = 0;
+
+        if($info['status'] == 0 && $info['num_remain'] > 0){
+            $info['button_status'] = 1;//能预约
+        }
+        if($info['status'] == 0 && $info['wait_remain'] > 0){
+            $info['button_status'] = 2;//能候补
+        }
+
+        //右上角备注
+        if(time() >= $info['starttime']){
+            $info['button_status'] = 0;//报名截止了,强制改
+        }
 
         //总价格乘出来
         $info['price'] = bcmul($info['price'],$number,2);
@@ -372,15 +408,20 @@ class Lesson extends Api
         $slot_id = input('slot_id',0,'intval');
         $number  = input('number',1,'intval');
         $remark  = input('remark','','trim');
-        $paytype = input('paytype',1,'intval');//支付类型:1=课程套餐,2=线上付款,3=购买套餐中,4=试课单
-        $packageorder_id = input('packageorder_id',0,'intval');
+        $paytype = input('paytype',1,'intval');//支付类型:1=课程套餐,2=线上付款,3=购买套餐中,4=试课单,5=候补
+        $packageorder_id   = input('packageorder_id',0,'intval');
         $trylessonorder_id = input('trylessonorder_id',0,'intval');
+        $jointype = input('jointype',1);//预约类型:1=预约,2=候补
+
         if($number <= 0){
             $this->error('预约人数错误');
         }
         if(!in_array($paytype,[1,2,3,4])){
             $this->error();
         }
+        if(!in_array($jointype,[1,2])){
+            $this->error();
+        }
 
         //课时
         $info = Db::name('lesson_slot')->alias('slot')
@@ -394,16 +435,43 @@ class Lesson extends Api
             $this->error('课程已经结束了,不能再进行预约');
         }
 
-        //报名人数不能超限
+
+        //剩余空位数量
         $pay_number = Db::name('lesson_order')->where('slot_id',$slot_id)->where('order_status',10)->sum('usernumber');
         $num_remain = $info['num_max'] - $pay_number;
         if($num_remain < 0){
             $num_remain = 0;
         }
-        if($num_remain < $number){
-            $this->error(__('预约名额只剩N名',['number'=>$num_remain]));
+
+        //剩余候补数量
+        $wait_remain = 0;
+        if($info['waitnum_max'] > 0){
+            $wait_number = Db::name('lesson_order')->where('slot_id',$info['id'])->where('jointype',2)->sum('usernumber');
+            $wait_remain = $info['waitnum_max'] - $wait_number;
+            if($wait_remain < 0){
+                $wait_remain = 0;
+            }
+        }
+
+        //报名人数不能超限
+        if($jointype == 1){
+            if($num_remain < $number){
+                $this->error(__('预约名额只剩N名',['number'=>$num_remain]));
+            }
+            //可以继续
+        }else{
+            if($num_remain > 0){
+                $this->error(__('预约还有名额,不能候补'));
+            }
+            //候补人数不能超限
+            if($wait_remain < $number){
+                $this->error(__('候补名额只剩N名',['number'=>$wait_remain]));
+            }
+            //可以继续
+            $paytype = 5;//强改支付方式,候补
         }
 
+        //
         $number_hours = bcmul($number,$info['hours'],1);//每日课时按小时扣
         $lesson_order = [
             'order_no' => createUniqueNo('S',$this->auth->id),
@@ -412,7 +480,7 @@ class Lesson extends Api
             'lesson_id' => $info['lesson_id'],
             'order_amount' => bcmul($info['price'],$number,2),
             'order_status' => 0,
-            'paytype' => $paytype, //支付类型:1=课程套餐,2=线上付款,3=购买套餐中,4=试课单
+            'paytype' => $paytype, //支付类型:1=课程套餐,2=线上付款,3=购买套餐中,4=试课单,5=候补
             'paytime' => 0,
             'createtime' => time(),
 //            'updatetime' => ,
@@ -422,6 +490,8 @@ class Lesson extends Api
             'userremark' => $remark,
             'package_order_id' => 0,
             'package_remark' => '',//  比如:5/30,5-7/30
+
+            'jointype' => $jointype,
         ];
 
         //
@@ -557,7 +627,9 @@ class Lesson extends Api
                 'returntype' => 3,
             ];
             $this->success(1,$return);
-        }elseif($paytype == 4){
+        }
+        elseif($paytype == 4)
+        {
             //使用试课单
             $map = [
                 'id' => $trylessonorder_id,
@@ -607,6 +679,61 @@ class Lesson extends Api
             Db::commit();
             $this->success('预约成功',['returntype'=>4]);
         }
+        elseif($paytype == 5)
+        {
+            //检查可用的套餐,找一个就可以
+            $map = [
+                'user_id' =>$this->auth->id,
+
+                'endtime' => ['gt',time()],
+                'remain' => ['gt',$number_hours],
+                'order_status' => 1,
+                'use_status'   => 1, //已激活的
+            ];
+            $package_order = Db::name('package_order')->where($map)->where('find_in_set(:lesson_ids,lesson_ids)', ['lesson_ids' => $info['lesson_id']])->find();
+            if(!$package_order){
+                Db::rollback();
+                $this->error('没有找到足够预约人数的配套,请刷新重试');
+            }
+
+            //扣除一节。延迟到转正的时候处理
+            /*$update = [
+                'remain'     => bcsub($package_order['remain'],$number_hours,1),
+                'updatetime' => time(),
+            ];
+            $rs1 = Db::name('package_order')->where('id',$packageorder_id)->update($update);
+            if($rs1 === false){
+                Db::rollback();
+                $this->error('扣除套餐余额失败');
+            }*/
+
+            //修改预约单数据。延迟到转正的时候处理
+            /*$lesson_order['order_amount'] = 0;
+            $lesson_order['order_status'] = 10;
+            $lesson_order['paytime'] = time();
+            $lesson_order['package_order_id'] = $packageorder_id;
+
+            $lesson_order['package_remark'] = ($package_order['sessions'] - $package_order['remain']) . '-' . ($package_order['sessions'] - $package_order['remain'] + $number_hours) .'/'. $package_order['sessions'];*/
+
+
+            //预约单写入
+            $lesson_order_id = Db::name('lesson_order')->insertGetId($lesson_order);
+            if(!$lesson_order_id){
+                Db::rollback();
+                $this->error('预约失败');
+            }
+
+            //更新已预约人数。候补就不需要了
+            /*$pay_number = Db::name('lesson_order')->where('slot_id',$slot_id)->where('order_status',10)->sum('usernumber');
+            $rs_slot = Db::name('lesson_slot')->where('id',$slot_id)->update(['bookednum' => $pay_number]);
+            if($rs_slot === false){
+                Db::rollback();
+                $this->error('预约失败');
+            }*/
+
+            Db::commit();
+            $this->success('预约候补成功',['returntype'=>5]);
+        }
     }
 
     //配套商店 所有套餐列表