Lessonslot.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. /**
  14. * Lessonslot模型对象
  15. * @var \app\admin\model\Lessonslot
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Lessonslot;
  22. $this->view->assign("statusList", $this->model->getStatusList());
  23. $this->view->assign("noticeStatusList", $this->model->getNoticeStatusList());
  24. $this->view->assign("isShowList", $this->model->getIsShowList());
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. //当前是否为关联查询
  37. $this->relationSearch = true;
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if ($this->request->isAjax()) {
  41. //如果发送的来源是Selectpage,则转发到Selectpage
  42. if ($this->request->request('keyField')) {
  43. return $this->selectpage();
  44. }
  45. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  46. $list = $this->model
  47. ->with(['coach','lesson'])
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->paginate($limit);
  51. foreach ($list as $row) {
  52. $row->getRelation('coach')->visible(['nickname']);
  53. $row->getRelation('lesson')->visible(['name','name_en']);
  54. }
  55. $result = array("total" => $list->total(), "rows" => $list->items());
  56. return json($result);
  57. }
  58. return $this->view->fetch();
  59. }
  60. /**
  61. * 取消
  62. */
  63. public function cancel(){
  64. $id = input('id');
  65. $info = Db::name('lesson_slot')->where('id',$id)->where('status',0)->find();
  66. if(!$info){
  67. $this->error('请刷新重试');
  68. }
  69. if($this->request->isPost()){
  70. $remark = input('remark','');
  71. $cancel_reason = input('cancel_reason','');
  72. $cancel_time = strtotime(input('cancel_time',''));
  73. Db::startTrans();
  74. $update = [
  75. 'status' => 30,
  76. 'remark' => $remark,
  77. 'cancel_reason' => $cancel_reason,
  78. 'cancel_time' => $cancel_time
  79. ];
  80. $rs1 = Db::name('lesson_slot')->where('id',$id)->where('status',0)->update($update);
  81. if($rs1 === false){
  82. Db::rollback();
  83. $this->error('取消失败,请刷新重试');
  84. }
  85. //找到所有的已报名订单
  86. $lesson_order_list = Db::name('lesson_order')->where('slot_id',$id)->where('order_status',10)->lock(true)->select();
  87. if(!empty($lesson_order_list)){
  88. foreach($lesson_order_list as $lesson_order){
  89. //套餐给加回去
  90. if($lesson_order['paytype'] == 1){
  91. $package_order = Db::name('package_order')->where('id',$lesson_order['package_order_id'])->lock(true)->find();
  92. $update = [
  93. 'remain' => $package_order['remain'] + $lesson_order['usernumber'],
  94. 'updatetime' => time(),
  95. ];
  96. $rs_remain = Db::name('package_order')->where('id',$lesson_order['package_order_id'])->update($update);
  97. if($rs_remain === false){
  98. Db::rollback();
  99. $this->error('取消失败');
  100. }
  101. }
  102. if($lesson_order['paytype'] == 4){
  103. $update = [
  104. 'order_status' => 10,
  105. 'lesson_order_id' => 0,
  106. 'updatetime' => time(),
  107. ];
  108. $rs_remain = Db::name('trylesson_order')->where('id',$lesson_order['trylesson_order_id'])->update($update);
  109. if($rs_remain === false){
  110. Db::rollback();
  111. $this->error('取消失败');
  112. }
  113. }
  114. //现金支付不给退,线下处理
  115. //取消预约单
  116. $update = [
  117. 'order_status' => 30,
  118. 'cancel_time' => $cancel_time,
  119. 'cancel_reason' => $cancel_reason,
  120. ];
  121. if($lesson_order['paytype'] == 1){
  122. $update['order_status'] = 40;
  123. }
  124. $rs = Db::name('lesson_order')->where('id',$lesson_order['id'])->update($update);
  125. if($rs === false){
  126. Db::rollback();
  127. $this->error('取消失败');
  128. }
  129. //给用户发通知
  130. $user_info = Db::name('user')->where('id',$lesson_order['user_id'])->find();
  131. if($user_info['notice_email'] == 1 && !empty($user_info['email'])){
  132. $obj = new Email();
  133. $result = $obj
  134. ->to($user_info['email'])
  135. ->subject('Elin Dance Studio 订单取消!')
  136. ->message('Hi,'.$user_info['firstname']. ' ' .$user_info['lastname'].',您预约的'.date('Y-m-d H:i',$info['starttime']).'的课程已被取消,原因:'.$cancel_reason.'!')
  137. ->send();
  138. }
  139. }
  140. }
  141. Db::commit();
  142. $this->success('取消完成');
  143. }
  144. $this->view->assign('row',$info);
  145. return $this->view->fetch();
  146. }
  147. /**
  148. * 复制本周的课程表
  149. */
  150. public function copyweek(){
  151. $starttime = strtotime('this week Monday'); // 获取本周一的时间戳
  152. $endtime = $starttime + 86400*7;
  153. $list = Db::name('lesson_slot')->where('is_show',1)->where('starttime','BETWEEN',[$starttime,$endtime])->select();
  154. if(empty($list)){
  155. $this->error('本周还没有课程表');
  156. }
  157. foreach($list as $key => &$val){
  158. unset($val['id']);
  159. $val['starttime'] = $val['starttime'] + 86400*7;
  160. $val['endtime'] = $val['endtime'] + 86400*7;
  161. $val['status'] = 0;
  162. $val['notice_status'] = 0;
  163. $val['finishtime'] = 0;
  164. $val['cancel_reason'] = '';
  165. $val['cancel_time'] = 0;
  166. }
  167. Db::name('lesson_slot')->insertAll($list);
  168. $this->success('已复制到下周');
  169. }
  170. }