Lessonpackage.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. }
  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([/*'lessona',*/'lessonb'])
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->paginate($limit);
  55. foreach ($list as $row) {
  56. // $row->getRelation('lessona')->visible(['name','name_en']);
  57. $row->getRelation('lessonb')->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. * @param $ids
  68. * @return string
  69. * @throws DbException
  70. * @throws \think\Exception
  71. */
  72. public function edit($ids = null)
  73. {
  74. $row = $this->model->get($ids);
  75. if (!$row) {
  76. $this->error(__('No Results were found'));
  77. }
  78. $adminIds = $this->getDataLimitAdminIds();
  79. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  80. $this->error(__('You have no permission'));
  81. }
  82. if (false === $this->request->isPost()) {
  83. $this->view->assign('row', $row);
  84. return $this->view->fetch();
  85. }
  86. $params = $this->request->post('row/a');
  87. if (empty($params)) {
  88. $this->error(__('Parameter %s can not be empty', ''));
  89. }
  90. $params = $this->preExcludeFields($params);
  91. $result = false;
  92. Db::startTrans();
  93. try {
  94. //是否采用模型验证
  95. if ($this->modelValidate) {
  96. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  97. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  98. $row->validateFailException()->validate($validate);
  99. }
  100. //修改所有配套订单
  101. $rs_up = Db::name('package_order')->where('package_id',$ids)->where('is_gift',0)->update(['lesson_ids'=>$params['lesson_ids']]);
  102. $rs_up2 = Db::name('package_order')->where('package_id',$ids)->where('is_gift',1)->update(['lesson_ids'=>$params['gift_lesson_id']]);
  103. if($rs_up === false || $rs_up2 === false){
  104. Db::rollback();
  105. $this->error('修改失败');
  106. }
  107. //
  108. $result = $row->allowField(true)->save($params);
  109. Db::commit();
  110. } catch (ValidateException|PDOException|Exception $e) {
  111. Db::rollback();
  112. $this->error($e->getMessage());
  113. }
  114. if (false === $result) {
  115. $this->error(__('No rows were updated'));
  116. }
  117. $this->success();
  118. }
  119. }