<?php

namespace web\admin\develop;

class code_saas {
    static function setwhere($db, $post) {
        $query = $post->get('query');
        $csql = new \ciy\sql('zc_dev_dbsaas');
        $order = objstr($query, 'order', 'id desc');
        $csql->order($order);
        $query['order'] = $order;
        return [$query, $csql];
    }

    public static function json_list() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        list($where, $csql) = self::setwhere($db, $post);
        $pageno = $post->getint('pageno', 1);
        $pagecount = $post->getint('pagecount', 10);
        $csql->limit($pageno, $pagecount);
        $mainrowcount = $post->getint('count');
        $rows = $db->get($csql, $mainrowcount);
        if ($rows === false)
            return errjson($db->error);
        $ret = array('searchwhere' => $where, 'pageno' => $pageno, 'pagecount' => $pagecount, 'count' => $mainrowcount, 'list' => $rows);
        if ($post->getbool('field')) {
            $field = array();
            $fshow = $db->getfield($field, 'zc_dev_dbsaas');
            foreach ($field as $fr => $v) {
                if (get('_' . $fr))
                    $field[$fr]['c'] = ',' . $field[$fr]['c'];
            }
            $fshow = fieldadd($fshow, $field, 0, '_btn', '操作');
            $ret['field'] = $field;
            $ret['fshow'] = $fshow;
        }
        if ($post->getbool('once')) {
            $ret['once'] = true;
        }
        return succjson($ret);
    }

    public static function json_getdata() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('id');
        $act = $post->get('act');
        if ($id > 0) {
            $csql = new \ciy\sql('zc_dev_dbsaas');
            $csql->where('id', $id);
            $row = $db->getone($csql);
            if (!is_array($row))
                return errjson('数据不存在');
        } else {
            $row = array();
        }
        $ret['data'] = $row;
        if ($act == 'edit') {
            $csql = new \ciy\sql('INFORMATION_SCHEMA.SCHEMATA');
            $csql->column('SCHEMA_NAME as id,SCHEMA_NAME as name');
            $sehrows = $db->get($csql);
            $ret['schema'] = $sehrows;
        }
        return succjson($ret);
    }

    public static function json_update() {
        global $db;
        $rsuser = verifyfast();
        //if (nopower($db, $rsuser['id'], 'p u'))
        //    return errjson('您未被授权操作');
        $post = new \ciy\post();
        $id = $post->getint('id');
        $name = $post->get('name');
        $schem = $post->get('schem');
        $subpath = $post->get('subpath');
        $verifyfunc = $post->get('verifyfunc');
        $codetable = $post->get('codetable');
        $saasfield = $post->get('saasfield');
        $saasusr = $post->get('saasusr');
        if (strpos($subpath, '\\') === false)
            return errjson('相对路径需反斜杠\\');
        if (dirmake(PATH_WEB . $subpath) === false)
            return errjson('创建目录失败');
        $datarow = null;
        if ($id > 0) {
            $csql = new \ciy\sql('zc_dev_dbsaas');
            $csql->where('id', $id);
            $datarow = $db->getone($csql);
            if (!is_array($datarow))
                return errjson('数据不存在');
        }

        try {
            $db->begin();
            $updata = array();
            $updata['name'] = $name;
            $updata['schem'] = $schem;
            $updata['subpath'] = $subpath;
            $updata['verifyfunc'] = $verifyfunc;
            $updata['codetable'] = $codetable;
            $updata['saasfield'] = $saasfield;
            $updata['saasusr'] = $saasusr;
            $csql = new \ciy\sql('zc_dev_dbsaas');
            if ($id > 0) {
                $csql->where('id', $id);
                if ($db->update($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
            } else {
                $updata['id'] = 0;
                if ($db->insert($csql, $updata) === false)
                    throw new \Exception('新增失败:' . $db->error);
                $id = $db->insert_id();
            }
            $updata['id'] = $id;
            //savelogdb($db, $rsuser['id'], 'zc_dev_dbsaas', $datarow, $updata);
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $ret['data'] = $updata;
        $ret['hf_providerbase'] = getrelation($db, [$updata], 'hf_providerbase', 'providerid');
        return succjson($ret);
    }

    public static function json_del() {
        global $db;
        $rsuser = verifyfast();
        //if (nopower($db, $rsuser['id'], 'p d'))
        //    return errjson('您未被授权操作');
        $post = new \ciy\post();
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $csql = new \ciy\sql('zc_dev_dbsaas');
        $csql->where('id in', $ids);
        $rows = $db->get($csql);
        $vids = array();
        try {
            $db->begin();
            foreach ($rows as $row) {
                $delid = $row['id'];
                //delcheck($db, $delid, 'tablexx', 'xxid', '管理员');
                //delall($db, $delid, 'tablexx', 'xxid', '运动员');  //deltimeall
                delme($db, $delid, 'zc_dev_dbsaas');
                savelogdb($db, $rsuser['id'], 'zc_dev_dbsaas', $row, null);
                $vids[] = $delid;
            }
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $ret['ids'] = $vids;
        return succjson($ret);
    }
}
