mix_ratio_model.js 2.0 KB

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