Areasite.php 5.7 KB
<?php


namespace app\api\controller\inspection;


use app\admin\model\inspection\Area;
use app\common\controller\Api;
use fast\Tree;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\Request;
use think\Validate;

class Areasite extends Api
{
    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['*'];

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

    //获取区域列表
    public function areaList(){
        $tree = Tree::instance();
        $areaModel = new Area();
        $tree->init(collection($areaModel->order('weigh asc,id asc')->select())->toArray(), 'pid');
        $areadata = [];
        foreach ($tree->getTreeList($tree->getTreeArray(0), 'area_name') as $k => $v) {
            $areadata[] = $v;
        }
        $this->success('成功', $areadata);
    }

    //获取区域列表(多维数组)
    public function area(){
        $tree = Tree::instance();
        $areaModel = new Area();
        $tree->init(collection($areaModel->order('weigh asc,id asc')->select())->toArray(), 'pid');
        $list = $tree->getTreeArray(0);
        $this->success('成功', $list);
    }

    //添加巡检点
    public function add(){
        $params = request()->param();
        $result = false;
        Db::startTrans();
        try {
            $validate = new Validate([
                'area_id|巡检区域'  => 'require|integer',
                'site_name|巡检点名称' => 'require|max:32',
                'verify|验证方式' => 'require|in:0,1',
            ]);
            if (!$validate->check($params)) {
                exception((string)$validate->getError());
            }
            if ($params['verify'] == 1 && empty($params['distance'])){
                exception('请填写有效距离');
            }
            if (empty($params['weigh'])){
                $params['weigh'] = 0;
            }

            if(empty($params['lng']) || empty($params['lat'])){
                $this->error("请完善经纬度信息");
            }
            $params['lnglat'] = $params['lng'].",".$params['lat'];

            $areaSiteModel = new \app\admin\model\inspection\Areasite();
            $result = $areaSiteModel->allowField(true)->save($params);
            if ($result > 0) {
                $id = $areaSiteModel->id;
                $qrCode = \addons\qrcode\library\Service::qrcode(['text'=>Request::instance()->domain() . "/api/v7/inspection/mission/getItemByAreasite?id=".$id]);
                $qrcodePath = ROOT_PATH . 'public/xj/qrcode/';
                if (!is_dir($qrcodePath)) {
                    @mkdir($qrcodePath);
                }
                if (is_really_writable($qrcodePath)) {
                    $filename = date('YmdHis') . '.png';
                    $filePath = $qrcodePath . $filename;
                    $qrCode->writeFile($filePath);
                    Db::name('inspection_area_site')->where('id',$id)->update(['qrcode_src'=>'/xj/qrcode/'.$filename]);
                }
                // 如果检查项不为空,添加检查项
                if (!empty($params['item_json']) && is_array($params['item_json'])){
                    foreach ($params['item_json'] as $item){
                        if (mb_strlen($item['item_name']) > 20){
                            throw new ValidateException('检查项名称长度不能大于20个字符');
                        }
                        $item['areasite_id'] = $id;
                        $itemId = Db::name('inspection_area_item')->insertGetId($item);
                        if (!$itemId){
                            throw new Exception('添加检查项 '. $item['item_name'] . ' 失败');
                        }
                    }
                }
            }
            Db::commit();
        } catch (ValidateException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result !== false) {
            $this->success('操作成功');
        } else {
            $this->error(__('No rows were inserted'));
        }
    }

    //添加巡检区域
    public function saveArea(){
        $params = request()->param();
        $result = false;
        Db::startTrans();
        try {
            $validate = new Validate([
                'pid|父级区域'  => 'require|integer',
                'area_name|区域名称' => 'require|max:32',
                'lnglat|区域坐标' => 'require'
//                'reservoir_id|水库' => 'integer',
//                'weigh|排序值' => 'integer',
            ]);
            if (!$validate->check($params)) {
                exception((string)$validate->getError());
            }
            if (empty($params['id'])){
                $params['weigh'] = 0;
                $areaModel = new \app\admin\model\inspection\Area();
            }else{
                $areaModel = \app\admin\model\inspection\Area::get($params['id']);
            }
            $result = $areaModel->allowField(true)->save($params);
            Db::commit();
        } catch (ValidateException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (PDOException $e) {
            Db::rollback();
            $this->error($e->getMessage());
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        if ($result !== false) {
            $this->success('操作成功');
        } else {
            $this->error(__('No rows were inserted'));
        }
    }

}