bills_template_model.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * Created by Mai on 2017/4/14.
  3. * 清单模板,新建项目使用
  4. */
  5. import BaseModel from "../../common/base/base_model";
  6. import BillsTemplateSchema from "./schemas/bills_template";
  7. class BillsTemplateModel extends BaseModel {
  8. /**
  9. * 构造函数
  10. *
  11. * @return {void}
  12. */
  13. constructor() {
  14. let parent = super();
  15. parent.model = BillsTemplateSchema;
  16. parent.init();
  17. }
  18. /**
  19. * 获取计价类别对应的清单模板
  20. * @param valuationId
  21. * @returns {*}
  22. */
  23. async getTemplateData (valuationId) {
  24. // 筛选字段
  25. let field = {_id: 1, valuationId: 1, ID: 1, ParentID: 1, NextSiblingID: 1, code: 1, name: 1, unit: 1};
  26. let data = await this.findDataByCondition({valuationId: valuationId}, field, false);
  27. return data === null ? [] : data;
  28. }
  29. /**
  30. * 新建项目时,获取计价类别对应的清单模板
  31. * @param valuationId
  32. * @returns {*}
  33. */
  34. async getTemplateDataForNewProj (valuationId) {
  35. // 筛选字段
  36. let field = {ID: 1, ParentID: 1, NextSiblingID: 1, code: 1, name: 1, unit: 1};
  37. let data = await this.findDataByCondition({valuationId: valuationId}, field, false);
  38. return data === null ? [] : data;
  39. }
  40. async updateTemplate (valuationId, datas) {
  41. try {
  42. for (let data of datas) {
  43. data.valuationId = valuationId;
  44. let condition = {valuationId: valuationId, ID: data.data.ID}, result;
  45. if (data.updateType === 'update') {
  46. result = await this.db.update(condition, data.data);
  47. } else if (data.updateType === 'new') {
  48. result = await this.db.create(data.data);
  49. } else if (data.updateType === 'delete') {
  50. result = await this.db.delete(condition);
  51. }
  52. if (result === undefined || result.ok ===undefined || !result.ok) {
  53. throw '更新数据错误';
  54. }
  55. }
  56. return true;
  57. } catch (error) {
  58. console.log(error);
  59. }
  60. }
  61. };
  62. export default BillsTemplateModel;