Lessonslot.php 31 KB

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