coe.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * Created by CSL on 2017/5/3.
  3. * 系数表。
  4. */
  5. //modiyied by zhong on 2017/9/21
  6. const mongoose = require('mongoose');
  7. const coeListModel = mongoose.model('std_ration_lib_coe_list');
  8. let counter = require('../../../public/counter/counter');
  9. const rationModel = mongoose.model('std_ration_lib_ration_items');
  10. import async from 'async';
  11. let coeListDAO = function(){};
  12. coeListDAO.prototype.getCoeReference = async function (rationRepId, coeID) {
  13. return await rationModel.find({rationRepId, 'rationCoeList.ID': coeID}, '-_id code').sort({code: 1});
  14. };
  15. coeListDAO.prototype.getCoe = function (data, callback) {
  16. coeListModel.findOne({
  17. "libID": data.libID,
  18. "ID": data.ID,
  19. "$or": [{"isDeleted": null}, {"isDeleted": false}]
  20. },
  21. function (err, doc) {
  22. if (err) callback("获取系数明细错误!", null)
  23. else callback(null, doc);
  24. })
  25. };
  26. coeListDAO.prototype.getCoeItemsByIDs = function (data, callback) {
  27. coeListModel.find({
  28. "libID": data.libID,
  29. "ID": {"$in":data.coeIDs}
  30. }, ["libID","ID","serialNo","name","content","-_id"],
  31. function (err, doc) {
  32. if (err) callback("批量获取系数明细错误!", null)
  33. else callback(null, doc);
  34. })
  35. };
  36. coeListDAO.prototype.getCoeItemsByNos = function (data, callback) {
  37. coeListModel.find({
  38. "libID": data.libID,
  39. "serialNo": {"$in":data.coeNos}
  40. }, ["libID","ID","serialNo","name","content","-_id"],
  41. function (err, doc) {
  42. if (err) callback("批量获取系数明细错误!", null)
  43. else callback(null, doc);
  44. })
  45. };
  46. coeListDAO.prototype.getCoesByLibID = function (libID, callback) {
  47. coeListModel.find({ "libID": libID },
  48. function (err, doc) {
  49. if (err) callback("获取定额库系数表错误", null)
  50. else callback(null, doc);
  51. })
  52. };
  53. coeListDAO.prototype.saveToCoeList = function(data, callback) {
  54. let me = this;
  55. /*if (data.addArr.length > 0) {
  56. me.addItems(data.addArr, callback);
  57. }
  58. if (data.deleteArr.length > 0) {
  59. me.deleteItems(data.deleteArr, callback);
  60. }
  61. if (data.updateArr.length > 0) {
  62. me.updateItems(data.updateArr, callback);
  63. }*/
  64. if (data.addArr.length > 0 && data.updateArr.length > 0) {
  65. async.parallel([
  66. function (cb) {
  67. me.addItems(data.addArr, cb);
  68. },
  69. function (cb) {
  70. me.updateItems(data.updateArr, cb);
  71. }
  72. ], function (err, result) {
  73. callback(err, 'mixed', result);
  74. });
  75. }
  76. else if(data.addArr.length > 0){
  77. me.addItems(data.addArr, callback);
  78. }
  79. else if (data.updateArr.length > 0) {
  80. me.updateItems(data.updateArr, callback);
  81. }
  82. else if (data.deleteArr.length > 0) {
  83. me.deleteItems(data.deleteArr, callback);
  84. }
  85. };
  86. coeListDAO.prototype.addItems = function(addArr, callback) {
  87. if (addArr && addArr.length > 0) {
  88. counter.counterDAO.getIDAfterCount(counter.moduleName.coeList, addArr.length, function(err, result){
  89. let maxId = result.sequence_value;
  90. let functions = [];
  91. for (let i = 0; i < addArr.length; i++) {
  92. let obj = new coeListModel(addArr[i]);
  93. obj.ID = (maxId - (addArr.length - 1) + i);
  94. functions.push((function (obj) {
  95. return function (cb) {
  96. coeListModel.create(obj, function(err) {
  97. if (err) {
  98. cb(true, null);
  99. } else {
  100. cb(false, obj);
  101. };
  102. });
  103. }
  104. })(obj));
  105. }
  106. async.parallel(functions, function (err, result) {
  107. if(err) callback(true, 'add fail', null);
  108. else callback(false, 'addSc', result);
  109. })
  110. });
  111. } else {
  112. callback(true, "no source", null);
  113. }
  114. };
  115. coeListDAO.prototype.updateItems = function(updateArr, callback) {
  116. if (updateArr && updateArr.length > 0) {
  117. let functions = [];
  118. for (let i = 0; i < updateArr.length; i++) {
  119. let obj = updateArr[i];
  120. let needSet = {};
  121. for(let attr in obj){
  122. if(attr !== 'libID' || attr !== 'ID'){
  123. needSet[attr] = obj[attr];
  124. }
  125. }
  126. functions.push((function (obj) {
  127. return function (cb) {
  128. coeListModel.update({"libID": obj.libID, "ID": obj.ID}, needSet, function(err) {
  129. if (err) {
  130. cb(true);
  131. } else {
  132. cb(false);
  133. }
  134. });
  135. }
  136. })(obj));
  137. }
  138. async.parallel(functions, function (err, result) {
  139. if(err) callback(true, 'update fail', null);
  140. else callback(false, 'updateSc', null);
  141. })
  142. } else {
  143. callback(true, "no source", null);
  144. }
  145. };
  146. coeListDAO.prototype.deleteItems = function(deleteArr, callback) {
  147. if (deleteArr && deleteArr.length > 0) {
  148. let functions = [];
  149. for (let i = 0; i < deleteArr.length; i++) {
  150. let obj = deleteArr[i];
  151. functions.push((function (obj) {
  152. return function (cb) {
  153. coeListModel.remove({"libID": obj.libID, "ID": obj.ID}, function(err) {
  154. if (err) {
  155. cb(true, null);
  156. } else {
  157. cb(false, obj.ID);
  158. }
  159. });
  160. }
  161. })(obj));
  162. }
  163. async.parallel(functions, function (err, result) {
  164. if(err) callback(true, 'delete fail', null);
  165. else callback(false, 'sc', result);
  166. })
  167. } else {
  168. callback(true, "no source", null);
  169. }
  170. };
  171. module.exports = new coeListDAO();