repository_map.js 7.4 KB

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