<?php
function code_pc_php($post) {
    $tabname = $post->get('table', '', 'all');
    $subpath = $post->get('subpath', '', 'all');
    $verifyfunc = $post->get('verifyfunc', '', 'all');
    $codetable = $post->get('codetable', '', 'all');
    $saasfield = $post->get('saasfield', '', 'all');
    $saasusr = $post->get('saasusr', '', 'all');
    $titname = $post->get('_bcod_titname', '', 'all');
    $liiddict = str_replace('，', ',', $post->get('_bcod_liiddict', '', 'all'));
    $selectorder = $post->get('_bcod_select_order', '', 'all');
    $btntop_newline = $post->get('_bcod_btntop_newline', '', 'all');
    $btnlist_line = $post->get('_bcod_btnlist_line', '', 'all');
    $btntop_multibtn = $post->get('_bcod_btntop_multibtn', '', 'all');
    $savepc = $post->get('_bcod_savepc', '', 'all');
    $flds = calfld($post);

    $hasview = strpos($btnlist_line, '查看') !== false;
    $hasadd = strpos($btntop_newline, '添加') !== false; //有添加能力
    $hasexcelin = strpos($btntop_newline, '导入') !== false;
    $hasexcelout = strpos($btntop_newline, '导出') !== false;
    $hasreview = strpos($btnlist_line, '审核') !== false;
    $hasaudit = strpos($btntop_multibtn, '审核通过') !== false;
    $chk_audit = false; //审核
    $chk_matter = false; //流程
    $chk_getupdate = false; //编辑，查看时，先请求getdata
    $chk_deltimes = false; //删除时，加删除标记
    $chk_multistatus = false; //批量列更新
    $hasedit = strpos($btnlist_line, '修改') !== false;
    if (!$hasedit)
        $hasedit = $hasadd;
    $hasdel = strpos($btnlist_line, '删除') !== false;
    if ($hasdel === false)
        $hasdel = strpos($btntop_multibtn, '删除') !== false;

    if ($btntop_multibtn) {
        $btns = explode(',', $btntop_multibtn);
        foreach ($btns as $btn) {
            if ($btn != '批量删除' && $btn != '审核通过' && $btn != '驳回')
                $chk_multistatus = true;
            if ($btn[0] == 'A')
                $chk_multistatus = true;
        }
    }
    $nocolumn = array();
    foreach ($flds as $fld) {
        if ($fld['col'] == 'auditstatus')
            $chk_audit = true; //auditstatus/audituser/audittimes/auditmsg: 加审核按钮
        if ($fld['col'] == 'matterstatus')
            $chk_matter = true; //matterstatus/matternextuser/mattertimes: 加流程功能，是否可编辑/删除
        if ($fld['col'] == 'deltimes')
            $chk_deltimes = true;
        if ($fld['set']['listload'] == 'rel') {
            if (!$fld['set']['noup'])
                $chk_getupdate = true;
        }
        if ($fld['set']['nocmn'] == 'no') {
            $chk_getupdate = true;
            $nocolumn[] = $fld['col'];
        }
        //list($dicttype, $dictname) = splitdict($fld['data']);
    }

    $savepc = str_replace('/', '\\', $savepc);
    $savepc = substr($savepc, strpos($savepc, 'web\\'));
    $namespace = substr($savepc, 0, strrpos($savepc, '\\'));
    $classname = substr($savepc, strrpos($savepc, '\\') + 1);

    $codex = "<?php";
    $codex .= "\n";
    $codex .= "\nnamespace {$namespace};";
    $codex .= "\n";
    $codex .= "\nclass {$classname} {";
    $codex .= "\n    static function setwhere(\$db, \$post, \$rsuser) {";
    $codex .= "\n        \$query = \$post->get('query', array());";
    $codex .= "\n        \$csql = new \ciy\sql('{$tabname}');";
    if (!empty($liiddict)) {
        $codex .= "\n        \$liid = objint(\$query, 'liid');";
        if (strpos($liiddict, ',') === false) {
            $codex .= "\n        if (\$liid > 0)";
            $codex .= "\n            \$csql->where('{$liiddict}', \$liid);";
        } else {
            $lidicts = explode(',', $liiddict);
            for ($i = 0; $i < count($lidicts); $i++) {
                if ($i == 0)
                    continue;
                $codex .= "\n        if (\$liid == {$i})";
                $codex .= "\n            \$csql->where('');";
            }
        }
    }

    foreach ($flds as $fld) {
        $col = $fld['col'];
        if (!$fld['bqry']) {
            if ($fld['breq']) {
                $codex .= "\n        \$csql->where('{$col}', get('_{$col}'));";
            }
            continue;
        }
        $cate = $fld['cate'];
        if ($cate == 'DB' || $cate == 'CATU' || $cate == 'CATS' || $cate == 'CATM') {
            $dbquery = isset($fld['set']['dbquery']) ? $fld['set']['dbquery'] : '';
            if ($dbquery == 'select' || $dbquery == 'window') {
                if ($cate == 'CATS')
                    $codex .= "\n        \$csql->where('{$col} like', ',' . objstr(\$query, '{$col}') . ',');";
                else
                    $codex .= "\n        \$csql->where('{$col}', objstr(\$query, '{$col}'));";
            } else {
                $datas = explode('|', $fld['data']);
                $codex .= "\n        \$val = objstr(\$query, '{$col}');";
                $codex .= "\n        if (!empty(\$val)) {";
                list($dicttype, $dictname) = splitdict($fld['data']);
                if ($dicttype == 'dictcata') {
                    $codex .= "\n            \$csqlt = new \ciy\sql('{$codetable}');";
                    $codex .= "\n            \$csqlt->where('cbid in (select id from {$codetable} where cbid=0 and codeid=\'{$col}\')');";
                    $codex .= "\n            \$csqlt->where('name like', \$val);";
                    $codex .= "\n            \$trow = \$db->getone(\$csqlt);";
                    $codex .= "\n            if (is_array(\$trow)) {";
                    $codex .= "\n                \$csql->where('{$col}', \$trow['codeid']);";
                } else {
                    $codex .= "\n            \$csqlt = new \ciy\sql('{$datas[0]}');";
                    $codex .= "\n            \$csqlt->where('name like', \$val);";
                    $codex .= "\n            \$trow = \$db->getone(\$csqlt);";
                    $codex .= "\n            if (is_array(\$trow)) {";
                if ($cate == 'CATS')
                    $codex .= "\n                \$csql->where('{$col} like', ',' . \$trow['id'] . ',');";
                else
                    $codex .= "\n                \$csql->where('{$col}', \$trow['id']);";
                }
                $codex .= "\n                \$query['{$col}'] = \$trow['name'];";
                $codex .= "\n            } else {";
                $codex .= "\n                \$csql->where('{$col}=0');";
                $codex .= "\n            }";
                $codex .= "\n        }";
                if ($fld['breq']) {
                    $codex .= "\n        else";
                    $codex .= "\n            \$csql->where('{$col}', get('_{$col}'));";
                }
            }
        } else if ($cate == 'TBIN') {
            $codex .= "\n        \$val = objint(\$query, '{$col}');";
            $codex .= "\n        if (\$val > 0) {";
            $codex .= "\n            \$val = 1 << (\$val - 1);";
            $codex .= "\n            \$csql->where('{$col}&' . \$val . '=' . \$val);";
            $codex .= "\n        }";
        } else if (
            $cate == 'METRE' || $cate == 'FSIZE' || $cate == 'WCNY' || $cate == 'MCNY' || $cate == 'SEC' || $cate == 'TON' || $cate == 'BET'
            || $cate == 'PCT' || $cate == 'CNY' || $cate == 'NUM' || $cate == 'KG' || $cate == 'TC' || $cate == 'INT'
            || $cate == 'LNY' || $cate == 'SNY' || $cate == 'WGT' || $cate == 'LGH'
        ) {
            $bet = 1000;
            if ($cate == 'FSIZE')
                $bet = 1024;
            else if ($cate == 'WCNY' || $cate == 'TON')
                $bet = 1000000;
            else if ($cate == 'MCNY')
                $bet = 10000;
            else if ($cate == 'SEC' || $cate == 'NUM' || $cate == 'INT')
                $bet = 1;
            else if ($cate == 'PCT' || $cate == 'CNY' || $cate == 'LNY' || $cate == 'SNY')
                $bet = 100;
            else if ($cate == 'BET') {
                $bet = flddata($fld['data'], 1, 100);
            }
            $codex .= "\n        \$csql->wherenumrange('{$col}', objstr(\$query, '{$col}_1'), objstr(\$query, '{$col}_2'), {$bet});";
        } else if ($cate == 'DATE') {
            $datequery = isset($fld['set']['datequery']) ? $fld['set']['datequery'] : '';
            if ($datequery == 'month' || $datequery == 'day')
                $codex .= "\n        \$csql->wheredaterange('{$col}', objstr(\$query, '{$col}'), '{$datequery}');";
            else
                $codex .= "\n        \$csql->wheredaterange('{$col}', objstr(\$query, '{$col}'));";
        } else if ($cate == 'CATA' || $cate == 'BOOL' || $cate == 'TINT') {
            $codex .= "\n        \$csql->where('{$col}', objstr(\$query, '{$col}'));";
        } else {
            $codex .= "\n        \$csql->where('{$col} like', objstr(\$query, '{$col}'));";
        }
    }
    $codex .= "\n        \$order = objstr(\$query, 'order', 'id desc');";
    $codex .= "\n        \$csql->order(\$order);";
    $codex .= "\n        \$query['order'] = \$order;";
    $codex .= "\n        return [\$query, \$csql];";
    $codex .= "\n    }";

    $codex .= "\n";
    $codex .= "\n    public static function json_list() {";
    $codex .= "\n        global \$db;";
    if ($verifyfunc)
        $codex .= "\n        \$rsuser = {$verifyfunc}();";
    $codex .= "\n        \$post = new \ciy\post();";
    $codex .= "\n        list(\$where, \$csql) = self::setwhere(\$db, \$post, \$rsuser);";
    if ($saasfield) {
        if (!empty($saasusr))
            $codex .= "\n        \$csql->where('{$saasfield}', \$rsuser['{$saasusr}']);";
    }
    if (count($nocolumn) > 0) {
        $codex .= "\n        \$csql->column('!" . implode(',', $nocolumn) . "', \$db->getraw('show full fields from {$tabname}'));";
    }
    $codex .= "\n        \$pageno = \$post->getint('pageno', 1);";
    $codex .= "\n        \$pagecount = \$post->getint('pagecount', 10);";
    $codex .= "\n        \$csql->limit(\$pageno, \$pagecount);";
    $codex .= "\n        \$mainrowcount = \$post->getint('count');";
    $codex .= "\n        \$mrows = \$db->get(\$csql, \$mainrowcount);";
    $codex .= "\n        if (\$mrows === false)";
    $codex .= "\n            return errjson(\$db->error);";
    $codex .= "\n        \$ret = array('searchwhere' => \$where, 'pageno' => \$pageno, 'pagecount' => \$pagecount, 'count' => \$mainrowcount, 'list' => \$mrows);";
    $codex .= "\n        if (\$post->getbool('field')) {";
    $codex .= "\n            \$field = array();";
    $codex .= "\n            \$fshow = \$db->getfield(\$field, '{$tabname}');";
    $codex .= "\n            foreach (\$field as \$fr => \$v) {";
    $codex .= "\n                if (get('_' . \$fr))";
    $codex .= "\n                    \$field[\$fr]['c'] = ',' . \$field[\$fr]['c'];";
    if ($saasfield) {
        $codex .= "\n                if (\$fr == '{$saasfield}')";
        $codex .= "\n                    \$field[\$fr]['c'] = ',' . \$field[\$fr]['c'];";
    }
    $codex .= "\n            }";
    if ($btnlist_line)
        $codex .= "\n            \$fshow = fieldadd(\$fshow, \$field, 0, '_btn', '操作');";
    foreach ($flds as $fld) {
        if ($fld['bsrt'])
            $codex .= "\n            \$field['{$fld['col']}']['order'] = 'r';";
    }
    $codex .= "\n            \$ret['field'] = \$field;";
    $codex .= "\n            \$ret['fshow'] = \$fshow;";
    $codex .= "\n        }";
    $codex .= "\n        if (\$post->getbool('once')) {";
    $codex .= "\n            \$ret['once'] = true;";
    $codex .= "\n            \$input = array();";
    foreach ($flds as $fld) {
        if (!$fld['bqry'])
            continue;
        //$input[] = array('type' => 'switch', 'form' => 'locc', 'name' => '搜附近', 'value' => '附近,全部', 'def' => 2, 'tip' => '水电费' );
        $cate = $fld['cate'];
        $col = $fld['col'];
        if ($cate == 'DB' || $cate == 'CATU' || $cate == 'CATS' || $cate == 'CATM') {
            $dbquery = isset($fld['set']['dbquery']) ? $fld['set']['dbquery'] : '';
            $pspec = '';
            if ($fld['breq']) {
                $codex .= "\n            if (!get('_{$col}'))";
                $pspec = '    ';
            }
            if ($dbquery == 'window') {
                //暂未实现
            } else if ($dbquery == 'select') {
                $datas = explode('|', $fld['data']);
                $codex .= "\n{$pspec}            \$input[] = array('type' => 'select', 'form' => '{$col}', 'name' => '{$fld['name']}', 'all' => '全部', 'select' => '{$datas[0]}');";
            } else {
                $codex .= "\n{$pspec}            \$input[] = array('type' => 'input', 'form' => '{$col}', 'name' => '{$fld['name']}', 'prop' => ' style=\"width:8em;\"');";
            }
        } else if ($cate == 'CATA' || $cate == 'BOOL') {
            $cabquery = isset($fld['set']['cabquery']) ? $fld['set']['cabquery'] : '';
            $datas = explode('|', $fld['data']);
            if ($cate == 'CATA')
                $select = explode('|', $fld['data'])[0];
            else {
                $exts = explode('.', $fld['data']);
                if (count($exts) == 1)
                    $exts[1] = '';
                $exts[0] = empty($exts[0]) ? '✔' : $exts[0];
                $exts[1] = empty($exts[1]) ? '✘' : $exts[1];
                $select = "{$exts[0]}.{$exts[1]}";
            }
            if ($cabquery == 'radio')
                $codex .= "\n            \$input[] = array('type' => 'radio', 'form' => '{$col}', 'name' => '{$fld['name']}', 'all' => '全部', 'select' => '{$select}');";
            else
                $codex .= "\n            \$input[] = array('type' => 'select', 'form' => '{$col}', 'name' => '{$fld['name']}', 'all' => '全部', 'select' => '{$select}');";
        } else if ($cate == 'TBIN' || $cate == 'TINT') {
            $datas = explode('|', $fld['data']);
            $codex .= "\n            \$input[] = array('type' => 'select', 'form' => '{$col}', 'name' => '{$fld['name']}', 'all' => '全部', 'select' => '{$datas[0]}');";
        } else if ($cate == 'DATE') {
            $datequery = isset($fld['set']['datequery']) ? $fld['set']['datequery'] : '';
            if ($datequery == 'month' || $datequery == 'day')
                $codex .= "\n            \$input[] = array('type' => '{$datequery}', 'form' => '{$col}', 'name' => '{$fld['name']}');";
            else
                $codex .= "\n            \$input[] = array('type' => 'daterange', 'form' => '{$col}', 'name' => '{$fld['name']}');";
        } else if (
            $cate == 'METRE' || $cate == 'FSIZE' || $cate == 'WCNY' || $cate == 'MCNY' || $cate == 'SEC' || $cate == 'TON'
            || $cate == 'PCT' || $cate == 'CNY' || $cate == 'NUM' || $cate == 'KG' || $cate == 'TC' || $cate == 'INT'
            || $cate == 'BET' || $cate == 'LNY' || $cate == 'SNY' || $cate == 'WGT' || $cate == 'LGH'
        ) {
            $codex .= "\n            \$input[] = array('type' => 'num', 'form' => '{$col}', 'name' => '{$fld['name']}', 'prop' => ' style=\"width:4em;\"');";
        } else {
            $codex .= "\n            \$input[] = array('type' => 'input', 'form' => '{$col}', 'name' => '{$fld['name']}', 'prop' => ' style=\"width:8em;\"');";
        }
    }
    if ($selectorder) {
        $sos = explode(',', $selectorder);
        $codex .= "\n            \$vals = array();";
        foreach ($sos as $so) {
            $soarr = explode(':', $so);
            if (count($soarr) != 2)
                continue;
            $codex .= "\n            \$vals[] = array('id' => '{$soarr[0]}', 'name' => '{$soarr[1]}');";
        }
        $codex .= "\n            \$input[] = array('type' => 'select', 'form' => 'order', 'name' => '排序', 'select' => \$vals);";
    }
    $codex .= "\n            \$ret['searchinput'] = \$input;";

    foreach ($flds as $fld) {
        $cate = $fld['cate'];
        if ($cate == 'DB' || $cate == 'CATU' || $cate == 'CATS' || $cate == 'CATM') {
            if (empty($fld['set']['listload'])) {
                list($dicttype, $dictname) = splitdict($fld['data']);
                if ($dicttype == 'dictdb') {
                    $ext = '';
                    if ($cate == 'CATM')
                        $ext = ',upid';
                    $codex .= "\n            \$csql = (new \ciy\sql('{$dictname}'))->column('id,name{$ext}');";
                    $codex .= "\n            \$ret['{$dictname}'] = \$db->get(\$csql);";
                }
            }
        }
    }
    $codex .= "\n        }";
    $rels = array();
    foreach ($flds as $fld) {
        if (!$fld['show'])
            continue;
        if ($saasfield == $fld['col'])
            continue;
        $cate = $fld['cate'];
        if ($cate == 'DB' || $cate == 'CATU' || $cate == 'CATS' || $cate == 'CATM') {
            if (isset($fld['set']['listload']) && $fld['set']['listload'] == 'rel') {
                list($dicttype, $dictname) = splitdict($fld['data']);
                if ($dicttype == 'dictdb') {
                    if (isset($rels[$dictname]))
                        $rels[$dictname][] = $fld['col'];
                    else
                        $rels[$dictname] = array($fld['col']);
                }
            }
        }
    }
    foreach ($rels as $dictname => $cols) {
        $colsn = implode(',', $cols);
        $codex .= "\n        \$ret['{$dictname}'] = getrelation(\$db, \$mrows, '{$dictname}', '{$colsn}');";
    }
    $codex .= "\n        return succjson(\$ret);";
    $codex .= "\n    }";

    if ($chk_getupdate && ($hasedit || $hasview || $hasreview)) {
        $codex .= "\n";
        $codex .= "\n    public static function json_getdata() {";
        $codex .= "\n        global \$db;";
        if ($verifyfunc)
            $codex .= "\n        \$rsuser = {$verifyfunc}();";
        $codex .= "\n        \$post = new \ciy\post();";
        $codex .= "\n        \$id = \$post->getint('id');";
        $codex .= "\n        \$act = \$post->get('act');";
        $codex .= "\n        if (\$id > 0) {";
        $codex .= "\n            \$csql = new \ciy\sql('{$tabname}');";
        if ($saasfield) {
            if (!empty($saasusr))
                $codex .= "\n            \$csql->where('{$saasfield}', \$rsuser['{$saasusr}']);";
        }
        $codex .= "\n            \$csql->where('id', \$id);";
        $codex .= "\n            \$mrow = \$db->getone(\$csql);";
        $codex .= "\n            if (!is_array(\$mrow))";
        $codex .= "\n                return errjson('数据不存在');";
        $codex .= "\n            if (\$act == 'view' || \$act == 'review') {";
        foreach ($flds as $fld) {
            $cate = $fld['cate'];
            if ($cate == 'DB' || $cate == 'CATU' || $cate == 'CATS' || $cate == 'CATM') {
                if (!$fld['show'] && empty($fld['set']['noup']) && isset($fld['set']['listload']) && $fld['set']['listload'] == 'rel') {
                    list($dicttype, $dictname) = splitdict($fld['data']);
                    if ($dicttype == 'dictdb') {
                        $codex .= "\n                \$csql = (new \ciy\sql('{$dictname}'))->column('id,name');";
                        $codex .= "\n                \$csql->where('id', \$mrow['{$fld['col']}']);";
                        $codex .= "\n                \$ret['{$dictname}'] = \$db->get(\$csql);";
                    }
                }
            }
        }
        $codex .= "\n            }";
        $codex .= "\n        } else {";
        $codex .= "\n            \$mrow = array();";
        $codex .= "\n        }";
        $codex .= "\n        \$ret['data'] = \$mrow;";
        $codex .= "\n        if (\$act == 'edit') {";
        foreach ($flds as $fld) {
            $cate = $fld['cate'];
            if ($cate == 'DB' || $cate == 'CATU' || $cate == 'CATS' || $cate == 'CATM') {
                if (empty($fld['set']['noup']) && @$fld['set']['dbedit'] != 'text' && @$fld['set']['listload'] == 'rel') {
                    list($dicttype, $dictname) = splitdict($fld['data']);
                    if ($dicttype == 'dictdb') {
                        $codex .= "\n            \$csql = (new \ciy\sql('{$dictname}'))->column('id,name');";
                        $codex .= "\n            \$ret['{$dictname}'] = \$db->get(\$csql);";
                    }
                }
            }
        }
        $codex .= "\n        }";
        $codex .= "\n        return succjson(\$ret);";
        $codex .= "\n    }";
        $codex .= "\n";
    }
    if ($hasedit) {
        $codex .= "\n    public static function json_update() {";
        $codex .= "\n        global \$db;";
        if ($verifyfunc) {
            $codex .= "\n        \$rsuser = {$verifyfunc}();";
            $codex .= "\n        //if (nopower(\$db, \$rsuser['id'], 'p u'))";
            $codex .= "\n        //    return errjson('您未被授权操作');";
        }
        $codex .= "\n        \$post = new \ciy\post();";
        $codex .= "\n        //\$db->trace(\$post->get('_pf'), \$rsuser);";
        foreach ($flds as $fld) {
            $col = $fld['col'];
            // if ($col == $saasfield || $col == 'addtimes' || $col == 'ip' || $col == 'uptimes')
            //     continue;
            // if ($col == 'auditstatus' || $col == 'audittimes' || $col == 'audituser' || $col == 'auditmsg')
            //     continue;
            // if ($col == 'matterstatus' || $col == 'matternextuser' || $col == 'mattertimes')
            //     continue;
            if ($col != 'id' && !empty($fld['set']['noup']) && $fld['set']['noup'] != 'fow')
                continue;
            $type = $fld['type'];
            if ($type == 'bigint' || $type == 'int')
                $typefc = 'getint';
            else if ($type == 'double' || $type == 'float')
                $typefc = 'getfloat';
            else
                $typefc = 'get';
            if ($fld['cate'] == 'FILEMD')
                $codex .= "\n        \${$col}_text = \$post->get('{$col}');";
            else 
                $codex .= "\n        \${$col} = \$post->{$typefc}('{$col}');";
        }
        foreach ($flds as $fld) {
            if (!$fld['bimp'])
                continue;
            if (!empty($fld['set']['noup']))
                continue;
            $col = $fld['col'];
            $cate = $fld['cate'];
            $name = $fld['name'];
            $type = $fld['type'];
            if ($cate == 'MD')
                $codex .= "\n        if (\${$col} == '[MD]')";
            else if ($cate == 'FILEMD')
                $codex .= "\n        if (\${$col}_text == '[MD]')";
            else if ($type == 'bigint' || $type == 'int' || $type == 'double' || $type == 'float')
                $codex .= "\n        if (\${$col} == 0)";
            else
                $codex .= "\n        if (empty(\${$col}))";
            $codex .= "\n            return errjson('请填写{$name}');";
        }
        if ($hasadd) {
            $codex .= "\n        \$datarow = null;";
            $codex .= "\n        if (\$id > 0) {";
            $codex .= "\n            \$csql = new \ciy\sql('{$tabname}');";
            if ($saasfield) {
                if (!empty($saasusr))
                    $codex .= "\n            \$csql->where('{$saasfield}', \$rsuser['{$saasusr}']);";
            }
            $codex .= "\n            \$csql->where('id', \$id);";
            $codex .= "\n            \$datarow = \$db->getone(\$csql);";
            $codex .= "\n            if (!is_array(\$datarow))";
            $codex .= "\n                return errjson('数据不存在');";
            $codex .= "\n        }";
        } else {
            $codex .= "\n        \$csql = new \ciy\sql('{$tabname}');";
            if ($saasfield) {
                if (!empty($saasusr))
                    $codex .= "\n        \$csql->where('{$saasfield}', \$rsuser['{$saasusr}']);";
            }
            $codex .= "\n        \$csql->where('id', \$id);";
            $codex .= "\n        \$datarow = \$db->getone(\$csql);";
            $codex .= "\n        if (!is_array(\$datarow))";
            $codex .= "\n            return errjson('数据不存在');";
        }
        foreach ($flds as $fld) {
            $col = $fld['col'];
            if ($fld['cate'] == 'FILEMD'){
                $codex .= "\n        \${$col} = (isset(\$datarow['{$col}']) ? \$datarow['{$col}'] + 1 : 1);";
            }
        }
        $codex .= "\n        try {";
        $codex .= "\n            \$db->begin();";
        $dups = array();
        foreach ($flds as $fld) {
            if (!$fld['bdup'])
                continue;
            $dups[] = $fld['col'];
        }
        if (count($dups) > 0) {
            $codex .= "\n            \$csql = new \ciy\sql('{$tabname}');";
            if ($saasfield) {
                if (!empty($saasusr))
                    $codex .= "\n            \$csql->where('{$saasfield}', \$rsuser['{$saasusr}']);";
            }
            foreach ($dups as $dup) {
                $codex .= "\n            \$csql->where('{$dup}', \${$dup});";
            }
            $codex .= "\n            \$csql->column('id');";
            $codex .= "\n            \$chkid = toint(\$db->get1(\$csql));";
            $codex .= "\n            if (\$chkid > 0 && ((\$id > 0 && \$chkid != \$id) || \$id == 0))";
            $codex .= "\n                throw new \Exception('CIYIGN数据已存在');";
        }
        $codex .= "\n            \$updata = array();";
        foreach ($flds as $fld) {
            $col = $fld['col'];
            if ($col == 'id' || $col == 'addtimes')
                continue;
            // if ($col == 'auditstatus' || $col == 'audittimes' || $col == 'audituser' || $col == 'auditmsg')
            //     continue;
            // if ($col == 'matterstatus' || $col == 'matternextuser' || $col == 'mattertimes')
            //     continue;
            // if ($col == 'uptimes')
            //     $codex .= "\n            \$updata['{$col}'] = tostamp();";
            // else if ($col == 'ip')
            //     $codex .= "\n            \$updata['{$col}'] = getip();";
            if (!empty($fld['set']['noup'])) {
                if ($fld['set']['noup'] == 'now')
                    $codex .= "\n            \$updata['{$col}'] = tostamp();";
                else if ($fld['set']['noup'] == 'me')
                    $codex .= "\n            \$updata['{$col}'] = \$rsuser['id'];";
                else if ($fld['set']['noup'] == 'fow')
                    $codex .= "\n            \$updata['{$col}'] = \${$col};";
                else if ($fld['set']['noup'] == 'ip')
                    $codex .= "\n            \$updata['{$col}'] = getip();";
            } else
                $codex .= "\n            \$updata['{$col}'] = \${$col};";
        }
        $codex .= "\n            \$csql = new \ciy\sql('{$tabname}'); //auto";
        if ($hasadd) {
            $codex .= "\n            if (\$id > 0) {";
            $codex .= "\n                \$csql->where('id', \$id);";
            $codex .= "\n                if (\$db->update(\$csql, \$updata) === false)";
            $codex .= "\n                    throw new \Exception('更新失败:' . \$db->error);";
            $codex .= "\n            } else {";
            foreach ($flds as $fld) {
                $col = $fld['col'];
                if ($col == 'id') {
                } else if ($fld['set']['noup'] == 'no') {
                    if ($fld['type'] == 'int' || $fld['type'] == 'bigint')
                        $codex .= "\n                \$updata['{$col}'] = 0;";
                    else
                        $codex .= "\n                \$updata['{$col}'] = '';";
                } else if ($saasfield) {
                    if (!empty($saasusr))
                        $codex .= "\n                \$updata['{$col}'] = \$rsuser['{$saasusr}'];";
                } else if ($col == 'addtimes')
                    $codex .= "\n                \$updata['{$col}'] = tostamp();";
                else if ($col == 'auditstatus')
                    $codex .= "\n                \$updata['{$col}'] = 20;";
                else if ($col == 'matterstatus')
                    $codex .= "\n                \$updata['{$col}'] = 2;";
            }
            $codex .= "\n                if (\$db->insert(\$csql, \$updata) === false)";
            $codex .= "\n                    throw new \Exception('新增失败:' . \$db->error);";
            $codex .= "\n                \$id = \$db->insert_id();";
            $codex .= "\n            }";
            $codex .= "\n            \$updata['id'] = \$id;";
        } else {
            $codex .= "\n            \$csql->where('id', \$id);";
            $codex .= "\n            if (\$db->update(\$csql, \$updata) === false)";
            $codex .= "\n                throw new \Exception('更新失败:' . \$db->error);";
            $codex .= "\n            \$updata['id'] = \$id;";
        }
        $codex .= "\n            //savelogdb(\$db, \$rsuser['id'], '{$tabname}', \$datarow, \$updata);";
        $codex .= "\n            \$db->commit();";
        $codex .= "\n        } catch (\Exception \$ex) {";
        $codex .= "\n            \$db->rollback();";
        $codex .= "\n            savelogfile('err_db', \$ex->getMessage());";
        $codex .= "\n            return errjson(\$ex->getMessage());";
        $codex .= "\n        }";
        $codex .= "\n        \$ret['data'] = \$updata;";
        $rels = array();
        foreach ($flds as $fld) {
            if ($saasfield == $fld['col'])
                continue;
            $cate = $fld['cate'];
            if ($cate == 'DB' || $cate == 'CATU' || $cate == 'CATS' || $cate == 'CATM') {
                if (isset($fld['set']['listload']) && $fld['set']['listload'] == 'rel') {
                    list($dicttype, $dictname) = splitdict($fld['data']);
                    if ($dicttype == 'dictdb') {
                        if (isset($rels[$dictname]))
                            $rels[$dictname][] = $fld['col'];
                        else
                            $rels[$dictname] = array($fld['col']);
                    }
                }
            }
        }
        foreach ($rels as $dictname => $cols) {
            $colsn = implode(',', $cols);
            $codex .= "\n        \$ret['{$dictname}'] = getrelation(\$db, [\$updata], '{$dictname}', '{$colsn}');";
        }
        foreach ($flds as $fld) {
            $col = $fld['col'];
            if ($fld['cate'] == 'FILEMD'){
                $codex .= "\n        if (storsave('/md/{$fld['set']['savepath']}/' . \$id . '_' . \${$col} . '.txt', \${$col}_text) === false)";
                $codex .= "\n            return errjson('写入失败');";
            }
        }
        $codex .= "\n        return succjson(\$ret);";
        // $codex .= "\n        \$csql = new \ciy\sql('{$tabname}');";
        // $codex .= "\n        \$csql->where('id', \$id);";
        // $codex .= "\n        \$datarow = \$db->getone(\$csql);";
        // $codex .= "\n        \$ret['data'] = \$datarow;";
        // $codex .= "\n        return succjson(\$ret);";
        $codex .= "\n    }";
    }
    if ($hasdel) {
        $codex .= "\n";
        $codex .= "\n    public static function json_del() {";
        $codex .= "\n        global \$db;";
        if ($verifyfunc) {
            $codex .= "\n        \$rsuser = {$verifyfunc}();";
            $codex .= "\n        //if (nopower(\$db, \$rsuser['id'], 'p d'))";
            $codex .= "\n        //    return errjson('您未被授权操作');";
        }
        $codex .= "\n        \$post = new \ciy\post();";
        $codex .= "\n        //\$db->trace(\$post->get('_pf'), \$rsuser);";
        $codex .= "\n        \$ids = \$post->get('ids');";
        $codex .= "\n        if (empty(\$ids))";
        $codex .= "\n            return errjson('请选择至少一条');";
        $codex .= "\n        \$csql = new \ciy\sql('{$tabname}');";
        if ($saasfield) {
            if (!empty($saasusr))
                $codex .= "\n        \$csql->where('{$saasfield}', \$rsuser['{$saasusr}']);";
        }
        $codex .= "\n        \$csql->where('id in', \$ids);";
        $codex .= "\n        \$mrows = \$db->get(\$csql);";
        $codex .= "\n        \$vids = array();";
        $codex .= "\n        try {";
        $codex .= "\n            \$db->begin();";
        $codex .= "\n            foreach (\$mrows as \$mrow) {";
        $codex .= "\n                \$delid = \$mrow['id'];";
        $codex .= "\n                //delcheck(\$db, \$delid, 'tablexx', 'xxid', '管理员');";
        if ($chk_deltimes) {
            $codex .= "\n                //deltimeall(\$db, \$delid, 'tablexx', 'xxid', '运动员');";
            $codex .= "\n                deltimeme(\$db, \$delid, '{$tabname}');";
        } else {
            $codex .= "\n                //delall(\$db, \$delid, 'tablexx', 'xxid', '运动员');  //deltimeall";
            $codex .= "\n                delme(\$db, \$delid, '{$tabname}');";
        }
        $codex .= "\n                savelogdb(\$db, \$rsuser['id'], '{$tabname}', \$mrow, null);";
        $codex .= "\n                \$vids[] = \$delid;";
        $codex .= "\n            }";
        $codex .= "\n            \$db->commit();";
        $codex .= "\n        } catch (\Exception \$ex) {";
        $codex .= "\n            \$db->rollback();";
        $codex .= "\n            savelogfile('err_db', \$ex->getMessage());";
        $codex .= "\n            return errjson(\$ex->getMessage());";
        $codex .= "\n        }";
        $codex .= "\n        \$ret['ids'] = \$vids;";
        $codex .= "\n        return succjson(\$ret);";
        $codex .= "\n    }";
    }

    if ($hasreview || $hasaudit) {
        $codex .= "\n";
        $codex .= "\n    public static function json_audit() {";
        $codex .= "\n        global \$db;";
        if ($verifyfunc) {
            $codex .= "\n        \$rsuser = {$verifyfunc}();";
            $codex .= "\n        //if (nopower(\$db, \$rsuser['id'], 'p a'))";
            $codex .= "\n        //    return errjson('您未被授权操作');";
        }
        $codex .= "\n        \$post = new \ciy\post();";
        $codex .= "\n        //\$db->trace(\$post->get('_pf'), \$rsuser);";
        $codex .= "\n        \$ids = \$post->get('ids');";
        $codex .= "\n        if (empty(\$ids))";
        $codex .= "\n            return errjson('请选择至少一条');";
        $codex .= "\n        \$auditstatus = \$post->getint('auditstatus');";
        foreach ($flds as $fld) {
            if ($fld['col'] == 'auditmsg') {
                $codex .= "\n        \$auditmsg = \$post->get('auditmsg');";
                $codex .= "\n        if (\$auditstatus == 90 && empty(\$auditmsg))";
                $codex .= "\n            return errjson('请填写驳回原因');";
            }
        }
        $codex .= "\n        \$csql = new \ciy\sql('{$tabname}');";
        if ($saasfield) {
            if (!empty($saasusr))
                $codex .= "\n        \$csql->where('{$saasfield}', \$rsuser['{$saasusr}']);";
        }
        $codex .= "\n        \$csql->where('id in', \$ids);";
        $codex .= "\n        \$mrows = \$db->get(\$csql);";
        $codex .= "\n        \$ids = array();";
        $codex .= "\n        try {";
        $codex .= "\n            \$db->begin();";
        $codex .= "\n            foreach (\$mrows as \$mrow) {";
        $codex .= "\n                if (\$auditstatus == 100) {";
        $codex .= "\n                }";
        $codex .= "\n                \$updata = array();";
        foreach ($flds as $fld) {
            $col = $fld['col'];
            if ($col == 'audituser')
                $codex .= "\n                \$updata['{$col}'] = \$rsuser['id'];";
            if ($col == 'audittimes')
                $codex .= "\n                \$updata['{$col}'] = tostamp();";
            if ($col == 'auditstatus')
                $codex .= "\n                \$updata['{$col}'] = \$auditstatus;";
            if ($col == 'auditmsg')
                $codex .= "\n                \$updata['{$col}'] = \$auditmsg;";
        }
        $codex .= "\n                \$csql = new \ciy\sql('{$tabname}');";
        $codex .= "\n                \$csql->where('id', \$mrow['id']);";
        $codex .= "\n                if (\$db->update(\$csql, \$updata) === false)";
        $codex .= "\n                    throw new \Exception('审核失败:' . \$db->error);";
        $codex .= "\n                \$ids[] = \$mrow['id'];";
        $codex .= "\n            }";
        $codex .= "\n            \$db->commit();";
        $codex .= "\n        } catch (\Exception \$ex) {";
        $codex .= "\n            \$db->rollback();";
        $codex .= "\n            savelogfile('err_db', \$ex->getMessage());";
        $codex .= "\n            return errjson(\$ex->getMessage());";
        $codex .= "\n        }";
        $codex .= "\n        \$ret['data'] = \$updata;";
        $codex .= "\n        \$ret['ids'] = \$ids;";
        $codex .= "\n        return succjson(\$ret);";
        $codex .= "\n    }";
    }

    if ($chk_multistatus) {
        $codex .= "\n";
        $codex .= "\n    public static function json_multi_status() {";
        $codex .= "\n        global \$db;";
        if ($verifyfunc) {
            $codex .= "\n        \$rsuser = {$verifyfunc}();";
            $codex .= "\n        //if (nopower(\$db, \$rsuser['id'], 'p m'))";
            $codex .= "\n        //    return errjson('您未被授权操作');";
        }
        $codex .= "\n        \$post = new \ciy\post();";
        $codex .= "\n        //\$db->trace(\$post->get('_pf'), \$rsuser);";
        $codex .= "\n        \$status = \$post->getint('status');";
        $codex .= "\n        \$ids = \$post->get('ids');";
        $codex .= "\n        if (empty(\$ids))";
        $codex .= "\n            return errjson('请选择至少一条');";
        $codex .= "\n        \$csql = new \ciy\sql('{$tabname}');";
        if ($saasfield) {
            if (!empty($saasusr))
                $codex .= "\n        \$csql->where('{$saasfield}', \$rsuser['{$saasusr}']);";
        }
        $codex .= "\n        \$csql->where('id in', \$ids);";
        $codex .= "\n        \$mrows = \$db->get(\$csql);";
        $codex .= "\n        \$ids = array();";
        $codex .= "\n        try {";
        $codex .= "\n            \$db->begin();";
        $codex .= "\n            foreach (\$mrows as \$mrow) {";
        $codex .= "\n                \$updata = array();";
        $codex .= "\n                \$updata['status'] = \$status;";
        $codex .= "\n                \$csql = new \ciy\sql('{$tabname}');";
        $codex .= "\n                \$csql->where('id', \$mrow['id']);";
        $codex .= "\n                if (\$db->update(\$csql, \$updata) === false)";
        $codex .= "\n                    throw new \Exception('批量操作失败:' . \$db->error);";
        $codex .= "\n                \$ids[] = \$mrow['id'];";
        $codex .= "\n            }";
        $codex .= "\n            \$db->commit();";
        $codex .= "\n        } catch (\Exception \$ex) {";
        $codex .= "\n            \$db->rollback();";
        $codex .= "\n            savelogfile('err_db', \$ex->getMessage());";
        $codex .= "\n            return errjson(\$ex->getMessage());";
        $codex .= "\n        }";
        $codex .= "\n        \$ret['data'] = \$updata;";
        $codex .= "\n        \$ret['ids'] = \$ids;";
        $codex .= "\n        return succjson(\$ret);";
        $codex .= "\n    }";
    }
    if ($hasexcelout) {
        $codex .= "\n";
        $codex .= "\n    public static function json_exportxls() {";
        $codex .= "\n        global \$db;";

        $codex .= "\n        \$rsuser = verifyfast();";
        $codex .= "\n        //if (nopower(\$db, \$rsuser['id'], 'p e'))";
        $codex .= "\n        //    return errjson('您未被授权操作');";

        $codex .= "\n        \$post = new \\ciy\\post();";
        $codex .= "\n        list(\$where, \$csql) = self::setwhere(\$db, \$post, \$rsuser);";
        $codex .= "\n        \$mrows = \$db->get(\$csql);";
        $codex .= "\n        if (count(\$mrows) > 10000)";
        $codex .= "\n            return errjson('将导出' . count(\$mrows) . '条，不建议超过1万条，请筛选缩小范围');";
        $codex .= "\n        \$fields = array();";
        foreach ($flds as $fld) {
            if (!$fld['bout'])
                continue;
            if ($saasfield == $fld['col'])
                continue;
            $col = $fld['col'];
            $name = $fld['name'];
            $cate = $fld['cate'];
            $type = $fld['type'];
            $width = 100;
            if ($col == 'id') {
                $width = 60;
                $name = '行码';
            }
            if ($type == 'varchar' || $cate == 'CATS' || $cate == 'CATM')
                $width = 150;
            if ($cate == 'IMG')
                $width = 300;
            $codex .= "\n        \$fields[] = array('style' => '{$fld['align']}', 'width' => {$width},  'field' => '{$col}', 'name' => '{$name}');";
        }
        foreach ($flds as $fld) {
            if (!$fld['bout'])
                continue;
            if ($saasfield == $fld['col'])
                continue;
            $col = $fld['col'];
            $cate = $fld['cate'];
            if ($cate == 'BOOL') {
                $codex .= "\n        \$code_{$col} = \$db->getdbcodes('{$tabname}', '{$col}');";
            } else if ($cate == 'TINT' || $cate == 'TBIN' || $cate == 'CATS' || $cate == 'CATM' || $cate == 'DB' || $cate == 'CATA' || $cate == 'CATU') {
                $ext = '';
                if ($cate == 'CATM')
                    $ext = ',upid';
                list($dicttype, $dictname) = splitdict($fld['data']);
                if ($dicttype == 'dictciy') {
                    $codex .= "\n        \$code_{$col} = \$db->get((new \\ciy\\sql('{$dictname}'))->column('id,name{$ext}'));";
                } else if ($dicttype == 'dictdb') {
                    if (isset($fld['set']['listload']) && $fld['set']['listload'] == 'rel')
                        $codex .= "\n        \$code_{$col} = getrelation(\$db, \$mrows, '{$dictname}', '{$col}', 'id,name{$ext}');";
                    else
                        $codex .= "\n        \$code_{$col} = \$db->get((new \\ciy\\sql('{$dictname}'))->column('id,name{$ext}'));";
                } else if ($dicttype == 'dictcata') {
                    $codex .= "\n        \$code_{$col} = getcatas(\$db, '{$dictname}');";
                } else {
                    $codex .= "\n        \$code_{$col} = \$db->getdbcodes('{$tabname}', '{$col}');";
                }
            }
        }
        $codex .= "\n        \$datas = array();";
        $codex .= "\n        foreach (\$mrows as \$mrow) {";
        $codex .= "\n            \$dat = array();";
        $codex .= "\n            foreach (\$fields as \$field) {";
        $codex .= "\n                \$field = \$field['field'];";
        $codex .= "\n                \$val = isset(\$mrow[\$field]) ? \$mrow[\$field] : '';";

        foreach ($flds as $fld) {
            if ($fld['col'] != 'id' && !$fld['bout'])
                continue;
            if ($saasfield == $fld['col'])
                continue;
            $col = $fld['col'];
            $cate = $fld['cate'];
            $ext = '';
            if ($col == 'id') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = enid(\$val);";
            } else if ($cate == 'FSIZE') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = number_format(\$val / 1024, 2) . 'KB';";
            } else if ($cate == 'METRE') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = number_format(\$val / 1000, 2) . '米';";
            } else if ($cate == 'DATE') {
                $codex .= "\n                if (\$field == '{$col}')";
                if (strpos('H', $fld['data']) !== false)
                    $codex .= "\n                    \$val = (\$val <= 0 ? '--' : date('Y-m-d H:i', \$val));";
                else
                    $codex .= "\n                    \$val = (\$val <= 0 ? '--' : date('Y-m-d', \$val));";
            } else if ($cate == 'TIME') {
                $codex .= "\n                if (\$field == '{$col}')";
                if (strpos(':s', $fld['data']) !== false)
                    $ext = ', true';
                $codex .= "\n                    \$val = (\$val <= 0 ? '--' : totimepoint(\$val{$ext}));";
            } else if ($cate == 'WCNY') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = number_format(\$val / 1000000, 3) . '万元';";
            } else if ($cate == 'MCNY') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = number_format(\$val / 10000, 4) . '元';";
            } else if ($cate == 'BOOL') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = (\$val == 1 ? \$code_{$col}[0]['name'] : @\$code_{$col}[1]['name']);";
            } else if ($cate == 'UNIT') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = (\$val == '' ? '--' : implode(',', tounit(\$val)));";
            } else if ($cate == 'CATA' || $cate == 'TINT' || $cate == 'DB' || $cate == 'CATU') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = ccode(\$code_{$col}, \$val);";
            } else if ($cate == 'TBIN') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = implode(',', bcode(\$code_{$col}, \$val));";
            } else if ($cate == 'CATS') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = implode(',', scode(\$code_{$col}, \$val));";
            } else if ($cate == 'CATM') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = implode('-', mcode(\$code_{$col}, \$val));";
            } else if ($cate == 'CYC') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = (\$val == 0 ? '--' : tocyc(\$val));";
            } else if ($cate == 'SEC') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val .= '秒';";
            } else if ($cate == 'BET') {
                $unit = flddata($fld['data'], 0, '');
                $bet = flddata($fld['data'], 1, 100);
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = number_format(\$val / {$bet}, 3) . '{$unit}';";
            } else if ($cate == 'LNY') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = number_format(\$val / 100, 2) . '元';";
            } else if ($cate == 'LNY') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = number_format(\$val / 100, 2) . '¤';";
            } else if ($cate == 'WGT') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = number_format(\$val / 1000, 3) . 'KG';";
            } else if ($cate == 'LGH') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = number_format(\$val / 1000, 3) . '米';";
            } else if ($cate == 'TON') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = number_format(\$val / 1000000, 3) . '吨';";
            } else if ($cate == 'PCT') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = number_format(\$val / 100, 2) . '%';";
            } else if ($cate == 'CNY') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = number_format(\$val / 100, 2);";
            } else if ($cate == 'KG') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = number_format(\$val / 1000, 3) . 'KG';";
            } else if ($cate == 'TC') {
                $codex .= "\n                if (\$field == '{$col}')";
                $codex .= "\n                    \$val = number_format(\$val / 1000, 1) . '℃';";
            } else {
            }
        }
        $codex .= "\n                \$dat[] = \$val;";
        $codex .= "\n            }";
        $codex .= "\n            \$datas[] = \$dat;";
        $codex .= "\n        }";

        $codex .= "\n        \$param = array();";
        $codex .= "\n        \$param['field'] = \$fields;";
        $codex .= "\n        \$param['data'] = \$datas;";

        $codex .= "\n        \$param['sheetname'] = '数据报表';";
        $codex .= "\n        \$param['titleheight'] = '25'; //列头高度";
        $codex .= "\n        \$param['landscape'] = true; //横向打印";
        $codex .= "\n        \$param['fixtopage'] = true; //打印整个工作表";

        $codex .= "\n        \$param['toptitle'] = '{$titname}数据报表';";
        $codex .= "\n        \$str = \\ciy\\excel::general_excel_xml(\$param);";
        $codex .= "\n        \$filename = '';";
        $codex .= "\n        if (empty(\$filename))";
        $codex .= "\n            \$filename = date('Y-m-d_H-i-s') . rand(1000, 9999);";
        $codex .= "\n        \$filename .= '.xls';";
        $codex .= "\n        file_put_contents(PATH_WEB . 'ud/tmp/' . \$filename, \$str);";
        $codex .= "\n        return succjson(array('url' => '/ud/tmp/' . \$filename));";
        $codex .= "\n    }";
    }
    if ($hasexcelin) {
        $codex .= "\n";
        $codex .= "\n    public static function json_importxls_in() {";
        $codex .= "\n        global \$db;";
        $codex .= "\n        \$rsuser = verifyfast();";
        $codex .= "\n        //if (nopower(\$db, \$rsuser['id'], 'p i'))";
        $codex .= "\n        //    return errjson('您未被授权操作');";

        $codex .= "\n        \$post = new \ciy\post();";
        $codex .= "\n        \$file = PATH_WEB . '/ud' . \$post->get('file');";
        $codex .= "\n        if (!file_exists(\$file))";
        $codex .= "\n            return errjson('文件不存在');";
        $codex .= "\n        require_once PATH_ROOT . '../libs/phpoffice/autoload.php';";
        $codex .= "\n        \$spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load(\$file);";
        $codex .= "\n        \$sheet = \$spreadsheet->getActiveSheet();";
        $codex .= "\n        \$datas = \$sheet->toArray('', true, true, false);";
        $codex .= "\n        \$datacnt = count(\$datas);";
        $codex .= "\n        if (\$datacnt < 2)";
        $codex .= "\n            return errjson('数据为空');";
        $codex .= "\n        \$html = '';";
        $codex .= "\n        \$headsn = array();";

        foreach ($flds as $fld) {
            $col = $fld['col'];
            if ($fld['cate'] == 'FILEMD')
                continue;
            if ($col == 'id') {
                $codex .= "\n        \$headsn[] = '行码.id';";
                continue;
            }
            if (!$fld['bmin'])
                continue;
            $name = $fld['name'];
            $codex .= "\n        \$headsn[] = '{$name}.{$col}';";
        }
        $codex .= "\n        \$xlsidx = 1;";
        $codex .= "\n        if (empty(\$datas[0][count(\$headsn) - 1]))";
        $codex .= "\n            \$xlsidx = 2;";
        $codex .= "\n        \$heads = array();";
        $codex .= "\n        foreach (\$headsn as \$_head) {";
        $codex .= "\n            \$hd = explode('.', \$_head);";
        $codex .= "\n            if (count(\$hd) < 2)";
        $codex .= "\n                continue;";
        $codex .= "\n            \$heads[] = array(";
        $codex .= "\n                'idx' => array_search(\$hd[0], \$datas[\$xlsidx - 1]), 'fld' => \$hd[1], 'name' => \$hd[0]";
        $codex .= "\n            );";
        $codex .= "\n        }";

        foreach ($flds as $fld) {
            if (!$fld['bmin'])
                continue;
            $col = $fld['col'];
            $cate = $fld['cate'];
            if ($cate == 'BOOL') {
                $codex .= "\n        \$code_{$col} = \$db->getdbcodes('{$tabname}', '{$col}');";
            } else if ($cate == 'TINT' || $cate == 'TBIN' || $cate == 'CATS' || $cate == 'CATM' || $cate == 'DB' || $cate == 'CATA' || $cate == 'CATU') {
                $ext = '';
                if ($cate == 'CATM')
                    $ext = ',upid';
                list($dicttype, $dictname) = splitdict($fld['data']);
                if ($dicttype == 'dictciy') {
                    $codex .= "\n        \$code_{$col} = \$db->get((new \\ciy\\sql('{$dictname}'))->column('id,name{$ext}'));";
                } else if ($dicttype == 'dictdb') {
                    $codex .= "\n        \$code_{$col} = \$db->get((new \\ciy\\sql('{$dictname}'))->column('id,name{$ext}'));";
                } else if ($dicttype == 'dictcata') {
                    $codex .= "\n        \$code_{$col} = getcatas(\$db, '{$dictname}');";
                } else {
                    $codex .= "\n        \$code_{$col} = \$db->getdbcodes('{$tabname}', '{$col}');";
                }
            }
        }
        $codex .= "\n        \$html .= '<div class=\"table\" style=\"width: 100%;height: calc(100% - 2.2em);overflow: auto;\">';";
        $codex .= "\n        \$html .= '<table><tbody><tr>';";
        $codex .= "\n        \$html .= '<th>#</th>';";
        $codex .= "\n        foreach (\$heads as \$arr) {";
        $codex .= "\n            \$html .= '<th>' . \$arr['name'] . '</th>';";
        $codex .= "\n        }";
        $codex .= "\n        \$html .= '</tr>';";
        $codex .= "\n        \$cnt = 0;";
        $codex .= "\n        \$uniques = array();";
        $codex .= "\n        \$id = 0;";
        $codex .= "\n        for (\$rowidx = \$xlsidx; \$rowidx < \$datacnt; \$rowidx++) {";
        $codex .= "\n            \$bfull = false;";
        $codex .= "\n            foreach (\$datas[\$rowidx] as \$dat) {";
        $codex .= "\n                if (empty(\$dat))";
        $codex .= "\n                    continue;";
        $codex .= "\n                \$bfull = true;";
        $codex .= "\n                break;";
        $codex .= "\n            }";
        $codex .= "\n            if (!\$bfull)";
        $codex .= "\n                continue;";
        $codex .= "\n            \$lineidx = \$rowidx - \$xlsidx + 1;";
        $codex .= "\n            \$hrhtml = '';";
        $codex .= "\n            \$firsthtml = '<td><div>' . \$lineidx . '</div></td>';";
        $codex .= "\n            \$bempty = true;";
        $codex .= "\n            \$unqs = array();";
        $codex .= "\n            \$csql = new \ciy\sql('{$tabname}');";
        $codex .= "\n            foreach (\$heads as \$arr) {";
        $codex .= "\n                \$name = \$arr['name'];";
        $codex .= "\n                \$errmsg = ''; //数据有误，显示红色说明";
        $codex .= "\n                \$showdat = ''; //显示在表格中的数据";
        $codex .= "\n                if (\$arr['idx'] > -1)";
        $codex .= "\n                    \$showdat = trim(\$datas[\$rowidx][\$arr['idx']]);";
        $codex .= "\n                if (\$showdat == '--')";
        $codex .= "\n                    \$showdat = '';";
        $codex .= "\n                \$value = \$showdat; //在表单中的数据(转换后)";
        $codex .= "\n                \$ext = ''; //扩展表单";
        $codex .= "\n                if (\$name == '行码') {";
        $codex .= "\n                    if (empty(\$showdat)) {";
        $codex .= "\n                        \$value = 0;";
        $codex .= "\n                        \$showdat = '<kbd>新增</kbd>';";
        $codex .= "\n                    } else {";
        $codex .= "\n                        \$id = deid(\$showdat);";
        $codex .= "\n                        if (\$id == 0)";
        $codex .= "\n                            \$errmsg = \$name . '解析错误';";
        $codex .= "\n                        else {";
        $codex .= "\n                            \$csqlchk = new \ciy\sql('{$tabname}');";
        $codex .= "\n                            \$csqlchk->where('id', \$id)->column('id');";
        $codex .= "\n                            \$chkid = toint(\$db->get1(\$csqlchk));";
        $codex .= "\n                            if (\$chkid != \$id)";
        $codex .= "\n                                \$errmsg = \$name . '在数据库中不存在';";
        $codex .= "\n                            \$value = \$id;";
        $codex .= "\n                        }";
        $codex .= "\n                    }";

        foreach ($flds as $fld) {
            if ($fld['col'] == 'id' || !$fld['bmin'])
                continue;
            $col = $fld['col'];
            if($col == 'FILEMD')
                continue;
            $name = $fld['name'];
            $cate = $fld['cate'];
            $type = $fld['type'];
            $ext = '';
            $codex .= "\n                } else if (\$name == '{$name}') {";
            $codex .= "\n                    if (empty(\$showdat)) {";
            if ($fld['bimp']) {
                $codex .= "\n                        \$errmsg = \$name . '为必填项';";
            } else {
                if ($type == 'bigint' || $type == 'int' || $type == 'double' || $type == 'float')
                    $codex .= "\n                        \$value = 0;";
                else
                    $codex .= "\n                        \$value = '';";
            }
            $codex .= "\n                    } else {";
            if ($fld['bdup']) {
                $codex .= "\n                        \$csql->where('{$col}', \$showdat);";
                $codex .= "\n                        \$unqs[] = \$showdat;";
            }

            if ($cate == 'CATA' || $cate == 'TINT' || $cate == 'DB' || $cate == 'CATU') {
                $codex .= "\n                        \$value = dcode(\$code_{$col}, \$showdat);";
                $codex .= "\n                        if (\$value == -1)";
                $codex .= "\n                            \$errmsg = \$name . '文字与系统数据不匹配';";
            } else if ($cate == 'DATE') {
                $codex .= "\n                        \$value = tostamp(\$showdat);";
                $codex .= "\n                        if (\$value === 0) {";
                $codex .= "\n                            \$errmsg = \$name . '时间格式错误';";
                $codex .= "\n                        } else {";
                if (strpos('H', $fld['data']) !== false)
                    $codex .= "\n                            \$showdat = date('Y-m-d H:i', \$value);";
                else
                    $codex .= "\n                            \$showdat = date('Y-m-d', \$value);";
                $codex .= "\n                        }";
            } else if ($cate == 'TIME') {
                $codex .= "\n                        \$tps = explode(':', \$showdat);";
                $codex .= "\n                        \$value = 0;";
                $codex .= "\n                        if (count(\$tps) == 2)";
                $codex .= "\n                            \$value = \$tps[0] * 3600 + \$tps[1] * 60;";
                $codex .= "\n                        else if (count(\$tps) == 3)";
                $codex .= "\n                            \$value = \$tps[0] * 3600 + \$tps[1] * 60 + \$tps[2];";
                $codex .= "\n                        if (\$value === 0) {";
                $codex .= "\n                            \$errmsg = \$name . '时间格式错误';";
                $codex .= "\n                        } else {";
                $codex .= "\n                            \$showdat = totimepoint(\$value, true);";
                $codex .= "\n                        }";
            } else if ($cate == 'BOOL') {
                $codex .= "\n                        if (\$showdat == \$code_{$col}[0]['name']) {";
                $codex .= "\n                            \$value = 1;";
                $codex .= "\n                            \$showdat = \$code_{$col}[0]['name'];";
                $codex .= "\n                        } else {";
                $codex .= "\n                            \$value = 2;";
                $codex .= "\n                            \$showdat = \$code_{$col}[1]['name'];";
                $codex .= "\n                        }";
            } else if ($cate == 'UNIT') {
                $codex .= "\n                        \$us = explode(',', \$showdat);";
                $codex .= "\n                        \$us = array_reverse(\$us);";
                $codex .= "\n                        if (preg_match('/1(.*?)(\d+)(.*)/', \$us[0], \$matches)) {";
                $codex .= "\n                            \$unitx = \$matches[1];";
                $codex .= "\n                            \$value = \$matches[3] . '|' . \$matches[2] . '|' . \$matches[1];";
                $codex .= "\n                            if (isset(\$us[1])) {";
                $codex .= "\n                                if (preg_match('/1(.*?)(\d+)(.*)/', \$us[1], \$matches)) {";
                $codex .= "\n                                    if (\$unitx != \$matches[3]) {";
                $codex .= "\n                                        \$errmsg = \$name . '中间单位不匹配';";
                $codex .= "\n                                    } else {";
                $codex .= "\n                                        \$value .= '|' . \$matches[2] . '|' . \$matches[1];";
                $codex .= "\n                                    }";
                $codex .= "\n                                } else {";
                $codex .= "\n                                    \$errmsg = \$name . '第二段格式错误';";
                $codex .= "\n                                }";
                $codex .= "\n                            }";
                $codex .= "\n                        } else {";
                $codex .= "\n                            if (preg_match('/\d/', \$us[0]) > 0)";
                $codex .= "\n                                \$errmsg = \$name . '第一段不能包含数字';";
                $codex .= "\n                            else";
                $codex .= "\n                                \$value = \$us[0];";
                $codex .= "\n                        }";
            } else if ($cate == 'TBIN') {
                $codex .= "\n                        \$dats = explode(',', \$showdat);";
                $codex .= "\n                        \$tdat = 0;";
                $codex .= "\n                        foreach (\$dats as \$da) {";
                $codex .= "\n                            \$bti = dcode(\$code_{$col}, \$da);";
                $codex .= "\n                            if (\$bti < 1) {";
                $codex .= "\n                                \$errmsg = \$name . '文字与系统数据不匹配';";
                $codex .= "\n                            } else {";
                $codex .= "\n                                \$tdat += (1 << (\$bti - 1));";
                $codex .= "\n                            }";
                $codex .= "\n                        }";
                $codex .= "\n                        \$value = \$tdat;";
            } else if ($cate == 'CATS') {
                $codex .= "\n                        \$tdats = explode(',', \$showdat);";
                $codex .= "\n                        \$vals = array();";
                $codex .= "\n                        foreach (\$tdats as \$da) {";
                $codex .= "\n                            \$tval = dcode(\$code_{$col}, \$da);";
                $codex .= "\n                            if (\$tval == -1)";
                $codex .= "\n                                \$errmsg = \$name . '文字与系统数据不匹配';";
                $codex .= "\n                            \$vals[] = \$tval;";
                $codex .= "\n                        }";
                $codex .= "\n                        \$value = implode(',', \$vals);";
            } else if ($cate == 'CATM') {
                $codex .= "\n                        \$dats = explode('-', \$showdat);";
                $codex .= "\n                        \$value = dcode(\$code_{$col}, \$dats[count(\$dats) - 1]);";
                $codex .= "\n                        if (\$value == -1)";
                $codex .= "\n                            \$errmsg = \$name . '文字与系统数据不匹配';";
            } else if ($cate == 'CYC') {
                $codex .= "\n                        \$last = substr(\$showdat, -3);";
                $codex .= "\n                        \$dd = toint(substr(\$showdat, 0, -3));";
                $codex .= "\n                        if (\$dd == 0) {";
                $codex .= "\n                            \$errmsg = \$name . '周期数字错误';";
                $codex .= "\n                        } else if (\$last == '天') {";
                $codex .= "\n                            \$value = \$dd * 86400;";
                $codex .= "\n                        } else if (\$last == '月') {";
                $codex .= "\n                            \$value = -\$dd;";
                $codex .= "\n                        } else if (\$last == '秒') {";
                $codex .= "\n                            \$value = \$dd;";
                $codex .= "\n                        } else {";
                $codex .= "\n                            \$errmsg = \$name . '周期单位错误';";
                $codex .= "\n                        }";
            } else if ($type == 'double' || $type == 'float' || $type == 'int' || $type == 'bigint') {
                $unit = '';
                $bet = 1;
                if ($cate == 'FSIZE') {
                    $unit = 'KB';
                    $bet = 1024;
                } else if ($cate == 'METRE') {
                    $unit = '米';
                    $bet = 1000;
                } else if ($cate == 'WCNY') {
                    $unit = '万元';
                    $bet = 1000000;
                } else if ($cate == 'SEC') {
                    $unit = '秒';
                } else if ($cate == 'BET') {
                    $unit = flddata($fld['data'], 0, '');
                    $bet = flddata($fld['data'], 1, 100);
                } else if ($cate == 'LGH') {
                    $unit = '米';
                    $bet = 1000;
                } else if ($cate == 'WGT') {
                    $unit = 'KG';
                    $bet = 1000;
                } else if ($cate == 'LNY') {
                    $unit = '元';
                    $bet = 100;
                } else if ($cate == 'SNY') {
                    $unit = '¤';
                    $bet = 100;
                } else if ($cate == 'TON') {
                    $unit = '吨';
                    $bet = 1000000;
                } else if ($cate == 'CNY') {
                    $bet = 100;
                } else if ($cate == 'PCT') {
                    $unit = '%';
                    $bet = 100;
                } else if ($cate == 'KG') {
                    $unit = 'KG';
                    $bet = 1000;
                } else if ($cate == 'TC') {
                    $unit = '℃';
                    $bet = 1000;
                }
                if (empty($unit))
                    $codex .= "\n                        \$showdat = str_replace(',', '', \$showdat);";
                else
                    $codex .= "\n                        \$showdat = str_replace(',', '', str_replace('{$unit}', '', \$showdat));";
                $codex .= "\n                        if (!is_numeric(\$showdat))";
                $codex .= "\n                            \$errmsg = \$name . '不是数字';";
                $codex .= "\n                        else";
                if ($bet > 1)
                    $codex .= "\n                            \$value = toint((float)\$showdat * {$bet});";
                else if ($type == 'double' || $type == 'float')
                    $codex .= "\n                            \$value = (float)\$showdat;";
                else
                    $codex .= "\n                            \$value = toint(\$showdat);";
                if (!empty($unit))
                    $codex .= "\n                            \$showdat .= '{$unit}';";
            }
            $codex .= "\n                    }";
        }
        $codex .= "\n                }";

        $codex .= "\n                if (!empty(\$showdat))";
        $codex .= "\n                    \$bempty = false;";

        $codex .= "\n                if (empty(\$errmsg))";
        $codex .= "\n                    \$hrhtml .= '<td><div>' . \$showdat . '<input type=\"hidden\" name=\"' . \$arr['fld'] . '_' . \$lineidx . '\" value=\"' . \$value . '\"/>' . \$ext . '</div></td>';";
        $codex .= "\n                else";
        $codex .= "\n                    \$hrhtml .= '<td style=\"background:#ffe8c5;\" title=\"#' . \$lineidx . ':' . \$errmsg . '\"><div>' . \$showdat . '</div></td>';";
        $codex .= "\n            }";
        $codex .= "\n            if (\$bempty)";
        $codex .= "\n                continue;";

        $codex .= "\n            if (count(\$unqs) > 0) {";
        $codex .= "\n                \$unq = implode('|', \$unqs);";
        $codex .= "\n                if (in_array(\$unq, \$uniques))";
        $codex .= "\n                    \$firsthtml = '<td style=\"background:#ffe8c5;\" title=\"#' . \$lineidx . ':该行与待导入数据有重复\"><div class=\"lang\">重复</div></td>';";
        $codex .= "\n                else {";
        $codex .= "\n                    \$uniques[] = \$unq;";
        $codex .= "\n                    \$csql->column('id');";
        $codex .= "\n                    \$chkid = toint(\$db->get1(\$csql));";
        $codex .= "\n                    if (\$chkid > 0 && ((\$id > 0 && \$chkid != \$id) || \$id == 0))";
        $codex .= "\n                        \$firsthtml = '<td style=\"background:#ffe8c5;\" title=\"#' . \$lineidx . ':该行与数据库数据有重复\"><div class=\"lang\">重复</div></td>';";
        $codex .= "\n                }";
        $codex .= "\n            }";
        $codex .= "\n            \$html .= '<tr>' . \$firsthtml . \$hrhtml . '</tr>';";
        $codex .= "\n            \$cnt++;";
        $codex .= "\n        }";
        $codex .= "\n        \$html .= '</tbody></table></div>';";
        $codex .= "\n        \$html .= '<input type=\"hidden\" name=\"total\" value=\"' . \$cnt . '\"/>';";
        $codex .= "\n        \$html .= '<code>共' . \$cnt . '条数据</code>';";
        $codex .= "\n        return succjson(array('html' => \$html, 'count' => \$cnt));";
        $codex .= "\n    }";
        $codex .= "\n";
        $codex .= "\n    public static function json_importxls_data() {";
        $codex .= "\n        global \$db;";

        $codex .= "\n        \$rsuser = verifyfast();";
        $codex .= "\n        //if (nopower(\$db, \$rsuser['id'], 'p i'))";
        $codex .= "\n        //    return errjson('您未被授权操作');";
        $codex .= "\n        \$post = new \ciy\post();";
        $codex .= "\n        //\$db->trace(\$post->get('_pf'), \$rsuser);";
        $codex .= "\n        \$total = \$post->getint('total');";
        $codex .= "\n        try {";
        $codex .= "\n            \$db->begin();";
        $codex .= "\n            for (\$i = 1; \$i <= \$total; \$i++) {";
        $codex .= "\n                \$id = \$post->getint('id_' . \$i);";
        $dups = array();
        foreach ($flds as $fld) {
            if (!$fld['bdup'])
                continue;
            $col = $fld['col'];
            $dups[] = $col;
            $codex .= "\n                \${$col} = \$post->get('{$col}_' . \$i);";
        }
        if (count($dups) > 0) {
            $codex .= "\n                \$csql = new \ciy\sql('{$tabname}');";
            if ($saasfield) {
                if (!empty($saasusr))
                    $codex .= "\n                \$csql->where('{$saasfield}', \$rsuser['{$saasusr}']);";
            }
            foreach ($dups as $dup) {
                $codex .= "\n                \$csql->where('{$dup}', \${$dup});";
            }
            $codex .= "\n                \$csql->column('id');";
            $codex .= "\n                \$chkid = toint(\$db->get1(\$csql));";
            $codex .= "\n                if (\$chkid > 0 && ((\$id > 0 && \$chkid != \$id) || \$id == 0))";
            $codex .= "\n                    throw new \Exception('CIYIGN发现数据有重复');";
        }
        $codex .= "\n                \$updata = array();";
        foreach ($flds as $fld) {
            if (!$fld['bmin'])
                continue;
            $col = $fld['col'];
            if ($col == 'id')
                continue;
            if ($fld['cate'] == 'FILEMD')
                continue;
            $codex .= "\n                \$updata['{$col}'] = \$post->get('{$col}_' . \$i);";
        }
        $codex .= "\n                \$csql = new \ciy\sql('{$tabname}');";
        $codex .= "\n                if (\$id == 0) {";
        foreach ($flds as $fld) {
            $col = $fld['col'];
            if ($col == $saasfield) {
                if (!empty($saasusr))
                    $codex .= "\n                    \$updata['{$col}'] = \$rsuser['{$saasusr}'];";
            }
            if ($col == 'addtimes')
                $codex .= "\n                    \$updata['{$col}'] = tostamp();";
            if ($col == 'auditstatus')
                $codex .= "\n                    \$updata['{$col}'] = 2;";
            if ($col == 'matterstatus')
                $codex .= "\n                    \$updata['{$col}'] = 2;";
        }
        $codex .= "\n                    if (\$db->insert(\$csql, \$updata) === false)";
        $codex .= "\n                        throw new \Exception('新增导入失败:' . \$db->error);";
        $codex .= "\n                } else {";
        $codex .= "\n                    \$csql->where('id', \$id);";
        $codex .= "\n                    if (\$db->update(\$csql, \$updata) === false)";
        $codex .= "\n                        throw new \Exception('更新导入失败:' . \$db->error);";
        $codex .= "\n                }";
        $codex .= "\n            }";
        $codex .= "\n            \$db->commit();";
        $codex .= "\n        } catch (\Exception \$ex) {";
        $codex .= "\n            \$db->rollback();";
        $codex .= "\n            savelogfile('err_db', \$ex->getMessage());";
        $codex .= "\n            return errjson(\$ex->getMessage());";
        $codex .= "\n        }";
        $codex .= "\n        return succjson();";
        $codex .= "\n    }";
    }
    $codex .= "\n}";
    $codex .= "\n";
    return $codex;
}
