River.php 16.0 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
<?php
/**
 * Created by PhpStorm.
 * User: pcl
 * Date: 2022/7/28
 * Time: 15:32
 * 河长制接口River
 */

namespace app\api\controller\v1;

use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\Request;
use fast\Distance;

class River extends Base
{

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

    /***
     * 获得当前用户身份
     *
     */
    public function getUserLevel()
    {
        $uid = $this->auth->id;
        if (empty($uid)) {
            $this->error('请先登录');
        }
        $user = $this->userLevel;

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

    /***
     * 用户上报问题
     *
     */
    public function askAdd()
    {
        if ($this->request->isPost()) {
            $uid = $this->auth->id;
            if (empty($uid)) {
                $this->error('请先登录');
            }
            $data = $this->request->post();
            if (empty($data['river_id'])) {
                $this->error('请选择河段');
            }
            if (empty($data['cat_id'])) {
                $this->error('请选择爆料分类');
            }
            if (empty($data['title'])) {
                $this->error('请问题描述内容');
            }
            if (!empty($data['image'])) {
                $data['image'] = implode(',', $data['image']);
            } else {
                unset($data['image']);
            }
            if (!empty($data['vdourl'])) {
                $data['vdourl'] = implode(',', $data['vdourl']);
            } else {
                unset($data['vdourl']);
            }
            $data['createtime'] = time();
            $data['ask_com'] = 1;
            $data['user_id'] = $uid;
            //上级河段
            $river = Db::name('river_list')
                ->where(['id' => $data['river_id']])
                ->field('id,river_pid')
                ->find();
            $data['river_pid'] = $river['river_pid'];
            $rs = Db::name('river_asklist')->insertGetId($data);
            if ($rs != false) {
                $this->success('上报成功');
            } else {
                $this->error('提交失败,请重试');
            }
        }
    }

    /***
     * 我上报的问题列表
     */
    public function getMyAskList()
    {
        if ($this->request->isPost()) {
            $uid = $this->auth->id;
            if (empty($uid)) {
                $this->error('请先登录');
            }
            $page = $this->request->post('page', 1);
            $total = $this->request->post('total', 12);
            $type = $this->request->post('type', '');

            $wh['a.user_id'] = ['=', $uid];
            if ($type > 0) {
                $wh['a.status'] = ['=', $type];
            }
            $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')
                ->where($wh)
                ->field('a.id,a.title,a.image,a.vdourl,a.status,a.address,a.lat,a.lng,a.createtime,ac.catname,l.name')
                ->order("a.id desc")
                ->paginate($total, false, ['page' => $page])
                ->toArray();
            if (!empty($list)) {
                foreach ($list['data'] as $k => $v) {
                    $list['data'][$k]['createtime'] = setdateTimes($v['createtime']);
                    if (!empty($v['image'])) {
                        $imgarr = explode(',', $v['image']);
                        $list['data'][$k]['image'] = full_image($imgarr);
                    }
                    if (!empty($v['vdourl'])) {
                        $vdourlarr = explode(',', $v['vdourl']);
                        $list['data'][$k]['vdourl'] = full_image($vdourlarr);
                    }
                }
            }

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

    /***
     * 我上报的问题记录详情
     */
    public function myAskInfo()
    {
        if ($this->request->isPost()) {
            $uid = $this->auth->id;
            if (empty($uid)) {
                $this->error('请先登录');
            }
            $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')
                ->where(['a.user_id' => $uid, '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')
                ->find();
            if (empty($row)) {
                $this->error('内容不存在');
            }
            //处理图片和视频地址
            $row['createtime'] = setdateTimes($row['createtime']);
            if (!empty($row['image'])) {
                $imgarr = explode(',', $row['image']);
                $row['image'] = full_image($imgarr);
            }
            if (!empty($row['vdourl'])) {
                $vdourlarr = explode(',', $row['vdourl']);
                $row['vdourl'] = full_image($vdourlarr);
            }

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

    /****
     * 判断当前用户是否有进行中的巡河
     *
     */
    public function getNowPatrolStu()
    {
        if ($this->request->isPost()) {
            $uid = $this->auth->id;
            if (empty($uid)) {
                $this->error('请先登录');
            }
            $wh['user_id'] = ['=', $uid];
            $wh['endtime'] = ['=', 0];
            $cks = Db::name('river_patrol_list')
                ->where($wh)
                ->find();
            if (empty($cks)) {
                //没有进行中的巡河
                $data['stu'] = 2;
                $data['stu_msg'] = '当前没有进行中的巡河';
                $this->success('获取成功', $data);
            } else {
                //河的名称
                $river = Db::name('river_list')
                    ->where(['id' => $cks['river_id']])
                    ->field('id,name')
                    ->find();
                $cks['river_name'] = $river['name'];
                //上报的坐标列表
                $latList = Db::name('river_patrol_logs')
                    ->where(['ordersn' => $cks['ordersn'], 'river_id' => $cks['river_id']])
                    ->where("lat>'0'")
                    ->order('id', 'asc')
                    ->select();
                $lat_list = [];
                if (!empty($latList)) {
                    foreach ($latList as $k => $v) {
                        $lat_list[$k]['latitude'] = $v['lat'];
                        $lat_list[$k]['longitude'] = $v['lng'];
                    }
                }
                $cks['lat_list'] = $lat_list;
                $cks['stu'] = 1;
                $cks['stu_msg'] = '巡河中';
                $this->success('获取成功', $cks);
            }
        }
    }

    /***
     * 点开始巡河
     * 生成巡河记录,保存坐标
     * 生成这次巡河订单id
     */
    public function patrolRivelStart()
    {
        if ($this->request->isPost()) {
            $uid = $this->auth->id;
            if (empty($uid)) {
                $this->error('请先登录');
            }
            //判断是否有未结束巡河状态
            $data = $this->request->post();
            if (empty($data['river_id'])) {
                $this->error('请选择河段');
            }
            $pcks = Db::name('river_patrol_list')
                ->where(['user_id' => $uid, 'endtime' => 0])
                ->select();
            if (!empty($pcks)) {
                $this->error('存在未结束的巡河状态');
            }
            //生成订单id
            $arr['ordersn'] = getOrderSn();
            $arr['river_id'] = $data['river_id'];
            $arr['user_id'] = $uid;
            $arr['createtime'] = time();
            $arr['daytime'] = date('Ymd', time());
            $rs = Db::name('river_patrol_list')->insertGetId($arr);
            if ($rs != false) {
                //保存巡河坐标日志
                $logs['user_id'] = $uid;
                $logs['createtime'] = time();
                $logs['ordersn'] = $arr['ordersn'];
                $logs['river_id'] = $data['river_id'];
                $logs['lat'] = $data['lat'];
                $logs['lng'] = $data['lng'];
                Db::name('river_patrol_logs')->insertGetId($logs);
                $this->success('成功', $logs['ordersn']);
            } else {
                $this->error('操作失败');
            }
        }
    }

    /***
     * 巡河中 上报坐标记录
     *
     */
    public function addlogs()
    {
        if ($this->request->isPost()) {
            $uid = $this->auth->id;
            if (empty($uid)) {
                $this->error('请先登录');
            }
            //判断是否有未结束巡河状态
            $data = $this->request->post();
            if (empty($data['river_id'])) {
                $this->error('请选择河段');
            }
            $cks = Db::name('river_patrol_list')
                ->where(['user_id' => $uid, 'river_id' => $data['river_id'], 'ordersn' => $data['ordersn']])
                ->find();
            if (empty($cks)) {
                $this->error('参数错误');
            }
            if (!empty($cks) && $cks['endtime'] > 0) {
                $this->error('巡河已经结束了');
            }

            //保存巡河坐标日志
            $logs['user_id'] = $uid;
            $logs['createtime'] = time();
            $logs['ordersn'] = $data['ordersn'];
            $logs['river_id'] = $data['river_id'];
            $logs['lat'] = $data['lat'];
            $logs['lng'] = $data['lng'];
            $rs = Db::name('river_patrol_logs')->insertGetId($logs);
            if ($rs !== false) {
                $this->success('保存成功', $rs);
            } else {
                $this->success('保存失败', $rs);
            }
        }
    }

    /***
     * 结束巡河
     *
     */
    public function endPatrolRivel()
    {
        if ($this->request->isPost()) {
            $uid = $this->auth->id;
            if (empty($uid)) {
                $this->error('请先登录');
            }
            //判断是否有未结束巡河状态
            $data = $this->request->post();
            if (empty($data['river_id'])) {
                $this->error('请选择河段');
            }
            $cks = Db::name('river_patrol_list')
                ->where(['user_id' => $uid, 'river_id' => $data['river_id'], 'ordersn' => $data['ordersn']])
                ->find();
            if (empty($cks)) {
                $this->error('参数错误');
            }
            if (!empty($cks) && $cks['endtime'] > 0) {
                $this->error('巡河已经结束了');
            }
            //更新巡河状态
            $uptime['endtime'] = time();
            $uptime['updatetime'] = time();
            //计算巡河时长
            $timeAll = time_diff($cks['createtime'], time());
            $uptime['river_time'] = json_encode($timeAll['time'], JSON_UNESCAPED_UNICODE);
            $uptime['river_time_all'] = $timeAll['timeall'];
            //计算巡河距离
            //开始的坐标
            $stratLat = Db::name('river_patrol_logs')
                ->where(['user_id' => $uid, 'river_id' => $data['river_id'], 'ordersn' => $data['ordersn']])
                ->find();
            $obj = new Distance();
            $enddis = $obj->get_distance_by_geo($stratLat['lat'], $stratLat['lng'], $data['lat'], $data['lng']);
            $uptime['river_distance'] = round($enddis, 2);
            $row = Db::name('river_patrol_list')
                ->where(['user_id' => $uid, 'river_id' => $data['river_id'], 'ordersn' => $data['ordersn']])
                ->update($uptime);
            if ($row != false) {
                //保存巡河坐标日志
                $logs['user_id'] = $uid;
                $logs['createtime'] = time();
                $logs['ordersn'] = $data['ordersn'];
                $logs['river_id'] = $data['river_id'];
                $logs['lat'] = $data['lat'];
                $logs['lng'] = $data['lng'];
                $rs = Db::name('river_patrol_logs')->insertGetId($logs);
                if ($rs !== false) {
                    $this->success('操作成功', $rs);
                } else {
                    $this->success('操作失败', $rs);
                }
            }
        }
    }

    /***
     * 我的巡河记录
     *
     */
    public function myPatrolLogs()
    {
        if ($this->request->isPost()) {
            $uid = $this->auth->id;
            if (empty($uid)) {
                $this->error('请先登录');
            }
            $page = $this->request->post('page', 1);
            $total = $this->request->post('total', 12);

            $wh['r.user_id'] = ['=', $uid];

            $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')
                ->paginate($total, false, ['page' => $page])
                ->toArray();
            if (!empty($list)) {
                foreach ($list['data'] as $k => $v) {
                    $ctime = $v['createtime'];
                    $list['data'][$k]['createtime'] = setdateTimes($v['createtime']);

                    //状态判断
                    if ($v['endtime'] > 0) {
                        $endtime = $v['endtime'];
                        $list['data'][$k]['endtime'] = setdateTimes($v['endtime']);
                        $list['data'][$k]['pstu'] = 1;
                        $list['data'][$k]['pstu_msg'] = '巡河结束';
                    } else {
                        $endtime = time();
                        $list['data'][$k]['pstu'] = 0;
                        $list['data'][$k]['pstu_msg'] = '巡河中';
                    }
                    //巡河时长
                    $list['data'][$k]['timeAll'] = json_decode($v['river_time'], true);
                }
            }

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

    /****
     * 我的巡河记录详情
     *
     */
    public function myPatrolInfo()
    {
        if ($this->request->isPost()) {
            $uid = $this->auth->id;
            if (empty($uid)) {
                $this->error('请先登录');
            }
            $id = $this->request->post('id', 0);
            $wh['r.user_id'] = ['=', $uid];
            $wh['r.id'] = ['=', $id];
            $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')
                ->find();
            if (empty($row)) {
                $this->error('内容不存在');
            }
            if (!empty($list)) {
                $ctime = $list['createtime'];
                $list['createtime'] = setdateTimes($list['createtime']);

                //状态判断
                if ($list['endtime'] > 0) {
                    $endtime = $list['endtime'];
                    $list['endtime'] = setdateTimes($list['endtime']);
                    $list['pstu'] = 1;
                    $list['pstu_msg'] = '巡河结束';
                } else {
                    $endtime = time();
                    $list['pstu'] = 0;
                    $list['pstu_msg'] = '巡河中';
                }
                //计算巡河时长
                $list['timeAll'] = json_decode($list['river_time'], true);
            }

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

}