Lessonpackage.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 Lessonpackage extends Backend
  15. {
  16. /**
  17. * Lessonpackage模型对象
  18. * @var \app\admin\model\Lessonpackage
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\Lessonpackage;
  25. $this->view->assign("isShowList", $this->model->getIsShowList());
  26. $this->view->assign("buytimesList", $this->model->getBuytimesList());
  27. $this->view->assign("newuserList", $this->model->getNewuserList());
  28. $this->view->assign("olduserList", $this->model->getOlduserList());
  29. $this->view->assign("typeList", $this->model->getTypeList());
  30. $this->view->assign("giftTypeList", $this->model->getGiftTypeList());
  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([/*'lessona',*/'lessonb'])
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->paginate($limit);
  57. foreach ($list as $row) {
  58. // $row->getRelation('lessona')->visible(['name','name_en']);
  59. $row->getRelation('lessonb')->visible(['name','name_en']);
  60. }
  61. $result = array("total" => $list->total(), "rows" => $list->items());
  62. return json($result);
  63. }
  64. return $this->view->fetch();
  65. }
  66. /**
  67. * 编辑
  68. *
  69. * @param $ids
  70. * @return string
  71. * @throws DbException
  72. * @throws \think\Exception
  73. */
  74. public function edit($ids = null)
  75. {
  76. $row = $this->model->get($ids);
  77. if (!$row) {
  78. $this->error(__('No Results were found'));
  79. }
  80. $adminIds = $this->getDataLimitAdminIds();
  81. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  82. $this->error(__('You have no permission'));
  83. }
  84. if (false === $this->request->isPost()) {
  85. $this->view->assign('row', $row);
  86. return $this->view->fetch();
  87. }
  88. $params = $this->request->post('row/a');
  89. if (empty($params)) {
  90. $this->error(__('Parameter %s can not be empty', ''));
  91. }
  92. $params = $this->preExcludeFields($params);
  93. $result = false;
  94. Db::startTrans();
  95. try {
  96. //是否采用模型验证
  97. if ($this->modelValidate) {
  98. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  99. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  100. $row->validateFailException()->validate($validate);
  101. }
  102. //修改所有配套订单
  103. $rs_up = Db::name('package_order')->where('package_id',$ids)->where('is_gift',0)->update(['lesson_ids'=>$params['lesson_ids']]);
  104. $rs_up2 = Db::name('package_order')->where('package_id',$ids)->where('is_gift',1)->update(['lesson_ids'=>$params['gift_lesson_id']]);
  105. if($rs_up === false || $rs_up2 === false){
  106. Db::rollback();
  107. abort(500,'修改失败');
  108. }
  109. //
  110. $result = $row->allowField(true)->save($params);
  111. Db::commit();
  112. } catch (ValidateException|PDOException|Exception $e) {
  113. Db::rollback();
  114. $this->error($e->getMessage());
  115. }
  116. if (false === $result) {
  117. $this->error(__('No rows were updated'));
  118. }
  119. $this->success();
  120. }
  121. }