Task.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. $this->assignConfig('rel_Ids', []);
  37. $this->assignConfig('rel_Names', []);
  38. return $this->view->fetch();
  39. }
  40. $params = $this->request->post('row/a');
  41. if (empty($params)) {
  42. $this->error(__('Parameter %s can not be empty', ''));
  43. }
  44. if(empty($params['fault_id'])){
  45. $this->error('请选择任务故障');
  46. }
  47. if(count(explode(',',$params['fault_id']))!=3){
  48. $this->error('故障部位必须为3个');
  49. }
  50. $params = $this->preExcludeFields($params);
  51. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  52. $params[$this->dataLimitField] = $this->auth->id;
  53. }
  54. $result = false;
  55. Db::startTrans();
  56. try {
  57. $fult_ids = $params['fault_id'];
  58. //是否采用模型验证
  59. if ($this->modelValidate) {
  60. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  61. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  62. $this->model->validateFailException()->validate($validate);
  63. }
  64. $result = $this->model->allowField(true)->save($params);
  65. foreach(explode(',',$fult_ids) as $item){
  66. if(!empty($item)){
  67. $arr = [
  68. 'task_id' => $this->model->task_id,
  69. 'fault_id' => $item,
  70. 'flag' => 1,
  71. 'update_time' => date('Y-m-d H:i:s'),
  72. ];
  73. Db::name('task_fault')->insert($arr);
  74. }
  75. }
  76. Db::commit();
  77. } catch (ValidateException|PDOException|Exception $e) {
  78. Db::rollback();
  79. $this->error($e->getMessage());
  80. }
  81. if ($result === false) {
  82. $this->error(__('No rows were inserted'));
  83. }
  84. $this->success();
  85. }
  86. public function edit($ids = null)
  87. {
  88. $row = $this->model->get($ids);
  89. if (!$row) {
  90. $this->error(__('No Results were found'));
  91. }
  92. $where = [
  93. 'sim_type' => $row->sim_type,
  94. 'fault_type' => 1,
  95. 'fault_state'=>0
  96. ];
  97. $selectData = Fault::where($where)->select();
  98. foreach ($selectData as $key => $value){
  99. $children = Fault::where(['parent_fault_id'=>$value['fault_id'],'fault_type' => 3,'fault_state'=>0])->select();
  100. $selectData[$key]['children'] = $children;
  101. }
  102. unset($value);
  103. $selectData = collection($selectData)->toArray();
  104. $row['selectData'] = $selectData;
  105. $row['fault_id_arr'] = !empty($row['fault_id']) ? explode(',',$row['fault_id']):'';
  106. if (false === $this->request->isPost()) {
  107. $this->view->assign('row', $row);
  108. $this->assignConfig('rel_Ids',explode(',',$row->fault_id));
  109. $this->assignConfig('rel_Names',explode(',',$row->fault_name));
  110. return $this->view->fetch();
  111. }
  112. $params = $this->request->post('row/a');
  113. if (empty($params)) {
  114. $this->error(__('Parameter %s can not be empty', ''));
  115. }
  116. if(empty($params['fault_id'])){
  117. $this->error('请选择任务故障');
  118. }
  119. if(count(explode(',',$params['fault_id']))!=3){
  120. $this->error('故障部位必须为3个');
  121. }
  122. $params = $this->preExcludeFields($params);
  123. $result = false;
  124. Db::startTrans();
  125. try {
  126. $fult_ids = $params['fault_id'];
  127. $result = $row->allowField(true)->save($params);
  128. foreach(explode(',',$fult_ids) as $item){
  129. if(!empty($item)){
  130. $isset = Db::name('task_fault')->where(['task_id'=>$row->task_id,'fault_id'=>$item])->find();
  131. if(empty($isset)){
  132. $arr = [
  133. 'task_id' => $row->task_id,
  134. 'fault_id' => $item,
  135. 'flag' => 1,
  136. 'update_time' => date('Y-m-d H:i:s'),
  137. ];
  138. Db::name('task_fault')->insert($arr);
  139. }
  140. }
  141. }
  142. //删除任务和故障关联表中 编辑后不存在的数据
  143. Db::name('task_fault')->where(['task_id'=>$row->task_id,'fault_id'=>['not in',$params['fault_id']]])->delete();
  144. Db::commit();
  145. } catch (ValidateException|PDOException|Exception $e) {
  146. Db::rollback();
  147. $this->error($e->getMessage());
  148. }
  149. if (false === $result) {
  150. $this->error(__('No rows were updated'));
  151. }
  152. $this->success();
  153. }
  154. //删除的时候,同步删除好几个表
  155. }