Task.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. protected $noNeedLogin = ['selectlist'];
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\teacher\Task;
  26. $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList());
  27. $this->assign('sim_sim_type', ConfigModel::getSimTypeList());
  28. }
  29. /**
  30. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  31. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  32. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33. */
  34. public function add()
  35. {
  36. if (false === $this->request->isPost()) {
  37. $this->assignConfig('rel_Ids', []);
  38. $this->assignConfig('rel_Names', []);
  39. $this->assignConfig('prel_Ids', []);
  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['fault_id'])){
  47. $this->error('请选择任务故障');
  48. }
  49. if(count(explode(',',$params['fault_id']))>3){
  50. $this->error('故障部位最多为3个');
  51. }
  52. $params['fault_total'] = count(explode(',',$params['fault_id']));
  53. $params = $this->preExcludeFields($params);
  54. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  55. $params[$this->dataLimitField] = $this->auth->id;
  56. }
  57. $result = false;
  58. Db::startTrans();
  59. try {
  60. $fult_ids = $params['fault_id'];
  61. //是否采用模型验证
  62. if ($this->modelValidate) {
  63. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  64. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  65. $this->model->validateFailException()->validate($validate);
  66. }
  67. $result = $this->model->allowField(true)->save($params);
  68. foreach(explode(',',$fult_ids) as $item){
  69. if(!empty($item)){
  70. $arr = [
  71. 'task_id' => $this->model->task_id,
  72. 'fault_id' => $item,
  73. 'flag' => 1,
  74. 'update_time' => date('Y-m-d H:i:s'),
  75. ];
  76. Db::name('task_fault')->insert($arr);
  77. }
  78. }
  79. Db::commit();
  80. } catch (ValidateException|PDOException|Exception $e) {
  81. Db::rollback();
  82. $this->error($e->getMessage());
  83. }
  84. if ($result === false) {
  85. $this->error(__('No rows were inserted'));
  86. }
  87. $this->success();
  88. }
  89. public function edit($ids = null)
  90. {
  91. $row = $this->model->get($ids);
  92. if (!$row) {
  93. $this->error(__('No Results were found'));
  94. }
  95. $where = [
  96. 'sim_type' => $row->sim_type,
  97. 'fault_type' => 1,
  98. 'fault_state'=>0
  99. ];
  100. $selectData = Fault::where($where)->select();
  101. foreach ($selectData as $key => $value){
  102. $children = Fault::where(['parent_fault_id'=>$value['fault_id'],'fault_type' => 3,'fault_state'=>0])->select();
  103. $selectData[$key]['children'] = $children;
  104. }
  105. unset($value);
  106. $selectData = collection($selectData)->toArray();
  107. $row['selectData'] = $selectData;
  108. $row['fault_id_arr'] = !empty($row['fault_id']) ? explode(',',$row['fault_id']):'';
  109. if (false === $this->request->isPost()) {
  110. $this->view->assign('row', $row);
  111. $this->assignConfig('rel_Ids',explode(',',$row->fault_id));
  112. $this->assignConfig('rel_Names',explode(',',$row->fault_name));
  113. $this->assignConfig('prel_Ids',explode(',',$row->pfault_id));
  114. return $this->view->fetch();
  115. }
  116. $params = $this->request->post('row/a');
  117. if (empty($params)) {
  118. $this->error(__('Parameter %s can not be empty', ''));
  119. }
  120. if(empty($params['fault_id'])){
  121. $this->error('请选择任务故障');
  122. }
  123. if(count(explode(',',$params['fault_id']))>3){
  124. $this->error('故障部位必须最多为3个');
  125. }
  126. $params['fault_total'] = count(explode(',',$params['fault_id']));
  127. $params = $this->preExcludeFields($params);
  128. $result = false;
  129. Db::startTrans();
  130. try {
  131. $fult_ids = $params['fault_id'];
  132. $result = $row->allowField(true)->save($params);
  133. foreach(explode(',',$fult_ids) as $item){
  134. if(!empty($item)){
  135. $isset = Db::name('task_fault')->where(['task_id'=>$row->task_id,'fault_id'=>$item])->find();
  136. if(empty($isset)){
  137. $arr = [
  138. 'task_id' => $row->task_id,
  139. 'fault_id' => $item,
  140. 'flag' => 1,
  141. 'update_time' => date('Y-m-d H:i:s'),
  142. ];
  143. Db::name('task_fault')->insert($arr);
  144. }
  145. }
  146. }
  147. //删除任务和故障关联表中 编辑后不存在的数据
  148. Db::name('task_fault')->where(['task_id'=>$row->task_id,'fault_id'=>['not in',$params['fault_id']]])->delete();
  149. Db::commit();
  150. } catch (ValidateException|PDOException|Exception $e) {
  151. Db::rollback();
  152. $this->error($e->getMessage());
  153. }
  154. if (false === $result) {
  155. $this->error(__('No rows were updated'));
  156. }
  157. $this->success();
  158. }
  159. //删除的时候,同步删除好几个表
  160. /**
  161. * 全部3个故障内容的训练任务
  162. * @return void
  163. */
  164. public function selectlist()
  165. {
  166. $where = [];
  167. $where['fault_total'] = 3;
  168. $res = $this->model->where($where)->select();
  169. foreach ($res as $kk=> $vv) {
  170. $options[$vv['task_id']] = $vv['name'];
  171. }
  172. return json($options);
  173. }
  174. }