Index.php 15.7 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
<?php

namespace app\api\controller\v7\screen;

use app\admin\model\inspection\Plan;
use app\admin\model\inspection\Project;
use app\admin\model\inspection\Staff;
use app\admin\model\inspection\Warning;
use app\admin\model\inspection\Warningreply;
use app\admin\model\inspection\Workorder;
use app\admin\model\inspection\Workorderdeal;
use app\admin\model\pipes\Line;
use app\admin\model\pipes\Route;
use app\admin\model\pipes\Waterhead;
use app\admin\model\pipes\Well;
use app\common\controller\Api;
use think\Db;

/**
 * 一图看
 * 一河五库基础信息相关接口
 * Class Network
 * @package app\api\controller\v7\screen\Index
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    protected $workorderStatusList = [
        '0' => '待处理',
        '1' => '待审核',
        '2' => '已处理',
        '3' => '审核未通过',
        '4' => '无法处理'
    ];

    protected $warningStatusList = [
        '-1' => '审核失败',
        '0' => '待审核',
        '1' => '审核通过',
        '2' => '处理完成',
    ];

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

    /**
     * 一河五库基础信息
     */
    public function info()
    {
        $pictures = config("site.yhwk_baseinfo_pictures");
        $video = config("site.yhwk_baseinfo_vedio");
        $message = config("site.yhwk_baseinfo_message");
        $result = [
            'base_statistics' => [
                'total_device_num' => 159,
                'cover_an_area' => '798.6亩',
                'water_num' => 2,
                'offline_device_num' => '--',
                'network_mileage' => '--',
                'water_supply' => '--',
            ],
            'supply_map' => [
                'hour' => [
                    'x' => [date('H:i',strtotime('-6 hour')),date('H:i',strtotime('-5 hour')),date('H:i',strtotime('-4 hour')),date('H:i',strtotime('-3 hour')),date('H:i',strtotime('-2 hour')),date('H:i',strtotime('-1 hour')),date('H:i')],
                    'in' => [random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000)],
                    'out' => [random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000)],
                ],
                'week' => [
                    'x' => ['六天前','五天前','四天前','三天前','两天前','一天前','今天'],
                    'in' => [random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000)],
                    'out' => [random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000)],
                ],
                'month' => [
                    'x' => ['六月前','五月前','四月前','三月前','两月前','一月前','本月'],
                    'in' => [random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000)],
                    'out' => [random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000),random_int(1000,2000)],
                ],
            ],
            'message' => $message,
            'pictures' => full_image_kevin($pictures),
            'video' => full_image_kevin($video),
        ];

        $this->success("一河五库基础信息获取成功", $result);
    }

    /**
     * 巡检统计
     */
    public function inspection(){
        $projectModel = new Project();
        $total_num = $projectModel->count();
        $finish_num = $projectModel->where(['status' => 1])->count();
        $completion_rate = round($finish_num/$total_num * 100, 2) . '%';

        //获取最近的异常上报数据30条
        $warningModel = new Warning();
        $warningList = $warningModel
            ->alias('w')
            ->field('w.id,w.title,s.staff_name,w.createtime')
            ->join('inspection_staff s', 's.id=w.staff_id', 'LEFT')
            ->order(['id' => 'desc'])
            ->page(1, 30)
            ->select();
        foreach ($warningList as $item){
            $item['createtime_text'] = date('Y-m-d H:i', $item['createtime']);
        }
        $result = [
            'completion_rate' => $completion_rate,
            'finish_num' => $finish_num,
            'total_num' => $total_num,
            'warning_list' => $warningList
        ];
        $this->success('成功', $result);
    }

    /**
     * 工单统计
     */
    public function workorder(){
        $workorderModel = new Workorder();
        //今天
        $datetime = strtotime(date('Y-m-d'));
        //近7天
        $datetime7 = strtotime(date('Y-m-d')) - 86400*6;
        $today = $workorderModel
            ->alias('w')
            ->field('w.id,w.message,s.staff_name,w.start_time,w.status')
            ->join('inspection_staff s', 's.id=w.staff_id', 'LEFT')
            ->where(['start_time' => ['egt', $datetime]])
            ->order(['start_time' => 'desc'])
            ->select();
        foreach ($today as $item){
            $item['start_time_text'] = date('Y-m-d H:i', $item['start_time']);
            $item['status'] = $this->workorderStatusList[$item['status']];
        }
        $seven = $workorderModel
            ->alias('w')
            ->field('w.id,w.message,s.staff_name,w.start_time,w.status')
            ->join('inspection_staff s', 's.id=w.staff_id', 'LEFT')
            ->where(['start_time' => ['egt', $datetime7]])
            ->order(['start_time' => 'desc'])
            ->select();
        foreach ($seven as $value){
            $value['start_time_text'] = date('Y-m-d H:i', $value['start_time']);
            $value['status'] = $this->workorderStatusList[$value['status']];
        }
        $result = [
            'today' => $today,
            'seven' => $seven,
        ];
        $this->success('成功', $result);
    }

    /**
     * 异常详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function warningDetail(){
        $params = $this->request->get();
        if (empty($params['id'])){
            $this->error('id参数必须');
        }
        $warningModel = new Warning();
        $warning = $warningModel
            ->alias('w')
            ->field([
                'w.*',
                'a.area_name',
                's.site_name',
                'i.item_name',
                'c.name as category_name',
                'c_p.name as category_pname',
            ])
            ->join('inspection_area a', 'a.id=w.area_id', 'LEFT')
            ->join('inspection_area_site s', 's.id=w.areasite_id', 'LEFT')
            ->join('inspection_area_item i', 'i.id=w.areaitem_id', 'LEFT')
            ->join('inspection_warning_category c', 'c.id=w.category_id', 'LEFT')
            ->join('inspection_warning_category c_p', 'c_p.id=c.pid', 'LEFT')
            ->where([
                'w.id' => $params['id']
            ])
            ->find();
        if ($warning){
            $warning = $warning->toArray();
            if (!empty($warning['cover_images'])){
                $warning['cover_images'] = explode(',', $warning['cover_images']);
            }else{
                $warning['cover_images'] = [];
            }

            $warning['createtime'] = date('Y-m-d H:i', $warning['createtime']);
            //查询员工staff_id
            $staffModel = new Staff();
            $warning['staff_info'] = $staffModel
                ->where(['id' => $warning['staff_id']])
                ->find();
            if (isset($warning['staff_info']['createtime'])){
                $warning['staff_info']['createtime'] = date('Y-m-d H:i', $warning['staff_info']['createtime']);
            }
            $warning['status_text'] = $this->warningStatusList[$warning['status']];
            //查询回复数据
            $warningReplyModel = new Warningreply();
            $replyRecord = $warningReplyModel->where(['wid' => $warning['id']])->order(['createtime' => 'desc'])->select();
            if (!empty($replyRecord)){
                foreach ($replyRecord as &$r){
                    $r['createtime'] = date('Y-m-d H:i', $r['createtime']);
                }
            }
            $warning['reply_record'] = $replyRecord;
            $this->success('成功', $warning);
        }else{
            $this->error('隐患不存在');
        }
    }

    /**
     * 工单详情
     */
    public function workorderDetail(){
        $id = $this->request->get('id');
        if (empty($id)){
            $this->error('工单id必须');
        }

        $workorder = Workorder::get($id);
        if (!$workorder){
            $this->error('工单不存在');
        }
        $workorder = $workorder->toArray();
        $workorder['status_text'] = $this->workorderStatusList[$workorder['status']];
        //查询异常报告信息
        $warningModel = new Warning();
        $warning = $warningModel
            ->alias('w')
            ->field([
                'w.*',
                'a.area_name',
                's.site_name',
                'i.item_name',
                'wc.name as category_name',
            ])
            ->join('inspection_area a','a.id=w.area_id', 'LEFT')
            ->join('inspection_area_site s','s.id=w.areasite_id', 'LEFT')
            ->join('inspection_area_item i','i.id=w.areaitem_id', 'LEFT')
            ->join('inspection_warning_category wc','wc.id=w.category_id', 'LEFT')
            ->where([
                'w.id' => $workorder['warning_id']
            ])
            ->find();
        $workorder['warning_info'] = $warning;
        //查询处理人员信息
        $staffModel = new Staff();
        $staff = $staffModel
            ->alias('s')
            ->field([
                's.*',
                'u.nickname',
                'u.avatar'
            ])
            ->join('user u','u.id=s.user_id', 'LEFT')
            ->where([
                's.id' => $workorder['staff_id']
            ])
            ->find();
        $workorder['staff_info'] = $staff;
        //获取工单处理结果
        $workorderdealModel = new Workorderdeal();
        $workorderdeal = $workorderdealModel
            ->where([
                'workorder_id' => $workorder['id']
            ])
            ->order([
                'id' => 'desc'
            ])
            ->select();
        $workorder['deal_info'] = $workorderdeal;
        $this->success('成功', $workorder);
    }

    //获取所有的放水口
    public function wellList(){
        $params = $this->request->get();
        $where = [];
        if (!empty($params['query'])) {
            $where['well.well_name|well.well_code'] = ['LIKE', "%{$params['query']}%", 'OR'];
        }

        if (!empty($params['network_id'])) {
            $where['well.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['well.network_id'] = $find['id'];
            }
        }

        $list = (new Well())
            ->with('network')
            ->where($where)
            ->select();
        $this->success('成功', $list);
    }

    //获取最新一条工单数据
    public function firstWorkorder(){
        $workorder = (new Workorder())->order(['id' => 'desc'])->find();
        if (!$workorder){
            $this->error('暂无工单');
        }
        $workorder = $workorder->toArray();
        $workorder['status_text'] = $this->workorderStatusList[$workorder['status']];
        //查询异常报告信息
        $warningModel = new Warning();
        $warning = $warningModel
            ->alias('w')
            ->field([
                'w.*',
                'a.area_name',
                's.site_name',
                'i.item_name',
                'wc.name as category_name',
            ])
            ->join('inspection_area a','a.id=w.area_id', 'LEFT')
            ->join('inspection_area_site s','s.id=w.areasite_id', 'LEFT')
            ->join('inspection_area_item i','i.id=w.areaitem_id', 'LEFT')
            ->join('inspection_warning_category wc','wc.id=w.category_id', 'LEFT')
            ->where([
                'w.id' => $workorder['warning_id']
            ])
            ->find();
        $workorder['warning_info'] = $warning;
        //查询处理人员信息
        $staffModel = new Staff();
        $staff = $staffModel
            ->alias('s')
            ->field([
                's.*',
                'u.nickname',
                'u.avatar'
            ])
            ->join('user u','u.id=s.user_id', 'LEFT')
            ->where([
                's.id' => $workorder['staff_id']
            ])
            ->find();
        $workorder['staff_info'] = $staff;
        //获取工单处理结果
        $workorderdealModel = new Workorderdeal();
        $workorderdeal = $workorderdealModel
            ->where([
                'workorder_id' => $workorder['id']
            ])
            ->order([
                'id' => 'desc'
            ])
            ->select();
        $workorder['deal_info'] = $workorderdeal;
        $this->success('成功', $workorder);
    }

    //获取巡检员行走轨迹
    public function getTravel(){
        $params = $this->request->get();
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 50;
        if (empty($params['datetime'])){
            //默认取当天0点的时间戳
            $datetime = strtotime(date('Y-m-d'));
        }else{
            $datetime = strtotime(date('Y-m-d', $params['datetime']));
        }
        if (empty($params['plan_id'])){
            $this->error('计划ID必须');
        }
        $plan = Plan::get($params['plan_id']);
        if (!$plan){
            $this->error('计划不存在');
        }

        $list = [];
        $total = Db::name('inspection_position')
            ->where([
                'plan_id' => $params['plan_id'],
                'createtime' => ['between', [$datetime, $datetime + 24*3600 - 1]]
            ])
            ->count();
        if ($total > 0){
            $list = Db::name('inspection_position')
                ->where([
                    'plan_id' => $params['plan_id'],
                    'createtime' => ['between', [$datetime, $datetime + 24*3600 - 1]]
                ])
                ->order(['createtime' => 'asc'])
                ->page($page, $limit)
                ->select();
        }
        //巡检人员详情
        $staff = Staff::get($plan['staff_id']);
        //查询巡检人员位置当前位置
        $position = Db::name('inspection_position')
            ->where([
                'plan_id' => $params['plan_id'],
                'createtime' => ['between', [$datetime, $datetime + 24*3600 - 1]]
            ])
            ->order(['createtime' => 'desc'])
            ->find();
        $result = [
            'page' => $page,
            'limit' => $limit,
            'total' => $total,
            'rows' => $list,
            'position' => $position,
            'staff_info' => $staff,
        ];
        $this->success('成功', $result);
    }

    //平台开始运行时间、安全运行天数、当前在线人数
    public function loginPage(){
        $result = [
            'start_time' => date('Y年m月d日 H:i:s'),
            'run_days' => 10,
            'online_num' => 20,
        ];
        $this->success('成功', $result);
    }
}