model = new \app\admin\model\Lessonslot; $this->view->assign("statusList", $this->model->getStatusList()); $this->view->assign("noticeStatusList", $this->model->getNoticeStatusList()); $this->view->assign("isShowList", $this->model->getIsShowList()); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { //当前是否为关联查询 $this->relationSearch = true; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = $this->model ->with(['coach','lesson','danceroom']) ->where($where) // ->order($sort, $order) ->order('lessonslot.starttime desc,lessonslot.id desc') ->paginate($limit); foreach ($list as $row) { $row->getRelation('coach')->visible(['nickname']); $row->getRelation('lesson')->visible(['name','name_en','type']); $row->getRelation('danceroom')->visible(['name','name_en']); } $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } /** * 预约 */ public function booking(){ $id = input('id'); $info = Db::name('lesson_slot')->where('id',$id)->find(); if(!$info){ $this->error('请刷新重试'); } if($info['status'] != 0){ $this->error('当前课程不能预约'); } if($this->request->isPost()){ $user_id = input('user_id',0,'intval'); //来自接口 $slot_id = input('slot_id',0,'intval'); $number = input('number',1,'intval'); $remark = input('remark','','trim'); $paytype = 1;//支付类型:1=课程套餐,2=线上付款,3=购买套餐中,4=试课单 $packageorder_id = input('packageorder_id',0,'intval'); if($number <= 0){ $this->error('预约人数错误'); } //课时 $info = Db::name('lesson_slot')->alias('slot') ->field('slot.*,lesson.name,lesson.name_en,lesson.image,lesson.price') ->join('lesson','slot.lesson_id = lesson.id','LEFT') ->where('slot.id',$slot_id)->where('slot.status',0)->find(); if(empty($info)){ $this->error('课程可能已取消,请刷新重试'); } if($info['endtime'] < time()){ $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])); } $number_hours = bcmul($number,$info['hours'],1);//每日课时按小时扣 $lesson_order = [ 'order_no' => createUniqueNo('S',$user_id), 'user_id' => $user_id, 'slot_id' => $slot_id, 'lesson_id' => $info['lesson_id'], 'order_amount' => bcmul($info['price'],$number,2), 'order_status' => 0, 'paytype' => $paytype, //支付类型:1=课程套餐,2=线上付款,3=购买套餐中,4=试课单 'paytime' => 0, 'createtime' => time(), // 'updatetime' => , // 'finishtime' => , 'usernumber' => $number, 'usernumber_hours' => $number_hours, 'userremark' => $remark, 'package_order_id' => 0, 'package_remark' => '',// 比如:5/30,5-7/30 ]; // Db::startTrans(); if($paytype == 1){ //检查已选择套餐 $map = [ 'user_id' =>$user_id, 'endtime' => ['gt',time()], 'remain' => ['gt',0], 'order_status' => 1, // 'use_status' => 1, //已激活的 'id' => $packageorder_id, ]; $package_order = Db::name('package_order')->where($map)->where('find_in_set(:lesson_ids,lesson_ids)', ['lesson_ids' => $info['lesson_id']])->lock(true)->find(); if(!$package_order){ Db::rollback(); $this->error('配套信息不正确,请刷新重试'); } //课时不足支撑报名人数 if($package_order['remain'] < $number_hours){ Db::rollback(); $this->error('该配套余额不足,可以使用其他配套'); } if($package_order['use_status'] != 1){ 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('预约成功'); } } $this->view->assign('row',$info); return $this->view->fetch(); } /** * 取消 */ public function cancel(){ $id = input('id'); $info = Db::name('lesson_slot')->where('id',$id)->find(); if(!$info){ $this->error('请刷新重试'); } if($info['status'] != 0){ $this->error('当前课程不能取消'); } if($this->request->isPost()){ $remark = input('remark',''); $cancel_reason = input('cancel_reason',''); $cancel_time = time(); Db::startTrans(); $info = Db::name('lesson_slot')->where('id',$id)->lock(true)->find(); if(!$info){ Db::rollback(); $this->error('请刷新重试'); } if($info['status'] != 0){ Db::rollback(); $this->error('当前课程不能取消'); } $update = [ 'status' => 30, 'remark' => $remark, 'cancel_reason' => $cancel_reason, 'cancel_time' => $cancel_time, 'bookednum' => 0, ]; $rs1 = Db::name('lesson_slot')->where('id',$id)->update($update); if($rs1 === false){ Db::rollback(); $this->error('取消失败,请刷新重试'); } //找到所有的已候补订单 $update = [ 'order_status' => 30, 'cancel_time' => $cancel_time, 'cancel_reason' => $cancel_reason, ]; $rs2 = Db::name('lesson_order')->where('slot_id',$id)->where('jointype',2)->where('order_status',0)->update($update); //找到所有的已报名订单 $lesson_order_list = Db::name('lesson_order')->where('slot_id',$id)->where('jointype',1)->where('order_status',10)->lock(true)->select(); if(!empty($lesson_order_list)){ foreach($lesson_order_list as $lesson_order){ //套餐给加回去 if($lesson_order['paytype'] == 1){ $package_order = Db::name('package_order')->where('id',$lesson_order['package_order_id'])->lock(true)->find(); $update = [ 'remain' => bcadd($package_order['remain'],$lesson_order['usernumber_hours'],1), 'updatetime' => time(), ]; $rs_remain = Db::name('package_order')->where('id',$lesson_order['package_order_id'])->update($update); if($rs_remain === false){ Db::rollback(); $this->error('取消失败'); } } //试课给改回去 if($lesson_order['paytype'] == 4){ $update = [ 'order_status' => 10, 'updatetime' => time(), 'lesson_order_id' => 0, ]; $rs_remain = Db::name('trylesson_order')->where('id',$lesson_order['trylesson_order_id'])->update($update); if($rs_remain === false){ Db::rollback(); $this->error('取消失败'); } } //现金支付不给退,线下处理 //取消预约单 $update = [ 'order_status' => 30, 'cancel_time' => $cancel_time, 'cancel_reason' => $cancel_reason, ]; if($lesson_order['paytype'] == 1 || $lesson_order['paytype'] == 4){ $update['order_status'] = 40; } $rs = Db::name('lesson_order')->where('id',$lesson_order['id'])->update($update); if($rs === false){ Db::rollback(); $this->error('取消失败'); } } } Db::commit(); $comefrom = input('comefrom',''); if($comefrom == 'backend'){ //后台来的 $this->success('取消完成'); }else{ //接口来的 $this->result('',1,'取消完成','json'); } } $this->view->assign('row',$info); return $this->view->fetch(); } /** * 复制本周的课程表 */ public function copyweek(){ exit; $starttime = strtotime('this week Monday'); // 获取本周一的时间戳 $endtime = $starttime + 86400*7; $list = Db::name('lesson_slot')->where('is_show',1)->where('starttime','BETWEEN',[$starttime,$endtime])->order('starttime asc')->select(); if(empty($list)){ $this->error('本周还没有课程表'); } foreach($list as $key => &$val){ unset($val['id']); $val['starttime'] = $val['starttime'] + 86400*7; $val['endtime'] = $val['endtime'] + 86400*7; $val['status'] = 0; $val['notice_status'] = 0; $val['finishtime'] = 0; $val['cancel_reason'] = ''; $val['cancel_time'] = 0; } Db::name('lesson_slot')->insertAll($list); $this->success('已复制到下周'); } /** * 查看 */ public function vue_index() { $where = [ 'slot.status' => ['NEQ',30], ]; $coach_id = input('coach_id',''); $lesson_id = input('lesson_id',''); $danceroom_id = input('danceroom_id',0); $bookstatus = input('bookstatus',0); $starttime = input('starttime',0);//默认看今天 $endtime = input('endtime',0); if(empty($starttime) || empty($endtime)){ $starttime = strtotime(date('Y-m-d')); $endtime = $starttime + 86399; } if(!empty($coach_id)){ $where['slot.coach_ids'] = ['IN',$coach_id]; } if(!empty($lesson_id)){ $where['slot.lesson_id'] = ['IN',$lesson_id]; } if(!empty($danceroom_id)){ $where['slot.danceroom_id'] = $danceroom_id; } $where['slot.starttime'] = ['BETWEEN',[$starttime,$endtime]]; $wherenew = ''; if(!empty($bookstatus)){ if($bookstatus == 1){ $wherenew = 'slot.bookednum = 0'; } if($bookstatus == 2){ $wherenew = 'slot.bookednum != 0 and slot.bookednum < num_max'; } if($bookstatus == 3){ $wherenew = 'slot.bookednum = num_max'; } } $field = [ 'slot.id','slot.starttime','slot.endtime','slot.num_max','slot.bookednum','coach_ids', 'coach.nickname as Staff','coach.bgcolor', 'lesson.name_en as title', 'danceroom.name_en as Location', ]; $list = Db::name('lesson_slot')->alias('slot') ->join('coach','slot.coach_ids = coach.id','LEFT') ->join('lesson','slot.lesson_id = lesson.id','LEFT') ->join('danceroom','slot.danceroom_id = danceroom.id','LEFT') ->field($field) ->where($where) ->where($wherenew) ->order('slot.id desc') ->select(); if(!empty($list)){ foreach($list as $key => $val){ $val['resourceId'] = $val['coach_ids']; $val['start'] = date('Y-m-d H:i',$val['starttime']); $val['end'] = date('Y-m-d H:i',$val['endtime']); //无人约 $val['backgroundColor'] = '#ffffff'; $val['borderColor'] = $val['bgcolor']; $val['textColor'] = '#333333'; //有人约 if($val['bookednum'] > 0){ $val['backgroundColor'] = $val['bgcolor']; $val['borderColor'] = $val['bgcolor']; $val['textColor'] = '#ffffff'; } //详情 $val['extendedProps'] = [ 'Session' => $val['title'], 'time' => date('D, M d Y, H:i',$val['starttime']).' - '.date('H:i',$val['endtime']), 'Staff' => $val['Staff'], 'Location' => $val['Location'], 'Participar' => $val['bookednum'].'/'.$val['num_max'].' participants', 'id' => $val['id'], ]; $list[$key] = $val; } } $this->result($list,1,'success','json'); } //教练表头 public function vue_staff() { $coach_id = input('coach_id',''); //教练列表 $coach_map = []; if(!empty($coach_id)){ $coach_map['id'] = ['IN',$coach_id]; } $coach_list = Db::name('coach')->where($coach_map)->order('id desc')->select(); $result = []; if(!empty($coach_list)){ foreach($coach_list as $ckey => $cval){ $result[] = [ 'id' => $cval['id'], 'title' => $cval['nickname'], 'extendedProps' => [ 'name' => $cval['nickname'], 'avatar' => localpath_to_netpath($cval['avatar']), ], ]; } } $this->result($result,1,'success','json'); } //课程新增 public function slot_add(){ $field = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','address','remark','address_en','remark_en','is_show']; $require = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','is_show']; $data = request_post_hub($field,$require); $data['endtime'] = $data['starttime'] + ($data['hours'] * 3600); $id = Db::name('lesson_slot')->insertGetId($data); $this->result($id,1,'添加完成','json'); } //课程详情 public function slot_info(){ $id = input('id',0); $info = Db::name('lesson_slot')->where('id',$id)->find(); $name = Db::name('lesson')->where('id',$info['lesson_id'])->value('name_en'); $info['lesson_name'] = $name; $this->result($info,1,'success','json'); } //课程编辑 public function slot_edit(){ $id = input('id',0); $field = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','address','remark','address_en','remark_en','is_show']; $require = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','is_show']; $data = request_post_hub($field,$require); $data['endtime'] = $data['starttime'] + ($data['hours'] * 3600); Db::name('lesson_slot')->where('id',$id)->update($data); $this->result('',1,'修改完成','json'); } }