repository_map.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /**
  2. * Created by Tony on 2017/4/24.
  3. * 重新构造,不适宜生成多个定额库db,还是得统一在一个表
  4. */
  5. let async = require("async");
  6. let moment = require('moment');
  7. const mongoose = require('mongoose');
  8. let counter = require('../../../public/counter/counter');
  9. const gljMapModel = mongoose.model('std_glj_lib_map');
  10. const rationRepository = mongoose.model('std_ration_lib_map');
  11. const rationChapterTreeModel = mongoose.model('std_ration_lib_ration_chapter_trees');
  12. const rationItemModel = mongoose.model('std_ration_lib_ration_items');
  13. const rationCoeModel = mongoose.model('std_ration_lib_coe_list');
  14. const rationInstallFeeItem = mongoose.model('std_ration_lib_installation');
  15. const rationInstallSection = mongoose.model('std_ration_lib_installationSection');
  16. const engLibModel = mongoose.model('engineering_lib');
  17. function createNewLibModel(rationLibObj){
  18. var rst = {};
  19. rst.dispName = rationLibObj.dispName;
  20. rst.appType = rationLibObj.appType?rationLibObj.appType:'construct';
  21. rst.compilationId = rationLibObj.compilationId;
  22. rst.compilationName = rationLibObj.compilationName;
  23. rst.gljLib = rationLibObj.gljLib;
  24. rst.creator = rationLibObj.creator;
  25. rst.createDate = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
  26. rst.recentOpr = [{operator: rationLibObj.creator, operateDate: rst.createDate}],
  27. rst.deleted = false;
  28. return rst;
  29. }
  30. var rationRepositoryDao = function(){};
  31. rationRepositoryDao.prototype.getRationLibsByCompilation = async function (compilationId) {
  32. return await rationRepository.find({compilationId: compilationId, deleted: false});
  33. };
  34. rationRepositoryDao.prototype.updateGljLib = function (gljLibId, callback) {
  35. rationRepository.update({gljLib: gljLibId}, {$set: {gljLib: -1}}, {multi: true}, function (err) {
  36. if(err){
  37. callback(err);
  38. }
  39. else{
  40. callback(null);
  41. }
  42. })
  43. };
  44. rationRepositoryDao.prototype.updateOprArr= function(findSet, oprtor, oprDate, cb){
  45. rationRepository.find(findSet, function (err, result) {
  46. if(err){
  47. cb(err);
  48. }
  49. else{
  50. if(result.length === 1){
  51. let recentOprArr = result[0].recentOpr;
  52. let isExist = false;
  53. for(let i =0 ; i<recentOprArr.length; i++){
  54. if(recentOprArr[i].operator === oprtor){
  55. recentOprArr[i].operateDate = oprDate;
  56. isExist = true;
  57. }
  58. }
  59. if(!isExist){
  60. if(recentOprArr.length < 5){
  61. recentOprArr.push({operator: oprtor, operateDate: oprDate});
  62. }
  63. else if(recentOprArr.length === 5){
  64. recentOprArr.sort(function (a, b) {
  65. if(a.operateDate > b.operateDate){
  66. return -1;
  67. }else {
  68. return 1;
  69. }
  70. return 0;
  71. });
  72. recentOprArr.splice(recentOprArr.length -1, 1);
  73. recentOprArr.splice(0, 1, {operator: oprtor, operateDate: oprDate});
  74. }
  75. }
  76. rationRepository.update(findSet, {$set: {recentOpr: recentOprArr}}, function (err) {
  77. if(err){
  78. cb(err);
  79. }
  80. else{
  81. cb(null);
  82. }
  83. });
  84. }
  85. else{
  86. cb(err);
  87. }
  88. }
  89. })
  90. };
  91. rationRepositoryDao.prototype.getRealLibName = function(dispName,callback){
  92. if (callback) {
  93. rationRepository.find({"dispName": dispName}, function(err,data){
  94. if (err) {
  95. callback('Error', null);
  96. } else {
  97. callback(false, data);
  98. }
  99. });
  100. } else {
  101. var rst = rationRepository.find({"dispName": dispName}).exec();
  102. return rst;
  103. }
  104. };
  105. rationRepositoryDao.prototype.getLibIDByName = function(dispName, callback){
  106. rationRepository.findOne({"dispName": dispName}, function(err,data){
  107. if (err) {
  108. callback('Error', null);
  109. } else {
  110. callback(false, data.ID);
  111. }
  112. });
  113. };
  114. rationRepositoryDao.prototype.getRepositoryById = function(repId,callback = null){
  115. if (callback) {
  116. rationRepository.find({"ID": repId}, function(err,data){
  117. if (err) {
  118. callback('Error', null);
  119. } else {
  120. callback(false, data);
  121. }
  122. });
  123. } else {
  124. var rst = rationRepository.find({"ID": repId}).exec();
  125. return rst;
  126. }
  127. };
  128. rationRepositoryDao.prototype.addRationRepository = function( rationLibObj,callback){
  129. counter.counterDAO.getIDAfterCount(counter.moduleName.rationMap, 1, function(err, result){
  130. var rMap = createNewLibModel(rationLibObj);
  131. rMap.ID = result.sequence_value;
  132. new rationRepository(rMap).save(function(err, result) {
  133. if (err) callback("Error", null);
  134. else{
  135. //向引用工料机库的添加标记
  136. gljMapModel.update({ID: rMap.gljLib, deleted: false}, {$addToSet: {rationLibs: {ID: rMap.ID, dispName: rMap.dispName}}}, function (err, data) {
  137. if(err){
  138. rationRepository.remove({ID: rMap.ID}, function (err) {
  139. if(err){
  140. callback("创建失败且回滚失败!", null);
  141. }
  142. else{
  143. callback("创建失败,已回滚!", null);
  144. }
  145. });
  146. }
  147. else{
  148. callback(false, result);
  149. }
  150. });
  151. }
  152. });
  153. });
  154. };
  155. rationRepositoryDao.prototype.getRationLib = function(libId, callback) {
  156. rationRepository.find({ID: libId, "deleted": false}, function(err, data){
  157. if (err) {
  158. callback( 'Error', null);
  159. } else {
  160. callback(0, data);
  161. }
  162. });
  163. };
  164. rationRepositoryDao.prototype.getDisplayRationLibs = function(callback) {
  165. rationRepository.find({"deleted": false}, function(err, data){
  166. if (err) {
  167. callback( 'Error', null);
  168. } else {
  169. callback( false, data);
  170. }
  171. });
  172. };
  173. rationRepositoryDao.prototype.updateName = function(oprtor, renameObj, callback){
  174. rationRepository.update({ID: renameObj.ID, deleted: false}, {$set: {dispName: renameObj.newName}}, function (err) {
  175. if(err){
  176. callback(err, '重命名失败!');
  177. } else{
  178. new rationRepositoryDao().updateOprArr({ID: renameObj.ID, deleted: false}, oprtor, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  179. if(err){
  180. callback(err, '更新最近操作者失败!');
  181. }
  182. else{
  183. engLibModel.update({'ration_lib.id': renameObj.ID}, {$set: {'ration_lib.$.name': renameObj.newName}}, {multi: true}, function (err) {
  184. if(err){
  185. callback(err, '更新工程专业引用失败!');
  186. }
  187. else {
  188. callback(null, '成功!');
  189. }
  190. });
  191. }
  192. });
  193. }
  194. });
  195. }
  196. rationRepositoryDao.prototype.deleteRationLib = function(oprtor, libId, callback){
  197. new rationRepositoryDao().updateOprArr({ID: libId, deleted: false}, oprtor, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  198. if(err){
  199. callback(err, '失败!')
  200. }
  201. else{
  202. rationRepository.find({ID: libId, deleted: false}, function (err, result) {
  203. if(err){
  204. callback(err, "没有数据!");
  205. }
  206. else{
  207. rationRepository.remove({ID: libId}, function (err) {
  208. if(err){
  209. callback(err, '移除定额库失败!');
  210. }
  211. else{
  212. async.parallel([
  213. function (cb) {
  214. //移除工料机库内被引用标记
  215. gljMapModel.update({ID: result[0].gljLib, deleted: false}, {$pull: {rationLibs: {ID: libId}}}, function (err) {
  216. if(err){
  217. cb(err);
  218. }
  219. else{
  220. cb(null);
  221. }
  222. });
  223. },
  224. //删除库下定额
  225. function (cb) {
  226. rationItemModel.remove({rationRepId: libId}, function (err) {
  227. if(err)cb(err);
  228. else cb(null);
  229. });
  230. },
  231. //删除库下章节树
  232. function (cb) {
  233. rationChapterTreeModel.remove({rationRepId: libId}, function (err) {
  234. if(err)cb(err);
  235. else cb(null);
  236. })
  237. },
  238. //删除子目换算
  239. function (cb) {
  240. rationCoeModel.remove({libID: libId}, function (err) {
  241. if(err)cb(err);
  242. else cb(null);
  243. });
  244. },
  245. //删除安装
  246. async function (cb) {
  247. try{
  248. let feeItems = await rationInstallFeeItem.find({rationRepId: libId});
  249. let sectionIDs = [];
  250. for(let item of feeItems){
  251. for(let section of item.section){
  252. sectionIDs.push(section.ID);
  253. }
  254. }
  255. await rationInstallFeeItem.remove({rationRepId: libId});
  256. await rationInstallSection.remove({ID: {$in: sectionIDs}});
  257. cb(null);
  258. }
  259. catch (err){
  260. cb(err);
  261. }
  262. }
  263. ], function (err) {
  264. if(err) callback(err, 'fail');
  265. else callback(null, 'sc')
  266. });
  267. }
  268. });
  269. }
  270. });
  271. }
  272. });
  273. }
  274. module.exports = new rationRepositoryDao();