Route.php 6.8 KB
<?php

namespace app\admin\controller\inspection;

use app\admin\model\inspection\Routesite;
use app\common\controller\Backend;
use fast\Tree;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 巡检路线管理
 *
 * @icon fa fa-circle-o
 */
class Route extends Backend
{
    
    /**
     * Route模型对象
     * @var \app\admin\model\inspection\Route
     */
    protected $model = null;
    protected $noNeedRight = ['dynamicselect','sel_area_site'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\inspection\Route;

    }

    public function import()
    {
        parent::import();
    }

    /**
     * 选择巡检点
     */
    public function sel_area_site(){

        return $this->fetch();
    }


    public function dynamicselect(){
        $route = new \app\admin\model\inspection\Route();
        $list = $route->order("id desc")->select();
        foreach($list as $k=>$v){
            $agentJson[$v['id']] = $v['route_name'];
        }

        echo json_encode($agentJson,JSON_FORCE_OBJECT);
    }

    public function add(){
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);

                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                $result = false;
                Db::startTrans();
                try {
                    //是否采用模型验证
                    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(true)->validate($validate);
                    }
                    $result = $this->model->allowField(true)->save($params);
                    if($result){
                        if(empty($params['configgroup'])){
                            $this->error('请选择巡检点');
                        }
                        foreach($params['configgroup'] as $k=>$v){
                            $routesiteModel = new Routesite();
                            if(!$routesiteModel->where([
                                'route_id'=>$this->model->id,
                                'area_site_id'=>$v['id']
                            ])->find()){
                                $routesiteModel = new Routesite();
                                $routesiteModel->save([
                                    'route_id'=>$this->model->id,
                                    'area_site_id'=>$v['id']
                                ]);
                            }

                        }
                    }
                    Db::commit();
                } catch (ValidateException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (PDOException $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error($e->getMessage());
                }
                if ($result !== false) {
                    $this->success();
                } else {
                    $this->error(__('No rows were inserted'));
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $area_id = $this->request->get("areasite_id");
        $this->assign('areasite_id',$area_id);
        return $this->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {

            $params = $this->request->post("row/a");
            if ($params) {
                $params = $this->preExcludeFields($params);
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);

                    if(empty($params['configgroup'])){
                        $this->error('请选择巡检点');
                    }
                    $routesiteModel = new Routesite();
                    $routesiteModel->where(['route_id'=>$row->id])->delete();

                    foreach($params['configgroup'] as $k=>$v){
                        $routesiteModel = new Routesite();

                        if(!$routesiteModel->where([
                            'route_id'=>$row->id,
                            'area_site_id'=>$v['id']
                        ])->find()){

                            $routesiteModel = new Routesite();
                            $routesiteModel->save([
                                'route_id'=>$row->id,
                                'area_site_id'=>$v['id']
                            ]);
                        }
                    }


                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);

        //获取巡检点
        $routesiteModel = new Routesite();
        $routesiteList = $routesiteModel
            ->alias("routesite")
            ->field("routesite.*,site.site_name")
            ->join("inspection_area_site site","routesite.area_site_id=site.id")
            ->where(['routesite.route_id'=>$row['id']])->select();

        $this->assign('routesiteList',$routesiteList);
        return $this->view->fetch();
    }
    

}