repositoryMap.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * Created by Tony on 2017/4/24.
  3. * 重新构造,不适宜生成多个定额库db,还是得统一在一个表
  4. */
  5. var mongoose = require('mongoose');
  6. var dbm = require("../../../config/db/db_manager");
  7. //var stringUtil = require('../../../public/stringUtil');
  8. var rationLibdb = dbm.getCfgConnection("rationRepository");
  9. var Schema = mongoose.Schema;
  10. var RepositoryMapSchema = new Schema({
  11. "ID": Number,
  12. "dispName" : String,
  13. "appType" : String, //如:"建筑" / "公路"
  14. "localeType": String, //如 各个省份 / 部颁
  15. "descr" : String,
  16. "deleted": Boolean
  17. });
  18. var counter = require('../../../public/counter/counter');
  19. var rationRepository = rationLibdb.model("repositoryMap", RepositoryMapSchema, "repositoryMap");
  20. function createNewLibModel(rationLibObj){
  21. var rst = {};
  22. rst.dispName = rationLibObj.dispName;
  23. rst.appType = rationLibObj.appType?rationLibObj.appType:'construct';
  24. rst.localeType = rationLibObj.localeType?rationLibObj.localeType:'';
  25. rst.descr = rationLibObj.descr;
  26. rst.deleted = false;
  27. return rst;
  28. }
  29. rationRepositoryDao = function(){};
  30. rationRepositoryDao.prototype.getRealLibName = function(dispName,callback){
  31. if (callback) {
  32. rationRepository.find({"dispName": dispName}, function(err,data){
  33. if (err) {
  34. callback('Error', null);
  35. } else {
  36. callback(false, data);
  37. }
  38. });
  39. } else {
  40. var rst = rationRepository.find({"dispName": dispName}).exec();
  41. return rst;
  42. }
  43. };
  44. rationRepositoryDao.prototype.getRepositoryById = function(repId,callback){
  45. if (callback) {
  46. rationRepository.find({"ID": repId}, function(err,data){
  47. if (err) {
  48. callback('Error', null);
  49. } else {
  50. callback(false, data);
  51. }
  52. });
  53. } else {
  54. var rst = rationRepository.find({"ID": repId}).exec();
  55. return rst;
  56. }
  57. };
  58. rationRepositoryDao.prototype.addRationRepository = function( rationLibObj,callback){
  59. counter.counterDAO.getIDAfterCount(counter.moduleName.rationMap, 1, function(err, result){
  60. var rMap = createNewLibModel(rationLibObj);
  61. rMap.ID = result.value.sequence_value;
  62. new rationRepository(rMap).save(function(err, result) {
  63. if (err) callback("Error", null)
  64. else
  65. callback(false, result);
  66. });
  67. });
  68. };
  69. rationRepositoryDao.prototype.getDisplayRationLibs = function(callback) {
  70. rationRepository.find({"deleted": false}, function(err, data){
  71. if (err) {
  72. callback( 'Error', null);
  73. } else {
  74. callback( false, data);
  75. }
  76. });
  77. };
  78. rationRepositoryDao.prototype.updateName = function(orgName,newName,callback){
  79. rationRepository.find({"dispName":newName, "deleted": false}, function(err, data){
  80. if (data.length == 0) {
  81. rationRepository.update({dispName:orgName},{$set:{dispName:newName}},function(err){
  82. if(err) callback("err",false);
  83. else callback(false,"ok")
  84. })
  85. } else
  86. callback("不可重名!",false);
  87. });
  88. }
  89. rationRepositoryDao.prototype.deleteRationLib = function(rationName,callback){
  90. rationRepository.find({"dispName":rationName, "deleted": false}, function(err, data){
  91. if (data.length == 1) {
  92. rationRepository.update({dispName:rationName},{$set:{deleted: true}},function(err){
  93. if(err) callback("err",false);
  94. else callback(false,"ok")
  95. })
  96. } else
  97. callback("删除失败!",false);
  98. });
  99. }
  100. module.exports = new rationRepositoryDao();