Trylessonorder.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use Exception;
  6. use think\exception\DbException;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 试课预约订单
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Trylessonorder extends Backend
  15. {
  16. protected $noNeedRight = ['selectpagenew'];
  17. /**
  18. * Trylessonorder模型对象
  19. * @var \app\admin\model\Trylessonorder
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\Trylessonorder;
  26. $this->view->assign("orderStatusList", $this->model->getOrderStatusList());
  27. $this->view->assign("payTypeList", $this->model->getPayTypeList());
  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. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  49. $list = $this->model
  50. ->with(['user','trylesson'])
  51. ->where($where)
  52. ->where('trylessonorder.order_status','IN',[10,20])
  53. ->order($sort, $order)
  54. ->paginate($limit);
  55. foreach ($list as $row) {
  56. $row->getRelation('user')->visible(['firstname','lastname']);
  57. $row->getRelation('trylesson')->visible(['name','name_en']);
  58. }
  59. $result = array("total" => $list->total(), "rows" => $list->items());
  60. return json($result);
  61. }
  62. return $this->view->fetch();
  63. }
  64. /**
  65. * 用户下单
  66. *
  67. * @return string
  68. * @throws \think\Exception
  69. */
  70. public function add()
  71. {
  72. if (false === $this->request->isPost()) {
  73. $trylesson_id = input('trylesson_id',0);
  74. $this->assign('trylesson_id',$trylesson_id);
  75. return $this->view->fetch();
  76. }
  77. //
  78. $trylesson_id = input('post.trylesson_id');
  79. $user_id = input('user_id');
  80. $trylesson_info = Db::name('trylesson')->where('id',$trylesson_id)->find();
  81. $time = time();
  82. //套餐订单
  83. $data = [
  84. 'order_no' => createUniqueNo('T',$user_id),
  85. 'user_id' => $user_id,
  86. 'trylesson_id' => $trylesson_id,
  87. 'lesson_ids' => $trylesson_info['lesson_ids'],
  88. 'order_amount' => $trylesson_info['price'],
  89. 'order_status' => 10,
  90. 'paytime' => $time,
  91. 'createtime' => $time,
  92. 'updatetime' => $time,
  93. 'starttime' => $time,
  94. 'endtime' => $time + (30 * 86400),
  95. 'remark' => input('remark',''),
  96. 'seller' => input('seller',''),
  97. 'pay_type' => 2,
  98. ];
  99. //入库
  100. Db::startTrans();
  101. $order_id = Db::name('trylesson_order')->insertGetId($data);
  102. if(!$order_id){
  103. Db::rollback();
  104. $this->error('下单失败');
  105. }
  106. Db::commit();
  107. $this->success('下单成功');
  108. }
  109. public function selectpagenew()
  110. {
  111. $user_id = input('user_id',0);
  112. $slot_id = input('slot_id',0);
  113. $number = input('number',1,'intval');
  114. //试课只能约一人
  115. if($number != 1){
  116. return json(['list' => [], 'total' => 0]);
  117. }
  118. $info = Db::name('lesson_slot')->where('id',$slot_id)->find();
  119. //可用试课
  120. $try_map = [
  121. 'o.user_id' =>$user_id,
  122. 'o.endtime' => ['gt',time()],
  123. 'o.order_status' => 10,
  124. ];
  125. $tryorder_list = Db::name('trylesson_order')->alias('o')
  126. ->join('trylesson','o.trylesson_id = trylesson.id','LEFT')
  127. ->field('o.id,o.starttime,o.endtime,trylesson.name,trylesson.name_en')
  128. ->where($try_map)
  129. ->where('find_in_set(:lesson_ids,o.lesson_ids)', ['lesson_ids' => $info['lesson_id']])
  130. ->order('o.endtime asc')->select();
  131. if(!empty($tryorder_list)){
  132. foreach($tryorder_list as $key => &$val){
  133. $val['time_text'] = date('M d,Y',$val['starttime']).'-'.date('M d,Y',$val['endtime']);
  134. $val['name'] = $val['id'].'['.$val['name'].']--'.$val['time_text'];
  135. }
  136. }
  137. return json(['list' => $tryorder_list, 'total' => 0]);
  138. }
  139. }