Task.php 6.2 KB

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