counter.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /**
  2. * Created by Tony on 2017/3/21.
  3. */
  4. var mongoose = require('mongoose');
  5. var dbm = require("../../config/db/db_manager");
  6. var projectdb = dbm.getCfgConnection("scConstruct");
  7. var Schema = mongoose.Schema;
  8. var counterSchema = new Schema({
  9. _id: String,
  10. sequence_value: Number
  11. });
  12. counterSchema.statics.findAndModify = function (query, sort, doc, options, callback) {
  13. return this.collection.findAndModify(query, sort, doc, options, callback);
  14. };
  15. var counterModel = projectdb.model("counters", counterSchema);
  16. const PROJECT_COUNTER = 'projects', USER_COUNTER = 'users', BILL_COUNTER = 'bills', RATION_COUNTER = 'rations',
  17. REPORT_COUNTER = 'rptTemplates', FEE_COUNTER = 'fees'
  18. var counterDAO = function(){};
  19. counterDAO.prototype.getIDAfterCount = function(moduleName, stepCount, callback) {
  20. var sc = stepCount;
  21. if (isNaN(stepCount) || (stepCount < 0)) {
  22. sc = 1;
  23. } else if (!(/^-?\d+$/.test(stepCount))) {
  24. sc = Math.round(stepCount + 0.5);
  25. }
  26. counterModel.findAndModify({_id: moduleName}, [], { $inc: { sequence_value: sc } }, {'new':true}, callback);
  27. }
  28. counterDAO.prototype.getCurrentID = function(moduleName, callback) {
  29. if (callback) {
  30. counterModel.findOne({_id: moduleName}).exec()
  31. .then(function(result, err) {
  32. if (callback) {
  33. callback(result, err);
  34. }
  35. }
  36. );
  37. return null;
  38. } else {
  39. var rst = counterModel.findOne({_id: moduleName}).exec() ;
  40. return rst;
  41. }
  42. }
  43. module.exports = new counterDAO();