ration_item.js 6.2 KB

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