ration_item.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. //以下是基于已有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.getRationItem = function (sectionId, code, callback) {
  76. if (callback) {
  77. rationItemModel.findOne({sectionId: sectionId, code: code}, '-_id').exec()
  78. .then(function (result, err) {
  79. if (err) {
  80. callback(1, '找不到定额“' + code +'”' , null);
  81. } else {
  82. callback(0, '', result);
  83. }
  84. });
  85. return null;
  86. } else {
  87. return rationItemModel.findOne({sectionId: sectionId, code: code}, '-_id').exec();
  88. }
  89. }
  90. rationItemDAO.prototype.addRationItems = function(sectionId, items,callback){
  91. if (items && items.length > 0) {
  92. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  93. var maxId = result.value.sequence_value;
  94. var arr = [];
  95. for (var i = 0; i < items.length; i++) {
  96. var obj = new rationItemModel(items[i]);
  97. obj.ID = (maxId - (items.length - 1) + i);
  98. obj.sectionId = sectionId;
  99. arr.push(obj);
  100. }
  101. rationItemModel.collection.insert(arr, null, function(err, docs){
  102. if (err) {
  103. callback(true, "Fail to save", false);
  104. } else {
  105. callback(false, "Save successfully", docs);
  106. }
  107. })
  108. });
  109. } else {
  110. callback(true, "Source error!", false);
  111. }
  112. };
  113. rationItemDAO.prototype.updateRationItems = function(sectionId, items,callback){
  114. var functions = [];
  115. for (var i=0; i < items.length; i++) {
  116. functions.push((function(doc) {
  117. return function(cb) {
  118. var filter = {};
  119. if (doc.ID) {
  120. filter.ID = doc.ID;
  121. } else {
  122. filter.sectionId = sectionId;
  123. filter.code = doc.code;
  124. }
  125. rationItemModel.update(filter, doc, cb);
  126. };
  127. })(items[i]));
  128. }
  129. async.parallel(functions, function(err, results) {
  130. callback(err, results);
  131. });
  132. };
  133. module.exports = new rationItemDAO()