volume_price_model.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /**
  2. * Created by Mai on 2017/7/25.
  3. */
  4. let consts = require('../../main/models/project_consts');
  5. let commonConsts = consts.commonConst;
  6. let mongoose = require('mongoose');
  7. let volumePrice = require("./volume_price_schema");
  8. let async = require("async");
  9. let baseModel = require('../../main/models/base_model');
  10. class volumePriceModel extends baseModel {
  11. constructor (name) {
  12. super(volumePrice);
  13. this.collectionName = name;
  14. };
  15. getData (projectID, callback) {
  16. volumePrice.find({'$or': [
  17. {
  18. projectID: projectID,
  19. deleteInfo: null
  20. }, {
  21. projectID: projectID,
  22. 'deleteInfo.deleted': {$in: [null, false]}
  23. }
  24. ]},(err,datas)=>{
  25. if(err){
  26. callback(1, '', null);
  27. }else {
  28. // console.log(JSON.stringify(datas));
  29. callback(0, consts.projectConst.VOLUMEPRICE, datas);
  30. }
  31. })
  32. };
  33. save (user_id, datas, callback) {
  34. let funs = [];
  35. function saveOne(doc) {
  36. // console.log('-----------------------------------------------------------------------------' + JSON.stringify(doc));
  37. return function (cb) {
  38. switch (doc.updateType) {
  39. case commonConsts.UT_UPDATE:
  40. volumePrice.update({projectID: doc.updateData.projectID, ID: doc.updateData.ID}, doc.updateData, cb);
  41. break;
  42. case commonConsts.UT_CREATE:
  43. volumePrice.create(doc.updateData, cb);
  44. break;
  45. case commonConsts.UT_DELETE:
  46. doc.updateData.deleteInfo = {deleted: true, deleteDateTime: new Date(), deleteBy: user_id};
  47. volumePrice.update({projectID: doc.updateData.projectID, ID: doc.updateData.ID}, doc.updateData, cb);
  48. break;
  49. }
  50. }
  51. }
  52. for (let data of datas){
  53. funs.push(saveOne(data));
  54. }
  55. async.parallel(funs, callback);
  56. };
  57. };
  58. module.exports = new volumePriceModel(consts.projectConst.VOLUMEPRICE);