Lessonslot.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  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. try {
  262. $message =
  263. 'Hi,'.$userinfo['firstname']. ' ' .$userinfo['lastname'].'!<br/>
  264. Your booking is confirmed and you’re all set now to fly with us! Here are the details of your booking:
  265. Class:'.$info['name_en'].'<br/>
  266. Instructor:'.$info['coach_nickname'].'<br/>
  267. Location: '. $info['danceroom_name_en'] .'<br/>
  268. Date: '.date('d F Y',$info['starttime']).'<br/>
  269. Time: '.date('H:i a',$info['starttime']).'<br/>
  270. Please arrive 10 minutes before the class timing to prepare for the harness fitting!<br/>
  271. We are located at 450 Alexandra Road, #02-01,<br/>
  272. 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/>
  273. We look forward to seeing you for an amazing session! ❤<br/>
  274. Best Regards,<br/>
  275. Elin Dance Studio<br/>
  276. <img src="'.config('website_url').'/assets/img/logo3.png" style="width:136px;height:115px">
  277. ';
  278. //给这些用户发邮件
  279. $obj = new Email();
  280. $result = $obj
  281. ->to($userinfo['email'])
  282. ->subject('You are now booked in for this class! ')
  283. ->message($message)
  284. ->send();
  285. $obj = '';
  286. //发whatsapp
  287. $parameters = [
  288. [
  289. 'type' => 'text',
  290. 'text' => $userinfo['firstname'].' '.$userinfo['lastname'],
  291. ],
  292. [
  293. 'type' => 'text',
  294. 'text' => $info['name_en'],
  295. ],
  296. [
  297. 'type' => 'text',
  298. 'text' => date('D l Y',$info['starttime']) .' at '.date('H:i a',$info['starttime']),
  299. ],
  300. [
  301. 'type' => 'text',
  302. 'text' => $info['danceroom_name_en'],
  303. ],
  304. ];
  305. $this->whatapp($userinfo['whatsapp'],'class_booked_confirmation','en_US',$parameters);
  306. } catch (Exception $e) {
  307. }
  308. //额外的通知
  309. $this->success('预约成功');
  310. }
  311. $this->view->assign('row',$info);
  312. return $this->view->fetch();
  313. }
  314. /**
  315. * 取消
  316. */
  317. public function cancel(){
  318. $id = input('id');
  319. $info = Db::name('lesson_slot')->where('id',$id)->find();
  320. if(!$info){
  321. $this->error('请刷新重试');
  322. }
  323. if($info['status'] != 0){
  324. $this->error('当前课程不能取消');
  325. }
  326. if($this->request->isPost()){
  327. $remark = input('remark','');
  328. $cancel_reason = input('cancel_reason','');
  329. $cancel_time = time();
  330. Db::startTrans();
  331. $info = Db::name('lesson_slot')->where('id',$id)->lock(true)->find();
  332. if(!$info){
  333. Db::rollback();
  334. $this->error('请刷新重试');
  335. }
  336. if($info['status'] != 0){
  337. Db::rollback();
  338. $this->error('当前课程不能取消');
  339. }
  340. //更新订单
  341. $update = [
  342. 'status' => 30,
  343. 'remark' => $remark,
  344. 'cancel_reason' => $cancel_reason,
  345. 'cancel_time' => $cancel_time,
  346. 'bookednum' => 0,
  347. ];
  348. $rs1 = Db::name('lesson_slot')->where('id',$id)->update($update);
  349. if($rs1 === false){
  350. Db::rollback();
  351. $this->error('取消失败,请刷新重试');
  352. }
  353. //发送通知
  354. $this->auto_lesson_slot_cancel($id);
  355. //找到所有的已候补订单
  356. $update = [
  357. 'order_status' => 30,
  358. 'cancel_time' => $cancel_time,
  359. 'cancel_reason' => $cancel_reason,
  360. ];
  361. $rs2 = Db::name('lesson_order')->where('slot_id',$id)->where('jointype',2)->where('order_status',0)->update($update);
  362. //找到所有的已报名订单
  363. $lesson_order_list = Db::name('lesson_order')->where('slot_id',$id)->where('jointype',1)->where('order_status',10)->lock(true)->select();
  364. if(!empty($lesson_order_list)){
  365. foreach($lesson_order_list as $lesson_order){
  366. //套餐给加回去
  367. if($lesson_order['paytype'] == 1){
  368. $package_order = Db::name('package_order')->where('id',$lesson_order['package_order_id'])->lock(true)->find();
  369. $update = [
  370. 'remain' => bcadd($package_order['remain'],$lesson_order['usernumber_hours'],1),
  371. 'updatetime' => time(),
  372. ];
  373. $rs_remain = Db::name('package_order')->where('id',$lesson_order['package_order_id'])->update($update);
  374. if($rs_remain === false){
  375. Db::rollback();
  376. $this->error('取消失败');
  377. }
  378. }
  379. //试课给改回去
  380. if($lesson_order['paytype'] == 4){
  381. $update = [
  382. 'order_status' => 10,
  383. 'updatetime' => time(),
  384. 'lesson_order_id' => 0,
  385. ];
  386. $rs_remain = Db::name('trylesson_order')->where('id',$lesson_order['trylesson_order_id'])->update($update);
  387. if($rs_remain === false){
  388. Db::rollback();
  389. $this->error('取消失败');
  390. }
  391. }
  392. //现金支付不给退,线下处理
  393. //取消预约单
  394. $update = [
  395. 'order_status' => 30,
  396. 'cancel_time' => $cancel_time,
  397. 'cancel_reason' => $cancel_reason,
  398. ];
  399. if($lesson_order['paytype'] == 1 || $lesson_order['paytype'] == 4){
  400. $update['order_status'] = 40;
  401. }
  402. $rs = Db::name('lesson_order')->where('id',$lesson_order['id'])->update($update);
  403. if($rs === false){
  404. Db::rollback();
  405. $this->error('取消失败');
  406. }
  407. }
  408. }
  409. Db::commit();
  410. $comefrom = input('comefrom','');
  411. if($comefrom == 'backend'){
  412. //后台来的
  413. $this->success('取消完成');
  414. }else{
  415. //接口来的
  416. $this->result('',1,'取消完成','json');
  417. }
  418. }
  419. $this->view->assign('row',$info);
  420. return $this->view->fetch();
  421. }
  422. //课程取消,通知。计划任务5分钟执行一次
  423. private function auto_lesson_slot_cancel($slot_id){
  424. $map = [
  425. 'slot.id' => $slot_id,
  426. ];
  427. $slot = Db::name('lesson_slot')->alias('slot')
  428. ->field('slot.*,coach.nickname as coach_nickname,coach.email as coach_email,coach.whatsapp as coach_whatsapp,lesson.name_en as lesson_name_en')
  429. ->join('coach','slot.coach_ids = coach.id','LEFT')
  430. ->join('lesson','slot.lesson_id = lesson.id','LEFT')
  431. ->where($map)->order('slot.starttime asc')->find();
  432. //找出这节课时的预约单
  433. $map = [
  434. 'order.slot_id' => $slot['id'],
  435. ];
  436. $order_list = Db::name('lesson_order')->alias('order')
  437. ->field('user.firstname,user.lastname,user.email,user.whatsapp')
  438. ->join('user','order.user_id = user.id','LEFT')
  439. ->where('(order.jointype = 1 and order.order_status = 10) or (order.jointype = 2 and order.order_status = 0)') //已支付的 或 候补单
  440. ->where($map)->order('order.id asc')->select();
  441. //把教练也加入到发送列表里
  442. $coach_sender = [
  443. 'firstname' => $slot['coach_nickname'],
  444. 'lastname' => '',
  445. 'email' => $slot['coach_email'],
  446. 'whatsapp' => $slot['coach_whatsapp'],
  447. ];
  448. $order_list[] = $coach_sender;
  449. if(empty($order_list)){
  450. return true;
  451. }
  452. //给这些预约单的用户发邮件
  453. try {
  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. $obj = new Email();
  468. $result = $obj
  469. ->to($order['email'])
  470. ->subject('Class is Cancelled!')
  471. ->message($message)
  472. ->send();
  473. $obj = '';
  474. }
  475. //发whatsapp
  476. $parameters = [
  477. [
  478. 'type' => 'text',
  479. 'text' => $order['firstname'].' '.$order['lastname'],
  480. ],
  481. [
  482. 'type' => 'text',
  483. 'text' => $slot['lesson_name_en'],
  484. ],
  485. [
  486. 'type' => 'text',
  487. 'text' => date('D l Y',$slot['starttime']) .' at '.date('H:i a',$slot['starttime']),
  488. ],
  489. ];
  490. if(!empty($order['whatsapp'])){
  491. $this->whatapp($order['whatsapp'],'class_cancelled','en_US',$parameters);
  492. }
  493. }
  494. } catch (Exception $e) {
  495. }
  496. return true;
  497. }
  498. /**
  499. * 复制本周的课程表
  500. */
  501. public function copyweek(){
  502. exit;
  503. $starttime = strtotime('this week Monday'); // 获取本周一的时间戳
  504. $endtime = $starttime + 86400*7;
  505. $list = Db::name('lesson_slot')->where('starttime','BETWEEN',[$starttime,$endtime])->order('starttime asc')->select();
  506. if(empty($list)){
  507. $this->error('本周还没有课程表');
  508. }
  509. foreach($list as $key => &$val){
  510. unset($val['id']);
  511. $val['starttime'] = $val['starttime'] + (86400*7);
  512. $val['endtime'] = $val['endtime'] + (86400*7);
  513. $val['bookednum'] = 0;
  514. $val['status'] = 0;
  515. $val['notice_status'] = 0;
  516. $val['finishtime'] = 0;
  517. $val['cancel_reason'] = '';
  518. $val['cancel_time'] = 0;
  519. $val['is_show'] = 0;
  520. $val['cancel_notice_status'] = 0;
  521. }
  522. Db::name('lesson_slot')->insertAll($list);
  523. $this->success('已复制到下周');
  524. }
  525. /**
  526. * 查看
  527. */
  528. public function vue_index()
  529. {
  530. $where = [
  531. 'slot.status' => ['NEQ',30],
  532. ];
  533. $coach_id = input('coach_id','');
  534. $lesson_id = input('lesson_id','');
  535. $danceroom_id = input('danceroom_id',0);
  536. $bookstatus = input('bookstatus',0);
  537. $starttime = input('starttime',0);//默认看今天
  538. $endtime = input('endtime',0);
  539. if(empty($starttime) || empty($endtime)){
  540. $starttime = strtotime(date('Y-m-d'));
  541. $endtime = $starttime + 86399;
  542. }
  543. if(!empty($coach_id)){
  544. $where['slot.coach_ids'] = ['IN',$coach_id];
  545. }
  546. if(!empty($lesson_id)){
  547. $where['slot.lesson_id'] = ['IN',$lesson_id];
  548. }
  549. if(!empty($danceroom_id)){
  550. $where['slot.danceroom_id'] = $danceroom_id;
  551. }
  552. $where['slot.starttime'] = ['BETWEEN',[$starttime,$endtime]];
  553. $wherenew = '';
  554. if(!empty($bookstatus)){
  555. if($bookstatus == 1){
  556. $wherenew = 'slot.bookednum = 0';
  557. }
  558. if($bookstatus == 2){
  559. $wherenew = 'slot.bookednum != 0 and slot.bookednum < num_max';
  560. }
  561. if($bookstatus == 3){
  562. $wherenew = 'slot.bookednum = num_max';
  563. }
  564. }
  565. $field = [
  566. 'slot.id','slot.starttime','slot.endtime','slot.num_max','slot.bookednum','coach_ids',
  567. 'coach.nickname as Staff','coach.bgcolor',
  568. 'lesson.name_en as title',
  569. 'danceroom.name_en as Location',
  570. ];
  571. $list = Db::name('lesson_slot')->alias('slot')
  572. ->join('coach','slot.coach_ids = coach.id','LEFT')
  573. ->join('lesson','slot.lesson_id = lesson.id','LEFT')
  574. ->join('danceroom','slot.danceroom_id = danceroom.id','LEFT')
  575. ->field($field)
  576. ->where($where)
  577. ->where($wherenew)
  578. ->order('slot.id desc')
  579. ->select();
  580. if(!empty($list)){
  581. foreach($list as $key => $val){
  582. $val['resourceId'] = $val['coach_ids'];
  583. $val['start'] = date('Y-m-d H:i',$val['starttime']);
  584. $val['end'] = date('Y-m-d H:i',$val['endtime']);
  585. //无人约
  586. $val['backgroundColor'] = '#ffffff';
  587. $val['borderColor'] = $val['bgcolor'];
  588. $val['textColor'] = '#333333';
  589. //有人约
  590. if($val['bookednum'] > 0){
  591. $val['backgroundColor'] = $val['bgcolor'];
  592. $val['borderColor'] = $val['bgcolor'];
  593. $val['textColor'] = '#ffffff';
  594. }
  595. //详情
  596. $val['extendedProps'] = [
  597. 'Session' => $val['title'],
  598. 'time' => date('D, M d Y, H:i',$val['starttime']).' - '.date('H:i',$val['endtime']),
  599. 'Staff' => $val['Staff'],
  600. 'Location' => $val['Location'],
  601. 'Participar' => $val['bookednum'].'/'.$val['num_max'].' participants',
  602. 'id' => $val['id'],
  603. ];
  604. $list[$key] = $val;
  605. }
  606. }
  607. $this->result($list,1,'success','json');
  608. }
  609. //教练表头
  610. public function vue_staff()
  611. {
  612. $coach_id = input('coach_id','');
  613. //教练列表
  614. $coach_map = [];
  615. if(!empty($coach_id)){
  616. $coach_map['id'] = ['IN',$coach_id];
  617. }
  618. $coach_list = Db::name('coach')->where($coach_map)->order('id desc')->select();
  619. $result = [];
  620. if(!empty($coach_list)){
  621. foreach($coach_list as $ckey => $cval){
  622. $result[] = [
  623. 'id' => $cval['id'],
  624. 'title' => $cval['nickname'],
  625. 'extendedProps' => [
  626. 'name' => $cval['nickname'],
  627. 'avatar' => localpath_to_netpath($cval['avatar']),
  628. ],
  629. ];
  630. }
  631. }
  632. $this->result($result,1,'success','json');
  633. }
  634. //课程新增
  635. public function slot_add(){
  636. $field = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','address','remark','address_en','remark_en','is_show'];
  637. // $require = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','is_show'];
  638. $data = request_post_hub($field/*,$require*/);
  639. if(empty($data['starttime'])){
  640. $data['starttime'] = time();
  641. }
  642. $data['endtime'] = $data['starttime'] + ($data['hours'] * 3600);
  643. $id = Db::name('lesson_slot')->insertGetId($data);
  644. $this->result($id,1,'添加完成','json');
  645. }
  646. //课程详情
  647. public function slot_info(){
  648. $id = input('id',0);
  649. $info = Db::name('lesson_slot')->where('id',$id)->find();
  650. $name = Db::name('lesson')->where('id',$info['lesson_id'])->value('name_en');
  651. $info['lesson_name'] = $name;
  652. $this->result($info,1,'success','json');
  653. }
  654. //课程编辑
  655. public function slot_edit(){
  656. $id = input('id',0);
  657. $field = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','address','remark','address_en','remark_en','is_show'];
  658. /*$require = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','is_show'];*/
  659. $data = request_post_hub($field/*,$require*/);
  660. if(empty($data['starttime'])){
  661. $data['starttime'] = time();
  662. }
  663. $data['endtime'] = $data['starttime'] + ($data['hours'] * 3600);
  664. Db::name('lesson_slot')->where('id',$id)->update($data);
  665. $this->result('',1,'修改完成','json');
  666. }
  667. //发送whatapp消息的方法
  668. private function whatapp($receive_mobile,$template,$code,$parameters){
  669. if(empty($receive_mobile)){return true;}
  670. $token = config('site.whatsapp_token');
  671. //发送者
  672. $mobile_id = '337736419413019'; //Elin Dance Stuido 2:+65 8015 4154 , WhatsApp Business Account ID: 336509229537586
  673. //发送
  674. $url = 'https://graph.facebook.com/v19.0/'.$mobile_id.'/messages';
  675. $header = [
  676. 'Authorization: Bearer ' . $token,
  677. 'Content-Type: application/json',
  678. ];
  679. $body = [
  680. 'messaging_product' => 'whatsapp',
  681. 'recipient_type' => 'individual',
  682. 'to' => $receive_mobile,
  683. 'type' => 'template',
  684. 'template' => [
  685. 'name' => $template,
  686. 'language' => [
  687. 'code' => $code
  688. ],
  689. 'components' => [
  690. [
  691. 'type' => 'body',
  692. 'parameters' => $parameters
  693. ]
  694. ],
  695. ],
  696. ];
  697. $body = json_encode($body);
  698. $rs = curl_post($url,$body,$header);
  699. return true;
  700. }
  701. }