repository_map.js 12 KB

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