ration_item.js 6.3 KB

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