ration_item.js 6.7 KB

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