repository_map.js 11 KB

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