ration_item.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * Created by Tony on 2017/4/28.
  3. */
  4. var mongoose = require("mongoose");
  5. var async = require("async");
  6. var rationItemModel = mongoose.model("std_ration_lib_ration_items");
  7. var counter = require('../../../public/counter/counter');
  8. const stdGljListModel = mongoose.model('std_glj_lib_gljList');
  9. var rationItemDAO = function(){};
  10. rationItemDAO.prototype.getRationItemsBySection = function(rationRepId, sectionId,callback){
  11. rationItemModel.find({"rationRepId": rationRepId, "sectionId": sectionId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]}, null, {sort: {code: 1}}, function(err,data){
  12. if(err) callback(true, "Fail to get items", "")
  13. else callback(false,"Get items successfully", data);
  14. })
  15. };
  16. rationItemDAO.prototype.getRationGljItemsBySection = async function(sectionId,callback){
  17. try{
  18. let rationItems = await rationItemModel.find({"sectionId": sectionId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]}, null, {sort: {code: 1}});
  19. for(let i = 0, len = rationItems.length; i < len; i++){
  20. let hint = '';
  21. let ration = rationItems[i];
  22. for(let j = 0, jLen = ration.rationGljList.length; j < jLen; j++){
  23. let rationGlj = ration.rationGljList[j];
  24. let glj = await stdGljListModel.find({ID: rationGlj.gljId, $or: [{isDeleted: null}, {isDeleted: false} ]}, '-_id code name unit');
  25. if(glj.length > 0){
  26. let unitHint = '' + glj[0].code + ' ' + glj[0].name + '' + glj[0].unit + ' ' + rationGlj.consumeAmt + '</br>';
  27. hint += unitHint;
  28. }
  29. }
  30. ration._doc.hint = hint;
  31. }
  32. callback(false,"Get items successfully", rationItems);
  33. }
  34. catch (err){
  35. callback(true, "Fail to get items", "")
  36. }
  37. };
  38. rationItemDAO.prototype.mixUpdateRationItems = function(rationLibId, sectionId, updateItems, addItems, rIds, callback){
  39. var me = this;
  40. if (updateItems.length == 0 && rIds.length == 0) {
  41. me.addRationItems(rationLibId, sectionId, addItems, callback);
  42. } else {
  43. me.removeRationItems(rIds, function(err, message, docs) {
  44. if (err) {
  45. callback(true, "Fail to remove", false);
  46. } else {
  47. me.updateRationItems(rationLibId, sectionId, updateItems, function(err, results){
  48. if (err) {
  49. callback(true, "Fail to save", false);
  50. } else {
  51. if (addItems && addItems.length > 0) {
  52. me.addRationItems(rationLibId, sectionId, addItems, callback);
  53. } else {
  54. callback(false, "Save successfully", results);
  55. }
  56. }
  57. });
  58. }
  59. })
  60. }
  61. };
  62. rationItemDAO.prototype.removeRationItems = function(rIds,callback){
  63. if (rIds.length > 0) {
  64. rationItemModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  65. if (err) {
  66. callback(true, "Fail to remove", false);
  67. } else {
  68. callback(false, "Remove successfully", docs);
  69. }
  70. })
  71. } else {
  72. callback(false, "No records were deleted!", null);
  73. }
  74. };
  75. rationItemDAO.prototype.getRationItemsByCode = function(repId, code,callback){
  76. rationItemModel.find({"rationRepId": repId, "code": {'$regex': code, $options: '$i'}, "$or": [{"isDeleted": null}, {"isDeleted": false}]},function(err,data){
  77. if(err) callback(true, "Fail to get items", "")
  78. else callback(false,"Get items successfully", data);
  79. })
  80. };
  81. rationItemDAO.prototype.findRation = function (repId, keyword, callback) {
  82. var filter = {
  83. 'rationRepId': repId,
  84. '$and': [{
  85. '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}]
  86. }, {
  87. '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}]
  88. }]
  89. };
  90. rationItemModel.find(filter, function (err, data) {
  91. if (err) {
  92. callback(true, 'Fail to find ration', null);
  93. } else {
  94. callback(false, '', data);
  95. }
  96. });
  97. };
  98. rationItemDAO.prototype.matchRation = function (repId, keyword, callback) {
  99. let filter = {
  100. 'rationRepId': repId,
  101. 'code': keyword,
  102. '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}]
  103. };
  104. rationItemModel.findOne(filter, function (err, data) {
  105. callback(err, JSON.parse(JSON.stringify(data)));
  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.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 = {
  169. Dao: new rationItemDAO(),
  170. Model: rationItemModel
  171. };
  172. //module.exports = new rationItemDAO()