repository_map.js 12 KB

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