<?php

namespace web\admin\ap;

class paper {
    static function setwhere($db, $post) {
        $query = $post->get('query');
        $csql = new \ciy\sql('ap_paper');
        $csql->where('name like', objstr($query, 'name'));
        $csql->wherenumrange('readcnt', objstr($query, 'readcnt_1'), objstr($query, 'readcnt_2'), 1);
        $csql->where('inputuser', objstr($query, 'inputuser'));
        $csql->wheredaterange('uptimes', objstr($query, 'uptimes'));
        $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);
        $csql->column('!content', $db->getraw('show full fields from ap_paper'));
        $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_paper');
            foreach ($field as $fr => $v) {
                if ($post->is('_' . $fr))
                    $field[$fr]['c'] = ',' . $field[$fr]['c'];
            }
            $fshow = fieldadd($fshow, $field, 0, '_btn', '操作');
            $field['name']['thwidth'] = '15em';
            $field['title']['thwidth'] = '15em';
            $field['readcnt']['order'] = 'r';
            $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' => 'num',
                'form' => 'readcnt',
                'name' => '阅读数',
                'prop' => ' style="width:4em;"'
            );
            $input[] = array(
                'type' => 'select',
                'form' => 'inputuser',
                'name' => '撰写人',
                'select' => 'adminuser',
                'all' => '全部'
            );
            $input[] = array(
                'type' => 'daterange',
                'form' => 'uptimes',
                'name' => '更新时间'
            );
            $ret['searchinput'] = $input;
        }
        return succjson($ret);
    }
    public static function json_update() {
        global $db;
        $rsuser = verifyfast();

        if (nopower($db, $rsuser['id'], 'p10201u'))
            return errjson('您未被授权操作');
        $post = new \ciy\post();
        $id = $post->getint('id');
        $name = $post->get('name');
        $title = $post->get('title');
        if (empty($name))
            return errjson('请填写文档标题');
        $content = $post->get('content');
        if ($content == '[MD]')
            return errjson('请填写内容');
        $datarow = null;
        if ($id > 0) {
            $csql = new \ciy\sql('ap_paper');

            $csql->where('id', $id);
            $datarow = $db->getone($csql);
            if (!is_array($datarow))
                return errjson('数据不存在');
        }
        try {
            $db->begin();
            $csql = new \ciy\sql('ap_paper');
            $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['title'] = $title;
            $updata['inputuser'] = $rsuser['id'];
            $updata['uptimes'] = tostamp();
            $updata['content'] = $content;
            $csql = new \ciy\sql('ap_paper');
            if ($id > 0) {
                $csql->where('id', $id);
                if ($db->update($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
            } else {
                if ($db->insert($csql, $updata) === false)
                    throw new \Exception('新增失败:' . $db->error);
                $id = $db->insert_id();
            }
            $updata['id'] = $id;
            savelogdb($db, $rsuser['id'], 'ap_paper', $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_getdata() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('id');
        $data = $post->getbool('data');
        if ($data) {
            $csql = new \ciy\sql('ap_paper');
            $csql->where('id', $id);
            $ret['data'] = $db->getone($csql);
        }
        return succjson($ret);
    }
    public static function json_del() {
        global $db;
        $rsuser = verifyfast();

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

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

        $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, 'tablexx', 'xxid', 'xxx删除失败');
                delme($db, $delid, 'ap_paper');
                savelogdb($db, $rsuser['id'], 'ap_paper', $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);
    }
}
