unit_price_file_model.js 3.8 KB

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