glj_repository.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**
  2. * Created by Tony on 2017/5/4.
  3. * 工料机的总库,根据不同定额库分类,参考原gljList表
  4. */
  5. var mongoose = require("mongoose");
  6. var async = require("async");
  7. var gljTypeModel = mongoose.model("std_glj_lib_gljClass");
  8. var gljItemModel = mongoose.model("std_glj_lib_gljList");
  9. var repositoryMap = require('./repository_map').Dao;
  10. var counter = require('../../../public/counter/counter');
  11. var gljItemDAO = function(){};
  12. gljItemDAO.prototype.getGljTypes = function(rationLibId, callback){
  13. gljTypeModel.find({"repositoryId": rationLibId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  14. if(data.length) callback(false,data);
  15. else if(err) callback("获取工料机类型错误!",false)
  16. else callback(false,false);
  17. })
  18. };
  19. gljItemDAO.prototype.getGljItemsByRep = function(repositoryId,callback){
  20. gljItemModel.find({"repositoryId": repositoryId},function(err,data){
  21. if(err) callback(true, "")
  22. else callback(false,data);
  23. })
  24. };
  25. gljItemDAO.prototype.getGljItemByType = function(repositoryId, type, callback){
  26. gljItemModel.find({"repositoryId": repositoryId, "gljType": type},function(err,data){
  27. if(err) callback(true, "")
  28. else callback(false, data);
  29. })
  30. };
  31. gljItemDAO.prototype.getGljItem = function(repositoryId, code, callback){
  32. gljItemModel.find({"repositoryId": repositoryId, "code": code},function(err,data){
  33. if(err) callback(true, "")
  34. else callback(false, data);
  35. })
  36. };
  37. gljItemDAO.prototype.getGljItems = function(gljIds, callback){
  38. gljItemModel.find({"ID": {"$in": gljIds}},function(err,data){
  39. if(err) callback(true, "")
  40. else callback(false, data);
  41. })
  42. };
  43. gljItemDAO.prototype.getGljItemsByCode = function(repositoryId, codes, callback){
  44. gljItemModel.find({"repositoryId": repositoryId,"code": {"$in": codes}},function(err,data){
  45. if(err) callback(true, "")
  46. else callback(false, data);
  47. })
  48. };
  49. gljItemDAO.prototype.mixUpdateGljItems = function(repId, updateItems, addItems, rIds, callback) {
  50. var me = this;
  51. if (updateItems.length == 0 && rIds.length == 0) {
  52. me.addGljItems(repId, addItems, callback);
  53. } else {
  54. me.removeGljItems(rIds, function(err, message, docs) {
  55. me.updateGljItems(repId, updateItems, function(err, results){
  56. if (err) {
  57. callback(true, "Fail to update", false);
  58. } else {
  59. if (addItems && addItems.length > 0) {
  60. me.addGljItems(repId, addItems, callback);
  61. } else {
  62. callback(false, "Save successfully", results);
  63. }
  64. }
  65. });
  66. });
  67. }
  68. };
  69. gljItemDAO.prototype.removeGljItems = function(rIds, callback) {
  70. if (rIds && rIds.length > 0) {
  71. gljItemModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  72. if (err) {
  73. callback(true, "Fail to remove", false);
  74. } else {
  75. callback(false, "Remove successfully", docs);
  76. }
  77. })
  78. } else {
  79. callback(false, "No records were deleted!", null);
  80. }
  81. };
  82. gljItemDAO.prototype.addGljItems = function(repId, items, callback) {
  83. if (items && items.length > 0) {
  84. counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, items.length, function(err, result){
  85. var maxId = result.sequence_value;
  86. var arr = [];
  87. for (var i = 0; i < items.length; i++) {
  88. var obj = new gljItemModel(items[i]);
  89. obj.ID = (maxId - (items.length - 1) + i);
  90. obj.repositoryId = repId;
  91. arr.push(obj);
  92. }
  93. gljItemModel.collection.insert(arr, null, function(err, docs){
  94. if (err) {
  95. callback(true, "Fail to add", false);
  96. } else {
  97. callback(false, "Add successfully", docs);
  98. }
  99. })
  100. });
  101. } else {
  102. callback(true, "No source", false);
  103. }
  104. };
  105. gljItemDAO.prototype.updateGljItems = function(repId, items, callback) {
  106. var functions = [];
  107. for (var i=0; i < items.length; i++) {
  108. functions.push((function(doc) {
  109. return function(cb) {
  110. var filter = {};
  111. if (doc.ID) {
  112. filter.ID = doc.ID;
  113. } else {
  114. filter.repositoryId = repId;
  115. filter.code = doc.code;
  116. }
  117. gljItemModel.update(filter, doc, cb);
  118. };
  119. })(items[i]));
  120. }
  121. async.parallel(functions, function(err, results) {
  122. callback(err, results);
  123. });
  124. };
  125. gljItemDAO.prototype.updateNodes = function(nodes, callback) {
  126. var functions = [];
  127. for (var i=0; i < nodes.length; i++) {
  128. functions.push((function(doc) {
  129. return function(cb) {
  130. gljTypeModel.update({ID: doc.ID}, doc, cb);
  131. };
  132. })(nodes[i]));
  133. }
  134. async.parallel(functions, function(err, results) {
  135. callback(err, results);
  136. });
  137. };
  138. gljItemDAO.prototype.removeNodes = function(nodeIds, preNodeId, preNodeNextId, callback){
  139. var functions = [];
  140. if (preNodeId != -1) {
  141. functions.push((function(nodeId, nextId) {
  142. return function(cb) {
  143. gljTypeModel.update({ID: nodeId}, {"NextSiblingID": nextId}, cb);
  144. };
  145. })(preNodeId, preNodeNextId));
  146. }
  147. for (var i=0; i < nodeIds.length; i++) {
  148. functions.push((function(nodeId) {
  149. return function(cb) {
  150. gljTypeModel.update({ID: nodeId}, {"isDeleted": true}, cb);
  151. };
  152. })(nodeIds[i]));
  153. }
  154. async.parallel(functions, function(err, results) {
  155. callback(err, results);
  156. });
  157. };
  158. gljItemDAO.prototype.createNewNode = function(repId, lastNodeId, nodeData, callback) {
  159. return counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, 1, function(err, result){
  160. nodeData.repositoryId = repId;
  161. nodeData.ID = result.sequence_value;
  162. var node = new gljTypeModel(nodeData);
  163. node.save(function (err, result) {
  164. if (err) {
  165. callback(true, "章节树ID错误!", false);
  166. } else {
  167. if (lastNodeId > 0) {
  168. gljTypeModel.update({ID: lastNodeId}, {"NextSiblingID": nodeData.ID}, function(err, rst){
  169. if (err) {
  170. callback(true, "章节树ID错误!", false);
  171. } else {
  172. callback(false, '', result);
  173. }
  174. });
  175. } else callback(false, '', result);
  176. }
  177. });
  178. });
  179. };
  180. module.exports = new gljItemDAO();