'use strict';
/*global require, module, Buffer, ccs*/

var union = ccs.lib.tools.union,
    intersect = ccs.lib.tools.intersect,
    UIDString = ccs.lib.json.UIDString,
    defautUser = ccs.lib.json.User,
    preAllocate = ccs.lib.json.UserPre,
    callbackFn = ccs.lib.tools.callbackFn,
    wrapCallback = ccs.lib.tools.wrapCallback,
    converter = ccs.lib.converter;

var that = ccs.dao.db.bind('gonggao', {
    getgonggao: function (callback) {
        //console.log("zzzzzzzzzzzzzzzzzzz");
        callback = callback || callbackFn;

        //that.find({},wrapCallback(callback));

        that.find({},{
            sort: {
                datetime: -1
            },
            fields: {
                _id: 1,
                title:1,
                content:1,
                datetime:1,
                yhmc:1
            },
            limit:5
        },wrapCallback(callback));
    },
    setgonggao: function (gonggaoObj,callback) {

        var setObj = {},
            newObj = {
                title: '',
                content:'',
                datetime:'',
                yhmc:''
            };
        newObj.title=gonggaoObj.ggname;
        newObj.content=gonggaoObj.ggnr;
        newObj.datetime=new Date();
        newObj.yhmc=gonggaoObj.username;
        setObj.$set = newObj;
    if (gonggaoObj.ID)
    {
        var obj_id = ccs.dao.db.ObjectID.createFromHexString(gonggaoObj.ID);
        that.update({
                "_id" : obj_id
            },setObj, {
                w: 1
            },callback);
    }else {
        that.insert(
            newObj, {
            w: 1
        },callback);
        }
    },
    delgonggao: function (ID,callback) {
        //console.log('aaaaaaaaaa======' + ID);
        var obj_id = ccs.dao.db.ObjectID.createFromHexString(ID);
        //console.log('bbbbbbb======' + obj_id);
        that.remove({
            "_id" : obj_id
        }, {
            safe:true
        }, wrapCallback(callback));


    }

});




module.exports = {
    getgonggao: that.getgonggao,
    setgonggao: that.setgonggao,
    delgonggao: that.delgonggao
};