unit_price_file_model.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * 单价文件显示业务模型(用于选择单价文件操作)
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/5
  6. * @version
  7. */
  8. import BaseModel from "../../common/base/base_model";
  9. import CounterModel from "./counter_model";
  10. const ProjectModel = require('../../pm/models/project_model').project;
  11. import {default as UnitPriceFileSchema, collectionName as collectionName} from "./schemas/unit_price_file";
  12. class UnitPriceFileModel extends BaseModel {
  13. /**
  14. * 构造函数
  15. *
  16. * @return {void}
  17. */
  18. constructor() {
  19. let parent = super();
  20. parent.model = UnitPriceFileSchema;
  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('name').required(true);
  34. this.model.schema.path('project_id').required(true);
  35. break;
  36. }
  37. }
  38. /**
  39. * 新增单价文件
  40. *
  41. * @param {object} data
  42. * @return {Promise}
  43. */
  44. async add(data) {
  45. let result = false;
  46. try {
  47. if (Object.keys(data).length <= 0) {
  48. throw '数据为空';
  49. }
  50. result = await this.db.create(data);
  51. } catch (error) {
  52. result = false;
  53. }
  54. return result;
  55. }
  56. /**
  57. * 根据项目id获取单价文件数据
  58. *
  59. * @param {Number} projectId
  60. * @return {Promise}
  61. */
  62. async getDataByProject(projectId) {
  63. let result = null;
  64. projectId = parseInt(projectId);
  65. try {
  66. if (isNaN(projectId) || projectId <= 0) {
  67. throw '标段id有误';
  68. }
  69. let unitPriceFileId =await ProjectModel.getUnitPriceFileId(projectId);
  70. if (unitPriceFileId <= 0) {
  71. throw '没有对应的单价文件';
  72. }
  73. result = await this.findDataByCondition({id: unitPriceFileId});
  74. } catch (error) {
  75. console.log('error:' + error);
  76. throw '没有对应的单价文件 '
  77. }
  78. return result;
  79. }
  80. /**
  81. * 新增单条工料机数据
  82. *
  83. * @param {object} data
  84. * @return {Promise}
  85. */
  86. async add(data) {
  87. if (Object.keys(data).length <= 0) {
  88. throw '新增数据为空';
  89. }
  90. let counterModel = new CounterModel();
  91. data.id = await counterModel.getId(collectionName);
  92. this.setScene('add');
  93. let result = await this.db.create(data);
  94. return result;
  95. }
  96. /**
  97. * 根据单位工程获取对应单价文件
  98. *
  99. * @param {Array} idList
  100. * @return {Promise|Array}
  101. */
  102. async getDataByTenderId(idList) {
  103. let result = [];
  104. if (idList.length <= 0) {
  105. return result;
  106. }
  107. let condition = {project_id: {$in: idList},deleteInfo:null};
  108. result = await this.findDataByCondition(condition, null, false);
  109. return result;
  110. }
  111. /**
  112. * 根据建设项目id获取单价文件数据
  113. *
  114. * @param {Number} root_project_id
  115. * @return {Promise}
  116. */
  117. async getDataByRootProject(root_project_id){
  118. let condition = {root_project_id: root_project_id, deleteInfo: null};
  119. return await this.findDataByCondition(condition, null, false);
  120. }
  121. /**
  122. * 根据用户获取对应被删除的单价文件
  123. *
  124. * @param {String} userID
  125. * @return {Promise}
  126. */
  127. async getGCUnitPriceFiles(userID){
  128. let condition = {user_id: userID, 'deleteInfo.deleted': true};
  129. let result = await this.findDataByCondition(condition, null, false);
  130. return result;
  131. }
  132. }
  133. export default UnitPriceFileModel;