unit_price_file_model.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /**
  2. * 单价文件显示业务模型(用于选择单价文件操作)
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/7/5
  6. * @version
  7. */
  8. const mongoose = require("mongoose");
  9. const BaseModel = require("../../common/base/base_model");
  10. const CounterModel = require("./counter_model");
  11. let collectionName = 'unit_price_file';
  12. let Projects = mongoose.model('projects');
  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. * 根据项目id获取单价文件数据
  41. *
  42. * @param {Number} projectId
  43. * @return {Promise}
  44. */
  45. async getDataByProject(projectId) {
  46. let result = null;
  47. projectId = parseInt(projectId);
  48. try {
  49. if (isNaN(projectId) || projectId <= 0) {
  50. throw '标段id有误';
  51. }
  52. let unitPriceFileId =await this.getUnitPriceFileId(projectId);
  53. if (unitPriceFileId <= 0) {
  54. throw '没有对应的单价文件';
  55. }
  56. result = await this.findDataByCondition({id: unitPriceFileId});
  57. } catch (error) {
  58. console.log('error:' + error);
  59. throw '没有对应的单价文件 '
  60. }
  61. return result;
  62. }
  63. async getUnitPriceFileId(projectId){
  64. let result = 0;
  65. let startTime = +new Date();
  66. let projectData = await Projects.find({ID: projectId},['property.unitPriceFile']);
  67. if (projectData === null) {
  68. return result;
  69. }
  70. let endTime = +new Date();
  71. console.log("取单价文件列表id时间-----"+(endTime - startTime));
  72. projectData = projectData[0];
  73. result = projectData.property.unitPriceFile !== undefined ? projectData.property.unitPriceFile.id : 0;
  74. return result;
  75. }
  76. /**
  77. * 新增单价文件
  78. *
  79. * @param {object} data
  80. * @return {Promise}
  81. */
  82. async add(data) {
  83. if (Object.keys(data).length <= 0) {
  84. throw '新增数据为空';
  85. }
  86. let counterModel = new CounterModel();
  87. data.id = await counterModel.getId(collectionName);
  88. let result = await this.db.create(data);
  89. return result;
  90. }
  91. /**
  92. * 根据单位工程获取对应单价文件
  93. *
  94. * @param {Array} idList
  95. * @return {Promise|Array}
  96. */
  97. async getDataByTenderId(idList) {
  98. let result = [];
  99. if (idList.length <= 0) {
  100. return result;
  101. }
  102. let condition = {project_id: {$in: idList},deleteInfo:null};
  103. result = await this.findDataByCondition(condition, null, false);
  104. return result;
  105. }
  106. /**
  107. * 根据建设项目id获取单价文件数据
  108. *
  109. * @param {Number} root_project_id
  110. * @return {Promise}
  111. */
  112. async getDataByRootProject(root_project_id){
  113. let condition = {root_project_id: root_project_id, deleteInfo: null};
  114. return await this.findDataByCondition(condition, null, false);
  115. }
  116. /**
  117. * 根据用户获取对应被删除的单价文件
  118. *
  119. * @param {String} userID
  120. * @return {Promise}
  121. */
  122. async getGCUnitPriceFiles(userID){
  123. let condition = {user_id: userID, 'deleteInfo.deleted': true, '$or': [{'deleteInfo.completeDeleted': false}, {'deleteInfo.completeDeleted': null}]};
  124. let result = await this.findDataByCondition(condition, null, false);
  125. return result;
  126. }
  127. }
  128. module.exports = UnitPriceFileModel;