Inspection.php 15.9 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
<?php

namespace app\api\controller\v6;
//允许所有的跨域请求
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: *");
header('Access-Control-Allow-Methods: POST,GET,OPTIONS');
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400');
date_default_timezone_set('PRC');

use fast\Tree;
use think\Db;
use app\common\controller\Api;


//v6 巡检端功能
class Inspection extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];
    public $staffInfo;

    //初始化
    public function _initialize()
    {
        parent::_initialize();
        if ($this->request->action() != "login" && $this->request->action() != "screen_mng_login") {
            $staffModel = new \app\admin\model\inspection\Staff();
            $staffInfo = $staffModel->where(['user_id' => $this->auth->id])->find();
            if (empty($staffInfo)) {
                $this->error('当前员工账号异常');
            }
            $this->staffInfo = $staffInfo;
        }
    }

    /**
     * 判断是巡检还是管理员
     * @return \think\response\Json
     */
    public function isInspectionOrSupervision()
    {
        $Data_ = [];
        $staff = json_decode($this->staffInfo, true);
        //不是大屏管理员 也不是 监管APP管理员 就是
        if ($staff['is_screen_mng'] == "0" && $staff['is_regulatory_mng'] == "0") {
            //巡检
            $Data_['is_inspection'] = true;
            $Data_['is_supervision'] = false;
        } else {
            //管理员
            $Data_['is_inspection'] = false;
            $Data_['is_supervision'] = true;
        }
        $staff = json_decode($this->staffInfo, true);
        $Data_['staff_id'] = $staff['id'];
        $Data_['explain'] = ['is_inspection' => '巡检员', 'is_supervision' => '监管员'];
        $this->success('成功', $Data_);
    }

    //判断是巡检还是管理员 (内部控制器使用)
    public function isInspectionOrSupervision_2()
    {
        $Data_ = [];
        $staff = json_decode($this->staffInfo, true);
        //不是大屏管理员 也不是 监管APP管理员 就是
        if ($staff['is_screen_mng'] == "0" && $staff['is_regulatory_mng'] == "0") {
            //巡检
            $Data_['is_inspection'] = true;
            $Data_['is_supervision'] = false;
        } else {
            //管理员
            $Data_['is_inspection'] = false;
            $Data_['is_supervision'] = true;
        }
        $Data_['explain'] = ['is_inspection' => '巡检员', 'is_supervision' => '监管员'];
        return ['code' => 1, 'msg' => '成功', 'data' => $Data_];
    }

    /**
     * 获取今日任务数量
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function todayInspectionTaskCount()
    {
        //获取今天日期
        $time = $this->request->post('time', date("Y-m-d"));
        $today = strtotime($time);
        //员工信息
        $staff = json_decode($this->staffInfo, true);
        $staff_id = $staff['id'];
        //判断是否身份 巡检员/监管员
        $isInspection = $this->isInspectionOrSupervision_2();
        if ($isInspection['data']['is_inspection']) {
            //是巡检员
            //追加条件 前端提交 时间 状态 id等字段,自动补充条件
            $planWhere['plan.staff_id'] = $staff_id;
        }
        //获取今天的巡检计划
        $planWhere['project.begintime'] = ['<=', $today];
        $planWhere['project.endtime'] = ['>=', $today + 3600 * 24 - 1];
        $field = "project.id,project.begintime,project.endtime,project.status,project.state,project.plan_id";
        $project = Db::name('inspection_plan')
            ->alias('plan')
            ->join('inspection_project project', 'plan.id = project.plan_id', 'left')
            ->where($planWhere)
            ->field($field)
            ->select();
        $Data_ = [
            'all_count' => 0, //总任务
            'already_count' => 0, //已经完成
            'not_count' => 0, //未完成
        ];
        foreach ($project as $v) {
            $Data_['all_count']++;
            //已完成 检查项为1 && 最终状态为1
            if ($v['status'] == '1' && $v['state'] == "1") {
                $Data_['already_count']++;
            } else {
                //未完成
                $Data_['not_count']++;
            }
        }
        $this->success('成功', $Data_);
    }

    /**
     * 获取总任务数量情况
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function allInspectionTaskCount()
    {
        //员工信息
        $staff = json_decode($this->staffInfo, true);
        $staff_id = $staff['id'];
        //判断是否身份 巡检员/监管员
        $isInspection = $this->isInspectionOrSupervision_2();
        $planWhere = [];
        if ($isInspection['data']['is_inspection']) {
            //是巡检员
            //追加条件 前端提交 时间 状态 id等字段,自动补充条件
            $planWhere['plan.staff_id'] = $staff_id;
        }
        //获取今天的巡检计划
        $field = "project.id,project.begintime,project.endtime,project.status,project.state,project.plan_id";
        $project = Db::name('inspection_plan')
            ->alias('plan')
            ->join('inspection_project project', 'plan.id = project.plan_id', 'left')
            ->where($planWhere)
            ->field($field)
            ->select();
        $Data_ = [
            'all_count' => 0, //总数
            'not_count' => 0, //未开始
            'ing_count' => 0, //进行中
            'already_count' => 0, //已完成
        ];
        foreach ($project as $v) {
            $Data_['all_count']++;
            //当前时间 < 任务开始时间
            if (time() < $v['begintime']) {
                $Data_['not_count']++;
            }
            if ($v['status'] == '1' && $v['state'] == "1") {
                //已完成 检查项为1 && 最终状态为1
                $Data_['already_count']++;
            } else if ($v['status'] == '0' && $v['state'] == "0") {
                //未完成
                $Data_['ing_count']++;
            }
        }
        $this->success('成功', $Data_);
    }

    /**
     * 获取巡检任务列表及内容
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getInspectionList()
    {
        $param = $this->request->post('', []);
        $where = [];
        //员工信息
        $staff = json_decode($this->staffInfo, true);
        $staff_id = $staff['id'];
        $staff_name = $staff['staff_name'];
        //判断是否身份 巡检员/监管员
        $isInspection = $this->isInspectionOrSupervision_2();
        if ($isInspection['data']['is_inspection']) {
            //是巡检员
            //追加条件 前端提交 时间 状态 id等字段,自动补充条件
            $where['plan.staff_id'] = $staff_id;
        }
        if (isset($param['time']) && $param['time'] != '') {
            $time = strtotime($param['time']);
            $where['project.begintime'] = ['<=', $time];
            $where['project.endtime'] = ['>=', $time + 3600 * 24 - 1];
        }
        if (isset($param['state']) && $param['state'] != '') {
            $where['project.state'] = $param['state'];
            $where['project.status'] = $param['state'];
        }
        if (isset($param['id']) && $param['id'] != '') {
            $where['project.id'] = $param['id'];
        }
        $field = "project.*,plan.plan_name,plan.staff_id,area.area_name,route.route_name";
        //读取任务
        $project_arr = Db::name("inspection_project")
            ->alias('project')
            ->join('inspection_plan plan', 'project.plan_id = plan.id', 'left') //计划表
            ->join('inspection_route route', 'project.route_id = route.id', 'left') //路线表
            ->join('inspection_area area', 'project.area_id = area.id', 'left') //区域表
            ->where($where)
            ->field($field)
            ->select();
        //组装数据
        $Data_ = [];
        foreach ($project_arr as $v) {
            $v['staff_name'] = $staff_name;
            $v['project_time'] = date('Y-m-d H:i:s', $v['begintime']) . '-' . date('Y-m-d H:i:s', $v['endtime']);
            $v['state_title'] = $v['state'] == 0 ? "进行中" : ($v['state'] == 1 ? "正常" : "异常");
            $v['state_explain'] = ['0' => "进行中", '1' => '正常', '2' => '异常'];
            //判断该任务是否正常或异常
            if ($v['state'] == 1 || $v['state'] == 2) {
                //获取运动轨迹
                $v['locus'] = Db::name('inspection_staff_locus')->where(['inspection_staff_id' => $staff_id, 'inspection_project_id' => $v['id']])->field('lat,lng')->select();
            }
            //判断是否有图片
            if (strlen($v['images']) > 0) {
                $img = explode(',', $v['images']);//逗号分割字符串
                $v['images'] = full_image($img); //给图片加前缀地址
            }
            //路线表巡检点 获取巡检点
            $awhere['areasite_id'] = $v['area_site_id'];
            //获取 所有巡检点的检查项
            $area_item = Db::name("inspection_area_item")->where($awhere)->column('id,areasite_id,item_name');
            //获取任务下对应的检查项
            $project_item = Db::name('inspection_project_item')->where('project_id', $v['id'])->column('id,item_name,type,value,images');
            //分组成 每个巡检点 下面的 检查项
            foreach ($area_item as $k2 => &$v2) {
                unset($v2['id']);
                unset($v2['areasite_id']);
                $v2['project_id'] = $v['id'];
                if (empty($project_item[$v2['item_name']])) {
                    //为空
                    $v2['type'] = '';
                    $v2['value'] = '';
                } else {
                    $v2['id'] = $project_item[$v2['item_name']]['id'];
                    $v2['type'] = $project_item[$v2['item_name']]['type'];
                    $v2['value'] = $project_item[$v2['item_name']]['value'];
                }
            }
            $v['project_item_arr'] = $area_item;
            array_push($Data_, $v);
        }
        $this->success('成功', $Data_);
    }

    /**
     * 获取隐患列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getWarningList()
    {
        $param = $this->request->post('', []);
        //员工信息
        $staff = json_decode($this->staffInfo, true);
        $staff_id = $staff['id'];
        $staff_name = $staff['staff_name'];
        //判断是否身份 巡检员/监管员
        $isInspection = $this->isInspectionOrSupervision_2();
        $where = [];
        if ($isInspection['data']['is_inspection']) {
            //是巡检员
            //追加条件 前端提交 时间 状态 id等字段,自动补充条件
            $where['waring.staff_id'] = $staff_id;
        }
        if (isset($param['time']) && $param['time'] != '') {
            $time = strtotime($param['time']);
            $where['waring.createtime'] = ['between', [$time, $time + 3600 * 24 - 1]];
        }
        if (isset($param['status']) && $param['status'] != '') {
            $where['waring.status'] = $param['status'];
        }
        if (isset($param['id']) && $param['id'] != '') {
            $where['waring.id'] = $param['id'];
        }

        $field = "waring.*,area.area_name";
        $warning_info = Db::name('inspection_warning')
            ->alias('waring')
            ->join('inspection_area area', 'waring.area_id = area.id', 'left') //区域表
            ->where($where)
            ->field($field)
            ->select();
        $Data_ = [];
        foreach ($warning_info as $v) {
            $v['staff_name'] = $staff_name;
            $v['create_time'] = date('Y-m-d H:i:s', $v['createtime']);
            $v['status_title'] = $v['status'] == 0 ? "待处理" : ($v['status'] == 1 ? "审核通过" : "处理完成");
            $v['status_explain'] = ['待处理' => "0", '审核通过' => '1', '处理完成' => '2'];
            //判断是否有图片
            if (strlen($v['cover_images']) > 0) {
                $img = explode(',', $v['cover_images']);//逗号分割字符串
                $v['cover_images'] = full_image($img); //给图片加前缀地址
            }
            array_push($Data_, $v);
        }
        $this->success('成功', $Data_);
    }

    /**
     * 巡检任务提交经纬度
     * @return void
     */
    public function inspectionSubmitLatLng()
    {
        $param = $this->request->param('', []);
        if (empty($param)) {
            $this->error('经纬度不能为空');
        }
        $Data_['inspection_staff_id'] = $param['staff_id'];
        $Data_['person_type'] = 1;
        $Data_['lat'] = $param['lat']; //纬度
        $Data_['lng'] = $param['lng']; //经度
        $Data_['createtime'] = time();//创建时间
        $Data_['inspection_project_id'] = $param['inspection_project_id']; //巡检任务id
        $res = Db::name('inspection_staff_locus')->insert($Data_);
        if ($res) {
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }

    /**
     * 巡检任务提交信息
     * @return void
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function inspectionSubmitInfo()
    {
        $param = $_POST;
        if (empty($param)) {
            $this->error('提交数据失败');
        }
        $id = $param['inspection_project_id'];
        $Data_['checktime'] = time(); //检查时间
        $Data_['jumptime'] = $param['jumptime']; //跳捡时间
        $Data_['jump_content'] = $param['jump_content']; //跳捡原因
        $Data_['images'] = $param['jump_content']; //图片
        $Data_['content'] = $param['content']; //备注
        $Data_['status'] = $param['status']; //-1漏检 0进行中 1已巡检 2跳捡
        $Data_['state'] = $param['state']; //状态 0进行中 1正常 2异常
        $Data_['staff_id'] = $param['staff_id']; //员工id
        $inspection_item = json_decode($param['param'], true);
        if (!empty($inspection_item)) {
            foreach ($inspection_item as $v) {
                //先添加或更新检查项结果
                if (isset($v['id'])) {
                    //存在id 就更新
                    Db::name('inspection_project_item')->where('id', $v['id'])->update($v);
                } else {
                    //不存在就添加
                    Db::name('inspection_project_item')->insert($v);
                }
            }
        }
        $res = Db::name('inspection_project')->where('id', $id)->update($Data_);
        if ($res) {
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }

    //修改员工信息
    public function saveStaffInfo(){
        $param = $this->request->param('', []);
        if (empty($param)) {
            $this->error('修改信息不能为空');
        }
        $id = $param['staff_id'];
        $saveData_['staff_name'] = $param['staff_name'];
        $saveData_['mobile'] = $param['mobile'];
        $res =  Db::name('inspection_staff')->where('id',$id)->update($saveData_);
        if ($res) {
            $this->success('成功');
        } else {
            $this->error('失败');
        }
    }
}