<?php

namespace web\admin\rigger;

class autotskrun {
    static function setwhere($db, $post) {
        $query = $post->get('query');
        $csql = new \ciy\sql('zc_autotsk_run');
        $csql->where('autotaskid', get('_autotaskid'));
        $csql->wheredaterange('addtimes', objstr($query, 'addtimes'));
        $csql->wherenumrange('runsec', objstr($query, 'runsec_1'), objstr($query, 'runsec_2'), 1);
        $csql->wherenumrange('logcnt', objstr($query, 'logcnt_1'), objstr($query, 'logcnt_2'), 1);
        $csql->where('msg like', objstr($query, 'msg'));
        $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_autotsk_run');
            foreach ($field as $fr => $v) {
                if ($post->is('_' . $fr))
                    $field[$fr]['c'] = ',' . $field[$fr]['c'];
            }
            $field['msg']['thwidth'] = '20em';
            $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' => 'runsec', 'name' => '时长', 'prop' => ' style="width:4em;"'
            );
            $input[] = array(
                'type' => 'num', 'form' => 'logcnt', 'name' => 'LOG统计', 'prop' => ' style="width:4em;"'
            );
            $input[] = array(
                'type' => 'input', 'form' => 'msg', 'name' => '任务简报', 'prop' => ' style="width:8em;"'
            );
            $ret['searchinput'] = $input;
            $csql = (new \ciy\sql('zc_autotask'))->column('id,name');
            $ret['zc_autotask'] = $db->get($csql);
        }
        return succjson($ret);
    }
    public static function json_del() {
        global $db;
        $rsuser = verifyfast();

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

        $post = new \ciy\post();
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $csql = new \ciy\sql('zc_autotsk_run');
        $csql->where('id in', $ids);
        $rows = $db->get($csql);
        $vids = array();
        try {
            $db->begin();
            foreach ($rows as $row) {
                $delid = $row['id'];
                delall($db, $delid, 'zc_autotsk_log', 'runtaskid', 'Log记录');
                delme($db, $delid, 'zc_autotsk_run');
                savelogdb($db, $rsuser['id'], $delid, $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'], 'p602l'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $csql = new \ciy\sql('zc_autotsk_run');
        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' => 'l', 'field' => 'autotaskid', 'name' => '所属任务');
        $fields[] = array('width' => 100, 'style' => 'l', 'field' => 'addtimes', 'name' => '执行时间');
        $fields[] = array('width' => 60, 'style' => 'r', 'field' => 'runsec', 'name' => '时长');
        $fields[] = array('width' => 60, 'style' => 'r', 'field' => 'logcnt', 'name' => 'LOG统计');
        $fields[] = array('width' => 500, 'style' => 'l', 'field' => 'msg', 'name' => '任务简报');
        $csql = (new \ciy\sql('zc_autotask'))->column('id,name');
        $code_autotaskid = $db->get($csql);
        $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 == 'autotaskid')
                    $val = ccode($code_autotaskid, $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));
    }

    public static function json_viewlog() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $runtaskid = $post->get('runtaskid');
        $csql = new \ciy\sql('zc_autotsk_log');
        $csql->where('runtaskid', $runtaskid);
        $csql->column('addtimes,msg');
        $csql->order('id');
        $rows = $db->get($csql);
        $ret['logs'] = $rows;
        return succjson($ret);
    }
}
