Collection.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace app\admin\controller\teacher;
  3. use app\common\controller\Backend;
  4. use app\common\model\Config as ConfigModel;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use app\admin\model\department\Department as DepartmentModel;
  9. /**
  10. * sim-考试集合管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Collection extends Backend
  15. {
  16. /**
  17. * Collection模型对象
  18. * @var \app\admin\model\teacher\Collection
  19. */
  20. protected $model = null;
  21. protected $whereExtend = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\teacher\Collection;
  26. $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList());
  27. $this->assign('sim_sim_type', ConfigModel::getSimTypeList());
  28. $this->assignConfig('sim_question_setting_method', ConfigModel::getSimQuestionList());
  29. $this->assign('sim_question_setting_method', ConfigModel::getSimQuestionList());
  30. $this->whereExtend['exam_collection_type'] = 3;
  31. }
  32. /**
  33. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  34. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  35. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  36. */
  37. public function add()
  38. {
  39. if (false === $this->request->isPost()) {
  40. return $this->view->fetch();
  41. }
  42. $params = $this->request->post('row/a');
  43. if (empty($params)) {
  44. $this->error(__('Parameter %s can not be empty', ''));
  45. }
  46. if(empty($params['depart_ids'])){
  47. $this->error('请选择区队名称');
  48. }
  49. $params = $this->preExcludeFields($params);
  50. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  51. $params[$this->dataLimitField] = $this->auth->id;
  52. }
  53. $result = false;
  54. Db::startTrans();
  55. try {
  56. $depart_ids = $params['depart_ids'];
  57. //是否采用模型验证
  58. if ($this->modelValidate) {
  59. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  60. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  61. $this->model->validateFailException()->validate($validate);
  62. }
  63. $result = $this->model->allowField(true)->save($params);
  64. foreach(explode(',',$depart_ids) as $item){
  65. if(!empty($item)){
  66. $arr = [
  67. 'exam_collection_id' => $this->model->exam_collection_id,
  68. 'dept_id' => $item,
  69. 'update_time' => date('Y-m-d H:i:s'),
  70. ];
  71. Db::name('real_exam_collection_dept')->insert($arr);
  72. }
  73. }
  74. Db::commit();
  75. } catch (ValidateException|PDOException|Exception $e) {
  76. Db::rollback();
  77. $this->error($e->getMessage());
  78. }
  79. if ($result === false) {
  80. $this->error(__('No rows were inserted'));
  81. }
  82. $this->success();
  83. }
  84. public function edit($ids = null)
  85. {
  86. $row = $this->model->get($ids);
  87. if (!$row) {
  88. $this->error(__('No Results were found'));
  89. }
  90. if (false === $this->request->isPost()) {
  91. $this->view->assign('row', $row);
  92. $this->assignConfig('row_info', $row);
  93. return $this->view->fetch();
  94. }
  95. $params = $this->request->post('row/a');
  96. if (empty($params)) {
  97. $this->error(__('Parameter %s can not be empty', ''));
  98. }
  99. $params = $this->preExcludeFields($params);
  100. $result = false;
  101. Db::startTrans();
  102. try {
  103. //是否采用模型验证
  104. if ($this->modelValidate) {
  105. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  106. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  107. $row->validateFailException()->validate($validate);
  108. }
  109. $result = $row->allowField(true)->save($params);
  110. Db::commit();
  111. } catch (ValidateException|PDOException|Exception $e) {
  112. Db::rollback();
  113. $this->error($e->getMessage());
  114. }
  115. if (false === $result) {
  116. $this->error(__('No rows were updated'));
  117. }
  118. $this->success();
  119. }
  120. public function multi($ids = null)
  121. {
  122. if (false === $this->request->isPost()) {
  123. $this->error(__('Invalid parameters'));
  124. }
  125. $ids = $ids ?: $this->request->post('ids');
  126. if (empty($ids)) {
  127. $this->error(__('Parameter %s can not be empty', 'ids'));
  128. }
  129. if (false === $this->request->has('params')) {
  130. $this->error(__('No rows were updated'));
  131. }
  132. parse_str($this->request->post('params'), $values);
  133. $values = true ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  134. // $values = $this->auth->isSuperAdmin() ? $values : array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
  135. if (empty($values)) {
  136. $this->error(__('You have no permission'));
  137. }
  138. $adminIds = $this->getDataLimitAdminIds();
  139. if (is_array($adminIds)) {
  140. $this->model->where($this->dataLimitField, 'in', $adminIds);
  141. }
  142. if(!empty($values['exam_collection_state'])){
  143. if($values['exam_collection_state']==2){
  144. $state_count = $this->model->where(['exam_collection_state'=>$values['exam_collection_state'],'exam_collection_type'=>3])->count();
  145. if($state_count>0){
  146. $this->error('已有启用的考试,请先关闭原来的考试');
  147. }
  148. }
  149. }
  150. $count = 0;
  151. Db::startTrans();
  152. try {
  153. $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
  154. foreach ($list as $item) {
  155. $count += $item->allowField(true)->isUpdate(true)->save($values);
  156. }
  157. Db::commit();
  158. } catch (PDOException|Exception $e) {
  159. Db::rollback();
  160. $this->error($e->getMessage());
  161. }
  162. if ($count) {
  163. $this->success();
  164. }
  165. $this->error(__('No rows were updated'));
  166. }
  167. }