Gameplace.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 排位赛场地
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Gameplace extends Backend
  11. {
  12. /**
  13. * Gameplace模型对象
  14. * @var \app\admin\model\Gameplace
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Gameplace;
  21. $this->view->assign("statusList", $this->model->getStatusList());
  22. }
  23. /**
  24. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  25. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  26. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  27. */
  28. /**
  29. * 查看
  30. */
  31. public function index()
  32. {
  33. $game_id = input('game_id', 0, 'intval'); //活动id
  34. if (!$game_id) {
  35. $game_id = input('ids', 0, 'intval'); //从活动列表第一次跳转过来
  36. }
  37. $this->assignconfig('game_id', $game_id);
  38. //当前是否为关联查询
  39. $this->relationSearch = true;
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if ($this->request->isAjax()) {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $list = $this->model
  49. ->with(['game', 'provincearea', 'cityarea', 'area'])
  50. ->where(['hu_game_place.game_id' => $game_id])
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->paginate($limit);
  54. foreach ($list as $row) {
  55. $row->getRelation('game')->visible(['name']);
  56. $row->getRelation('provincearea')->visible(['name']);
  57. $row->getRelation('cityarea')->visible(['name']);
  58. $row->getRelation('area')->visible(['name']);
  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 add()
  69. {
  70. $game_id = input('game_id', 0, 'intval');
  71. if ($this->request->isPost()) {
  72. $params = $this->request->post("row/a");
  73. if ($params) {
  74. $params = $this->preExcludeFields($params);
  75. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  76. $params[$this->dataLimitField] = $this->auth->id;
  77. }
  78. $result = false;
  79. $params['province_id'] = $params['province'];
  80. $params['city_id'] = $params['city'];
  81. $params['area_id'] = $params['area'];
  82. Db::startTrans();
  83. try {
  84. //是否采用模型验证
  85. if ($this->modelValidate) {
  86. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  87. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  88. $this->model->validateFailException(true)->validate($validate);
  89. }
  90. $result = $this->model->allowField(true)->save($params);
  91. Db::commit();
  92. } catch (ValidateException $e) {
  93. Db::rollback();
  94. $this->error($e->getMessage());
  95. } catch (PDOException $e) {
  96. Db::rollback();
  97. $this->error($e->getMessage());
  98. } catch (Exception $e) {
  99. Db::rollback();
  100. $this->error($e->getMessage());
  101. }
  102. if ($result !== false) {
  103. $this->success();
  104. } else {
  105. $this->error(__('No rows were inserted'));
  106. }
  107. }
  108. $this->error(__('Parameter %s can not be empty', ''));
  109. }
  110. $this->assign('game_id', $game_id);
  111. return $this->view->fetch();
  112. }
  113. /**
  114. * 编辑
  115. */
  116. public function edit($ids = null)
  117. {
  118. $row = $this->model->get($ids);
  119. if (!$row) {
  120. $this->error(__('No Results were found'));
  121. }
  122. $adminIds = $this->getDataLimitAdminIds();
  123. if (is_array($adminIds)) {
  124. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  125. $this->error(__('You have no permission'));
  126. }
  127. }
  128. if ($this->request->isPost()) {
  129. $params = $this->request->post("row/a");
  130. if ($params) {
  131. $params = $this->preExcludeFields($params);
  132. $result = false;
  133. if ($params['status'] == 0) {
  134. //判断是否有人报名
  135. $count = Db::name('game_people')->where(['game_place_id' => $ids, 'status' => 1])->count('id');
  136. if ($count) {
  137. $this->error('有人已报名该场地,暂不能隐藏');
  138. }
  139. }
  140. $params['province_id'] = $params['province'];
  141. $params['city_id'] = $params['city'];
  142. $params['area_id'] = $params['area'];
  143. Db::startTrans();
  144. try {
  145. //是否采用模型验证
  146. if ($this->modelValidate) {
  147. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  148. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  149. $row->validateFailException(true)->validate($validate);
  150. }
  151. $result = $row->allowField(true)->save($params);
  152. Db::commit();
  153. } catch (ValidateException $e) {
  154. Db::rollback();
  155. $this->error($e->getMessage());
  156. } catch (PDOException $e) {
  157. Db::rollback();
  158. $this->error($e->getMessage());
  159. } catch (Exception $e) {
  160. Db::rollback();
  161. $this->error($e->getMessage());
  162. }
  163. if ($result !== false) {
  164. $this->success();
  165. } else {
  166. $this->error(__('No rows were updated'));
  167. }
  168. }
  169. $this->error(__('Parameter %s can not be empty', ''));
  170. }
  171. $row['province_name'] = Db::name('area')->where(['id' => $row['province_id']])->value('name');
  172. $row['city_name'] = Db::name('area')->where(['id' => $row['city_id']])->value('name');
  173. $row['area_name'] = Db::name('area')->where(['id' => $row['area_id']])->value('name');
  174. $this->view->assign("row", $row);
  175. return $this->view->fetch();
  176. }
  177. /**
  178. * 删除
  179. */
  180. public function del($ids = "")
  181. {
  182. if (!$this->request->isPost()) {
  183. $this->error(__("Invalid parameters"));
  184. }
  185. $ids = $ids ? $ids : $this->request->post("ids");
  186. $count = Db::name('game_people')->where(['game_place_id' => $ids, 'status' => 1])->count('id');
  187. if ($count) {
  188. $this->error('有人已报名该场地,暂不能删除');
  189. }
  190. $this->error(__('Parameter %s can not be empty', 'ids'));
  191. }
  192. }