repository_map.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. let async = require("async");
  8. let moment = require('moment');
  9. //var stringUtil = require('../../../public/stringUtil');
  10. var rationLibdb = dbm.getCfgConnection("scConstruct");
  11. var Schema = mongoose.Schema;
  12. var RepositoryMapSchema = new Schema({
  13. "ID": Number,
  14. "dispName" : String,
  15. "appType" : String, //如:"建筑" / "公路"
  16. "compilationId": Number, //编办
  17. "compilationName": String,
  18. "descr" : String,
  19. "creator": String,
  20. "createDate": String,
  21. "recentOpr" :[],
  22. "deleted": Boolean
  23. }, {versionKey: false});
  24. var counter = require('../../../public/counter/counter');
  25. var rationRepository = rationLibdb.model("std_ration_lib_map", RepositoryMapSchema, "std_ration_lib_map");
  26. function createNewLibModel(rationLibObj){
  27. var rst = {};
  28. rst.dispName = rationLibObj.dispName;
  29. rst.appType = rationLibObj.appType?rationLibObj.appType:'construct';
  30. rst.localeType = rationLibObj.localeType?rationLibObj.localeType:'';
  31. rst.descr = rationLibObj.descr;
  32. rst.creator = rationLibObj.creator;
  33. rst.createDate = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
  34. rst.recentOpr = [{operator: rationLibObj.lastOperator, operateDate: rst.createDate}],
  35. rst.deleted = false;
  36. return rst;
  37. }
  38. var rationRepositoryDao = function(){};
  39. rationRepositoryDao.prototype.updateOprArr= function(findSet, oprtor, oprDate, cb){
  40. rationRepository.find(findSet, function (err, result) {
  41. if(err){
  42. cb(err);
  43. }
  44. else{
  45. if(result.length === 1){
  46. let recentOprArr = result[0].recentOpr;
  47. let isExist = false;
  48. for(let i =0 ; i<recentOprArr.length; i++){
  49. if(recentOprArr[i].operator === oprtor){
  50. recentOprArr[i].operateDate = oprDate;
  51. isExist = true;
  52. }
  53. }
  54. if(!isExist){
  55. if(recentOprArr.length < 5){
  56. recentOprArr.push({operator: oprtor, operateDate: oprDate});
  57. }
  58. else if(recentOprArr.length === 5){
  59. recentOprArr.sort(function (a, b) {
  60. if(a.operateDate > b.operateDate){
  61. return -1;
  62. }else {
  63. return 1;
  64. }
  65. return 0;
  66. });
  67. recentOprArr.splice(recentOprArr.length -1, 1);
  68. recentOprArr.splice(0, 1, {operator: oprtor, operateDate: oprDate});
  69. }
  70. }
  71. rationRepository.update(findSet, {$set: {recentOpr: recentOprArr}}, function (err) {
  72. if(err){
  73. cb(err);
  74. }
  75. else{
  76. cb(null);
  77. }
  78. });
  79. }
  80. else{
  81. cb(err);
  82. }
  83. }
  84. })
  85. };
  86. rationRepositoryDao.prototype.getRealLibName = function(dispName,callback){
  87. if (callback) {
  88. rationRepository.find({"dispName": dispName}, function(err,data){
  89. if (err) {
  90. callback('Error', null);
  91. } else {
  92. callback(false, data);
  93. }
  94. });
  95. } else {
  96. var rst = rationRepository.find({"dispName": dispName}).exec();
  97. return rst;
  98. }
  99. };
  100. rationRepositoryDao.prototype.getLibIDByName = function(dispName, callback){
  101. rationRepository.findOne({"dispName": dispName}, function(err,data){
  102. if (err) {
  103. callback('Error', null);
  104. } else {
  105. callback(false, data.ID);
  106. }
  107. });
  108. };
  109. rationRepositoryDao.prototype.getRepositoryById = function(repId,callback){
  110. if (callback) {
  111. rationRepository.find({"ID": repId}, function(err,data){
  112. if (err) {
  113. callback('Error', null);
  114. } else {
  115. callback(false, data);
  116. }
  117. });
  118. } else {
  119. var rst = rationRepository.find({"ID": repId}).exec();
  120. return rst;
  121. }
  122. };
  123. rationRepositoryDao.prototype.addRationRepository = function( rationLibObj,callback){
  124. counter.counterDAO.getIDAfterCount(counter.moduleName.rationMap, 1, function(err, result){
  125. var rMap = createNewLibModel(rationLibObj);
  126. rMap.ID = result.value.sequence_value;
  127. new rationRepository(rMap).save(function(err, result) {
  128. if (err) callback("Error", null)
  129. else
  130. callback(false, result);
  131. });
  132. });
  133. };
  134. rationRepositoryDao.prototype.getDisplayRationLibs = function(callback) {
  135. rationRepository.find({"deleted": false}, function(err, data){
  136. if (err) {
  137. callback( 'Error', null);
  138. } else {
  139. callback( false, data);
  140. }
  141. });
  142. };
  143. rationRepositoryDao.prototype.updateName = function(orgName,newName, lastOpr, callback){
  144. rationRepository.find({"dispName":newName, "deleted": false}, function(err, data){
  145. if (data.length == 0) {
  146. async.waterfall([
  147. function (cb) {
  148. rationRepository.update({dispName:orgName},{$set:{dispName:newName}},function(err){
  149. if(err) cb(err);
  150. else cb(null)
  151. });
  152. },
  153. function (cb) {
  154. new rationRepositoryDao().updateOprArr({dispName: newName}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  155. if(err){
  156. cb(err)
  157. }
  158. else{
  159. cb(null);
  160. }
  161. });
  162. }
  163. ], function (err) {
  164. if(err){
  165. callback('err', false);
  166. }
  167. else{
  168. callback(false, 'ok');
  169. }
  170. });
  171. } else
  172. callback("不可重名!",false);
  173. });
  174. }
  175. rationRepositoryDao.prototype.deleteRationLib = function(rationName, lastOpr, callback){
  176. rationRepository.find({"dispName":rationName, "deleted": false}, function(err, data){
  177. if (data.length == 1) {
  178. async.parallel([
  179. function (cb) {
  180. rationRepository.update({dispName:rationName, deleted: false},{$set: {deleted: true}},function(err){
  181. if(err) cb(err);
  182. else cb(null);
  183. })
  184. },
  185. function (cb) {
  186. new rationRepositoryDao().updateOprArr({dispName: rationName}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  187. if(err){
  188. cb(err);
  189. }
  190. else{
  191. cb(null);
  192. }
  193. });
  194. }
  195. ], function (err) {
  196. if(err){
  197. callback("err", false)
  198. }
  199. else{
  200. callback(false, "ok");
  201. }
  202. });
  203. } else
  204. callback("删除失败!",false);
  205. });
  206. }
  207. module.exports = new rationRepositoryDao();