RoomSignup.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace app\admin\controller\exam;
  3. use addons\exam\enum\RoomSignupStatus;
  4. use app\admin\model\exam\RoomModel;
  5. use app\common\controller\Backend;
  6. use think\Db;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. /**
  10. * 考场报名
  11. * @icon fa fa-circle-o
  12. */
  13. class RoomSignup extends Backend
  14. {
  15. /**
  16. * RoomSignupModel模型对象
  17. * @var \app\admin\model\exam\RoomSignupModel
  18. */
  19. protected $model = null;
  20. public function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new \app\admin\model\exam\RoomSignupModel;
  24. $this->view->assign("statusList", $this->model->getStatusList());
  25. }
  26. /**
  27. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  28. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  29. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  30. */
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. //当前是否为关联查询
  37. $this->relationSearch = true;
  38. //设置过滤方法
  39. $this->request->filter(['strip_tags', 'trim']);
  40. if ($this->request->isAjax()) {
  41. //如果发送的来源是Selectpage,则转发到Selectpage
  42. if ($this->request->request('keyField')) {
  43. return $this->selectpage();
  44. }
  45. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  46. $list = $this->model
  47. ->with(['room', 'user'])
  48. ->where($where)
  49. ->order($sort, $order)
  50. ->paginate($limit);
  51. foreach ($list as $row) {
  52. $row->getRelation('room')->visible(['name']);
  53. $row->getRelation('user')->visible(['nickname']);
  54. }
  55. $result = array("total" => $list->total(), "rows" => $list->items());
  56. return json($result);
  57. }
  58. return $this->view->fetch();
  59. }
  60. /**
  61. * 添加
  62. */
  63. public function add()
  64. {
  65. if ($this->request->isPost()) {
  66. $params = $this->request->post("row/a");
  67. if ($params) {
  68. $params = $this->preExcludeFields($params);
  69. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  70. $params[$this->dataLimitField] = $this->auth->id;
  71. }
  72. $result = false;
  73. Db::startTrans();
  74. try {
  75. //是否采用模型验证
  76. if ($this->modelValidate) {
  77. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  78. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  79. $this->model->validateFailException(true)->validate($validate);
  80. }
  81. // 验证提交数据
  82. $this->validSubmit($params);
  83. $result = $this->model->allowField(true)->save($params);
  84. Db::commit();
  85. } catch (ValidateException $e) {
  86. Db::rollback();
  87. $this->error($e->getMessage());
  88. } catch (PDOException $e) {
  89. Db::rollback();
  90. $this->error($e->getMessage());
  91. } catch (\Exception $e) {
  92. Db::rollback();
  93. $this->error($e->getMessage());
  94. }
  95. if ($result !== false) {
  96. $this->success();
  97. } else {
  98. $this->error(__('No rows were inserted'));
  99. }
  100. }
  101. $this->error(__('Parameter %s can not be empty', ''));
  102. }
  103. return $this->view->fetch();
  104. }
  105. /**
  106. * 编辑
  107. */
  108. public function edit($ids = null)
  109. {
  110. $row = $this->model->get($ids);
  111. if (!$row) {
  112. $this->error(__('No Results were found'));
  113. }
  114. $adminIds = $this->getDataLimitAdminIds();
  115. if (is_array($adminIds)) {
  116. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  117. $this->error(__('You have no permission'));
  118. }
  119. }
  120. if ($this->request->isPost()) {
  121. $params = $this->request->post("row/a");
  122. if ($params) {
  123. $params = $this->preExcludeFields($params);
  124. $result = false;
  125. Db::startTrans();
  126. try {
  127. //是否采用模型验证
  128. if ($this->modelValidate) {
  129. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  130. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  131. $row->validateFailException(true)->validate($validate);
  132. }
  133. // 验证提交数据
  134. $this->validSubmit($params);
  135. $result = $row->allowField(true)->save($params);
  136. Db::commit();
  137. } catch (ValidateException $e) {
  138. Db::rollback();
  139. $this->error($e->getMessage());
  140. } catch (PDOException $e) {
  141. Db::rollback();
  142. $this->error($e->getMessage());
  143. } catch (\Exception $e) {
  144. Db::rollback();
  145. $this->error($e->getMessage());
  146. }
  147. if ($result !== false) {
  148. $this->success();
  149. } else {
  150. $this->error(__('No rows were updated'));
  151. }
  152. }
  153. $this->error(__('Parameter %s can not be empty', ''));
  154. }
  155. $this->view->assign("row", $row);
  156. return $this->view->fetch();
  157. }
  158. /**
  159. * 批量更新
  160. */
  161. public function multi($ids = "")
  162. {
  163. if (!$this->request->isPost()) {
  164. $this->error(__("Invalid parameters"));
  165. }
  166. $ids = $ids ? $ids : $this->request->post("ids");
  167. if ($ids) {
  168. if ($this->request->has('params')) {
  169. parse_str($this->request->post("params"), $values);
  170. $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  171. if ($values) {
  172. $adminIds = $this->getDataLimitAdminIds();
  173. if (is_array($adminIds)) {
  174. $this->model->where($this->dataLimitField, 'in', $adminIds);
  175. }
  176. $count = 0;
  177. Db::startTrans();
  178. try {
  179. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  180. foreach ($list as $index => $item) {
  181. $this->validSubmit([
  182. 'room_id' => $item['room_id'],
  183. 'status' => $values['status'],
  184. ]);
  185. $count += $item->allowField(true)->isUpdate(true)->save($values);
  186. }
  187. Db::commit();
  188. } catch (PDOException $e) {
  189. Db::rollback();
  190. $this->error($e->getMessage());
  191. } catch (Exception $e) {
  192. Db::rollback();
  193. $this->error($e->getMessage());
  194. }
  195. if ($count) {
  196. $this->success();
  197. } else {
  198. $this->error(__('No rows were updated'));
  199. }
  200. } else {
  201. $this->error(__('You have no permission'));
  202. }
  203. }
  204. }
  205. $this->error(__('Parameter %s can not be empty', 'ids'));
  206. }
  207. /**
  208. * 验证提交数据
  209. * @param $params
  210. * @return void
  211. */
  212. // 验证提交数据
  213. protected function validSubmit($params)
  214. {
  215. $room = RoomModel::get($params['room_id']);
  216. if (!$room) {
  217. // throw new \Exception('考场信息不存在,请重新选择');
  218. $this->error('考场信息不存在,请重新选择');
  219. }
  220. if ($params['status'] == RoomSignupStatus::ACCEPT) {
  221. if ($room['people_count'] != 0 && $room['signup_count'] >= $room['people_count']) {
  222. // throw new \Exception('考场人数已满,无法通过该报名');
  223. $this->error('考场人数已满,无法通过该报名');
  224. }
  225. }
  226. // 递增报名成功人数
  227. $room->setInc('signup_count');
  228. }
  229. }