<?php

namespace web\admin\cemap;
//拷贝glb到model文件夹，然后运行index.html
class ceeditor {
    public static function json_init() {
        global $db;
        $rsuser = verifyfast();
        $id = getint('id');
        $tabname = get('tab');
        $binds = array();
        $binds[] = array('id' => 'v720_1', 'name' => '演示全景1');
        for ($i = 150; $i < 200; $i++)
            $binds[] = array('id' => 'camera_' . $i, 'name' => '演示摄像机' . $i);
        $ret['binds'] = $binds;
        $ret['base'] = array('lng' => 123, 'lat' => 44);
        if (!is_array($ret['base']))
            return errjson('数据不存在');
        $csql = new \ciy\sql('zc_cemap_data');
        $csql->where('tabname', $tabname);
        $csql->where('tabid', $id);
        $ret['cemap'] = $db->getone($csql);
        if (!is_array($ret['cemap'])) {
            $json = array();
            $json['wmts_source'] = 4;
            $json['wmts_style'] = 3;
            $json['setin'] = 3;
            $updata = array();
            $updata['tabname'] = $tabname;
            $updata['tabid'] = $id;
            $updata['mapjson'] = json_encode($json, JSON_PARTIAL_OUTPUT_ON_ERROR);
            $updata['uptimes'] = tostamp();
            $csql = new \ciy\sql('zc_cemap_data');
            $db->insert($csql, $updata);
            $ret['cemap'] = $updata;
        }
        return succjson($ret);
    }
    public static function json_save() {
        global $db;
        $rsuser = verifyfast();
        $id = getint('id');
        $tabname = get('tab');
        $post = new \ciy\post();
        $set = $post->get('set');

        try {
            $db->begin();
            $updata = array();
            $updata['tabname'] = $tabname;
            $updata['tabid'] = $id;
            $updata['mapjson'] = json_encode($set, JSON_PARTIAL_OUTPUT_ON_ERROR);
            $updata['uptimes'] = tostamp();
            $csql = new \ciy\sql('zc_cemap_data');
            $csql->where('tabname', $tabname);
            $csql->where('tabid', $id);
            $datarow = $db->getone($csql);
            if (is_array($datarow)) {
                $csql = new \ciy\sql('zc_cemap_data');
                $csql->where('id', $datarow['id']);
                if ($db->update($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
            } else {
                if ($db->insert($csql, $updata) === false)
                    throw new \Exception('新增失败:' . $db->error);
            }
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        return succjson();
    }
    public static function json_getbills() {
        global $db;
        $post = new \ciy\post();
        $cpath = $post->getint('cpath');
        $csql = new \ciy\sql('zc_cemap_bill');
        $csql->where('cpath', $cpath);
        $rows = $db->get($csql);
        $ret['list'] = $rows;
        return succjson($ret);
    }
    public static function json_getbillpath() {
        global $db;
        $csql = new \ciy\sql('zc_cemap_pbill');
        $rows = $db->get($csql);
        $ret['list'] = $rows;
        return succjson($ret);
    }
    public static function json_getglbs() {
        global $db;
        $post = new \ciy\post();
        $cpath = $post->getint('cpath');
        $csql = new \ciy\sql('zc_cemap_glb');
        $csql->where('cpath', $cpath);
        $rows = $db->get($csql);
        $ret['list'] = $rows;
        return succjson($ret);
    }
    public static function json_getglbpath() {
        global $db;
        $csql = new \ciy\sql('zc_cemap_pglb');
        $rows = $db->get($csql);
        $ret['list'] = $rows;
        return succjson($ret);
    }
}
