<?php

namespace web\admin\rigger;

class funcslow {
    static function setwhere($db, $post) {
        $query = $post->get('query');
        $csql = new \ciy\sql('zc_funcslow');
        $csql->wheredaterange('addtimes', objstr($query, 'addtimes'));
        $csql->wherenumrange('ms', objstr($query, 'ms_1'), objstr($query, 'ms_2'), 1);
        $csql->where('uri like', objstr($query, 'uri'));
        $csql->where('posts like', objstr($query, 'posts'));
        $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_funcslow');
            $fshow = fieldadd($fshow, $field, 0, '_btn', '操作');
            $field['ms']['thwidth'] = '5em';
            $field['ms']['order'] = true;
            $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' => 'ms', 'name' => '用时', 'prop' => ' style="width:4em;"'
            );
            $input[] = array(
                'type' => 'input', 'form' => 'uri', 'name' => '请求源', 'prop' => ' style="width:8em;"'
            );
            $input[] = array(
                'type' => 'input', 'form' => 'posts', 'name' => '参数', 'prop' => ' style="width:8em;"'
            );
            $ret['searchinput'] = $input;
        }
        return succjson($ret);
    }
    public static function json_del() {
        global $db;
        $rsuser = verifyfast();

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

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

        $csql->where('id in', $ids);
        $rows = $db->get($csql);
        $vids = array();
        try {
            $db->begin();
            foreach ($rows as $row) {
                $delid = $row['id'];
                //tran_delcheck($db, $delid, $row['name'], 'xxid', 'tablexx', '存在xxx，不能删除');
                //delall($db, $delid, 'xxid', 'tablexx', 'xxx删除失败');
                delme($db, $delid, 'zc_funcslow');
                savelogdb($db, $rsuser['id'], 'zc_funcslow', $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'], 'p556e'))
            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' => 60, 'style' => 'r', 'field' => 'ms', 'name' => '用时');
        $fields[] = array('width' => 300, 'style' => 'l', 'field' => 'uri', 'name' => '请求源');
        $fields[] = array('width' => 500, 'style' => 'l', 'field' => 'posts', '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));
    }
}
