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();
}
//按周翻页
$week_current = input('week_current','');
if($week_current !== ''){
$starttime = strtotime('this week Monday') + (86400*7*$week_current);
$endtime = $starttime + (86400*7);
$where = ['lessonslot.starttime'=>['between',[$starttime,$endtime]]];
$limit = 999999;
}else{
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')
// ->select(false);echo $list;exit;
->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']);
}
$items = $list->items();
foreach($items as $key => $val){
$items[$key]['week'] = date('l',$val['starttime']);
}
//上-周,本周,下-周
if(empty($week_current)){$week_current = 0;}
$this_week = 0;
$last_week = $week_current-1;
$next_week = $week_current+1;
$extend = [
'this_week'=>$this_week,
'last_week'=>$last_week,
'next_week'=>$next_week,
];
$result = array("total" => $list->total(), "rows" => $items,'extend'=>$extend);
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('当前课程不能预约');//状态:0=报名中,20=已点名,30=已取消
}
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');
$packageorder_id = input('packageorder_id',0,'intval');
$trylessonorder_id = input('trylessonorder_id',0,'intval');
$params = $this->request->post('row/a');
$paytype = $params['paytype'];//支付类型:1=课程套餐,4=试课单
if($number <= 0){
$this->error('预约人数错误');
}
if($paytype == 4){
$number = 1; //试课只能选一个人
}
if(empty($packageorder_id) && empty($trylessonorder_id)){
$this->error('请选择任意一种支付方式');
}
//课时
$info = Db::name('lesson_slot')->alias('slot')
->field('slot.*,lesson.name,lesson.name_en,lesson.image,lesson.price,coach.nickname as coach_nickname,danceroom.name_en as danceroom_name_en')
->join('lesson','slot.lesson_id = lesson.id','LEFT')
->join('coach','slot.coach_ids = coach.id','LEFT')
->join('danceroom','slot.danceroom_id = danceroom.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('预约失败');
}
}
elseif($paytype == 4){
//使用试课单
$map = [
'id' => $trylessonorder_id,
'user_id' =>$user_id,
'endtime' => ['gt',time()],
'order_status' => 10,
];
$trylesson_order = Db::name('trylesson_order')->where($map)->where('find_in_set(:lesson_ids,lesson_ids)', ['lesson_ids' => $info['lesson_id']])->lock(true)->find();
if(!$trylesson_order){
Db::rollback();
$this->error('未找到对应的试课');
}
//修改预约单数据
$lesson_order['order_amount'] = 0;
$lesson_order['order_status'] = 10;
$lesson_order['paytime'] = time();
$lesson_order['trylesson_order_id'] = $trylessonorder_id;
//预约单写入
$lesson_order_id = Db::name('lesson_order')->insertGetId($lesson_order);
if(!$lesson_order_id){
Db::rollback();
$this->error('预约失败');
}
//改掉试课单状态
$update = [
'order_status' => 20,
'updatetime' => time(),
'lesson_order_id' => $lesson_order_id,
];
$rs1 = Db::name('trylesson_order')->where('id',$trylessonorder_id)->update($update);
if($rs1 === false){
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();
//额外的通知
$userinfo = Db::name('user')->where('id',$user_id)->find();
$obj = new Email();
try {
$message =
'Hi,'.$userinfo['firstname']. ' ' .$userinfo['lastname'].'!
Your booking is confirmed and you’re all set now to fly with us! Here are the details of your booking:
Class:'.$info['name_en'].'
Instructor:'.$info['coach_nickname'].'
Location: '. $info['danceroom_name_en'] .'
Date: '.date('d F Y',$info['starttime']).'
Time: '.date('H:i a',$info['starttime']).'
Please arrive 10 minutes before the class timing to prepare for the harness fitting!
We are located at 450 Alexandra Road, #02-01,
Singapore 119960. For those who are driving, parking is available on level 2 of the building. For those who are taking public transport, the nearest mrt is Labrador Park MRT (7-10 minutes walk) and there is also a bus stop available just outside the building!
We look forward to seeing you for an amazing session! ❤
Best Regards,
Elin Dance Studio
';
//给这些用户发邮件
$result = $obj
->to($userinfo['email'])
->subject('You are now booked in for this class! ')
->message($message)
->send();
//发whatsapp
$parameters = [
[
'type' => 'text',
'text' => $userinfo['firstname'].' '.$userinfo['lastname'],
],
[
'type' => 'text',
'text' => $info['name_en'],
],
[
'type' => 'text',
'text' => date('D l Y',$info['starttime']) .' at '.date('H:i a',$info['starttime']),
],
[
'type' => 'text',
'text' => $info['danceroom_name_en'],
],
];
$this->whatapp($userinfo['whatsapp'],'class_booked_confirmation','en_US',$parameters);
} catch (Exception $e) {
}
//额外的通知
$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('取消失败,请刷新重试');
}
//发送通知
$this->auto_lesson_slot_cancel($id);
//找到所有的已候补订单
$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();
}
//课程取消,通知。计划任务5分钟执行一次
private function auto_lesson_slot_cancel($slot_id){
$map = [
'slot.id' => $slot_id,
];
$slot = Db::name('lesson_slot')->alias('slot')
->field('slot.*,coach.nickname as coach_nickname,coach.email as coach_email,coach.whatsapp as coach_whatsapp,lesson.name_en as lesson_name_en')
->join('coach','slot.coach_ids = coach.id','LEFT')
->join('lesson','slot.lesson_id = lesson.id','LEFT')
->where($map)->order('slot.starttime asc')->find();
//找出这节课时的预约单
$map = [
'order.slot_id' => $slot['id'],
];
$order_list = Db::name('lesson_order')->alias('order')
->field('user.firstname,user.lastname,user.email,user.whatsapp')
->join('user','order.user_id = user.id','LEFT')
->where('(order.jointype = 1 and order.order_status = 10) or (order.jointype = 2 and order.order_status = 0)') //已支付的 或 候补单
->where($map)->order('order.id asc')->select();
//把教练也加入到发送列表里
$coach_sender = [
'firstname' => $slot['coach_nickname'],
'lastname' => '',
'email' => $slot['coach_email'],
'whatsapp' => $slot['coach_whatsapp'],
];
$order_list[] = $coach_sender;
if(empty($order_list)){
return true;
}
//给这些预约单的用户发邮件
try {
$obj = new Email();
foreach($order_list as $order){
$message =
'Hi,'.$order['firstname']. ' ' .$order['lastname'].'!
We regret to inform you that the following class has been cancelled.
Class:'.$slot['lesson_name_en'].'
Date: '.date('d F Y',$slot['starttime']).'
Time: '.date('H:i a',$slot['starttime']).'
Thank you for your kind understanding and look forward to seeing you in our studio soon!❤
Best Regards,
Elin Dance Studio
';
if(!empty($order['email'])){
$result = $obj
->to($order['email'])
->subject('Class is Cancelled!')
->message($message)
->send();
}
//发whatsapp
$parameters = [
[
'type' => 'text',
'text' => $order['firstname'].' '.$order['lastname'],
],
[
'type' => 'text',
'text' => $slot['lesson_name_en'],
],
[
'type' => 'text',
'text' => date('D l Y',$slot['starttime']) .' at '.date('H:i a',$slot['starttime']),
],
];
if(!empty($order['whatsapp'])){
$this->whatapp($order['whatsapp'],'class_cancelled','en_US',$parameters);
}
}
} catch (Exception $e) {
}
return true;
}
/**
* 复制本周的课程表
*/
public function copyweek(){
exit;
$starttime = strtotime('this week Monday'); // 获取本周一的时间戳
$endtime = $starttime + 86400*7;
$list = Db::name('lesson_slot')->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['bookednum'] = 0;
$val['status'] = 0;
$val['notice_status'] = 0;
$val['finishtime'] = 0;
$val['cancel_reason'] = '';
$val['cancel_time'] = 0;
$val['is_show'] = 0;
$val['cancel_notice_status'] = 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*/);
if(empty($data['starttime'])){
$data['starttime'] = time();
}
$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*/);
if(empty($data['starttime'])){
$data['starttime'] = time();
}
$data['endtime'] = $data['starttime'] + ($data['hours'] * 3600);
Db::name('lesson_slot')->where('id',$id)->update($data);
$this->result('',1,'修改完成','json');
}
//发送whatapp消息的方法
private function whatapp($receive_mobile,$template,$code,$parameters){
if(empty($receive_mobile)){return true;}
$token = config('site.whatsapp_token');
//发送者
$mobile_id = '337736419413019'; //Elin Dance Stuido 2:+65 8015 4154 , WhatsApp Business Account ID: 336509229537586
//发送
$url = 'https://graph.facebook.com/v19.0/'.$mobile_id.'/messages';
$header = [
'Authorization: Bearer ' . $token,
'Content-Type: application/json',
];
$body = [
'messaging_product' => 'whatsapp',
'recipient_type' => 'individual',
'to' => $receive_mobile,
'type' => 'template',
'template' => [
'name' => $template,
'language' => [
'code' => $code
],
'components' => [
[
'type' => 'body',
'parameters' => $parameters
]
],
],
];
$body = json_encode($body);
$rs = curl_post($url,$body,$header);
return true;
}
}