ration_item.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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": {'$regex': code, $options: '$i'}, "$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.findRation = function (repId, keyword, callback) {
  79. var filter = {
  80. 'rationRepId': repId,
  81. '$and': [{
  82. '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}]
  83. }, {
  84. '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}]
  85. }]
  86. };
  87. rationItemModel.find(filter, function (err, data) {
  88. if (err) {
  89. callback(true, 'Fail to find ration', null);
  90. } else {
  91. callback(false, '', data);
  92. }
  93. })
  94. }
  95. rationItemDAO.prototype.getRationItem = function (repId, code, callback) {
  96. if (callback) {
  97. rationItemModel.findOne({rationRepId: repId, code: code, "$or": [{"isDeleted": null}, {"isDeleted": false}]}, '-_id').exec()
  98. .then(function (result, err) {
  99. if (err) {
  100. callback(1, '找不到定额“' + code +'”' , null);
  101. } else {
  102. callback(0, '', result);
  103. }
  104. });
  105. return null;
  106. } else {
  107. return rationItemModel.findOne({rationRepId: repId, code: code, "$or": [{"isDeleted": null}, {"isDeleted": false}]}, '-_id').exec();
  108. }
  109. };
  110. rationItemDAO.prototype.addRationItems = function(rationLibId, sectionId, items,callback){
  111. if (items && items.length > 0) {
  112. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  113. var maxId = result.value.sequence_value;
  114. var arr = [];
  115. for (var i = 0; i < items.length; i++) {
  116. var obj = new rationItemModel(items[i]);
  117. obj.ID = (maxId - (items.length - 1) + i);
  118. obj.sectionId = sectionId;
  119. obj.rationRepId = rationLibId;
  120. arr.push(obj);
  121. }
  122. rationItemModel.collection.insert(arr, null, function(err, docs){
  123. if (err) {
  124. callback(true, "Fail to save", false);
  125. } else {
  126. callback(false, "Save successfully", docs);
  127. }
  128. })
  129. });
  130. } else {
  131. callback(true, "Source error!", false);
  132. }
  133. };
  134. rationItemDAO.prototype.updateRationItems = function(rationLibId, sectionId, items,callback){
  135. var functions = [];
  136. for (var i=0; i < items.length; i++) {
  137. functions.push((function(doc) {
  138. return function(cb) {
  139. var filter = {};
  140. if (doc.ID) {
  141. filter.ID = doc.ID;
  142. } else {
  143. filter.sectionId = sectionId;
  144. if (rationLibId) filter.rationRepId = rationLibId;
  145. filter.code = doc.code;
  146. }
  147. rationItemModel.update(filter, doc, cb);
  148. };
  149. })(items[i]));
  150. }
  151. async.parallel(functions, function(err, results) {
  152. callback(err, results);
  153. });
  154. };
  155. module.exports = new rationItemDAO()