counter.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. // All counter Must predefine in DB
  17. const COUNTER_MODULE_NAME = {
  18. project: 'projects',
  19. user: 'users',
  20. bills: 'bills',
  21. ration: 'rations',
  22. report: 'rptTemplates',
  23. fee: 'fees',
  24. template_bills: 'temp_bills'
  25. }
  26. /*const PROJECT_COUNTER = 'projects', USER_COUNTER = 'users', BILL_COUNTER = 'bills', RATION_COUNTER = 'rations',
  27. REPORT_COUNTER = 'rptTemplates', FEE_COUNTER = 'fees'*/
  28. var counterDAO = function(){};
  29. /*
  30. * callback = function (err, result) {
  31. * result.value.sequence_value ΪÐ޸ĺóµÄid
  32. * }
  33. */
  34. counterDAO.prototype.getIDAfterCount = function(moduleName, stepCount, callback) {
  35. var sc = stepCount;
  36. if (isNaN(stepCount) || (stepCount < 0)) {
  37. sc = 1;
  38. } else if (!(/^-?\d+$/.test(stepCount))) {
  39. sc = Math.round(stepCount + 0.5);
  40. }
  41. counterModel.findAndModify({_id: moduleName}, [], { $inc: { sequence_value: sc } }, {'new':true}, callback);
  42. }
  43. counterDAO.prototype.getCurrentID = function(moduleName, callback) {
  44. if (callback) {
  45. counterModel.findOne({_id: moduleName}).exec()
  46. .then(function(result, err) {
  47. if (callback) {
  48. callback(result, err);
  49. }
  50. }
  51. );
  52. return null;
  53. } else {
  54. var rst = counterModel.findOne({_id: moduleName}).exec() ;
  55. return rst;
  56. }
  57. }
  58. module.exports = {
  59. counterDAO: new counterDAO(),
  60. moduleName: COUNTER_MODULE_NAME
  61. };