<?php

namespace web\admin\rigger;

class autotask {
    static function setwhere($db, $post) {
        $query = $post->get('query');
        $csql = new \ciy\sql('zc_autotask');
        $liid = objint($query, 'liid');
        if ($liid > 0)
            $csql->where('autotaskstatus', $liid);
        $csql->where('name like', objstr($query, 'name'));
        $csql->where('runfunc like', objstr($query, 'runfunc'));
        $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);
        $rows[] = array('id' => '', 'runcycle' => 0, 'nexttimes' => tostamp());
        $ret = array('searchwhere' => $where, 'pageno' => $pageno, 'pagecount' => $pagecount, 'count' => $mainrowcount, 'list' => $rows);
        if ($post->getbool('field')) {
            $field = array();
            $fshow = $db->getfield($field, 'zc_autotask');
            $fshow = fieldadd($fshow, $field, -1, '_btn', '操作');
            $field['runparam']['thwidth'] = '8em';
            $field['nexttimes']['thwidth'] = '10em';
            $field['runcycle']['thwidth'] = '9em';
            $field['autotaskstatus']['thwidth'] = '6em';
            $ret['field'] = $field;
            $ret['fshow'] = $fshow;
        }
        if ($post->getbool('once')) {
            $ret['once'] = true;
            $input = array();
            $input[] = array(
                'type' => 'input',
                'form' => 'name',
                'name' => '任务名称',
                'prop' => ' style="width:8em;"'
            );
            $input[] = array(
                'type' => 'input',
                'form' => 'runfunc',
                'name' => '入口函数',
                'prop' => ' style="width:8em;"'
            );
            $ret['searchinput'] = $input;
        }
        return succjson($ret);
    }

    public static function json_update() {
        global $db;
        $rsuser = verifyfast();

        if (nopower($db, $rsuser['id'], 'p602u'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $id = $post->getint('id');
        $name = $post->get('name');
        if (empty($name))
            return errjson('请填写任务名称');
        $runfunc = $post->get('runfunc');
        if (empty($runfunc))
            return errjson('请填写入口函数');
        $runparam = $post->get('runparam');
        $nexttimes = $post->getdate('nexttimes');
        $runcycle = $post->getint('runcycle');
        if ($runcycle >= 0 && $runcycle < 60)
            return errjson('请填写超过1分钟的执行周期');
        $datarow = null;
        if ($id > 0) {
            $csql = new \ciy\sql('zc_autotask');
            $csql->where('id', $id);
            $datarow = $db->getone($csql);
            if (!is_array($datarow))
                return errjson('数据不存在');
        }
        try {
            $db->begin();
            $csql = new \ciy\sql('zc_autotask');
            $csql->where('name', $name);
            $csql->column('id');
            $chkid = toint($db->get1($csql));
            if ($chkid > 0 && (($id > 0 && $chkid != $id) || $id == 0))
                throw new \Exception('CIYIGN数据已存在');

            $updata = array();
            $updata['name'] = $name;
            $updata['runfunc'] = $runfunc;
            $updata['runparam'] = $runparam;
            $updata['nexttimes'] = $nexttimes;
            $updata['runcycle'] = $runcycle;
            $csql = new \ciy\sql('zc_autotask');
            if ($id > 0) {
                $csql->where('id', $id);
                if ($db->update($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
            } else {
                $updata['autotaskstatus'] = 90;
                if ($nexttimes == 0)
                    $updata['nexttimes'] = tostamp();
                if ($db->insert($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
                $id = $db->insert_id();
            }
            $updata['id'] = $id;
            savelogdb($db, $rsuser['id'], 'zc_autotask', $datarow, $updata);
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $ret['data'] = $updata;
        return succjson($ret);
    }

    public static function json_del() {
        global $db;
        $rsuser = verifyfast();

        if (nopower($db, $rsuser['id'], 'p602d'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $ids = $post->get('ids');
        if (empty($ids))
            return errjson('请选择至少一条');
        $csql = new \ciy\sql('zc_autotask');
        $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_autotask');
                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_status() {
        global $db;
        $rsuser = verifyfast();

        if (nopower($db, $rsuser['id'], 'p602u'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $id = $post->getint('id');
        $status = $post->getint('status');
        $csql = new \ciy\sql('zc_autotask');
        $csql->where('id', $id);
        $row = $db->getone($csql);
        if (!is_array($row))
            return errjson('数据不存在');
        try {
            $db->begin();
            $updata = array();
            $updata['autotaskstatus'] = $status;
            $csql = new \ciy\sql('zc_autotask');
            $csql->where('id', $id);
            if ($db->update($csql, $updata) === false)
                throw new \Exception('更新失败:' . $db->error);
            $updata['id'] = $id;
            savelogdb($db, $rsuser['id'], 'zc_autotask', $row, $updata);
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $ret['data'] = $updata;
        return succjson($ret);
    }
}
