ration_item.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. rationCoeList: Array
  26. });
  27. var rationItemModel = db.model("rationItems",rationItemSchema, "rationItems")
  28. var counter = require('../../../public/counter/counter');
  29. var rationItemDAO = function(){};
  30. rationItemDAO.prototype.getRationItemsBySection = function(sectionId,callback){
  31. rationItemModel.find({"sectionId": sectionId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
  32. if(err) callback(true, "Fail to get items", "")
  33. else callback(false,"Get items successfully", data);
  34. })
  35. };
  36. rationItemDAO.prototype.mixUpdateRationItems = function(rationLibId, sectionId, updateItems, addItems, rIds, callback){
  37. var me = this;
  38. if (updateItems.length == 0 && rIds.length == 0) {
  39. me.addRationItems(rationLibId, sectionId, addItems, callback);
  40. } else {
  41. me.removeRationItems(rIds, function(err, message, docs) {
  42. if (err) {
  43. callback(true, "Fail to remove", false);
  44. } else {
  45. me.updateRationItems(rationLibId, sectionId, updateItems, function(err, results){
  46. if (err) {
  47. callback(true, "Fail to save", false);
  48. } else {
  49. if (addItems && addItems.length > 0) {
  50. me.addRationItems(rationLibId, sectionId, addItems, callback);
  51. } else {
  52. callback(false, "Save successfully", results);
  53. }
  54. }
  55. });
  56. }
  57. })
  58. }
  59. };
  60. rationItemDAO.prototype.removeRationItems = function(rIds,callback){
  61. if (rIds.length > 0) {
  62. rationItemModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  63. if (err) {
  64. callback(true, "Fail to remove", false);
  65. } else {
  66. callback(false, "Remove successfully", docs);
  67. }
  68. })
  69. } else {
  70. callback(false, "No records were deleted!", null);
  71. }
  72. };
  73. rationItemDAO.prototype.getRationItemsByCode = function(repId, code,callback){
  74. rationItemModel.find({"rationRepId": repId, "code": {'$regex': code, $options: '$i'}, "$or": [{"isDeleted": null}, {"isDeleted": false}]},function(err,data){
  75. if(err) callback(true, "Fail to get items", "")
  76. else callback(false,"Get items successfully", data);
  77. })
  78. };
  79. rationItemDAO.prototype.findRation = function (repId, keyword, callback) {
  80. var filter = {
  81. 'rationRepId': repId,
  82. '$and': [{
  83. '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}]
  84. }, {
  85. '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}]
  86. }]
  87. };
  88. rationItemModel.find(filter, function (err, data) {
  89. if (err) {
  90. callback(true, 'Fail to find ration', null);
  91. } else {
  92. callback(false, '', data);
  93. }
  94. })
  95. }
  96. rationItemDAO.prototype.getRationItem = function (repId, code, callback) {
  97. if (callback) {
  98. rationItemModel.findOne({rationRepId: repId, code: code, "$or": [{"isDeleted": null}, {"isDeleted": false}]}, '-_id').exec()
  99. .then(function (result, err) {
  100. if (err) {
  101. callback(1, '找不到定额“' + code +'”' , null);
  102. } else {
  103. callback(0, '', result);
  104. }
  105. });
  106. return null;
  107. } else {
  108. return rationItemModel.findOne({rationRepId: repId, code: code, "$or": [{"isDeleted": null}, {"isDeleted": false}]}, '-_id').exec();
  109. }
  110. };
  111. rationItemDAO.prototype.addRationItems = function(rationLibId, sectionId, items,callback){
  112. if (items && items.length > 0) {
  113. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  114. var maxId = result.value.sequence_value;
  115. var arr = [];
  116. for (var i = 0; i < items.length; i++) {
  117. var obj = new rationItemModel(items[i]);
  118. obj.ID = (maxId - (items.length - 1) + i);
  119. obj.sectionId = sectionId;
  120. obj.rationRepId = rationLibId;
  121. arr.push(obj);
  122. }
  123. rationItemModel.collection.insert(arr, null, function(err, docs){
  124. if (err) {
  125. callback(true, "Fail to save", false);
  126. } else {
  127. callback(false, "Save successfully", docs);
  128. }
  129. })
  130. });
  131. } else {
  132. callback(true, "Source error!", false);
  133. }
  134. };
  135. rationItemDAO.prototype.updateRationItems = function(rationLibId, sectionId, items,callback){
  136. var functions = [];
  137. for (var i=0; i < items.length; i++) {
  138. functions.push((function(doc) {
  139. return function(cb) {
  140. var filter = {};
  141. if (doc.ID) {
  142. filter.ID = doc.ID;
  143. } else {
  144. filter.sectionId = sectionId;
  145. if (rationLibId) filter.rationRepId = rationLibId;
  146. filter.code = doc.code;
  147. }
  148. rationItemModel.update(filter, doc, cb);
  149. };
  150. })(items[i]));
  151. }
  152. async.parallel(functions, function(err, results) {
  153. callback(err, results);
  154. });
  155. };
  156. module.exports = new rationItemDAO()