Packageorder.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 Packageorder extends Backend
  15. {
  16. /**
  17. * Packageorder模型对象
  18. * @var \app\admin\model\Packageorder
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\Packageorder;
  25. $this->view->assign("orderStatusList", $this->model->getOrderStatusList());
  26. $this->view->assign("useStatusList", $this->model->getUseStatusList());
  27. $this->view->assign("isGiftList", $this->model->getIsGiftList());
  28. $this->view->assign("noticeStatusList", $this->model->getNoticeStatusList());
  29. }
  30. /**
  31. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  32. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  33. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  34. */
  35. /**
  36. * 查看
  37. */
  38. public function index()
  39. {
  40. //当前是否为关联查询
  41. $this->relationSearch = true;
  42. //设置过滤方法
  43. $this->request->filter(['strip_tags', 'trim']);
  44. if ($this->request->isAjax()) {
  45. //如果发送的来源是Selectpage,则转发到Selectpage
  46. if ($this->request->request('keyField')) {
  47. return $this->selectpage();
  48. }
  49. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  50. $list = $this->model
  51. ->with(['user','package'])
  52. ->where($where)
  53. ->where('packageorder.order_status',1)
  54. ->order($sort, $order)
  55. ->paginate($limit);
  56. foreach ($list as $row) {
  57. $row->getRelation('user')->visible(['firstname','lastname']);
  58. $row->getRelation('package')->visible(['name','name_en']);
  59. }
  60. $result = array("total" => $list->total(), "rows" => $list->items());
  61. return json($result);
  62. }
  63. return $this->view->fetch();
  64. }
  65. /**
  66. * 激活
  67. */
  68. public function jihuo(){
  69. $id = input('id',0);
  70. $package_order = Db::name('package_order')->where('id',$id)->find();
  71. if(empty($package_order)){
  72. $this->error(__('请刷新重试'));
  73. }
  74. if($package_order['use_status'] == 1){
  75. $this->error(__('请刷新重试'));
  76. }
  77. $time = time();
  78. $update = [
  79. 'updatetime' => $time,
  80. ];
  81. $update['use_status'] = 1;
  82. $update['starttime'] = $time;
  83. $update['endtime'] = $time + ($package_order['days'] * 86400);
  84. $rs = Db::name('package_order')->where('id',$id)->update($update); //这里不用order_no,主订单和赠品各自激活
  85. $this->success(__('已激活'));
  86. }
  87. /**
  88. * 添加
  89. *
  90. * @return string
  91. * @throws \think\Exception
  92. */
  93. public function add()
  94. {
  95. if (false === $this->request->isPost()) {
  96. return $this->view->fetch();
  97. }
  98. $params = $this->request->post('row/a');
  99. if (empty($params)) {
  100. $this->error(__('Parameter %s can not be empty', ''));
  101. }
  102. $params = $this->preExcludeFields($params);
  103. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  104. $params[$this->dataLimitField] = $this->auth->id;
  105. }
  106. $result = false;
  107. Db::startTrans();
  108. try {
  109. //是否采用模型验证
  110. if ($this->modelValidate) {
  111. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  112. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  113. $this->model->validateFailException()->validate($validate);
  114. }
  115. //
  116. $package_id = $params['package_id'];
  117. $package_info = Db::name('lesson_package')->where('id',$package_id)->find();
  118. //套餐订单
  119. $data = [
  120. 'order_no' => createUniqueNo('P',$params['user_id']),
  121. 'lesson_ids' => $package_info['lesson_ids'],
  122. 'sessions' => $package_info['sessions'],
  123. 'starttime' => 0,
  124. 'days' => $package_info['days'],
  125. 'endtime' => 0,
  126. 'price' => $package_info['price'],
  127. 'remain' => $package_info['sessions'],
  128. 'order_status'=> 1,
  129. 'use_status' => 0,//默认不激活
  130. 'paytime' => time(),
  131. 'pay_type' => 2, //线下支付
  132. 'is_gift' => 0,
  133. ];
  134. $params = array_merge($params,$data);
  135. //
  136. $result = $this->model->allowField(true)->save($params);
  137. Db::commit();
  138. } catch (ValidateException|PDOException|Exception $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. }
  142. if ($result === false) {
  143. $this->error(__('No rows were inserted'));
  144. }
  145. $this->success();
  146. }
  147. }