ration_item.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. var mongoose = require("mongoose");
  5. var dbm = require("../../../config/db/db_manager");
  6. var db = dbm.getCfgConnection("rationRepository")
  7. var async = require("async");
  8. var Schema = mongoose.Schema;
  9. var rationGljItemSchema = mongoose.Schema({
  10. gljId: Number,
  11. consumeAmt: Number,
  12. proportion: Number //配合比,暂时无需使用,默认0
  13. }, { _id: false });
  14. var rationItemSchema = mongoose.Schema({
  15. ID:Number,
  16. code: String,
  17. name: String,
  18. unit: String,
  19. basePrice: Number,
  20. sectionId: Number,
  21. rationRepId: Number,
  22. caption: String,
  23. feeType: Number,
  24. rationGljList: [rationGljItemSchema]
  25. });
  26. var rationItemModel = db.model("rationItems",rationItemSchema, "rationItems")
  27. var counter = require('../../../public/counter/counter');
  28. var rationItemDAO = function(){};
  29. rationItemDAO.prototype.getRationItemsBySection = function(sectionId,callback){
  30. rationItemModel.find({"sectionId": sectionId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  31. if(err) callback(true, "Fail to get items", "")
  32. else callback(false,"Get items successfully", data);
  33. })
  34. };
  35. rationItemDAO.prototype.mixUpdateRationItems = function(rationLibId, sectionId, updateItems, addItems, rIds, callback){
  36. var me = this;
  37. if (updateItems.length == 0 && rIds.length == 0) {
  38. me.addRationItems(rationLibId, sectionId, addItems, callback);
  39. } else {
  40. me.removeRationItems(rIds, function(err, message, docs) {
  41. if (err) {
  42. callback(true, "Fail to remove", false);
  43. } else {
  44. me.updateRationItems(rationLibId, sectionId, updateItems, function(err, results){
  45. if (err) {
  46. callback(true, "Fail to save", false);
  47. } else {
  48. if (addItems && addItems.length > 0) {
  49. me.addRationItems(rationLibId, sectionId, addItems, callback);
  50. } else {
  51. callback(false, "Save successfully", results);
  52. }
  53. }
  54. });
  55. }
  56. })
  57. }
  58. };
  59. rationItemDAO.prototype.removeRationItems = function(rIds,callback){
  60. if (rIds.length > 0) {
  61. rationItemModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  62. if (err) {
  63. callback(true, "Fail to remove", false);
  64. } else {
  65. callback(false, "Remove successfully", docs);
  66. }
  67. })
  68. } else {
  69. callback(false, "No records were deleted!", null);
  70. }
  71. };
  72. rationItemDAO.prototype.getRationItemsByCode = function(repId, code,callback){
  73. rationItemModel.find({"rationRepId": repId, "code": /code/, "$or": [{"isDeleted": null}, {"isDeleted": false}]},function(err,data){
  74. if(err) callback(true, "Fail to get items", "")
  75. else callback(false,"Get items successfully", data);
  76. })
  77. };
  78. rationItemDAO.prototype.getRationItem = function (sectionId, code, callback) {
  79. if (callback) {
  80. rationItemModel.findOne({sectionId: sectionId, code: code}, '-_id').exec()
  81. .then(function (result, err) {
  82. if (err) {
  83. callback(1, '找不到定额“' + code +'”' , null);
  84. } else {
  85. callback(0, '', result);
  86. }
  87. });
  88. return null;
  89. } else {
  90. return rationItemModel.findOne({sectionId: sectionId, code: code}, '-_id').exec();
  91. }
  92. };
  93. rationItemDAO.prototype.addRationItems = function(rationLibId, sectionId, items,callback){
  94. if (items && items.length > 0) {
  95. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  96. var maxId = result.value.sequence_value;
  97. var arr = [];
  98. for (var i = 0; i < items.length; i++) {
  99. var obj = new rationItemModel(items[i]);
  100. obj.ID = (maxId - (items.length - 1) + i);
  101. obj.sectionId = sectionId;
  102. obj.rationRepId = rationLibId;
  103. arr.push(obj);
  104. }
  105. rationItemModel.collection.insert(arr, null, function(err, docs){
  106. if (err) {
  107. callback(true, "Fail to save", false);
  108. } else {
  109. callback(false, "Save successfully", docs);
  110. }
  111. })
  112. });
  113. } else {
  114. callback(true, "Source error!", false);
  115. }
  116. };
  117. rationItemDAO.prototype.updateRationItems = function(rationLibId, sectionId, items,callback){
  118. var functions = [];
  119. for (var i=0; i < items.length; i++) {
  120. functions.push((function(doc) {
  121. return function(cb) {
  122. var filter = {};
  123. if (doc.ID) {
  124. filter.ID = doc.ID;
  125. } else {
  126. filter.sectionId = sectionId;
  127. if (rationLibId) filter.rationRepId = rationLibId;
  128. filter.code = doc.code;
  129. }
  130. rationItemModel.update(filter, doc, cb);
  131. };
  132. })(items[i]));
  133. }
  134. async.parallel(functions, function(err, results) {
  135. callback(err, results);
  136. });
  137. };
  138. module.exports = new rationItemDAO()