<?php

namespace web\admin\rigger;

class statsfunc {
    static function setwhere($db, $post) {
        $query = $post->get('query');
        $csql = new \ciy\sql('zc_stats');
        $csql->wheredaterange('addtimes', objstr($query, 'addtimes'));
        $csql->wherenumrange('func_succ', objstr($query, 'func_succ_1'), objstr($query, 'func_succ_2'), 1);
        $csql->wherenumrange('func_fail', objstr($query, 'func_fail_1'), objstr($query, 'func_fail_2'), 1);
        $csql->wherenumrange('func_rollback', objstr($query, 'func_rollback_1'), objstr($query, 'func_rollback_2'), 1);
        $csql->wherenumrange('func_runms', objstr($query, 'func_runms_1'), objstr($query, 'func_runms_2'), 1);
        $order = objstr($query, 'order', 'id desc');
        $csql->order($order);
        $query['order'] = $order;
        return [$query, $csql];
    }
    public static function json_init() {
        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);
        $ret = array('searchwhere' => $where, 'pageno' => $pageno, 'pagecount' => $pagecount, 'count' => $mainrowcount, 'list' => $rows);
        if ($post->getbool('field')) {
            $field = array();
            $fshow = $db->getfield($field, 'zc_stats');
            $ret['field'] = $field;
            $ret['fshow'] = $fshow;
        }

        if ($post->getbool('once')) {
            $ret['once'] = true;
            $input = array();
            $input[] = array(
                'type' => 'daterange', 'form' => 'addtimes', 'name' => '记录时间'
            );
            $input[] = array(
                'type' => 'num', 'form' => 'func_succ', 'name' => '成功数', 'prop' => ' style="width:4em;"'
            );
            $input[] = array(
                'type' => 'num', 'form' => 'func_fail', 'name' => '失败数', 'prop' => ' style="width:4em;"'
            );
            $input[] = array(
                'type' => 'num', 'form' => 'func_rollback', 'name' => '回滚数', 'prop' => ' style="width:4em;"'
            );
            $input[] = array(
                'type' => 'num', 'form' => 'func_runms', 'name' => '平均用时', 'prop' => ' style="width:4em;"'
            );
            $ret['searchinput'] = $input;
        }
        return succjson($ret);
    }
    public static function json_del() {
        global $db;
        $rsuser = verifyfast();

        if (nopower($db, $rsuser['id'], 'p550d'))
            return errjson('您未被授权操作');

        $post = new \ciy\post();
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $csql = new \ciy\sql('zc_stats');

        $csql->where('id in', $ids);
        $rows = $db->get($csql);
        $vids = array();
        try {
            $db->begin();
            foreach ($rows as $row) {
                $delid = $row['id'];
                delme($db, $delid, 'zc_stats');
                savelogdb($db, $rsuser['id'], 'zc_stats', $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);
    }
    public static function json_exportxls() {

        global $db;
        $rsuser = verifyfast();

        if (nopower($db, $rsuser['id'], 'p550e'))
            return errjson('您未被授权操作');

        $post = new \ciy\post();
        list($where, $csql) = self::setwhere($db, $post);
        $rows = $db->get($csql);
        if (count($rows) > 10000)
            return errjson('将导出' . count($rows) . '条，不建议超过1万条，请筛选缩小范围');
        $fields = array();
        $fields[] = array('field' => 'id', 'name' => '行码', 'width' => 60, 'style' => 'c');
        $fields[] = array('width' => 100, 'style' => 'c', 'field' => 'addtimes', 'name' => '记录时间');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'func_succ', 'name' => '成功数');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'func_fail', 'name' => '失败数');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'func_commit', 'name' => '提交数');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'func_rollback', 'name' => '回滚数');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'func_runms', 'name' => '平均用时');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'disk_sysfree', 'name' => '磁盘系统剩余空间');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'disk_datafree', 'name' => '磁盘数据剩余空间');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'disk_ioms', 'name' => '磁盘IO响应');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'cpu_free', 'name' => 'CPU空闲占比');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'cpu_db', 'name' => 'CPU数据库占比');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'cpu_web', 'name' => 'CPU应用占比');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'cpu_oth', 'name' => 'CPU其他占比');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'mem_free', 'name' => '空闲内存');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'mem_db', 'name' => '数据库内存占比');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'mem_web', 'name' => '应用内存占比');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'mem_oth', 'name' => '其他内存占比');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'net_tcp', 'name' => '网络连接数');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'net_ioms', 'name' => '网络响应');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'db_query', 'name' => '数据库查询数');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'db_commit', 'name' => '数据库提交数');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'db_rollback', 'name' => '数据库回滚数');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'db_keyhit', 'name' => '索引命中率');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'db_dbhit', 'name' => '缓存命中率');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'db_tmptable', 'name' => '临时表占比');
        $fields[] = array('width' => 100, 'style' => 'r', 'field' => 'db_lock', 'name' => '当前锁数量');
        $datas = array();
        foreach ($rows as $row) {
            $dat = array();
            foreach ($fields as $field) {
                $field = $field['field'];
                $val = isset($row[$field]) ? $row[$field] : '';
                if ($field == 'id')
                    $val = enid($val);
                if ($field == 'addtimes')
                    $val = ($val == 0 ? '--' : date('Y-m-d H:i', $val));
                $dat[] = $val;
            }
            $datas[] = $dat;
        }

        $param = array();
        $param['field'] = $fields;
        $param['data'] = $datas;

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

        $param['toptitle'] = '系统健康数据报表';
        // $param['total'] = array();
        // $param['total'][] = array('name'=>'合计','merge'=>8,'style'=>'l');
        // $param['total'][] = array('name'=>'=SUM(R[-'.count($datas).']C:R[-1]C)','style'=>'r');
        // $param['total'][] = array('name'=>'=MAX(R[-'.count($datas).']C:R[-1]C)','style'=>'r');
        // $param['total'][] = array('name'=>'','style'=>'l');
        // $param['total'][] = array('name'=>'','style'=>'l');

        //$param['rowstop'] = '<Row ss:Height="45"><Cell ss:MergeAcross="4" ss:StyleID="cap"><Data ss:Type="String">众产CIYPHP</Data></Cell></Row>';//顶部行
        //$param['rowsfooter'] = '<Row><Cell ss:MergeAcross="1"><Data ss:Type="String" ss:StyleID="r">合计</Data></Cell><Cell ss:Formula="=SUM(R[-2]C:R[-1]C)"><Data ss:Type="Number"></Data></Cell></Row>';//底部行

        $str = \ciy\excel::general_excel_xml($param);
        $filename = '';
        if (empty($filename))
            $filename = date('Y-m-d_H-i-s') . rand(1000, 9999);
        $filename .= '.xls';
        file_put_contents(PATH_WEB . 'ud/tmp/' . $filename, $str);
        return succjson(array('url' => '/ud/tmp/' . $filename));
    }
}
