Supervision.php 7.0 KB
<?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;
use app\api\controller\v6\Inspection;

//v6 监管端功能
class Supervision 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
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getReservoirStructure()
    {
        $DepartM = new \app\admin\model\inspection\Depart(); //部门表模型
        $array = collection($DepartM->order('weigh asc,id asc')->field('id,pid,depart_name,type')->select())->toArray();
        $shu = $this->getMenu($array);
        $this->success('成功', $shu);
    }

    protected function getMenu($array)
    {
        foreach ($array as $key => $items) {
            if ($items['pid'] == "0") {
                unset($this->menu[$key]);
                $menu[] = $this->buildMenuTree($items, $items['id'], $array);
            }
        }
        return $menu;
    }

    //生产多级菜单树
    protected function buildMenuTree($items, $rid, $array)
    {
        $childs = $this->getChildMenu($items, $rid, $array);
        if (isset($childs['child'])) {
            foreach ($childs['child'] as $key => $value) {
                $children = $this->buildMenuTree($value, $value['id'], $array);
                if (null != $children['child']) {
                    $childs['child'][$key]['child'] = $children['child'];
                }
            }
        }
        return $childs;
    }

    //获取子菜单
    protected function getChildMenu($items, $rid, $array)
    {
        foreach ($array as $key => $value) {
            if ($value['pid'] == $rid) {
                unset($array[$key]);
                $items['child'][] = $value;
            }
        }
        return $items;
    }

    /**
     * 获取巡检条件信息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getInspectionWhereInfo()
    {
        $field = "plan.id as plan_id,plan.plan_name,plan.route_id,plan.staff_id,route.route_name,staff.staff_name";
        //巡检计划
        $plan = Db::name('inspection_plan')
            ->alias('plan')
            ->join('inspection_route route', 'plan.route_id = route.id')
            ->join('inspection_staff staff', 'plan.staff_id = staff.id')
            ->field($field)
            ->select();
        //巡检区域
        $area = collection(Db::name('inspection_area')->field('id,pid,area_name,reservoir_id')->select())->toArray();
        if (!empty($area)) {
            $area = $this->getMenu($area);
            //获取每个巡检区域下的巡检点
            foreach ($area as &$v) {
                $area_site_where['area_id'] = $v['id'];
                //巡检点
                $v['area_site'] = collection(Db::name('inspection_area_site')->where($area_site_where)->field('id,area_id,site_name')->select())->toArray();
            }
        }
        $this->success('成功', ['plan' => $plan, 'area' => $area]);
    }

    /**
     * 发布巡检任务
     * @return \think\response\Json
     */
    public function releaseInspection()
    {
        $param = $this->request->param('', []);
        if (empty($param['area_site_id'])) {
            $this->error('失败');
        }
        $Data_ = [];
        $param['area_site_id'] = json_decode($param['area_site_id'], true);
        foreach ($param['area_site_id'] as $v) {
            $arr['plan_id'] = $param['plan_id'];
            $arr['route_id'] = $param['route_id'];
            $arr['area_id'] = $param['area_id'];
            $arr['area_site_id'] = $v;
            $arr['begintime'] = $param['begintime'];
            $arr['endtime'] = $param['endtime'];
            $arr['createtime'] = time();
            array_push($Data_, $arr);
        }
        $res = false;
        if (!empty($Data_)) {
            // 启动事务
            Db::startTrans();
            try {
                //添加巡检任务
                $result = Db::name('inspection_project')->insertAll($Data_);
                if (!$result) {
                    Db::rollback();
                }else{
                    //提交
                    Db::commit();
                    $res = true;
                }
            } catch (\Exception $e) {
                // 回滚事务
                Db::rollback();
            }
            //返回结果
            $res?$this->success('成功'):$this->error('失败');
        } else {
            $this->error('失败');
        }
    }

    //回复隐患内容
    public function replyWarning()
    {
        $param = $this->request->param('', []);
        if (empty($param)) {
            $this->error('回复内容不能为空');
        }
        //回复内容 Warning 表
        $id = $param['id'];
        $WarningData_['status'] = $param['status'];
        $WarningData_['reply'] = $param['reply'];
        //回复内容 Warning_reply 表
        $WarningReplyData_['wid'] = $param['id'];
        $WarningReplyData_['staffid'] = $param['staff_id'];
        $WarningReplyData_['status'] = $param['status'];
        $WarningReplyData_['reply'] = $param['reply'];
        $WarningReplyData_['createtime'] = time();
        $res = false;
        // 启动事务
        Db::startTrans();
        try {
            //更新隐患表
            $warningRes = Db::name('inspection_warning')->where('id', $id)->update($WarningData_);
            //更新隐患回复表
            $replyRes = Db::name('inspection_warning_reply')->insert($WarningReplyData_);
            if ($warningRes == 0 || $replyRes == 0) {
                Db::rollback();
            }else{
                // 提交事务
                Db::commit();
                $res = true;
            }
        }
        catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
        //返回结果
        $res?$this->success('成功'):$this->error('失败');
    }
}