Lessonslot.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  47. $list = $this->model
  48. ->with(['coach','lesson','danceroom'])
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. foreach ($list as $row) {
  53. $row->getRelation('coach')->visible(['nickname']);
  54. $row->getRelation('lesson')->visible(['name','name_en','type']);
  55. $row->getRelation('danceroom')->visible(['name','name_en']);
  56. }
  57. $result = array("total" => $list->total(), "rows" => $list->items());
  58. return json($result);
  59. }
  60. return $this->view->fetch();
  61. }
  62. /**
  63. * 预约
  64. */
  65. public function booking(){
  66. $id = input('id');
  67. $info = Db::name('lesson_slot')->where('id',$id)->find();
  68. if(!$info){
  69. $this->error('请刷新重试');
  70. }
  71. if($info['status'] != 0){
  72. $this->error('当前课程不能预约');
  73. }
  74. if($this->request->isPost()){
  75. $user_id = input('user_id',0,'intval');
  76. //来自接口
  77. $slot_id = input('slot_id',0,'intval');
  78. $number = input('number',1,'intval');
  79. $remark = input('remark','','trim');
  80. $paytype = 1;//支付类型:1=课程套餐,2=线上付款,3=购买套餐中,4=试课单
  81. $packageorder_id = input('packageorder_id',0,'intval');
  82. if($number <= 0){
  83. $this->error('预约人数错误');
  84. }
  85. //课时
  86. $info = Db::name('lesson_slot')->alias('slot')
  87. ->field('slot.*,lesson.name,lesson.name_en,lesson.image,lesson.price')
  88. ->join('lesson','slot.lesson_id = lesson.id','LEFT')
  89. ->where('slot.id',$slot_id)->where('slot.status',0)->find();
  90. if(empty($info)){
  91. $this->error('课程可能已取消,请刷新重试');
  92. }
  93. if($info['endtime'] < time()){
  94. $this->error('课程已经结束了,不能再进行预约');
  95. }
  96. //报名人数不能超限
  97. $pay_number = Db::name('lesson_order')->where('slot_id',$slot_id)->where('order_status',10)->sum('usernumber');
  98. $num_remain = $info['num_max'] - $pay_number;
  99. if($num_remain < 0){
  100. $num_remain = 0;
  101. }
  102. if($num_remain < $number){
  103. //$this->error(__('预约名额只剩N名',['number'=>$num_remain]));
  104. }
  105. $number_hours = bcmul($number,$info['hours'],1);//每日课时按小时扣
  106. $lesson_order = [
  107. 'order_no' => createUniqueNo('S',$user_id),
  108. 'user_id' => $user_id,
  109. 'slot_id' => $slot_id,
  110. 'lesson_id' => $info['lesson_id'],
  111. 'order_amount' => bcmul($info['price'],$number,2),
  112. 'order_status' => 0,
  113. 'paytype' => $paytype, //支付类型:1=课程套餐,2=线上付款,3=购买套餐中,4=试课单
  114. 'paytime' => 0,
  115. 'createtime' => time(),
  116. // 'updatetime' => ,
  117. // 'finishtime' => ,
  118. 'usernumber' => $number,
  119. 'usernumber_hours' => $number_hours,
  120. 'userremark' => $remark,
  121. 'package_order_id' => 0,
  122. 'package_remark' => '',// 比如:5/30,5-7/30
  123. ];
  124. //
  125. Db::startTrans();
  126. if($paytype == 1){
  127. //检查已选择套餐
  128. $map = [
  129. 'user_id' =>$user_id,
  130. 'endtime' => ['gt',time()],
  131. 'remain' => ['gt',0],
  132. 'order_status' => 1,
  133. // 'use_status' => 1, //已激活的
  134. 'id' => $packageorder_id,
  135. ];
  136. $package_order = Db::name('package_order')->where($map)->where('find_in_set(:lesson_ids,lesson_ids)', ['lesson_ids' => $info['lesson_id']])->lock(true)->find();
  137. if(!$package_order){
  138. Db::rollback();
  139. $this->error('配套信息不正确,请刷新重试');
  140. }
  141. //课时不足支撑报名人数
  142. if($package_order['remain'] < $number_hours){
  143. Db::rollback();
  144. $this->error('该配套余额不足,可以使用其他配套');
  145. }
  146. if($package_order['use_status'] != 1){
  147. Db::rollback();
  148. $this->error('该配套尚未激活');
  149. }
  150. //扣除一节
  151. $update = [
  152. 'remain' => bcsub($package_order['remain'],$number_hours,1),
  153. 'updatetime' => time(),
  154. ];
  155. $rs1 = Db::name('package_order')->where('id',$packageorder_id)->update($update);
  156. if($rs1 === false){
  157. Db::rollback();
  158. $this->error('扣除套餐余额失败');
  159. }
  160. //修改预约单数据
  161. $lesson_order['order_amount'] = 0;
  162. $lesson_order['order_status'] = 10;
  163. $lesson_order['paytime'] = time();
  164. $lesson_order['package_order_id'] = $packageorder_id;
  165. $lesson_order['package_remark'] = ($package_order['sessions'] - $package_order['remain']) . '-' . ($package_order['sessions'] - $package_order['remain'] + $number_hours) .'/'. $package_order['sessions'];
  166. //预约单写入
  167. $lesson_order_id = Db::name('lesson_order')->insertGetId($lesson_order);
  168. if(!$lesson_order_id){
  169. Db::rollback();
  170. $this->error('预约失败');
  171. }
  172. //更新已预约人数
  173. $pay_number = Db::name('lesson_order')->where('slot_id',$slot_id)->where('order_status',10)->sum('usernumber');
  174. $rs_slot = Db::name('lesson_slot')->where('id',$slot_id)->update(['bookednum' => $pay_number]);
  175. if($rs_slot === false){
  176. Db::rollback();
  177. $this->error('预约失败');
  178. }
  179. Db::commit();
  180. $this->success('预约成功');
  181. }
  182. }
  183. $this->view->assign('row',$info);
  184. return $this->view->fetch();
  185. }
  186. /**
  187. * 取消
  188. */
  189. public function cancel(){
  190. $id = input('id');
  191. $info = Db::name('lesson_slot')->where('id',$id)->find();
  192. if(!$info){
  193. $this->error('请刷新重试');
  194. }
  195. if($info['status'] != 0){
  196. $this->error('当前课程不能取消');
  197. }
  198. if($this->request->isPost()){
  199. $remark = input('remark','');
  200. $cancel_reason = input('cancel_reason','');
  201. $cancel_time = time();
  202. Db::startTrans();
  203. $update = [
  204. 'status' => 30,
  205. 'remark' => $remark,
  206. 'cancel_reason' => $cancel_reason,
  207. 'cancel_time' => $cancel_time,
  208. 'bookednum' => 0,
  209. ];
  210. $rs1 = Db::name('lesson_slot')->where('id',$id)->where('status',0)->update($update);
  211. if($rs1 === false){
  212. Db::rollback();
  213. $this->error('取消失败,请刷新重试');
  214. }
  215. // $lesson_info = Db::name('lesson')->where('id',$info['lesson_id'])->find();
  216. //找到所有的已报名订单
  217. $lesson_order_list = Db::name('lesson_order')->where('slot_id',$id)->where('order_status',10)->lock(true)->select();
  218. if(!empty($lesson_order_list)){
  219. foreach($lesson_order_list as $lesson_order){
  220. //套餐给加回去
  221. if($lesson_order['paytype'] == 1){
  222. $package_order = Db::name('package_order')->where('id',$lesson_order['package_order_id'])->lock(true)->find();
  223. $update = [
  224. 'remain' => bcadd($package_order['remain'],$lesson_order['usernumber_hours'],1),
  225. 'updatetime' => time(),
  226. ];
  227. $rs_remain = Db::name('package_order')->where('id',$lesson_order['package_order_id'])->update($update);
  228. if($rs_remain === false){
  229. Db::rollback();
  230. $this->error('取消失败');
  231. }
  232. }
  233. //试课给改回去
  234. if($lesson_order['paytype'] == 4){
  235. $update = [
  236. 'order_status' => 10,
  237. 'updatetime' => time(),
  238. 'lesson_order_id' => 0,
  239. ];
  240. $rs_remain = Db::name('trylesson_order')->where('id',$lesson_order['trylesson_order_id'])->update($update);
  241. if($rs_remain === false){
  242. Db::rollback();
  243. $this->error('取消失败');
  244. }
  245. }
  246. //现金支付不给退,线下处理
  247. //取消预约单
  248. $update = [
  249. 'order_status' => 30,
  250. 'cancel_time' => $cancel_time,
  251. 'cancel_reason' => $cancel_reason,
  252. ];
  253. if($lesson_order['paytype'] == 1 || $lesson_order['paytype'] == 4){
  254. $update['order_status'] = 40;
  255. }
  256. $rs = Db::name('lesson_order')->where('id',$lesson_order['id'])->update($update);
  257. if($rs === false){
  258. Db::rollback();
  259. $this->error('取消失败');
  260. }
  261. }
  262. }
  263. Db::commit();
  264. $comefrom = input('comefrom','');
  265. if($comefrom == 'backend'){
  266. //后台来的
  267. $this->success('取消完成');
  268. }else{
  269. //接口来的
  270. $this->result('',1,'取消完成','json');
  271. }
  272. }
  273. $this->view->assign('row',$info);
  274. return $this->view->fetch();
  275. }
  276. /**
  277. * 复制本周的课程表
  278. */
  279. public function copyweek(){
  280. exit;
  281. $starttime = strtotime('this week Monday'); // 获取本周一的时间戳
  282. $endtime = $starttime + 86400*7;
  283. $list = Db::name('lesson_slot')->where('is_show',1)->where('starttime','BETWEEN',[$starttime,$endtime])->order('starttime asc')->select();
  284. if(empty($list)){
  285. $this->error('本周还没有课程表');
  286. }
  287. foreach($list as $key => &$val){
  288. unset($val['id']);
  289. $val['starttime'] = $val['starttime'] + 86400*7;
  290. $val['endtime'] = $val['endtime'] + 86400*7;
  291. $val['status'] = 0;
  292. $val['notice_status'] = 0;
  293. $val['finishtime'] = 0;
  294. $val['cancel_reason'] = '';
  295. $val['cancel_time'] = 0;
  296. }
  297. Db::name('lesson_slot')->insertAll($list);
  298. $this->success('已复制到下周');
  299. }
  300. /**
  301. * 查看
  302. */
  303. public function vue_index()
  304. {
  305. $where = [
  306. 'slot.status' => ['NEQ',30],
  307. ];
  308. $coach_id = input('coach_id','');
  309. $lesson_id = input('lesson_id','');
  310. $danceroom_id = input('danceroom_id',0);
  311. $bookstatus = input('bookstatus',0);
  312. $starttime = input('starttime',0);//默认看今天
  313. $endtime = input('endtime',0);
  314. if(empty($starttime) || empty($endtime)){
  315. $starttime = strtotime(date('Y-m-d'));
  316. $endtime = $starttime + 86399;
  317. }
  318. if(!empty($coach_id)){
  319. $where['slot.coach_ids'] = ['IN',$coach_id];
  320. }
  321. if(!empty($lesson_id)){
  322. $where['slot.lesson_id'] = ['IN',$lesson_id];
  323. }
  324. if(!empty($danceroom_id)){
  325. $where['slot.danceroom_id'] = $danceroom_id;
  326. }
  327. $where['slot.starttime'] = ['BETWEEN',[$starttime,$endtime]];
  328. $wherenew = '';
  329. if(!empty($bookstatus)){
  330. if($bookstatus == 1){
  331. $wherenew = 'slot.bookednum = 0';
  332. }
  333. if($bookstatus == 2){
  334. $wherenew = 'slot.bookednum != 0 and slot.bookednum < num_max';
  335. }
  336. if($bookstatus == 3){
  337. $wherenew = 'slot.bookednum = num_max';
  338. }
  339. }
  340. $field = [
  341. 'slot.id','slot.starttime','slot.endtime','slot.num_max','slot.bookednum','coach_ids',
  342. 'coach.nickname as Staff','coach.bgcolor',
  343. 'lesson.name_en as title',
  344. 'danceroom.name_en as Location',
  345. ];
  346. $list = Db::name('lesson_slot')->alias('slot')
  347. ->join('coach','slot.coach_ids = coach.id','LEFT')
  348. ->join('lesson','slot.lesson_id = lesson.id','LEFT')
  349. ->join('danceroom','slot.danceroom_id = danceroom.id','LEFT')
  350. ->field($field)
  351. ->where($where)
  352. ->where($wherenew)
  353. ->order('slot.id desc')
  354. ->select();
  355. if(!empty($list)){
  356. foreach($list as $key => $val){
  357. $val['resourceId'] = $val['coach_ids'];
  358. $val['start'] = date('Y-m-d H:i',$val['starttime']);
  359. $val['end'] = date('Y-m-d H:i',$val['endtime']);
  360. //无人约
  361. $val['backgroundColor'] = '#ffffff';
  362. $val['borderColor'] = $val['bgcolor'];
  363. $val['textColor'] = '#333333';
  364. //有人约
  365. if($val['bookednum'] > 0){
  366. $val['backgroundColor'] = $val['bgcolor'];
  367. $val['borderColor'] = $val['bgcolor'];
  368. $val['textColor'] = '#ffffff';
  369. }
  370. //详情
  371. $val['extendedProps'] = [
  372. 'Session' => $val['title'],
  373. 'time' => date('D, M d Y, H:i',$val['starttime']).' - '.date('H:i',$val['endtime']),
  374. 'Staff' => $val['Staff'],
  375. 'Location' => $val['Location'],
  376. 'Participar' => $val['bookednum'].'/'.$val['num_max'].' participants',
  377. 'id' => $val['id'],
  378. ];
  379. $list[$key] = $val;
  380. }
  381. }
  382. $this->result($list,1,'success','json');
  383. }
  384. //教练表头
  385. public function vue_staff()
  386. {
  387. $coach_id = input('coach_id','');
  388. //教练列表
  389. $coach_map = [];
  390. if(!empty($coach_id)){
  391. $coach_map['id'] = ['IN',$coach_id];
  392. }
  393. $coach_list = Db::name('coach')->where($coach_map)->order('id desc')->select();
  394. $result = [];
  395. if(!empty($coach_list)){
  396. foreach($coach_list as $ckey => $cval){
  397. $result[] = [
  398. 'id' => $cval['id'],
  399. 'title' => $cval['nickname'],
  400. 'extendedProps' => [
  401. 'name' => $cval['nickname'],
  402. 'avatar' => localpath_to_netpath($cval['avatar']),
  403. ],
  404. ];
  405. }
  406. }
  407. $this->result($result,1,'success','json');
  408. }
  409. //课程新增
  410. public function slot_add(){
  411. $field = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','address','remark','is_show'];
  412. $require = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','is_show'];
  413. $data = request_post_hub($field,$require);
  414. $data['endtime'] = $data['starttime'] + ($data['hours'] * 3600);
  415. $id = Db::name('lesson_slot')->insertGetId($data);
  416. $this->result($id,1,'添加完成','json');
  417. }
  418. //课程详情
  419. public function slot_info(){
  420. $id = input('id',0);
  421. $info = Db::name('lesson_slot')->where('id',$id)->find();
  422. $name = Db::name('lesson')->where('id',$info['lesson_id'])->value('name_en');
  423. $info['lesson_name'] = $name;
  424. $this->result($info,1,'success','json');
  425. }
  426. //课程编辑
  427. public function slot_edit(){
  428. $id = input('id',0);
  429. $field = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','address','remark','is_show'];
  430. $require = ['starttime','hours','num_min','num_max','waitnum_max','coach_ids','lesson_id','danceroom_id','is_show'];
  431. $data = request_post_hub($field,$require);
  432. $data['endtime'] = $data['starttime'] + ($data['hours'] * 3600);
  433. Db::name('lesson_slot')->where('id',$id)->update($data);
  434. $this->result('',1,'修改完成','json');
  435. }
  436. }