Task.php 6.1 KB

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