Riverdata.php 13.2 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
<?php
/**
 * 河长制大屏接口
 * Created by PhpStorm.
 * User: pcl
 * Date: 2022/07/29
 * Time: 09:00
 */

namespace app\api\controller\v1;

use app\common\controller\Api;
use think\Db;
use think\Request;

header('Content-type:text/html; Charset=utf-8');
date_default_timezone_set('PRC');

class Riverdata extends Api
{
    protected $noNeedLogin = ['*'];//*无需验证登陆

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

    }

    /***
     * 今日问题上报列表
     */
    public function getAskList()
    {
        $list = Db::name('river_asklist')
            ->alias('a')
            ->join('river_ask_cat ac', 'a.cat_id=ac.id')
            ->join('river_list l', 'a.river_id=l.id')
            ->whereTime('a.createtime', 'today')
            ->field('a.id,a.title,a.status,a.address,a.lat,a.lng,a.createtime,ac.catname,l.name')
            ->order("a.id desc")
            ->limit(20)
            ->select();
        if (empty($list)) {
            $list = Db::name('river_asklist')
                ->alias('a')
                ->join('river_ask_cat ac', 'a.cat_id=ac.id')
                ->join('river_list l', 'a.river_id=l.id')
                ->field('a.id,a.title,a.status,a.address,a.lat,a.lng,a.createtime,ac.catname,l.name')
                ->order("a.id desc")
                ->limit(20)
                ->select();
        }
        if (!empty($list)) {
            foreach ($list as $k => $v) {
                $list[$k]['createtime'] = date('m/d H:i', $v['createtime']);
            }
        }

        $this->success('获取成功', $list);
    }

    /***
     * 问题详情信息
     */
    public function askInfo()
    {
        $aid = $this->request->post('aid', 0);
        $row = Db::name('river_asklist')
            ->alias('a')
            ->join('river_ask_cat ac', 'a.cat_id=ac.id')
            ->join('river_list l', 'a.river_id=l.id')
            ->join('user u', 'a.user_id=u.id')
            ->where(['a.id' => $aid])
            ->field('a.id,a.title,a.image,a.vdourl,a.status,a.address,a.lat,a.lng,a.createtime,ac.catname,l.name,u.username,u.mobile,u.avatar,u.group_id')
            ->find();
        if (empty($row)) {
            $this->error('内容不存在');
        }
        //处理图片和视频地址
        $row['createtime'] = setdateTimes($row['createtime']);
        if (!empty($row['image'])) {
            $imgarr = explode(',', $row['image']);
            $row['image'] = full_image_kevin($imgarr);
        }
        if (!empty($row['vdourl'])) {
            $vdourlarr = explode(',', $row['vdourl']);
            $row['vdourl'] = full_image_kevin($vdourlarr);
        }

        $this->success('获取成功', $row);
    }

    /***
     * 今日巡河用户及巡河轨迹
     * 列表
     */
    public function getLocationList()
    {
        //巡河列表
        $tday = date('Ymd', time());
        $wh['r.daytime'] = ['=', $tday];
        $wh['r.endtime'] = ['>', 0];
        $list = Db::name('river_patrol_list')
            ->alias('r')
            ->join('river_list l', 'r.river_id=l.id')
            ->join('user u', 'r.user_id=u.id')
            ->where($wh)
            ->field('r.id,r.createtime,r.endtime,r.ordersn,l.name,u.username,u.mobile,u.avatar,u.group_id,r.river_time,r.river_distance')
            ->order('r.id', 'desc')
            ->limit(50)
            ->select();
        if (empty($list)) {
            //今日没有记录,取最新50条
            $wh['r.endtime'] = ['>', 0];
            $list = Db::name('river_patrol_list')
                ->alias('r')
                ->join('river_list l', 'r.river_id=l.id')
                ->join('user u', 'r.user_id=u.id')
                ->where($wh)
                ->field('r.id,r.createtime,r.endtime,r.ordersn,l.name,u.username,u.mobile,u.avatar,u.group_id,r.river_time,r.river_distance')
                ->order('r.id', 'desc')
                ->limit(50)
                ->select();
        }
        $list2 = [];
        $ids2 = [];
        if (!empty($list)) {
            foreach ($list as $k => $v) {

                $ids2[$k] = $v['id'];
                $list2[$k][0] = $v['username'];
                $list2[$k][1] = $v['name'];
                //处理时间
                $list2[$k][2] = set_timearr_to_str($v['river_time']);
                $list2[$k][3] = $v['river_distance'] . 'm';
            }
        } else {
            $list2 = [];
            $ids2 = [];
        }
        $data['id'] = $ids2;
        $data['list'] = $list2;

        $this->success('获取成功', $data);
    }

    /****
     * 用户巡河轨迹详情
     */
    public function patrolInfo()
    {
        $id = $this->request->post('id', 0);
        $list = Db::name('river_patrol_list')
            ->alias('r')
            ->join('river_list l', 'r.river_id=l.id')
            ->join('user u', 'r.user_id=u.id')
            ->where(['r.id' => $id])
            ->field('r.id,r.createtime,r.endtime,r.ordersn,l.name,u.username,u.mobile,u.avatar,u.group_id,r.river_time,r.river_distance')
            ->find();
        if (empty($list)) {
            $this->error('数据不存在');
        }
        if (!empty($list)) {
            $list['river_time_str'] = set_timearr_to_str($list['river_time']);
            $list['river_time'] = json_decode($list['river_time'], true);
            $guiji = Db::name('river_patrol_logs')
                ->where(['ordersn' => $list['ordersn']])
                ->field('id,lat,lng')
                ->order('id', 'asc')
                ->select();
            $list['guiji'] = $guiji;
            $list['createtime'] = setdateTimes($list['createtime']);
            $list['endtime'] = setdateTimes($list['endtime']);
        }

        $this->success('获取成功', $list);
    }

    /****
     * 河湖列表
     */
    public function getRiverList()
    {
        //河流列表
        $helist = Db::name('river_list')
            ->where(['type' => '1', 'river_pid' => 0])
            ->field('id,river_code,name,type,cat_id,river_pid,user_name')
            ->order('id', 'desc')
            ->select();
        if (!empty($helist)) {
            foreach ($helist as $k => $v) {
                //获得分段列表
                $slist = Db::name('river_list')
                    ->where(['river_pid' => $v['id']])
                    ->field('id,river_code,name,type,cat_id,river_pid,user_name')
                    ->order('id', 'asc')
                    ->select();
                $helist[$k]['child'] = $slist;
            }
        }
        $data['heliu'] = $helist;
        //湖泊列表
        $hulist = Db::name('river_list')
            ->where(['type' => '2', 'river_pid' => 0])
            ->field('id,river_code,name,type,cat_id,river_pid,user_name')
            ->order('id', 'desc')
            ->select();
        if (!empty($hulist)) {
            foreach ($hulist as $kk => $vv) {
                //获得分段列表
                $slist2 = Db::name('river_list')
                    ->where(['river_pid' => $vv['id']])
                    ->field('id,river_code,name,type,cat_id,river_pid,user_name')
                    ->order('id', 'asc')
                    ->select();
                $hulist[$kk]['child'] = $slist2;
            }
        }
        $data['hupo'] = $hulist;

        $this->success('获取成功', $data);
    }

    /***
     * 河湖统计、巡河人数、巡河次数、
     * 巡河里程、巡河时长
     */
    public function riverCtAll()
    {
        //河湖统计
        $hehuct = Db::name('river_list')
            ->where(['type' => '1', 'river_pid' => 0])
            ->count();
        $data['heliu_all'] = $hehuct ? $hehuct : 0;
        $huct = Db::name('river_list')
            ->where(['type' => '2', 'river_pid' => 0])
            ->count();
        $data['hupo_all'] = $huct ? $huct : 0;
        //巡河人数
        $userAll = Db::name('river_patrol_list')
            ->group('user_id')
            ->count();
        $data['userAll'] = $userAll ? $userAll : 0;
        //巡河次数
        $numAll = Db::name('river_patrol_list')
            ->count();
        $data['num_All'] = $numAll ? $numAll : 0;
        //巡河时长
        $timeAll = Db::name('river_patrol_list')
            ->sum('river_time_all');
        // 时
        $remain = $timeAll % 86400;
        $hours = ($remain / 3600);
        // 分
        $remain = $timeAll % 3600;
        $mins = ($remain / 60);
        // 秒
        $secs = $remain % 60;
        $time_All = ['hours' => $hours, 'minutes' => $mins, 'seconds' => $secs];
        $data['time_All'] = $time_All;
        //巡河里程
        $distanceAll = Db::name('river_patrol_list')
            ->sum('river_distance');
        $data['distanceAll'] = $distanceAll;

        $this->success('获取成功', $data);
    }

    /***
     * 问题上报统计
     * 数量按分类统计
     */
    public function getAskCt()
    {
        //问题上报总数
        $numall = Db::name('river_asklist')
            ->count();
        $data['num_all'] = $numall ? $numall : 0;
        //未处理
        $status0 = Db::name('river_asklist')
            ->where(['status' => '0'])
            ->count();
        $data['status0'] = $status0 ? $status0 : 0;
        if ($status0 > 0) {
            $data['status0_pct'] = round(($status0 / $numall) * 100, 2);
        } else {
            $data['status0_pct'] = 0;
        }
        //处理中
        $status12 = Db::name('river_asklist')
            ->where(['status' => '1'])
            ->whereOr(['status' => '2'])
            ->count();
        $data['status12'] = $status12 ? $status12 : 0;
        if ($status12 > 0) {
            $data['status12_pct'] = round(($status12 / $numall) * 100, 2);
        } else {
            $data['status12_pct'] = 0;
        }
        //已完成
        $status3 = Db::name('river_asklist')
            ->where(['status' => '3'])
            ->count();
        $data['status3'] = $status3 ? $status3 : 0;
        if ($status3 > 0) {
            $data['status3_pct'] = round(($status3 / $numall) * 100, 2);
        } else {
            $data['status3_pct'] = 0;
        }
        //今日新增数量
        $dayAddCt = Db::name('river_asklist')
            ->whereTime('createtime', 'today')
            ->count();
        $data['dayAddCt'] = $dayAddCt ? $dayAddCt : 0;
        if ($dayAddCt > 0) {
            $data['dayAddCt_pct'] = round(($dayAddCt / $numall) * 100, 2);
        } else {
            $data['dayAddCt_pct'] = 0;
        }

        $this->success('获取成功', $data);
    }

    /****
     * 最新1条问题上报记录
     *
     */
    public function getNewInfo()
    {
        $row = Db::name('river_asklist')
            ->alias('a')
            ->join('river_ask_cat ac', 'a.cat_id=ac.id')
            ->join('river_list l', 'a.river_id=l.id')
            ->join('user u', 'a.user_id=u.id')
            ->where("a.lat>'0'")
            ->field('a.id,a.title,a.image,a.vdourl,a.status,a.address,a.lat,a.lng,a.createtime,ac.catname,l.name,u.nickname,u.avatar,u.mobile')
            ->order('id', 'desc')
            ->find();

        //处理图片和视频地址
        $row['createtime'] = setdateTimes($row['createtime']);
        if (!empty($row['image'])) {
            $imgarr = explode(',', $row['image']);
            $row['image'] = full_image_kevin($imgarr);
        }
        if (!empty($row['vdourl'])) {
            $vdourlarr = explode(',', $row['vdourl']);
            $row['vdourl'] = full_image_kevin($vdourlarr);
        }

        $this->success('获取成功', $row);
    }

    /***
     * 清四乱统计
     * 乱占、乱采、乱堆、乱建
     * [3,4,5,6]
     */
    public function askTypeCt()
    {
        $all = Db::name('river_asklist')
            ->count();
        $row = Db::name('river_asklist')
            ->where(['cat_id' => 3])
            ->count();
        $data['cat3'] = $row ? $row : 0;
        if (!empty($row)) {
            $data['cat3_pct'] = round(($row / $all) * 100, 2);
        } else {
            $data['cat3_pct'] = 0;
        }
        $row4 = Db::name('river_asklist')
            ->where(['cat_id' => 4])
            ->count();
        $data['cat4'] = $row4 ? $row4 : 0;
        if (!empty($row4)) {
            $data['cat4_pct'] = round(($row4 / $all) * 100, 2);
        } else {
            $data['cat4_pct'] = 0;
        }
        $row5 = Db::name('river_asklist')
            ->where(['cat_id' => 5])
            ->count();
        $data['cat5'] = $row5 ? $row5 : 0;
        if (!empty($row5)) {
            $data['cat5_pct'] = round(($row5 / $all) * 100, 2);
        } else {
            $data['cat5_pct'] = 0;
        }
        $row6 = Db::name('river_asklist')
            ->where(['cat_id' => 6])
            ->count();
        $data['cat6'] = $row6 ? $row6 : 0;
        if (!empty($row6)) {
            $data['cat6_pct'] = round(($row6 / $all) * 100, 2);
        } else {
            $data['cat6_pct'] = 0;
        }
        //其他类型的统计
        $wh['cat_id'] = ['not in', [3, 4, 5, 6]];
        $other = Db::name('river_asklist')
            ->where($wh)
            ->count();
        $data['other'] = $other ? $other : 0;
        if (!empty($other)) {
            $data['other_pct'] = round(($other / $all) * 100, 2);
        } else {
            $data['other_pct'] = 0;
        }
        $this->success('获取成功', $data);
    }

}