Line.php 21.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
<?php

namespace app\api\controller\pipes;

use app\admin\model\pipes\Category;
use app\admin\model\pipes\Section;
use fast\Random;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

/**
 * 管线
 */
class Line extends Base
{
    /**
     * Line模型对象
     * @var \app\admin\model\pipes\Line
     */
    protected $model = null;

    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\pipes\Line();
    }

    //获取管径分类列表
    public function category(){
        $categoryModel = new Category();
        $cateList = $categoryModel->field('start_diameter,end_diameter,pipe_color')->order(['id' => 'asc'])->select();
        foreach ($cateList as $cate){
            if ($cate['start_diameter'] == 0){
                $cate['title'] = '管径<'.$cate['end_diameter'];
            }
            if ($cate['end_diameter'] == 0){
                $cate['title'] = '管径>'.$cate['start_diameter'];
            }
            if ($cate['start_diameter'] != 0 && $cate['end_diameter'] != 0){
                $cate['title'] = $cate['start_diameter'].'<管径<'.$cate['end_diameter'];
            }
        }
        $this->success('成功', $cateList);
    }

    //获取里程段列表(供前端添加编辑用作选项)
    public function getSectionList(){
        $params = $this->request->get();
        if (!empty($params['network_id'])){
            $where['network_id'] = $params['network_id'];
        }else{
            //没有管网ID参数则默认获取第一个管网的ID
            $networkModel = new \app\admin\model\pipes\Network();
            $find = $networkModel->field('id')->order(['id' => 'asc'])->find();
            if ($find){
                $where['network_id'] = $find['id'];
            }
        }
        $sectionModel = new Section();
        $sectionList = $sectionModel
            ->where($where)
            ->select();
        $this->success('成功', $sectionList);
    }

    //根据路线返回所有的管线信息
    public function route(){
        $params = $this->request->post();
        if (!empty($params['route_ids'])){
            $where['id'] = ['in', $params['route_ids']];
        }else{
            //不传该参数,或者为空数组,则返回空数据
            $this->success('成功', []);
        }
        if (!empty($params['network_id'])){
            $where['network_id'] = $params['network_id'];
        }else{
            //没有管网ID参数则默认获取第一个管网的ID
            $networkModel = new \app\admin\model\pipes\Network();
            $find = $networkModel->field('id')->order(['id' => 'asc'])->find();
            if ($find){
                $where['network_id'] = $find['id'];
            }
        }
//        if (!empty($params['line_diameter_out'])){
//            if (!is_array($params['line_diameter_out'])){
//                $this->error('line_diameter_out参数格式错误');
//            }
//            $whereOr = $params['line_diameter_out'];
//        }else{
//            $this->success('',[]);
//        }
        $routeModel = new \app\admin\model\pipes\Route();
        $routeList = $routeModel
            ->where($where)
            ->select();
        foreach ($routeList as $route){
            $route['types'] = 'line';//前端需要的字段
            $lines = $this->model
                ->field('id,line_start_lng,line_start_lat,line_end_lng,line_end_lat')
                ->where(['route_id' => $route['id']])
//                ->where(function ($query) use($whereOr) {
//                    if (!empty($whereOr)){
//                        foreach ($whereOr as $wh){
//                            if (is_array($wh)){
//                                $start_diameter = $wh[0];
//                                $end_diameter = $wh[1];
//                            }else{
//                                $diameterArr = explode(',', $wh);
//                                $start_diameter = $diameterArr[0];
//                                $end_diameter = $diameterArr[1];
//                            }
//                            if ($end_diameter == 0){
//                                $query->whereOr(['line_diameter_out' => ['egt', $start_diameter]]);
//                            }else{
//                                $query->whereOr(['line_diameter_out' => ['between', $wh]]);
//                            }
//                        }
//                    }
//                })
                ->order(['id' => 'asc'])
                ->select();
            //查询所有管线坐标
            $positionArr = [];
            foreach ($lines as $line){
                $positionArr[] = [$line['line_start_lng'], $line['line_start_lat']];
                $positionArr[] = [$line['line_end_lng'], $line['line_end_lat']];
            }
            $route['position_arr'] = $positionArr;
            //查询第一条管线信息
            $start_line = $this->model
                ->where(['route_id' => $route['id']])
                ->order(['id' => 'asc'])
                ->find();
            //查询最后一条管线信息
            $end_line = $this->model
                ->where(['route_id' => $route['id']])
                ->order(['id' => 'desc'])
                ->find();
            $route['position_start_info'] = $start_line;
            $route['position_end_info'] = $end_line;
        }
        $this->success('成功', $routeList);
    }
    //根据里程段返回所有的管线信息
    public function section(){
        $params = $this->request->post();
        if (!empty($params['network_id'])){
            $where['network_id'] = $params['network_id'];
        }else{
            //没有管网ID参数则默认获取第一个管网的ID
            $networkModel = new \app\admin\model\pipes\Network();
            $find = $networkModel->field('id')->order(['id' => 'asc'])->find();
            if ($find){
                $where['network_id'] = $find['id'];
            }
        }
        if (!empty($params['line_diameter_out'])){
            if (!is_array($params['line_diameter_out'])){
                $this->error('line_diameter_out参数格式错误');
            }
            $whereOr = $params['line_diameter_out'];
        }else{
            //没这个条件则返回空数据
            $this->success('',[]);
        }
        $sectionModel = new Section();
        $sectionList = $sectionModel
            ->where($where)
            ->where(function ($query) use($whereOr) {
                if (!empty($whereOr)){
                    foreach ($whereOr as $wh){
                        if (is_array($wh)){
                            $start_diameter = $wh[0];
                            $end_diameter = $wh[1];
                        }else{
                            $diameterArr = explode(',', $wh);
                            $start_diameter = $diameterArr[0];
                            $end_diameter = $diameterArr[1];
                        }
                        if ($end_diameter == 0){
                            $query->whereOr(['line_diameter_out' => ['egt', $start_diameter]]);
                        }else{
                            $query->whereOr(['line_diameter_out' => ['between', $wh]]);
                        }
                    }
                }
            })
            ->select();
        //获取所有的管径分类
        $categoryModel = new Category();
        $categoryList = $categoryModel->select();
        $sectionListArr = [];
        foreach ($sectionList as $section){
            $section['types'] = 'line';//前端需要的字段
            //把颜色值附上
            foreach ($categoryList as $category){
                if ($section['line_diameter_out'] >= $category['start_diameter'] && $section['line_diameter_out'] < $category['end_diameter']){
                    $section['pipe_color'] = $category['pipe_color'];
                }elseif ($section['line_diameter_out'] >= $category['start_diameter'] && $category['end_diameter'] == 0){
                    $section['pipe_color'] = $category['pipe_color'];
                }
            }
            $lines = $this->model
                ->field('id,line_start_lng,line_start_lat,line_end_lng,line_end_lat')
                ->where(['section_id' => $section['id']])
                ->order(['id' => 'asc'])
                ->select();
            //查询所有管线坐标
            $positionArr = [];
            foreach ($lines as $line){
                $positionArr[] = [$line['line_start_lng'], $line['line_start_lat']];
                $positionArr[] = [$line['line_end_lng'], $line['line_end_lat']];
            }
            $section['position_arr'] = $positionArr;
            //查询第一条管线信息
            $start_line = $this->model
                ->where(['section_id' => $section['id']])
                ->order(['id' => 'asc'])
                ->find();
            //查询最后一条管线信息
            $end_line = $this->model
                ->where(['section_id' => $section['id']])
                ->order(['id' => 'desc'])
                ->find();
            $section['position_start_info'] = $start_line;
            $section['position_end_info'] = $end_line;
            if (!empty($positionArr)){
                $sectionListArr[] = $section;
            }
        }
        $this->success('成功', $sectionListArr);
    }

    //(大屏使用)根据管线和里程段获取数据
    public function routeSection(){
        $params = $this->request->post();
        if (!empty($params['network_id'])){
            $where['network_id'] = $params['network_id'];
        }else{
            //没有管网ID参数则默认获取第一个管网的ID
            $networkModel = new \app\admin\model\pipes\Network();
            $find = $networkModel->field('id')->order(['id' => 'asc'])->find();
            if ($find){
                $where['network_id'] = $find['id'];
            }
        }
        $sectionModel = new Section();
        //根据路线
        $routeModel = new \app\admin\model\pipes\Route();
        $routeList = $routeModel
            ->where($where)
            ->select();
        foreach ($routeList as $route){
            $sectionList = $sectionModel
                ->where(['route_id' => $route['id']])
                ->select();
            $sectionListArr = [];
            foreach ($sectionList as $section){
                $lines = $this->model
                    ->field('id,line_start_lng,line_start_lat,line_end_lng,line_end_lat,line_start_height,line_end_height')
                    ->where(['section_id' => $section['id']])
                    ->order(['id' => 'asc'])
                    ->select();
                //查询所有管线坐标
                $positionArr = [];
                foreach ($lines as $line){
                    $line['line_start_height'] = $line['line_start_height'] ?? 0;
                    $line['line_end_height'] = $line['line_end_height'] ?? 0;
                    $positionArr[] = [$line['line_start_lng'], $line['line_start_lat'], $line['line_start_height']];
                    $positionArr[] = [$line['line_end_lng'], $line['line_end_lat'], $line['line_end_height']];
                }
                $section['position_arr'] = $positionArr;
                //查询第一条管线信息
                $start_line = $this->model
                    ->where(['section_id' => $section['id']])
                    ->order(['id' => 'asc'])
                    ->find();
                //查询最后一条管线信息
                $end_line = $this->model
                    ->where(['section_id' => $section['id']])
                    ->order(['id' => 'desc'])
                    ->find();
                $section['position_start_info'] = $start_line;
                $section['position_end_info'] = $end_line;
                if (!empty($positionArr)){
                    $sectionListArr[] = $section;
                }
            }
            $route['section'] = $sectionListArr;

            $lines1 = $this->model
                ->field('id,line_start_lng,line_start_lat,line_end_lng,line_end_lat,line_start_height,line_end_height')
                ->where(['route_id' => $route['id']])
                ->order(['id' => 'asc'])
                ->select();
            //查询所有管线坐标
            $positionArr1 = [];
            foreach ($lines1 as $line1){
                $positionArr1[] = [$line1['line_start_lng'], $line1['line_start_lat'], $line1['line_start_height']];
                $positionArr1[] = [$line1['line_end_lng'], $line1['line_end_lat'], $line1['line_end_height']];
            }
            $route['position_arr'] = $positionArr1;
            //查询第一条管线信息
            $start_line1 = $this->model
                ->where(['route_id' => $route['id']])
                ->order(['id' => 'asc'])
                ->find();
            //查询最后一条管线信息
            $end_line1 = $this->model
                ->where(['route_id' => $route['id']])
                ->order(['id' => 'desc'])
                ->find();
            $route['position_start_info'] = $start_line1;
            $route['position_end_info'] = $end_line1;
            $route['id'] = $route['id']+100000;
        }
        $this->success('成功', $routeList);
    }

    //获取最新的一条管线信息
    public function getLast(){
        $list = $this->model
            ->with('network,route')
            ->order(['line.id' => 'desc'])
            ->find();
        $this->success('成功', $list);
    }

    public function lists(){
        $params = $this->request->post();
        $where = [];
        if (!empty($params['query'])){
            $where['line.line_start_name|line.line_end_name|line.line_code'] = ['LIKE', "%{$params['query']}%", 'OR'];
        }
        if (!empty($params['network_id'])){
            $where['line.network_id'] = $params['network_id'];
        }else{
            //没有管网ID参数则默认获取第一个管网的ID
            $networkModel = new \app\admin\model\pipes\Network();
            $find = $networkModel->field('id')->order(['id' => 'asc'])->find();
            if ($find){
                $where['line.network_id'] = $find['id'];
            }
        }
        $whereOr = [];
        if (!empty($params['line_diameter_out'])){
            if (!is_array($params['line_diameter_out'])){
                $this->error('line_diameter_out参数格式错误');
            }
            $whereOr = $params['line_diameter_out'];
        }else{
            //不传该参数,或者为空数组,则返回空数据
            $this->success('成功', []);
        }
        $list = $this->model
            ->field('line.*,n.network_name,r.route_name')
            ->alias('line')
            ->join('pipes_network n', 'n.id=line.network_id', 'LEFT')
            ->join('pipes_route r', 'r.id=line.route_id', 'LEFT')
            ->where($where)
            ->where(function ($query) use($whereOr) {
                if (!empty($whereOr)){
                    foreach ($whereOr as $wh){
                        if (is_array($wh)){
                            $start_diameter = $wh[0];
                            $end_diameter = $wh[1];
                        }else{
                            $diameterArr = explode(',', $wh);
                            $start_diameter = $diameterArr[0];
                            $end_diameter = $diameterArr[1];
                        }
                        if ($end_diameter == 0){
                            $query->whereOr(['line.line_diameter_out' => ['egt', $start_diameter]]);
                        }else{
                            $query->whereOr(['line.line_diameter_out' => ['between', $wh]]);
                        }
                    }
                }
            })
            ->select();
        //获取所有的管径分类
        $categoryModel = new Category();
        $categoryList = $categoryModel->select();
        foreach ($list as $item){
            $item['types'] = 'line';
            //把颜色值附上
            foreach ($categoryList as $category){
                if ($item['line_diameter_out'] >= $category['start_diameter'] && $item['line_diameter_out'] < $category['end_diameter']){
                    $item['pipe_color'] = $category['pipe_color'];
                }elseif ($item['line_diameter_out'] >= $category['start_diameter'] && $category['end_diameter'] == 0){
                    $item['pipe_color'] = $category['pipe_color'];
                }
            }
        }
        $this->success('成功', $list);
    }

    public function add(){
        $params = $this->request->post();

        //生成编号
        do {
            $params['line_code'] = 'L' . Random::alnum(5);
        } while ($this->model->where(['line_code' => $params['line_code']])->find());

        //数据验证
        $result = $this->validate($params,'app\admin\validate\pipes\Line');
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }

        //获取管网编码
        $network = \app\admin\model\pipes\Network::get($params['network_id']);
        if (!$network){
            $this->error('管网不存在');
        }

        $params['network_code'] = $network['network_code'];

        //获取路线编码
        $route = \app\admin\model\pipes\Route::get($params['route_id']);
        if (!$route){
            $this->error('路线不存在');
        }

        $params['route_code'] = $route['route_code'];

        //计算管线内径
        $params['line_diameter_in'] = bcsub($params['line_diameter_out'], $params['line_thickness'] * 2, 2);

        $result = $this->model->allowField(true)->save($params);
        if ($result !== false){
            $this->success('添加成功');
        }
        $this->error('添加失败');
    }

    //保存绘制的管线可以是多条数据
    public function save(){
        $params = $this->request->post();

        if (empty($params['network_id'])){
            $this->error('管网ID必须');
        }

        if (empty($params['line_arr'])){
            $this->error('管线坐标数据必须');
        }

        if (!is_array($params['line_arr'])){
            $this->error('管线坐标数据格式错误');
        }

        Db::startTrans();
        try {
            foreach ($params['line_arr'] as $line){
                if (empty($line['line_start_lng'])){
                    throw new ValidateException('缺少起点经度');
                }
                if (empty($line['line_start_lat'])){
                    throw new ValidateException('缺少起点纬度');
                }
                if (empty($line['line_end_lng'])){
                    throw new ValidateException('缺少终点经度');
                }
                if (empty($line['line_end_lat'])){
                    throw new ValidateException('缺少终点纬度');
                }
                $data = [
                    'network_id' => $params['network_id'],
                    'line_start_lng' => $line['line_start_lng'],
                    'line_start_lat' => $line['line_start_lat'],
                    'line_end_lng' => $line['line_end_lng'],
                    'line_end_lat' => $line['line_end_lat']
                ];

                //生成编号
                do {
                    $data['line_code'] = 'L' . Random::alnum(5);
                } while ($this->model->where(['line_code' => $data['line_code']])->find());

                //获取管网编码
                $network = \app\admin\model\pipes\Network::get($params['network_id']);
                if (!$network){
                    $this->error('管网不存在');
                }

                $data['network_code'] = $network['network_code'];

                $lineModel = new \app\admin\model\pipes\Line();

                $lineModel->allowField(true)->save($data);
            }
            Db::commit();
        }catch (ValidateException|PDOException|Exception $e){
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success('保存成功');
    }

    public function edit(){
        $params = $this->request->post();
        if (empty($params['id'])){
            $this->error('id必须');
        }
        $row = $this->model->where(['id' => $params['id']])->find();
        if (!$row){
            $this->error('数据不存在');
        }
        //数据验证
        $result = $this->validate($params,'app\admin\validate\pipes\Line');
        if(true !== $result){
            // 验证失败 输出错误信息
            $this->error($result);
        }

        //获取管网编码
        $network = \app\admin\model\pipes\Network::get($params['network_id']);
        if (!$network){
            $this->error('管网不存在');
        }

        $params['network_code'] = $network['network_code'];

        //获取路线编码
        $route = \app\admin\model\pipes\Route::get($params['route_id']);
        if (!$route){
            $this->error('路线不存在');
        }

        $params['route_code'] = $route['route_code'];

        //计算管线内径
        $params['line_diameter_in'] = bcsub($params['line_diameter_out'], $params['line_thickness'] * 2, 2);

        $result = $row->allowField(true)->save($params);
        if ($result !== false){
            $this->success('编辑成功');
        }
        $this->error('编辑失败');
    }

    public function del(){
        $params = $this->request->post();
        if (empty($params['id'])){
            $this->error('id必须');
        }
        $row = $this->model->where(['id' => $params['id']])->find();
        if (!$row){
            $this->error('数据不存在');
        }
        $result = $row->delete();
        if ($result !== false){
            $this->success('删除成功');
        }
        $this->error('删除失败');
    }
}