Trylessonorder.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. /**
  17. * Trylessonorder模型对象
  18. * @var \app\admin\model\Trylessonorder
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\Trylessonorder;
  25. $this->view->assign("orderStatusList", $this->model->getOrderStatusList());
  26. $this->view->assign("payTypeList", $this->model->getPayTypeList());
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 查看
  35. */
  36. public function index()
  37. {
  38. //当前是否为关联查询
  39. $this->relationSearch = true;
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if ($this->request->isAjax()) {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $list = $this->model
  49. ->with(['user','trylesson'])
  50. ->where($where)
  51. ->where('trylessonorder.order_status','IN',[10,20])
  52. ->order($sort, $order)
  53. ->paginate($limit);
  54. foreach ($list as $row) {
  55. $row->getRelation('user')->visible(['firstname','lastname']);
  56. $row->getRelation('trylesson')->visible(['name','name_en']);
  57. }
  58. $result = array("total" => $list->total(), "rows" => $list->items());
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 用户下单
  65. *
  66. * @return string
  67. * @throws \think\Exception
  68. */
  69. public function add()
  70. {
  71. if (false === $this->request->isPost()) {
  72. $trylesson_id = input('trylesson_id',0);
  73. $this->assign('trylesson_id',$trylesson_id);
  74. return $this->view->fetch();
  75. }
  76. //
  77. $trylesson_id = input('post.trylesson_id');
  78. $user_id = input('user_id');
  79. $trylesson_info = Db::name('trylesson')->where('id',$trylesson_id)->find();
  80. $time = time();
  81. //套餐订单
  82. $data = [
  83. 'order_no' => createUniqueNo('T',$user_id),
  84. 'user_id' => $user_id,
  85. 'trylesson_id' => $trylesson_id,
  86. 'lesson_ids' => $trylesson_info['lesson_ids'],
  87. 'order_amount' => $trylesson_info['price'],
  88. 'order_status' => 10,
  89. 'paytime' => $time,
  90. 'createtime' => $time,
  91. 'updatetime' => $time,
  92. 'starttime' => $time,
  93. 'endtime' => $time + (30 * 86400),
  94. 'remark' => input('remark',''),
  95. 'seller' => input('seller',''),
  96. 'pay_type' => 2,
  97. ];
  98. //入库
  99. Db::startTrans();
  100. $order_id = Db::name('trylesson_order')->insertGetId($data);
  101. if(!$order_id){
  102. Db::rollback();
  103. $this->error('下单失败');
  104. }
  105. Db::commit();
  106. $this->success('下单成功');
  107. }
  108. }