rationItem.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. });
  14. var rationItemSchema = mongoose.Schema({
  15. ID:Number,
  16. //以下是基于已有access库
  17. code: String,
  18. name: String,
  19. unit: String,
  20. basePrice: Number,
  21. sectionId: 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(sectionId, updateItems, addItems, rIds, callback){
  36. var me = this;
  37. if (updateItems.length == 0 && rIds.length == 0) {
  38. me.addRationItems(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(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(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(code,callback){
  73. //
  74. };
  75. rationItemDAO.prototype.addRationItems = function(sectionId, items,callback){
  76. if (items && items.length > 0) {
  77. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  78. var maxId = result.value.sequence_value;
  79. var arr = [];
  80. for (var i = 0; i < items.length; i++) {
  81. var obj = new rationItemModel(items[i]);
  82. obj.ID = (maxId - (items.length - 1) + i);
  83. obj.sectionId = sectionId;
  84. arr.push(obj);
  85. }
  86. rationItemModel.collection.insert(arr, null, function(err, docs){
  87. if (err) {
  88. callback(true, "Fail to save", false);
  89. } else {
  90. callback(false, "Save successfully", docs);
  91. }
  92. })
  93. });
  94. } else {
  95. callback(true, "Source error!", false);
  96. }
  97. };
  98. rationItemDAO.prototype.updateRationItems = function(sectionId, items,callback){
  99. var functions = [];
  100. for (var i=0; i < items.length; i++) {
  101. functions.push((function(doc) {
  102. return function(cb) {
  103. var filter = {};
  104. if (doc.ID) {
  105. filter.ID = doc.ID;
  106. } else {
  107. filter.sectionId = sectionId;
  108. filter.code = doc.code;
  109. }
  110. rationItemModel.update(filter, doc, cb);
  111. };
  112. })(items[i]));
  113. }
  114. async.parallel(functions, function(err, results) {
  115. callback(err, results);
  116. });
  117. };
  118. module.exports = new rationItemDAO()