mix_ratio_model.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * 配合比业务模型
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/12
  6. * @version
  7. */
  8. import mongoose from "mongoose";
  9. import BaseModel from "../../common/base/base_model";
  10. import CounterModel from "./counter_model"
  11. let collectionName = 'mix_ratio';
  12. class MixRatioModel extends BaseModel {
  13. /**
  14. * 构造函数
  15. *
  16. * @return {void}
  17. */
  18. constructor() {
  19. let parent = super();
  20. parent.model = mongoose.model(collectionName);
  21. parent.init();
  22. }
  23. /**
  24. * 设置场景
  25. *
  26. * @param {string} scene
  27. * @return {void}
  28. */
  29. setScene(scene = '') {
  30. switch (scene) {
  31. // 新增数据的验证规则
  32. case 'add':
  33. this.model.schema.path('glj_id').required(true);
  34. this.model.schema.path('consumption').required(true);
  35. this.model.schema.path('unit_price_file_id').required(true);
  36. this.model.schema.path('connect_key').required(true);
  37. break;
  38. }
  39. }
  40. /**
  41. * 新增配合比数据(自动判断是否存在)
  42. *
  43. * @param {object} data
  44. * @return {Promise}
  45. */
  46. async add(data) {
  47. let counterModel = new CounterModel();
  48. if (data instanceof Array) {
  49. for(let tmp in data) {
  50. data[tmp].id = await counterModel.getId(collectionName);
  51. }
  52. } else {
  53. data.id = await counterModel.getId(collectionName);
  54. }
  55. this.setScene('add');
  56. return this.db.model.create(data);
  57. }
  58. /**
  59. * 更新数据
  60. *
  61. * @param {Number} id
  62. * @param {Object} updateData
  63. * @return {Promise}
  64. */
  65. async updateById(id, updateData) {
  66. id = parseInt(id);
  67. if (isNaN(id) || id <= 0 || Object.keys(updateData).length <= 0) {
  68. return false;
  69. }
  70. let result = await this.db.update({id: id}, updateData);
  71. return result.ok !== undefined && result.ok === 1;
  72. }
  73. }
  74. export default MixRatioModel;