123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- <?php
- namespace app\api\controller\coach;
- use app\common\controller\Apic;
- use think\Db;
- /**
- * 售课
- */
- class Lesson extends Apic
- {
- // 无需登录的接口,*表示全部
- protected $noNeedLogin = [];
- // 无需鉴权的接口,*表示全部
- protected $noNeedRight = ['*'];
- //售课分类列表
- public function lesson_cate(){
- $result = [
- ['id'=>0,'name'=>'全部','name_en'=>'ALL']
- ];
- $list = Db::name('lesson_cate')->order('id asc')->select();
- foreach($list as $key => $val){
- $result[] = $val;
- }
- $result = $this->list_lang($result,['name']);
- $this->success(1,$result);
- }
- //课时首页
- public function slot_list(){
- $date = input('date',date('Y-m-d'),'strtotime');
- $where = [
- 'slot.starttime' => ['BETWEEN',[$date,$date+86399]],
- 'slot.is_show' => 1,
- ];
- //课时
- $list = 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($where);
- $list->where('find_in_set(:coach_ids,coach_ids)', ['coach_ids' => $this->auth->id]);
- $list = $list->order('slot.starttime asc')->select();
- if(empty($list)){
- $this->success(1,[]);
- }
- $list = list_domain_image($list,['image']);
- $list = $this->list_lang($list,['name']);
- foreach($list as $key => &$slot){
- //hours转换
- $slot['hours'] = floatval($slot['hours']);
- //已报名数量,(包含已支付,已点名,没有冲突)
- $pay_number = Db::name('lesson_order')->alias('order')
- ->field('order.id,user.avatar,order.usernumber')
- ->join('user','order.user_id = user.id','LEFT')
- ->where('order.slot_id',$slot['id'])
- ->where('order.order_status','IN',[10,20])
- ->select();
- $pay_number = list_domain_image($pay_number,['avatar']);
- $slot['usernumber'] = array_sum(array_column($pay_number,'usernumber'));
- $slot['useravatar'] = array_column($pay_number,'avatar');
- }
- $this->success(1,$list);
- }
- //课时列表首页
- //根据年,月获取当月剩余日期,有课程的标记1
- public function slot_index(){
- $getdate = input('date',date('Y-m'));
- if(empty($getdate)){
- $getdate = date('Y-m');
- }
- $year = substr($getdate,0,4);
- $month = substr($getdate,-2,2);
- //今天是哪日
- if($year == date('Y') && $month == date('m')){
- //如果是今年今月,默认今天开始
- $startday = date('d');
- }else{
- //从1号开始
- $startday = '01';
- }
- //一周
- $week_arr = [
- 1 => '周一',
- 2 => '周二',
- 3 => '周三',
- 4 => '周四',
- 5 => '周五',
- 6 => '周六',
- 7 => '周日',
- ];
- //本月结束日
- $endday_arr = [
- '01' => 31,
- '02' => 28,
- '03' => 31,
- '04' => 30,
- '05' => 31,
- '06' => 30,
- '07' => 31,
- '08' => 31,
- '09' => 30,
- '10' => 31,
- '11' => 30,
- '12' => 31,
- ];
- if($year%4 == 0){
- $endday_arr['02'] = 29;
- }
- $endday = $endday_arr[$month];
- //拿数据
- $startdate = $getdate.'-'.$startday;
- $enddate = $getdate.'-'.$endday;
- $where = [
- 'starttime' => ['BETWEEN',[strtotime($startdate),strtotime($enddate)+86399]],
- 'status' => 0,
- 'is_show' => 1,
- ];
- //dump($where);exit;
- $slots = Db::name('lesson_slot')
- ->where($where)
- ->where('find_in_set(:coach_ids,coach_ids)', ['coach_ids' => $this->auth->id])
- ->field('id,starttime')->select();
- $date_arr = [];
- for($i=intval($startday);$i<=$endday;$i++){
- $j = $i;
- if($j < 10){
- $j = 0 . $j;
- }
- $idate = $year.'-'.$month.'-'.$j;
- $itime = strtotime($idate);
- $week = $this->lang == 'en' ? date('D',$itime) : $week_arr[date('N',$itime)];
- $i_data = [
- 'yeah' =>$year,
- 'month'=>$month,
- 'day' => $j.'',
- 'date' => $idate,
- 'week' => $week,
- 'is_today' => 0,
- 'slot_num' => 0,
- ];
- foreach($slots as $slot){
- if(date('Y-m-d',$slot['starttime']) == $idate)
- {
- $i_data['slot_num'] += 1;
- }
- }
- if($idate == date('Y-m-d')){
- $i_data['is_today'] = 1;
- }
- $date_arr[] = $i_data;
- }
- $this->success(1,$date_arr);
- }
- //课时详情
- public function slot_info(){
- $slot_id = input('slot_id',0);
- //课时
- $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)->find();
- $info = info_domain_image($info,['image']);
- $info = $this->info_lang($info,['name']);
- //hours转换
- $info['hours'] = floatval($info['hours']);
- //已报名数量,(包含已支付,已点名,没有冲突)
- $pay_number = Db::name('lesson_order')->alias('order')
- ->field('order.id,user.firstname,user.lastname,user.nickname,user.avatar,order.usernumber,order.usernumber_sign')
- ->join('user','order.user_id = user.id','LEFT')
- ->where('order.slot_id',$info['id'])
- ->where('order.order_status','IN',[10,20])
- ->select();
- $pay_number = list_domain_image($pay_number,['avatar']);
- foreach($pay_number as $key => $val){
- $pay_number[$key]['showname'] = $val['firstname'].' '.$val['lastname'];
- }
- $info['usernumber'] = array_sum(array_column($pay_number,'usernumber'));
- $info['user_list'] = $pay_number;
- $this->success(1,$info);
- }
- //课时申请报名
- public function slot_sign(){
- if(!$this->apiLimit()){
- $this->error(__('点击太频繁,休息一下吧'));
- }
- $slot_id = input('slot_id',0,'intval');
- $signdata = input('signdata','','htmlspecialchars_decode');
- $signdata = json_decode($signdata,true);
- //测试
- /*$signdata = [
- 6 => 1,
- 7 => 2,
- ];*/
- //检查课时状态
- $slot_info = Db::name('lesson_slot')->where('id',$slot_id)->find();
- if($slot_info['status'] == 20){
- $this->error('此课程已经点过名了');
- }
- if($slot_info['status'] != 0){
- $this->error('此课程不能提交点名');
- }
- //检查报名单数据
- //逐个检查报名单,对比数量和状态。并保存
- Db::startTrans();
- $order_list = Db::name('lesson_order')->where('slot_id',$slot_id)->where('order_status',10)->order('id asc')->lock(true)->select();
- if(count($order_list) != count($signdata)){
- Db::rollback();
- $this->error('点名数据错误');
- }
- if(empty($order_list)){
- Db::rollback();
- $this->error('无人报名,请直接取消课程');
- }
- foreach($order_list as $order){
- if(!isset($signdata[$order['id']])){
- Db::rollback();
- $this->error('点名数据错误');
- }
- $usernumber_sign = $signdata[$order['id']];
- if($usernumber_sign < 0 || $usernumber_sign > $order['usernumber']){
- Db::rollback();
- $this->error('点名数据错误');
- }
- $update = [
- 'usernumber_sign' => $usernumber_sign,
- 'order_status' => 20,
- 'finishtime' => time(),
- ];
- $rs_sign = Db::name('lesson_order')->where('id',$order['id'])->update($update);
- if($rs_sign === false){
- Db::rollback();
- $this->error('点名失败');
- }
- }
- //修改课时状态
- $update = [
- 'status' => 20,
- 'finishtime' => time(),
- ];
- $slot_rs = Db::name('lesson_slot')->where('id',$slot_id)->update($update);
- if($slot_rs === false){
- Db::rollback();
- $this->error('点名失败');
- }
- Db::commit();
- $this->success('点名完成');
- }
- //我的课时记录,点过名的
- public function my_slot_history(){
- $date = strtotime(input('date',date('Y-m-01')));//月初
- $date_end = strtotime('+1 month', $date) - 1;//月末
- $cate_id = input('cate_id',0);
- $lesson_type = input('lesson_type',0);//私教课,普通课
- $where = [
- 'slot.starttime' => ['BETWEEN',[$date,$date_end]],
- 'slot.status' => 20,
- ];
- if($cate_id){
- $where['lesson.lessoncate_id'] = $cate_id;
- }
- if($lesson_type){
- $where['lesson.type'] = $lesson_type;
- }
- //课时
- $list = Db::name('lesson_slot')->alias('slot')
- ->field('slot.*,lesson.name,lesson.name_en')
- ->join('lesson','slot.lesson_id = lesson.id','LEFT')
- ->where($where);
- $list->where('find_in_set(:coach_ids,coach_ids)', ['coach_ids' => $this->auth->id]);
- $list = $list->order('slot.starttime asc')->select();
- $result = [
- 'list' => [],
- 'total' => 0,
- 'total_hours' => 0,
- ];
- if(empty($list)){
- $this->success(1,$result);
- }
- $list = $this->list_lang($list,['name']);
- $total_hours = 0;
- foreach($list as $key => &$slot){
- //hours转换
- $slot['hours'] = floatval($slot['hours']);
- //已报名数量
- $pay_number = Db::name('lesson_order')->where('slot_id',$slot['id'])->where('order_status',20)->sum('usernumber');
- $slot['usernumber'] = $pay_number;
- $total_hours = bcadd($total_hours,$slot['hours'],1);
- }
- $buttom = __('共计N节课,M小时',['total'=>count($list),'total_hours'=>$total_hours]);
- $result = [
- 'list' => $list,
- 'buttom' => $buttom,
- ];
- $this->success(1,$result);
- }
- }
|