Lessonslot.php 30 KB

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