<?php

namespace web\admin\develop;

class note {
    public static function json_init() {
        global $db;
        $rsuser = verifyfast();
        $typeid = getint('typeid');
        $csql = new \ciy\sql('zc_dev_note');
        $csql->where('typeid', $typeid);
        $csql->column('id,uptimes');
        $csql->order('id desc');
        $ret['ids'] = $db->get($csql);
        foreach ($ret['ids'] as $key => $value) {
            $ret['ids'][$key]['name'] = $value['id'] . '：'. date('Y-m-d', $value['uptimes']);
        }
        $ret['ids'][] = array('id' => 0, 'name' => '新建');
        $ret['id'] = $ret['ids'][0]['id'];
        
        return succjson($ret);
    }
    public static function json_getdata() {
        global $db;
        $rsuser = verifyfast();
        $post = new \ciy\post();
        $id = $post->getint('id');
        $csql = new \ciy\sql('zc_dev_note');
        $csql->where('id', $id);
        $ret['data'] = $db->getone($csql);
        if (!is_array($ret['data']))
            return errjson('数据不存在');
        return succjson($ret);
    }
    public static function json_update() {
        global $db;
        $rsuser = verifyfast();
        $typeid = getint('typeid');
        $post = new \ciy\post();
        $id = $post->getint('id');
        $content = $post->get('content');
        if (empty($content))
            return errjson('请填写内容');
        if ($id > 0) {
            $csql = new \ciy\sql('zc_dev_note');
            $csql->where('id', $id);
            $datarow = $db->getone($csql);
            if (!is_array($datarow))
                return errjson('数据不存在');
        }
        try {
            $db->begin();
            $updata = array();
            $updata['uptimes'] = tostamp();
            $updata['content'] = $content;
            $csql = new \ciy\sql('zc_dev_note');
            if ($id == 0) {
                $updata['typeid'] = $typeid;
                if ($db->insert($csql, $updata) === false)
                    throw new \Exception('新增失败:' . $db->error);
                $newid = $db->insert_id();
            } else {
                $csql->where('id', $id);
                if ($db->update($csql, $updata) === false)
                    throw new \Exception('更新失败:' . $db->error);
            }
            $db->commit();
        } catch (\Exception $ex) {
            $db->rollback();
            savelogfile('err_db', $ex->getMessage());
            return errjson($ex->getMessage());
        }
        if ($id > 0)
            return succjson();
        $ret['newid'] = $newid;
        return succjson($ret);
    }
}
