glj_repository.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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_ration_lib_glj_type");
  8. var gljItemModel = mongoose.model("std_ration_lib_glj_list");
  9. var repositoryMap = require('./repository_map');
  10. var counter = require('../../../public/counter/counter');
  11. let moment = require('moment');
  12. const compleGljModel = mongoose.model('complementary_glj_lib');
  13. const gljModel = mongoose.model('std_glj_lib_gljList');
  14. const gljClassModel = mongoose.model('std_glj_lib_gljClass');
  15. var gljItemDAO = function(){};
  16. gljItemDAO.prototype.getGljTypes = function(gljLibID, callback){
  17. gljClassModel.find({"repositoryId": gljLibID, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  18. if(data.length) callback(false,data);
  19. else if(err) callback("获取工料机类型错误!",false)
  20. else callback(false,false);
  21. })
  22. };
  23. gljItemDAO.prototype.getGljItemsByRep = function(repositoryId,callback){
  24. gljModel.find({"repositoryId": repositoryId},function(err,data){
  25. if(err) callback(true, "")
  26. else callback(false,data);
  27. })
  28. };
  29. gljItemDAO.prototype.getGljItemByType = function(repositoryId, type, callback){
  30. gljModel.find({"repositoryId": repositoryId, "gljType": type},function(err,data){
  31. if(err) callback(true, "")
  32. else callback(false, data);
  33. })
  34. };
  35. gljItemDAO.prototype.getGljItem = function(repositoryId, code, callback){
  36. gljModel.find({"repositoryId": repositoryId, "code": code},function(err,data){
  37. if(err) callback(true, "")
  38. else callback(false, data);
  39. })
  40. };
  41. gljItemDAO.prototype.getGljItems = function(gljIds, callback){
  42. gljModel.find({"ID": {"$in": gljIds}},function(err,data){
  43. if(err) callback(true, "")
  44. else callback(false, data);
  45. })
  46. };
  47. /*gljItemDAO.prototype.getStdCompleGljItems = async function (rationGljList, callback) {
  48. try{
  49. let rst = [];
  50. let stdIds = [], compleIds = []
  51. for(let glj of rationGljList){
  52. if(glj.type !== undefined && glj.type === 'complementary'){
  53. compleIds.push(glj.gljId);
  54. }
  55. else{
  56. stdIds.push(glj.gljId);
  57. }
  58. }
  59. if(compleIds.length > 0){
  60. let compleGlj = await compleGljModel.find({ID: {$in: compleIds}, deleteInfo: null}, {ID: 1, gljType: 1, basePrice: 1});
  61. rst = rst.concat(compleGlj);
  62. }
  63. if(stdIds.length > 0){
  64. let stdGlj = await gljModel.find({ID: {$in: stdIds}, $or: [{deleted: null}, {deleted: false}]}, {ID: 1, gljType: 1, basePrice: 1});
  65. rst = rst.concat(stdGlj);
  66. }
  67. callback(0, rst);
  68. }
  69. catch(err){
  70. callback(err, null);
  71. }
  72. };*/
  73. gljItemDAO.prototype.getStdCompleGljItems = async function (compleGljIds, stdGljIds, callback) {
  74. try{
  75. let rst = [];
  76. if(compleGljIds.length > 0){
  77. let compleGlj = await compleGljModel.find({ID: {$in: compleGljIds}, deleteInfo: null}, {ID: 1, gljType: 1, basePrice: 1});
  78. rst = rst.concat(compleGlj);
  79. }
  80. if(stdGljIds.length > 0){
  81. let stdGlj = await gljModel.find({ID: {$in: stdGljIds}, $or: [{deleted: null}, {deleted: false}]}, {ID: 1, gljType: 1, basePrice: 1});
  82. rst = rst.concat(stdGlj);
  83. }
  84. callback(0, rst);
  85. }
  86. catch(err){
  87. callback(err, null);
  88. }
  89. };
  90. gljItemDAO.prototype.getGljItemsByCode = function(repositoryId, codes, callback){
  91. gljModel.find({"repositoryId": repositoryId,"code": {"$in": codes}},function(err,data){
  92. if(err) callback(true, "")
  93. else callback(false, data);
  94. })
  95. };
  96. gljItemDAO.prototype.mixUpdateGljItems = function(repId, lastOpr, updateItems, addItems, rIds, callback) {
  97. var me = this;
  98. if (updateItems.length == 0 && rIds.length == 0) {
  99. me.addGljItems(repId, lastOpr, addItems, callback);
  100. } else {
  101. me.removeGljItems(repId, lastOpr, rIds, function(err, message, docs) {
  102. me.updateGljItems(repId, lastOpr, updateItems, function(err, results){
  103. if (err) {
  104. callback(true, "Fail to update", false);
  105. } else {
  106. if (addItems && addItems.length > 0) {
  107. me.addGljItems(repId, lastOpr, addItems, callback);
  108. } else {
  109. callback(false, "Save successfully", results);
  110. }
  111. }
  112. });
  113. });
  114. }
  115. };
  116. gljItemDAO.prototype.removeGljItems = function(repId, lastOpr, rIds, callback) {
  117. if (rIds && rIds.length > 0) {
  118. gljItemModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  119. if (err) {
  120. callback(true, "Fail to remove", false);
  121. } else {
  122. repositoryMap.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  123. if(err){
  124. callback(true, "Fail to update operator", false);
  125. }
  126. else{
  127. callback(false, "Remove successfully", docs);
  128. }
  129. });
  130. }
  131. })
  132. } else {
  133. callback(false, "No records were deleted!", null);
  134. }
  135. };
  136. gljItemDAO.prototype.addGljItems = function(repId, lastOpr, items, callback) {
  137. if (items && items.length > 0) {
  138. counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, items.length, function(err, result){
  139. var maxId = result.sequence_value;
  140. var arr = [];
  141. for (var i = 0; i < items.length; i++) {
  142. var obj = new gljItemModel(items[i]);
  143. obj.ID = (maxId - (items.length - 1) + i);
  144. obj.repositoryId = repId;
  145. arr.push(obj);
  146. }
  147. gljItemModel.collection.insert(arr, null, function(err, docs){
  148. if (err) {
  149. callback(true, "Fail to add", false);
  150. } else {
  151. repositoryMap.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  152. if(err){
  153. callback(true, "Fail to update Operator", false);
  154. }
  155. else{
  156. callback(false, "Add successfully", docs);
  157. }
  158. });
  159. }
  160. })
  161. });
  162. } else {
  163. callback(true, "No source", false);
  164. }
  165. };
  166. gljItemDAO.prototype.updateGljItems = function(repId, lastOpr, items, callback) {
  167. var functions = [];
  168. for (var i=0; i < items.length; i++) {
  169. functions.push((function(doc) {
  170. return function(cb) {
  171. var filter = {};
  172. if (doc.ID) {
  173. filter.ID = doc.ID;
  174. } else {
  175. filter.repositoryId = repId;
  176. filter.code = doc.code;
  177. }
  178. gljItemModel.update(filter, doc, cb);
  179. };
  180. })(items[i]));
  181. }
  182. functions.push((function () {
  183. return function (cb) {
  184. repositoryMap.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  185. if(err){
  186. cb(err);
  187. }
  188. else{
  189. cb(null);
  190. }
  191. })
  192. }
  193. })());
  194. async.parallel(functions, function(err, results) {
  195. callback(err, results);
  196. });
  197. };
  198. gljItemDAO.prototype.updateNodes = function(repId, lastOpr, nodes, callback) {
  199. var functions = [];
  200. for (var i=0; i < nodes.length; i++) {
  201. functions.push((function(doc) {
  202. return function(cb) {
  203. gljTypeModel.update({ID: doc.ID}, doc, cb);
  204. };
  205. })(nodes[i]));
  206. }
  207. functions.push((function () {
  208. return function (cb) {
  209. repositoryMap.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  210. if(err){
  211. cb(err);
  212. }
  213. else{
  214. cb(null);
  215. }
  216. })
  217. }
  218. })());
  219. async.parallel(functions, function(err, results) {
  220. callback(err, results);
  221. });
  222. };
  223. gljItemDAO.prototype.removeNodes = function(repId, lastOpr, nodeIds, preNodeId, preNodeNextId, callback){
  224. var functions = [];
  225. if (preNodeId != -1) {
  226. functions.push((function(nodeId, nextId) {
  227. return function(cb) {
  228. gljTypeModel.update({ID: nodeId}, {"NextSiblingID": nextId}, cb);
  229. };
  230. })(preNodeId, preNodeNextId));
  231. }
  232. for (var i=0; i < nodeIds.length; i++) {
  233. functions.push((function(nodeId) {
  234. return function(cb) {
  235. gljTypeModel.update({ID: nodeId}, {"isDeleted": true}, cb);
  236. };
  237. })(nodeIds[i]));
  238. }
  239. functions.push((function () {
  240. return function (cb) {
  241. repositoryMap.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  242. if(err){
  243. cb(err);
  244. }
  245. else{
  246. cb(null);
  247. }
  248. })
  249. }
  250. })());
  251. async.parallel(functions, function(err, results) {
  252. callback(err, results);
  253. });
  254. };
  255. gljItemDAO.prototype.createNewNode = function(repId, lastOpr, lastNodeId, nodeData, callback) {
  256. return counter.counterDAO.getIDAfterCount(counter.moduleName.GLJ, 1, function(err, result){
  257. nodeData.repositoryId = repId;
  258. nodeData.ID = result.sequence_value;
  259. var node = new gljTypeModel(nodeData);
  260. async.parallel([
  261. function (cb) {
  262. node.save(function (err, result) {
  263. if (err) {
  264. cb("章节树ID错误!", false);
  265. } else {
  266. if (lastNodeId > 0) {
  267. gljTypeModel.update({ID: lastNodeId}, {"NextSiblingID": nodeData.ID}, function(err, rst){
  268. if (err) {
  269. cb("章节树ID错误!", false);
  270. } else {
  271. cb(false, result);
  272. }
  273. });
  274. } else cb(false, result);
  275. }
  276. });
  277. },
  278. function (cb) {
  279. repositoryMap.updateOprArr({ID: repId}, lastOpr, moment(Date.now()).format('YYYY-MM-DD HH:mm:ss'), function (err) {
  280. if(err){
  281. cb(err);
  282. }
  283. else{
  284. cb(null);
  285. }
  286. })
  287. }
  288. ], function (err, result) {
  289. if(err){
  290. callback(true, "章节树错误!", false);
  291. }
  292. else{
  293. callback(false, '', result[0]);
  294. }
  295. })
  296. });
  297. };
  298. module.exports = new gljItemDAO();