| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 | <?phpnamespace app\admin\controller\teacher;use app\common\controller\Backend;use app\common\model\Config as ConfigModel;use think\Db;use think\exception\PDOException;use think\exception\ValidateException;use app\admin\model\Fault;/** * sim-任务管理 * * @icon fa fa-circle-o */class Task extends Backend{    /**     * Task模型对象     * @var \app\admin\model\teacher\Task     */    protected $model = null;    protected $noNeedLogin = ['selectlist'];    public function _initialize()    {        parent::_initialize();        $this->model = new \app\admin\model\teacher\Task;        $this->assignConfig('sim_sim_type', ConfigModel::getSimTypeList());        $this->assign('sim_sim_type', ConfigModel::getSimTypeList());    }    /**     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改     */    public function add()    {        if (false === $this->request->isPost()) {            $this->assignConfig('rel_Ids', []);            $this->assignConfig('rel_Names', []);            $this->assignConfig('prel_Ids', []);            return $this->view->fetch();        }        $params = $this->request->post('row/a');        if (empty($params)) {            $this->error(__('Parameter %s can not be empty', ''));        }        if(empty($params['fault_id'])){            $this->error('请选择任务故障');        }        if(count(explode(',',$params['fault_id']))>3){            $this->error('故障部位最多为3个');        }        $params['fault_total'] = count(explode(',',$params['fault_id']));        $params = $this->preExcludeFields($params);        if ($this->dataLimit && $this->dataLimitFieldAutoFill) {            $params[$this->dataLimitField] = $this->auth->id;        }        $result = false;        Db::startTrans();        try {            $fult_ids = $params['fault_id'];            //是否采用模型验证            if ($this->modelValidate) {                $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));                $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;                $this->model->validateFailException()->validate($validate);            }            $result = $this->model->allowField(true)->save($params);            foreach(explode(',',$fult_ids) as $item){                if(!empty($item)){                    $arr = [                        'task_id' => $this->model->task_id,                        'fault_id' => $item,                        'flag' => 1,                        'update_time' => date('Y-m-d H:i:s'),                    ];                    Db::name('task_fault')->insert($arr);                }            }            Db::commit();        } catch (ValidateException|PDOException|Exception $e) {            Db::rollback();            $this->error($e->getMessage());        }        if ($result === false) {            $this->error(__('No rows were inserted'));        }        $this->success();    }    public function edit($ids = null)    {        $row = $this->model->get($ids);        if (!$row) {            $this->error(__('No Results were found'));        }        $where = [            'sim_type' => $row->sim_type,            'fault_type' => 1,            'fault_state'=>0        ];        $selectData = Fault::where($where)->select();        foreach ($selectData as $key => $value){            $children = Fault::where(['parent_fault_id'=>$value['fault_id'],'fault_type' => 3,'fault_state'=>0])->select();            $selectData[$key]['children'] = $children;        }        unset($value);        $selectData = collection($selectData)->toArray();        $row['selectData'] = $selectData;        $row['fault_id_arr'] = !empty($row['fault_id']) ? explode(',',$row['fault_id']):'';        if (false === $this->request->isPost()) {            $this->view->assign('row', $row);            $this->assignConfig('rel_Ids',explode(',',$row->fault_id));            $this->assignConfig('rel_Names',explode(',',$row->fault_name));            $this->assignConfig('prel_Ids',explode(',',$row->pfault_id));            return $this->view->fetch();        }        $params = $this->request->post('row/a');        if (empty($params)) {            $this->error(__('Parameter %s can not be empty', ''));        }        if(empty($params['fault_id'])){            $this->error('请选择任务故障');        }        if(count(explode(',',$params['fault_id']))>3){            $this->error('故障部位必须最多为3个');        }        $params['fault_total'] = count(explode(',',$params['fault_id']));        $params = $this->preExcludeFields($params);        $result = false;        Db::startTrans();        try {            $fult_ids = $params['fault_id'];            $result = $row->allowField(true)->save($params);            foreach(explode(',',$fult_ids) as $item){                if(!empty($item)){                    $isset = Db::name('task_fault')->where(['task_id'=>$row->task_id,'fault_id'=>$item])->find();                    if(empty($isset)){                        $arr = [                            'task_id' => $row->task_id,                            'fault_id' => $item,                            'flag' => 1,                            'update_time' => date('Y-m-d H:i:s'),                        ];                        Db::name('task_fault')->insert($arr);                    }                }            }            //删除任务和故障关联表中  编辑后不存在的数据            Db::name('task_fault')->where(['task_id'=>$row->task_id,'fault_id'=>['not in',$params['fault_id']]])->delete();            Db::commit();        } catch (ValidateException|PDOException|Exception $e) {            Db::rollback();            $this->error($e->getMessage());        }        if (false === $result) {            $this->error(__('No rows were updated'));        }        $this->success();    }    //删除的时候,同步删除好几个表    /**     * 全部3个故障内容的训练任务     * @return void     */    public function selectlist()    {        $where = [];        $where['fault_total'] = 3;        $res = $this->model->where($where)->select();        foreach ($res as $kk=> $vv) {            $options[$vv['task_id']] = $vv['name'];        }        return json($options);    }}
 |