/** * Created by Tony on 2017/3/21. */ var mongoose = require('mongoose'); var dbm = require("../../config/db/db_manager"); var projectdb = dbm.getCfgConnection("scConstruct"); var Schema = mongoose.Schema; var counterSchema = new Schema({ _id: String, sequence_value: Number }); counterSchema.statics.findAndModify = function (query, sort, doc, options, callback) { return this.collection.findAndModify(query, sort, doc, options, callback); }; var counterModel = projectdb.model("counters", counterSchema); // All counter Must predefine in DB const COUNTER_MODULE_NAME = { project: 'projects', user: 'users', bills: 'bills', rations: 'rations', GLJ: 'glj', rationMap: 'rationMaps', rationTree: 'rationChapterTrees', report: 'rptTemplates', fee: 'fees', unitPriceFile: 'unitPriceFile', unitPriceGLJ: 'unitPriceGLJ', template_bills: 'temp_bills', billsLib: 'billsLib', coeList: 'coeList' } /*const PROJECT_COUNTER = 'projects', USER_COUNTER = 'users', BILL_COUNTER = 'bills', RATION_COUNTER = 'rations', REPORT_COUNTER = 'rptTemplates', FEE_COUNTER = 'fees'*/ var counterDAO = function(){}; /* * callback = function (err, result) { * result.value.sequence_value Ϊ�޸ĺ��id * } */ counterDAO.prototype.getIDAfterCount = function(moduleName, stepCount, callback) { var sc = stepCount; if (isNaN(stepCount) || (stepCount < 0)) { sc = 1; } else if (!(/^-?\d+$/.test(stepCount))) { sc = Math.round(stepCount + 0.5); } counterModel.findAndModify({_id: moduleName}, [], { $inc: { sequence_value: sc } }, {'new':true}, callback); } counterDAO.prototype.getCurrentID = function(moduleName, callback) { if (callback) { counterModel.findOne({_id: moduleName}).exec() .then(function(result, err) { if (callback) { callback(result, err); } } ); return null; } else { var rst = counterModel.findOne({_id: moduleName}).exec() ; return rst; } } module.exports = { counterDAO: new counterDAO(), moduleName: COUNTER_MODULE_NAME };