repository_map.js 12 KB

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