Lessonorder.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 Lessonorder extends Backend
  12. {
  13. /**
  14. * Lessonorder模型对象
  15. * @var \app\admin\model\Lessonorder
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Lessonorder;
  22. $this->view->assign("orderStatusList", $this->model->getOrderStatusList());
  23. $this->view->assign("paytypeList", $this->model->getPaytypeList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //当前是否为关联查询
  36. $this->relationSearch = true;
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax()) {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $list = $this->model
  46. ->with(['user','slot','lesson'])
  47. ->where($where)
  48. ->where('lessonorder.order_status','NEQ',0)
  49. ->order($sort, $order)
  50. ->paginate($limit);
  51. foreach ($list as $row) {
  52. $row->getRelation('user')->visible(['firstname','lastname']);
  53. $row->getRelation('slot')->visible(['starttime','endtime']);
  54. $row->getRelation('lesson')->visible(['name','name_en']);
  55. }
  56. $result = array("total" => $list->total(), "rows" => $list->items());
  57. return json($result);
  58. }
  59. return $this->view->fetch();
  60. }
  61. /**
  62. * 取消
  63. */
  64. public function cancel(){
  65. $id = input('id');
  66. $info = Db::name('lesson_order')->where('id',$id)->where('order_status',10)->find();
  67. if(!$info){
  68. $this->error('请刷新重试');
  69. }
  70. if($this->request->isPost()){
  71. $cancel_reason = input('cancel_reason','');
  72. $cancel_time = strtotime(input('cancel_time',''));
  73. Db::startTrans();
  74. //找到所有的已报名订单
  75. $info = Db::name('lesson_order')->where('id',$id)->where('order_status',10)->lock(true)->find();
  76. if(empty($info)){
  77. Db::rollback();
  78. $this->error('请刷新重试');
  79. }
  80. //套餐给加回去
  81. if($info['paytype'] == 1){
  82. $package_order = Db::name('package_order')->where('id',$info['package_order_id'])->lock(true)->find();
  83. $update = [
  84. 'remain' => $package_order['remain'] + $info['usernumber'],
  85. 'updatetime' => time(),
  86. ];
  87. $rs_remain = Db::name('package_order')->where('id',$info['package_order_id'])->update($update);
  88. if($rs_remain === false){
  89. Db::rollback();
  90. $this->error('取消失败');
  91. }
  92. }
  93. //现金支付不给退,线下处理
  94. //取消预约单
  95. $update2 = [
  96. 'order_status' => 30,
  97. 'cancel_time' => $cancel_time,
  98. 'cancel_reason' => $cancel_reason,
  99. ];
  100. if($info['paytype'] == 1){
  101. $update2['order_status'] = 40;
  102. }
  103. $rs = Db::name('lesson_order')->where('id',$info['id'])->update($update2);
  104. if($rs === false){
  105. Db::rollback();
  106. $this->error('取消失败');
  107. }
  108. $slot_info = Db::name('lesson_slot')->where('id',$info['slot_id'])->find();
  109. //给用户发通知
  110. $user_info = Db::name('user')->where('id',$info['user_id'])->find();
  111. if($user_info['notice_email'] == 1 && !empty($user_info['email'])){
  112. $obj = new Email();
  113. $result = $obj
  114. ->to($user_info['email'])
  115. ->subject('Elin Dance Studio 订单取消!')
  116. ->message('Hi,'.$user_info['firstname']. ' ' .$user_info['lastname'].',您预约的'.date('Y-m-d H:i',$slot_info['starttime']).'的课程已被取消,原因:'.$cancel_reason.'!')
  117. ->send();
  118. }
  119. Db::commit();
  120. $this->success('取消完成');
  121. }
  122. $this->view->assign('row',$info);
  123. return $this->view->fetch();
  124. }
  125. }