ration_item.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. import stdGljListModel from '../../common/std/schemas/std_ration_lib_glj_list';
  44. var rationItemDAO = function(){};
  45. rationItemDAO.prototype.getRationItemsBySection = function(sectionId,callback){
  46. rationItemModel.find({"sectionId": sectionId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]}, null, {sort: {code: 1}}, function(err,data){
  47. if(err) callback(true, "Fail to get items", "")
  48. else callback(false,"Get items successfully", data);
  49. })
  50. };
  51. rationItemDAO.prototype.getRationGljItemsBySection = async function(sectionId,callback){
  52. try{
  53. let rationItems = await rationItemModel.find({"sectionId": sectionId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]}, null, {sort: {code: 1}});
  54. for(let i = 0, len = rationItems.length; i < len; i++){
  55. let hint = '';
  56. let ration = rationItems[i];
  57. for(let j = 0, jLen = ration.rationGljList.length; j < jLen; j++){
  58. let rationGlj = ration.rationGljList[j];
  59. let glj = await stdGljListModel.find({ID: rationGlj.gljId, $or: [{isDeleted: null}, {isDeleted: false} ]}, '-_id code name unit');
  60. if(glj.length > 0){
  61. let unitHint = '' + glj[0].code + ' ' + glj[0].name + '' + glj[0].unit + ' ' + rationGlj.consumeAmt + '</br>';
  62. hint += unitHint;
  63. }
  64. }
  65. ration._doc.hint = hint;
  66. }
  67. callback(false,"Get items successfully", rationItems);
  68. }
  69. catch (err){
  70. callback(true, "Fail to get items", "")
  71. }
  72. };
  73. rationItemDAO.prototype.mixUpdateRationItems = function(rationLibId, sectionId, updateItems, addItems, rIds, callback){
  74. var me = this;
  75. if (updateItems.length == 0 && rIds.length == 0) {
  76. me.addRationItems(rationLibId, sectionId, addItems, callback);
  77. } else {
  78. me.removeRationItems(rIds, function(err, message, docs) {
  79. if (err) {
  80. callback(true, "Fail to remove", false);
  81. } else {
  82. me.updateRationItems(rationLibId, sectionId, updateItems, function(err, results){
  83. if (err) {
  84. callback(true, "Fail to save", false);
  85. } else {
  86. if (addItems && addItems.length > 0) {
  87. me.addRationItems(rationLibId, sectionId, addItems, callback);
  88. } else {
  89. callback(false, "Save successfully", results);
  90. }
  91. }
  92. });
  93. }
  94. })
  95. }
  96. };
  97. rationItemDAO.prototype.removeRationItems = function(rIds,callback){
  98. if (rIds.length > 0) {
  99. rationItemModel.collection.remove({ID: {$in: rIds}}, null, function(err, docs){
  100. if (err) {
  101. callback(true, "Fail to remove", false);
  102. } else {
  103. callback(false, "Remove successfully", docs);
  104. }
  105. })
  106. } else {
  107. callback(false, "No records were deleted!", null);
  108. }
  109. };
  110. rationItemDAO.prototype.getRationItemsByCode = function(repId, code,callback){
  111. rationItemModel.find({"rationRepId": repId, "code": {'$regex': code, $options: '$i'}, "$or": [{"isDeleted": null}, {"isDeleted": false}]},function(err,data){
  112. if(err) callback(true, "Fail to get items", "")
  113. else callback(false,"Get items successfully", data);
  114. })
  115. };
  116. rationItemDAO.prototype.findRation = function (repId, keyword, callback) {
  117. var filter = {
  118. 'rationRepId': repId,
  119. '$and': [{
  120. '$or': [{'code': {'$regex': keyword, $options: '$i'}}, {'name': {'$regex': keyword, $options: '$i'}}]
  121. }, {
  122. '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}]
  123. }]
  124. };
  125. rationItemModel.find(filter, function (err, data) {
  126. if (err) {
  127. callback(true, 'Fail to find ration', null);
  128. } else {
  129. callback(false, '', data);
  130. }
  131. });
  132. };
  133. rationItemDAO.prototype.matchRation = function (repId, keyword, callback) {
  134. let filter = {
  135. 'rationRepId': repId,
  136. 'code': keyword,
  137. '$or': [{'isDeleted': {"$exists":false}}, {'isDeleted': null}, {'isDeleted': false}]
  138. };
  139. rationItemModel.findOne(filter, function (err, data) {
  140. callback(err, JSON.parse(JSON.stringify(data)));
  141. });
  142. };
  143. rationItemDAO.prototype.getRationItem = function (repId, code, callback) {
  144. if (callback) {
  145. rationItemModel.findOne({rationRepId: repId, code: code, "$or": [{"isDeleted": null}, {"isDeleted": false}]}, '-_id').exec()
  146. .then(function (result, err) {
  147. if (err) {
  148. callback(1, '找不到定额“' + code +'”' , null);
  149. } else {
  150. callback(0, '', result);
  151. }
  152. });
  153. return null;
  154. } else {
  155. return rationItemModel.findOne({rationRepId: repId, code: code, "$or": [{"isDeleted": null}, {"isDeleted": false}]}, '-_id').exec();
  156. }
  157. };
  158. rationItemDAO.prototype.addRationItems = function(rationLibId, sectionId, items,callback){
  159. if (items && items.length > 0) {
  160. counter.counterDAO.getIDAfterCount(counter.moduleName.rations, items.length, function(err, result){
  161. var maxId = result.value.sequence_value;
  162. var arr = [];
  163. for (var i = 0; i < items.length; i++) {
  164. var obj = new rationItemModel(items[i]);
  165. obj.ID = (maxId - (items.length - 1) + i);
  166. obj.sectionId = sectionId;
  167. obj.rationRepId = rationLibId;
  168. arr.push(obj);
  169. }
  170. rationItemModel.collection.insert(arr, null, function(err, docs){
  171. if (err) {
  172. callback(true, "Fail to save", false);
  173. } else {
  174. callback(false, "Save successfully", docs);
  175. }
  176. })
  177. });
  178. } else {
  179. callback(true, "Source error!", false);
  180. }
  181. };
  182. rationItemDAO.prototype.updateRationItems = function(rationLibId, sectionId, items,callback){
  183. var functions = [];
  184. for (var i=0; i < items.length; i++) {
  185. functions.push((function(doc) {
  186. return function(cb) {
  187. var filter = {};
  188. if (doc.ID) {
  189. filter.ID = doc.ID;
  190. } else {
  191. filter.sectionId = sectionId;
  192. if (rationLibId) filter.rationRepId = rationLibId;
  193. filter.code = doc.code;
  194. }
  195. rationItemModel.update(filter, doc, cb);
  196. };
  197. })(items[i]));
  198. }
  199. async.parallel(functions, function(err, results) {
  200. callback(err, results);
  201. });
  202. };
  203. module.exports = new rationItemDAO()