Packageorder.php 6.2 KB

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