ration_item.js 8.2 KB

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