Task.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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\Fault;
  9. /**
  10. * sim-任务管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class Task extends Backend
  15. {
  16. /**
  17. * Task模型对象
  18. * @var \app\admin\model\teacher\Task
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\teacher\Task;
  25. $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList());
  26. $this->assign('sim_sim_type', ConfigModel::getSimTypeList());
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. public function add()
  34. {
  35. if (false === $this->request->isPost()) {
  36. return $this->view->fetch();
  37. }
  38. $params = $this->request->post('row/a');
  39. if (empty($params)) {
  40. $this->error(__('Parameter %s can not be empty', ''));
  41. }
  42. if(empty($params['fault_id'])){
  43. $this->error('请选择任务故障');
  44. }
  45. $params = $this->preExcludeFields($params);
  46. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  47. $params[$this->dataLimitField] = $this->auth->id;
  48. }
  49. $result = false;
  50. Db::startTrans();
  51. try {
  52. $fult_ids = $params['fault_id'];
  53. //是否采用模型验证
  54. if ($this->modelValidate) {
  55. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  56. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  57. $this->model->validateFailException()->validate($validate);
  58. }
  59. $result = $this->model->allowField(true)->save($params);
  60. foreach(explode(',',$fult_ids) as $item){
  61. if(!empty($item)){
  62. $arr = [
  63. 'task_id' => $this->model->task_id,
  64. 'fault_id' => $item,
  65. 'flag' => 1,
  66. 'update_time' => date('Y-m-d H:i:s'),
  67. ];
  68. Db::name('task_fault')->insert($arr);
  69. }
  70. }
  71. Db::commit();
  72. } catch (ValidateException|PDOException|Exception $e) {
  73. Db::rollback();
  74. $this->error($e->getMessage());
  75. }
  76. if ($result === false) {
  77. $this->error(__('No rows were inserted'));
  78. }
  79. $this->success();
  80. }
  81. public function edit($ids = null)
  82. {
  83. $row = $this->model->get($ids);
  84. if (!$row) {
  85. $this->error(__('No Results were found'));
  86. }
  87. $where = [
  88. 'sim_type' => $row->sim_type,
  89. 'fault_type' => 1,
  90. 'fault_state'=>0
  91. ];
  92. $selectData = Fault::where($where)->select();
  93. foreach ($selectData as $key => $value){
  94. $children = Fault::where(['parent_fault_id'=>$value['fault_id'],'fault_type' => 3,'fault_state'=>0])->select();
  95. $selectData[$key]['children'] = $children;
  96. }
  97. unset($value);
  98. $selectData = collection($selectData)->toArray();
  99. $row['selectData'] = $selectData;
  100. $row['fault_id_arr'] = !empty($row['fault_id']) ? explode(',',$row['fault_id']):'';
  101. if (false === $this->request->isPost()) {
  102. $this->view->assign('row', $row);
  103. return $this->view->fetch();
  104. }
  105. $params = $this->request->post('row/a');
  106. if (empty($params)) {
  107. $this->error(__('Parameter %s can not be empty', ''));
  108. }
  109. if(empty($params['fault_id'])){
  110. $this->error('请选择任务故障');
  111. }
  112. $params = $this->preExcludeFields($params);
  113. $result = false;
  114. Db::startTrans();
  115. try {
  116. $fult_ids = $params['fault_id'];
  117. //是否采用模型验证
  118. if ($this->modelValidate) {
  119. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  120. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  121. $row->validateFailException()->validate($validate);
  122. }
  123. $result = $row->allowField(true)->save($params);
  124. foreach(explode(',',$fult_ids) as $item){
  125. if(!empty($item)){
  126. $isset = Db::name('task_fault')->where(['task_id'=>$row->task_id,'fault_id'=>$item])->find();
  127. if(empty($isset)){
  128. $arr = [
  129. 'task_id' => $this->model->task_id,
  130. 'fault_id' => $item,
  131. 'flag' => 1,
  132. 'update_time' => date('Y-m-d H:i:s'),
  133. ];
  134. Db::name('task_fault')->insert($arr);
  135. }
  136. }
  137. }
  138. Db::commit();
  139. } catch (ValidateException|PDOException|Exception $e) {
  140. Db::rollback();
  141. $this->error($e->getMessage());
  142. }
  143. if (false === $result) {
  144. $this->error(__('No rows were updated'));
  145. }
  146. $this->success();
  147. }
  148. }