<?php

namespace web\admin\ap;

class logdb {
    static function setwhere($db, $post) {
        $query = $post->get('query');
        $csql = new \ciy\sql('ap_log');
        $liid = objint($query, 'liid');
        if ($liid == 1)
            $csql->where('readuser=0');
        if ($liid == 2)
            $csql->where('readuser>0');
        $val = objstr($query, 'loguser');
        if (!empty($val)) {
            $csqlt = new \ciy\sql('ap_user');
            $csqlt->where('name like', $val);
            $trow = $db->getone($csqlt);
            if (is_array($trow)) {
                $csql->where('loguser', $trow['id']);
                $query['loguser'] = $trow['name'];
            } else {
                $csql->where('loguser=0');
            }
        }
        $csql->where('readuser', objstr($query, 'readuser'));
        $csql->where('types', objstr($query, 'types'));
        $csql->wheredaterange('addtimes', objstr($query, 'addtimes'));
        $csql->where('logs like', objstr($query, 'logs'));
        $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, 'ap_log');
            $ret['field'] = $field;
            $ret['fshow'] = $fshow;
        }

        if ($post->getbool('once')) {
            $ret['once'] = true;
            $input = array();
            $input[] = array(
                'type' => 'input',
                'form' => 'loguser',
                'name' => '操作人',
                'prop' => ' style="width:6em;"',
            );
            $input[] = array(
                'type' => 'select',
                'form' => 'readuser',
                'name' => '审阅人',
                'prop' => ' style="min-width:6em;"',
                'select' => 'adminuser',
                'all' => '全部'
            );
            $input[] = array(
                'type' => 'input',
                'form' => 'types',
                'name' => 'LOG分类',
                'prop' => ' style="width:8em;"'
            );
            $input[] = array(
                'type' => 'daterange',
                'form' => 'addtimes',
                'name' => '发生日期'
            );
            $input[] = array(
                'type' => 'input',
                'form' => 'logs',
                'name' => '详情',
                'prop' => ' style="width:8em;"'
            );
            $ret['searchinput'] = $input;
        }
        $ret['ap_user'] = getrelation($db, $rows, 'ap_user', 'loguser');
        return succjson($ret);
    }
    public static function json_del() {
        global $db;
        $rsuser = verifyfast();

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

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

        $csql->where('id in', $ids);
        $rows = $db->get($csql);
        $vids = array();
        try {
            $db->begin();
            foreach ($rows as $row) {
                $delid = $row['id'];
                delme($db, $delid, 'ap_log');
                $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_multiread() {
        global $db;
        $rsuser = verifyfast();

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

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

        $csql->where('id in', $ids);
        $rows = $db->get($csql);
        $vids = array();
        try {
            $db->begin();
            foreach ($rows as $row) {
                $rowid = $row['id'];
                $updata = array();
                $updata['readuser'] = $rsuser['id'];
                $csql = new \ciy\sql('ap_log');
                $csql->where('id', $rowid);
                if ($db->update($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
                $vids[] = $rowid;
            }
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        $ret['ids'] = $vids;
        $ret['data'] = $updata;
        return succjson($ret);
    }
}
