Lessonslot.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use app\common\library\Email;
  6. /**
  7. * 每日课时
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Lessonslot extends Backend
  12. {
  13. protected $noNeedLogin = ['vue_index','vue_staff','slot_add','slot_info','slot_edit','cancel'];
  14. /**
  15. * Lessonslot模型对象
  16. * @var \app\admin\model\Lessonslot
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\Lessonslot;
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. $this->view->assign("noticeStatusList", $this->model->getNoticeStatusList());
  25. $this->view->assign("isShowList", $this->model->getIsShowList());
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. //当前是否为关联查询
  38. $this->relationSearch = true;
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if ($this->request->isAjax()) {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. //按周翻页
  47. $week_current = input('week_current','');
  48. if($week_current !== ''){
  49. $starttime = strtotime('this week Monday') + (86400*7*$week_current);
  50. $endtime = $starttime + (86400*7);
  51. $where = ['lessonslot.starttime'=>['between',[$starttime,$endtime]]];
  52. $limit = 999999;
  53. }else{
  54. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  55. }
  56. $list = $this->model
  57. ->with(['coach','lesson','danceroom'])
  58. ->where($where)
  59. // ->order($sort, $order)
  60. ->order('lessonslot.starttime desc,lessonslot.id desc')
  61. // ->select(false);echo $list;exit;
  62. ->paginate($limit);
  63. foreach ($list as $row) {
  64. $row->getRelation('coach')->visible(['nickname']);
  65. $row->getRelation('lesson')->visible(['name','name_en','type']);
  66. $row->getRelation('danceroom')->visible(['name','name_en']);
  67. }
  68. $items = $list->items();
  69. foreach($items as $key => $val){
  70. $items[$key]['week'] = date('l',$val['starttime']);
  71. }
  72. //上-周,本周,下-周
  73. if(empty($week_current)){$week_current = 0;}
  74. $this_week = 0;
  75. $last_week = $week_current-1;
  76. $next_week = $week_current+1;
  77. $extend = [
  78. 'this_week'=>$this_week,
  79. 'last_week'=>$last_week,
  80. 'next_week'=>$next_week,
  81. ];
  82. $result = array("total" => $list->total(), "rows" => $items,'extend'=>$extend);
  83. return json($result);
  84. }
  85. return $this->view->fetch();
  86. }
  87. /**
  88. * 预约
  89. */
  90. public function booking(){
  91. $id = input('id');
  92. $info = Db::name('lesson_slot')->where('id',$id)->find();
  93. if(!$info){
  94. $this->error('请刷新重试');
  95. }
  96. if($info['status'] != 0){
  97. $this->error('当前课程不能预约');//状态:0=报名中,20=已点名,30=已取消
  98. }
  99. if($this->request->isPost()){
  100. $user_id = input('user_id',0,'intval');
  101. //来自接口
  102. $slot_id = input('slot_id',0,'intval');
  103. $number = input('number',1,'intval');
  104. $remark = input('remark','','trim');
  105. $packageorder_id = input('packageorder_id',0,'intval');
  106. $trylessonorder_id = input('trylessonorder_id',0,'intval');
  107. $params = $this->request->post('row/a');
  108. $paytype = $params['paytype'];//支付类型:1=课程套餐,4=试课单
  109. if($number <= 0){
  110. $this->error('预约人数错误');
  111. }
  112. if($paytype == 4){
  113. $number = 1; //试课只能选一个人
  114. }
  115. if(empty($packageorder_id) && empty($trylessonorder_id)){
  116. $this->error('请选择任意一种支付方式');
  117. }
  118. //课时
  119. $info = Db::name('lesson_slot')->alias('slot')
  120. ->field('slot.*,lesson.name,lesson.name_en,lesson.image,lesson.price,coach.nickname as coach_nickname,danceroom.name_en as danceroom_name_en')
  121. ->join('lesson','slot.lesson_id = lesson.id','LEFT')
  122. ->join('coach','slot.coach_ids = coach.id','LEFT')
  123. ->join('danceroom','slot.danceroom_id = danceroom.id','LEFT')
  124. ->where('slot.id',$slot_id)->where('slot.status',0)->find();
  125. if(empty($info)){
  126. $this->error('课程可能已取消,请刷新重试');
  127. }
  128. if($info['endtime'] < time()){
  129. $this->error('课程已经结束了,不能再进行预约');
  130. }
  131. //报名人数不能超限
  132. $pay_number = Db::name('lesson_order')->where('slot_id',$slot_id)->where('order_status',10)->sum('usernumber');
  133. $num_remain = $info['num_max'] - $pay_number;
  134. if($num_remain < 0){
  135. $num_remain = 0;
  136. }
  137. if($num_remain < $number){
  138. //$this->error(__('预约名额只剩N名',['number'=>$num_remain]));
  139. }
  140. $number_hours = bcmul($number,$info['hours'],1);//每日课时按小时扣
  141. $lesson_order = [
  142. 'order_no' => createUniqueNo('S',$user_id),
  143. 'user_id' => $user_id,
  144. 'slot_id' => $slot_id,
  145. 'lesson_id' => $info['lesson_id'],
  146. 'order_amount' => bcmul($info['price'],$number,2),
  147. 'order_status' => 0,
  148. 'paytype' => $paytype, //支付类型:1=课程套餐,2=线上付款,3=购买套餐中,4=试课单
  149. 'paytime' => 0,
  150. 'createtime' => time(),
  151. // 'updatetime' => ,
  152. // 'finishtime' => ,
  153. 'usernumber' => $number,
  154. 'usernumber_hours' => $number_hours,
  155. 'userremark' => $remark,
  156. 'package_order_id' => 0,
  157. 'package_remark' => '',// 比如:5/30,5-7/30
  158. ];
  159. //
  160. Db::startTrans();
  161. if($paytype == 1){
  162. //检查已选择套餐
  163. $map = [
  164. 'user_id' =>$user_id,
  165. 'endtime' => ['gt',time()],
  166. 'remain' => ['gt',0],
  167. 'order_status' => 1,
  168. // 'use_status' => 1, //已激活的
  169. 'id' => $packageorder_id,
  170. ];
  171. $package_order = Db::name('package_order')->where($map)->where('find_in_set(:lesson_ids,lesson_ids)', ['lesson_ids' => $info['lesson_id']])->lock(true)->find();
  172. if(!$package_order){
  173. Db::rollback();
  174. $this->error('配套信息不正确,请刷新重试');
  175. }
  176. //课时不足支撑报名人数
  177. if($package_order['remain'] < $number_hours){
  178. Db::rollback();
  179. $this->error('该配套余额不足,可以使用其他配套');
  180. }
  181. if($package_order['use_status'] != 1){
  182. Db::rollback();
  183. $this->error('该配套尚未激活');
  184. }
  185. //扣除一节
  186. $update = [
  187. 'remain' => bcsub($package_order['remain'],$number_hours,1),
  188. 'updatetime' => time(),
  189. ];
  190. $rs1 = Db::name('package_order')->where('id',$packageorder_id)->update($update);
  191. if($rs1 === false){
  192. Db::rollback();
  193. $this->error('扣除套餐余额失败');
  194. }
  195. //修改预约单数据
  196. $lesson_order['order_amount'] = 0;
  197. $lesson_order['order_status'] = 10;
  198. $lesson_order['paytime'] = time();
  199. $lesson_order['package_order_id'] = $packageorder_id;
  200. $lesson_order['package_remark'] = ($package_order['sessions'] - $package_order['remain']) . '-' . ($package_order['sessions'] - $package_order['remain'] + $number_hours) .'/'. $package_order['sessions'];
  201. //预约单写入
  202. $lesson_order_id = Db::name('lesson_order')->insertGetId($lesson_order);
  203. if(!$lesson_order_id){
  204. Db::rollback();
  205. $this->error('预约失败');
  206. }
  207. //更新已预约人数
  208. $pay_number = Db::name('lesson_order')->where('slot_id',$slot_id)->where('order_status',10)->sum('usernumber');
  209. $rs_slot = Db::name('lesson_slot')->where('id',$slot_id)->update(['bookednum' => $pay_number]);
  210. if($rs_slot === false){
  211. Db::rollback();
  212. $this->error('预约失败');
  213. }
  214. }
  215. elseif($paytype == 4){
  216. //使用试课单
  217. $map = [
  218. 'id' => $trylessonorder_id,
  219. 'user_id' =>$user_id,
  220. 'endtime' => ['gt',time()],
  221. 'order_status' => 10,
  222. ];
  223. $trylesson_order = Db::name('trylesson_order')->where($map)->where('find_in_set(:lesson_ids,lesson_ids)', ['lesson_ids' => $info['lesson_id']])->lock(true)->find();
  224. if(!$trylesson_order){
  225. Db::rollback();
  226. $this->error('未找到对应的试课');
  227. }
  228. //修改预约单数据
  229. $lesson_order['order_amount'] = 0;
  230. $lesson_order['order_status'] = 10;
  231. $lesson_order['paytime'] = time();
  232. $lesson_order['trylesson_order_id'] = $trylessonorder_id;
  233. //预约单写入
  234. $lesson_order_id = Db::name('lesson_order')->insertGetId($lesson_order);
  235. if(!$lesson_order_id){
  236. Db::rollback();
  237. $this->error('预约失败');
  238. }
  239. //改掉试课单状态
  240. $update = [
  241. 'order_status' => 20,
  242. 'updatetime' => time(),
  243. 'lesson_order_id' => $lesson_order_id,
  244. ];
  245. $rs1 = Db::name('trylesson_order')->where('id',$trylessonorder_id)->update($update);
  246. if($rs1 === false){
  247. Db::rollback();
  248. $this->error('使用试课失败');
  249. }
  250. //更新已预约人数
  251. $pay_number = Db::name('lesson_order')->where('slot_id',$slot_id)->where('order_status',10)->sum('usernumber');
  252. $rs_slot = Db::name('lesson_slot')->where('id',$slot_id)->update(['bookednum' => $pay_number]);
  253. if($rs_slot === false){
  254. Db::rollback();
  255. $this->error('预约失败');
  256. }
  257. }
  258. Db::commit();
  259. //额外的通知
  260. $userinfo = Db::name('user')->where('id',$user_id)->find();
  261. $obj = new Email();
  262. try {
  263. $message =
  264. 'Hi,'.$userinfo['firstname']. ' ' .$userinfo['lastname'].'!<br/>
  265. Your booking is confirmed and you’re all set now to fly with us! Here are the details of your booking:
  266. Class:'.$info['name_en'].'<br/>
  267. Instructor:'.$info['coach_nickname'].'<br/>
  268. Location: '. $info['danceroom_name_en'] .'<br/>
  269. Date: '.date('d F Y',$info['starttime']).'<br/>
  270. Time: '.date('H:i a',$info['starttime']).'<br/>
  271. Please arrive 10 minutes before the class timing to prepare for the harness fitting!<br/>
  272. We are located at 450 Alexandra Road, #02-01,<br/>
  273. 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!<br/>
  274. We look forward to seeing you for an amazing session! ❤<br/>
  275. Best Regards,<br/>
  276. Elin Dance Studio<br/>
  277. <img src="'.config('website_url').'/assets/img/logo3.png" style="width:136px;height:115px">
  278. ';
  279. //给这些用户发邮件
  280. $result = $obj
  281. ->to($userinfo['email'])
  282. ->subject('You are now booked in for this class! ')
  283. ->message($message)
  284. ->send();
  285. //发whatsapp
  286. $parameters = [
  287. [
  288. 'type' => 'text',
  289. 'text' => $userinfo['firstname'].' '.$userinfo['lastname'],
  290. ],
  291. [
  292. 'type' => 'text',
  293. 'text' => $info['name_en'],
  294. ],
  295. [
  296. 'type' => 'text',
  297. 'text' => date('D l Y',$info['starttime']) .' at '.date('H:i a',$info['starttime']),
  298. ],
  299. [
  300. 'type' => 'text',
  301. 'text' => $info['danceroom_name_en'],
  302. ],
  303. ];
  304. $this->whatapp($userinfo['whatsapp'],'class_booked_confirmation','en_US',$parameters);
  305. } catch (Exception $e) {
  306. }
  307. //额外的通知
  308. $this->success('预约成功');
  309. }
  310. $this->view->assign('row',$info);
  311. return $this->view->fetch();
  312. }
  313. /**
  314. * 取消
  315. */
  316. public function cancel(){
  317. $id = input('id');
  318. $info = Db::name('lesson_slot')->where('id',$id)->find();
  319. if(!$info){
  320. $this->error('请刷新重试');
  321. }
  322. if($info['status'] != 0){
  323. $this->error('当前课程不能取消');
  324. }
  325. if($this->request->isPost()){
  326. $remark = input('remark','');
  327. $cancel_reason = input('cancel_reason','');
  328. $cancel_time = time();
  329. Db::startTrans();
  330. $info = Db::name('lesson_slot')->where('id',$id)->lock(true)->find();
  331. if(!$info){
  332. Db::rollback();
  333. $this->error('请刷新重试');
  334. }
  335. if($info['status'] != 0){
  336. Db::rollback();
  337. $this->error('当前课程不能取消');
  338. }
  339. //更新订单
  340. $update = [
  341. 'status' => 30,
  342. 'remark' => $remark,
  343. 'cancel_reason' => $cancel_reason,
  344. 'cancel_time' => $cancel_time,
  345. 'bookednum' => 0,
  346. ];
  347. $rs1 = Db::name('lesson_slot')->where('id',$id)->update($update);
  348. if($rs1 === false){
  349. Db::rollback();
  350. $this->error('取消失败,请刷新重试');
  351. }
  352. //发送通知
  353. $this->auto_lesson_slot_cancel($id);
  354. //找到所有的已候补订单
  355. $update = [
  356. 'order_status' => 30,
  357. 'cancel_time' => $cancel_time,
  358. 'cancel_reason' => $cancel_reason,
  359. ];
  360. $rs2 = Db::name('lesson_order')->where('slot_id',$id)->where('jointype',2)->where('order_status',0)->update($update);
  361. //找到所有的已报名订单
  362. $lesson_order_list = Db::name('lesson_order')->where('slot_id',$id)->where('jointype',1)->where('order_status',10)->lock(true)->select();
  363. if(!empty($lesson_order_list)){
  364. foreach($lesson_order_list as $lesson_order){
  365. //套餐给加回去
  366. if($lesson_order['paytype'] == 1){
  367. $package_order = Db::name('package_order')->where('id',$lesson_order['package_order_id'])->lock(true)->find();
  368. $update = [
  369. 'remain' => bcadd($package_order['remain'],$lesson_order['usernumber_hours'],1),
  370. 'updatetime' => time(),
  371. ];
  372. $rs_remain = Db::name('package_order')->where('id',$lesson_order['package_order_id'])->update($update);
  373. if($rs_remain === false){
  374. Db::rollback();
  375. $this->error('取消失败');
  376. }
  377. }
  378. //试课给改回去
  379. if($lesson_order['paytype'] == 4){
  380. $update = [
  381. 'order_status' => 10,
  382. 'updatetime' => time(),
  383. 'lesson_order_id' => 0,
  384. ];
  385. $rs_remain = Db::name('trylesson_order')->where('id',$lesson_order['trylesson_order_id'])->update($update);
  386. if($rs_remain === false){
  387. Db::rollback();
  388. $this->error('取消失败');
  389. }
  390. }
  391. //现金支付不给退,线下处理
  392. //取消预约单
  393. $update = [
  394. 'order_status' => 30,
  395. 'cancel_time' => $cancel_time,
  396. 'cancel_reason' => $cancel_reason,
  397. ];
  398. if($lesson_order['paytype'] == 1 || $lesson_order['paytype'] == 4){
  399. $update['order_status'] = 40;
  400. }
  401. $rs = Db::name('lesson_order')->where('id',$lesson_order['id'])->update($update);
  402. if($rs === false){
  403. Db::rollback();
  404. $this->error('取消失败');
  405. }
  406. }
  407. }
  408. Db::commit();
  409. $comefrom = input('comefrom','');
  410. if($comefrom == 'backend'){
  411. //后台来的
  412. $this->success('取消完成');
  413. }else{
  414. //接口来的
  415. $this->result('',1,'取消完成','json');
  416. }
  417. }
  418. $this->view->assign('row',$info);
  419. return $this->view->fetch();
  420. }
  421. //课程取消,通知。计划任务5分钟执行一次
  422. private function auto_lesson_slot_cancel($slot_id){
  423. $map = [
  424. 'slot.id' => $slot_id,
  425. ];
  426. $slot = Db::name('lesson_slot')->alias('slot')
  427. ->field('slot.*,coach.nickname as coach_nickname,coach.email as coach_email,coach.whatsapp as coach_whatsapp,lesson.name_en as lesson_name_en')
  428. ->join('coach','slot.coach_ids = coach.id','LEFT')
  429. ->join('lesson','slot.lesson_id = lesson.id','LEFT')
  430. ->where($map)->order('slot.starttime asc')->find();
  431. //找出这节课时的预约单
  432. $map = [
  433. 'order.slot_id' => $slot['id'],
  434. ];
  435. $order_list = Db::name('lesson_order')->alias('order')
  436. ->field('user.firstname,user.lastname,user.email,user.whatsapp')
  437. ->join('user','order.user_id = user.id','LEFT')
  438. ->where('(order.jointype = 1 and order.order_status = 10) or (order.jointype = 2 and order.order_status = 0)') //已支付的 或 候补单
  439. ->where($map)->order('order.id asc')->select();
  440. //把教练也加入到发送列表里
  441. $coach_sender = [
  442. 'firstname' => $slot['coach_nickname'],
  443. 'lastname' => '',
  444. 'email' => $slot['coach_email'],
  445. 'whatsapp' => $slot['coach_whatsapp'],
  446. ];
  447. $order_list[] = $coach_sender;
  448. if(empty($order_list)){
  449. return true;
  450. }
  451. //给这些预约单的用户发邮件
  452. try {
  453. $obj = new Email();
  454. foreach($order_list as $order){
  455. $message =
  456. 'Hi,'.$order['firstname']. ' ' .$order['lastname'].'!<br/>
  457. We regret to inform you that the following class has been cancelled.<br/>
  458. Class:'.$slot['lesson_name_en'].'<br/>
  459. Date: '.date('d F Y',$slot['starttime']).'<br/>
  460. Time: '.date('H:i a',$slot['starttime']).'<br/>
  461. Thank you for your kind understanding and look forward to seeing you in our studio soon!❤<br/>
  462. Best Regards,<br/>
  463. Elin Dance Studio<br/>
  464. <img src="'.config('website_url').'/assets/img/logo3.png" style="width:136px;height:115px">
  465. ';
  466. if(!empty($order['email'])){
  467. $result = $obj
  468. ->to($order['email'])
  469. ->subject('Class is Cancelled!')
  470. ->message($message)
  471. ->send();
  472. }
  473. //发whatsapp
  474. $parameters = [
  475. [
  476. 'type' => 'text',
  477. 'text' => $order['firstname'].' '.$order['lastname'],
  478. ],
  479. [
  480. 'type' => 'text',
  481. 'text' => $slot['lesson_name_en'],
  482. ],
  483. [
  484. 'type' => 'text',
  485. 'text' => date('D l Y',$slot['starttime']) .' at '.date('H:i a',$slot['starttime']),
  486. ],
  487. ];
  488. if(!empty($order['whatsapp'])){
  489. $this->whatapp($order['whatsapp'],'class_cancelled','en_US',$parameters);
  490. }
  491. }
  492. } catch (Exception $e) {
  493. }
  494. return true;
  495. }
  496. /**
  497. * 复制本周的课程表
  498. */
  499. public function copyweek(){
  500. exit;
  501. $starttime = strtotime('this week Monday'); // 获取本周一的时间戳
  502. $endtime = $starttime + 86400*7;
  503. $list = Db::name('lesson_slot')->where('starttime','BETWEEN',[$starttime,$endtime])->order('starttime asc')->select();
  504. if(empty($list)){
  505. $this->error('本周还没有课程表');
  506. }
  507. foreach($list as $key => &$val){
  508. unset($val['id']);
  509. $val['starttime'] = $val['starttime'] + (86400*7);
  510. $val['endtime'] = $val['endtime'] + (86400*7);
  511. $val['bookednum'] = 0;
  512. $val['status'] = 0;
  513. $val['notice_status'] = 0;
  514. $val['finishtime'] = 0;
  515. $val['cancel_reason'] = '';
  516. $val['cancel_time'] = 0;
  517. $val['is_show'] = 0;
  518. $val['cancel_notice_status'] = 0;
  519. }
  520. Db::name('lesson_slot')->insertAll($list);
  521. $this->success('已复制到下周');
  522. }
  523. /**
  524. * 查看
  525. */
  526. public function vue_index()
  527. {
  528. $where = [
  529. 'slot.status' => ['NEQ',30],
  530. ];
  531. $coach_id = input('coach_id','');
  532. $lesson_id = input('lesson_id','');
  533. $danceroom_id = input('danceroom_id',0);
  534. $bookstatus = input('bookstatus',0);
  535. $starttime = input('starttime',0);//默认看今天
  536. $endtime = input('endtime',0);
  537. if(empty($starttime) || empty($endtime)){
  538. $starttime = strtotime(date('Y-m-d'));
  539. $endtime = $starttime + 86399;
  540. }
  541. if(!empty($coach_id)){
  542. $where['slot.coach_ids'] = ['IN',$coach_id];
  543. }
  544. if(!empty($lesson_id)){
  545. $where['slot.lesson_id'] = ['IN',$lesson_id];
  546. }
  547. if(!empty($danceroom_id)){
  548. $where['slot.danceroom_id'] = $danceroom_id;
  549. }
  550. $where['slot.starttime'] = ['BETWEEN',[$starttime,$endtime]];
  551. $wherenew = '';
  552. if(!empty($bookstatus)){
  553. if($bookstatus == 1){
  554. $wherenew = 'slot.bookednum = 0';
  555. }
  556. if($bookstatus == 2){
  557. $wherenew = 'slot.bookednum != 0 and slot.bookednum < num_max';
  558. }
  559. if($bookstatus == 3){
  560. $wherenew = 'slot.bookednum = num_max';
  561. }
  562. }
  563. $field = [
  564. 'slot.id','slot.starttime','slot.endtime','slot.num_max','slot.bookednum','coach_ids',
  565. 'coach.nickname as Staff','coach.bgcolor',
  566. 'lesson.name_en as title',
  567. 'danceroom.name_en as Location',
  568. ];
  569. $list = Db::name('lesson_slot')->alias('slot')
  570. ->join('coach','slot.coach_ids = coach.id','LEFT')
  571. ->join('lesson','slot.lesson_id = lesson.id','LEFT')
  572. ->join('danceroom','slot.danceroom_id = danceroom.id','LEFT')
  573. ->field($field)
  574. ->where($where)
  575. ->where($wherenew)
  576. ->order('slot.id desc')
  577. ->select();
  578. if(!empty($list)){
  579. foreach($list as $key => $val){
  580. $val['resourceId'] = $val['coach_ids'];
  581. $val['start'] = date('Y-m-d H:i',$val['starttime']);
  582. $val['end'] = date('Y-m-d H:i',$val['endtime']);
  583. //无人约
  584. $val['backgroundColor'] = '#ffffff';
  585. $val['borderColor'] = $val['bgcolor'];
  586. $val['textColor'] = '#333333';
  587. //有人约
  588. if($val['bookednum'] > 0){
  589. $val['backgroundColor'] = $val['bgcolor'];
  590. $val['borderColor'] = $val['bgcolor'];
  591. $val['textColor'] = '#ffffff';
  592. }
  593. //详情
  594. $val['extendedProps'] = [
  595. 'Session' => $val['title'],
  596. 'time' => date('D, M d Y, H:i',$val['starttime']).' - '.date('H:i',$val['endtime']),
  597. 'Staff' => $val['Staff'],
  598. 'Location' => $val['Location'],
  599. 'Participar' => $val['bookednum'].'/'.$val['num_max'].' participants',
  600. 'id' => $val['id'],
  601. ];
  602. $list[$key] = $val;
  603. }
  604. }
  605. $this->result($list,1,'success','json');
  606. }
  607. //教练表头
  608. public function vue_staff()
  609. {
  610. $coach_id = input('coach_id','');
  611. //教练列表
  612. $coach_map = [];
  613. if(!empty($coach_id)){
  614. $coach_map['id'] = ['IN',$coach_id];
  615. }
  616. $coach_list = Db::name('coach')->where($coach_map)->order('id desc')->select();
  617. $result = [];
  618. if(!empty($coach_list)){
  619. foreach($coach_list as $ckey => $cval){
  620. $result[] = [
  621. 'id' => $cval['id'],
  622. 'title' => $cval['nickname'],
  623. 'extendedProps' => [
  624. 'name' => $cval['nickname'],
  625. 'avatar' => localpath_to_netpath($cval['avatar']),
  626. ],
  627. ];
  628. }
  629. }
  630. $this->result($result,1,'success','json');
  631. }
  632. //课程新增
  633. public function slot_add(){
  634. $field = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','address','remark','address_en','remark_en','is_show'];
  635. // $require = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','is_show'];
  636. $data = request_post_hub($field/*,$require*/);
  637. if(empty($data['starttime'])){
  638. $data['starttime'] = time();
  639. }
  640. $data['endtime'] = $data['starttime'] + ($data['hours'] * 3600);
  641. $id = Db::name('lesson_slot')->insertGetId($data);
  642. $this->result($id,1,'添加完成','json');
  643. }
  644. //课程详情
  645. public function slot_info(){
  646. $id = input('id',0);
  647. $info = Db::name('lesson_slot')->where('id',$id)->find();
  648. $name = Db::name('lesson')->where('id',$info['lesson_id'])->value('name_en');
  649. $info['lesson_name'] = $name;
  650. $this->result($info,1,'success','json');
  651. }
  652. //课程编辑
  653. public function slot_edit(){
  654. $id = input('id',0);
  655. $field = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','address','remark','address_en','remark_en','is_show'];
  656. /*$require = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','is_show'];*/
  657. $data = request_post_hub($field/*,$require*/);
  658. if(empty($data['starttime'])){
  659. $data['starttime'] = time();
  660. }
  661. $data['endtime'] = $data['starttime'] + ($data['hours'] * 3600);
  662. Db::name('lesson_slot')->where('id',$id)->update($data);
  663. $this->result('',1,'修改完成','json');
  664. }
  665. //发送whatapp消息的方法
  666. private function whatapp($receive_mobile,$template,$code,$parameters){
  667. if(empty($receive_mobile)){return true;}
  668. $token = config('site.whatsapp_token');
  669. //发送者
  670. $mobile_id = '337736419413019'; //Elin Dance Stuido 2:+65 8015 4154 , WhatsApp Business Account ID: 336509229537586
  671. //发送
  672. $url = 'https://graph.facebook.com/v19.0/'.$mobile_id.'/messages';
  673. $header = [
  674. 'Authorization: Bearer ' . $token,
  675. 'Content-Type: application/json',
  676. ];
  677. $body = [
  678. 'messaging_product' => 'whatsapp',
  679. 'recipient_type' => 'individual',
  680. 'to' => $receive_mobile,
  681. 'type' => 'template',
  682. 'template' => [
  683. 'name' => $template,
  684. 'language' => [
  685. 'code' => $code
  686. ],
  687. 'components' => [
  688. [
  689. 'type' => 'body',
  690. 'parameters' => $parameters
  691. ]
  692. ],
  693. ],
  694. ];
  695. $body = json_encode($body);
  696. $rs = curl_post($url,$body,$header);
  697. return true;
  698. }
  699. }