unit_price_file_model.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. * 新增单价文件
  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 this.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. async getUnitPriceFileId(projectId){
  82. let result = 0;
  83. let startTime = +new Date();
  84. let projectData = await Projects.find({ID: projectId},['property.unitPriceFile']);
  85. if (projectData === null) {
  86. return result;
  87. }
  88. let endTime = +new Date();
  89. console.log("取单价文件列表id时间-----"+(endTime - startTime));
  90. projectData = projectData[0];
  91. result = projectData.property.unitPriceFile !== undefined ? projectData.property.unitPriceFile.id : 0;
  92. return result;
  93. }
  94. /**
  95. * 新增单条工料机数据
  96. *
  97. * @param {object} data
  98. * @return {Promise}
  99. */
  100. async add(data) {
  101. if (Object.keys(data).length <= 0) {
  102. throw '新增数据为空';
  103. }
  104. let counterModel = new CounterModel();
  105. data.id = await counterModel.getId(collectionName);
  106. this.setScene('add');
  107. let result = await this.db.create(data);
  108. return result;
  109. }
  110. /**
  111. * 根据单位工程获取对应单价文件
  112. *
  113. * @param {Array} idList
  114. * @return {Promise|Array}
  115. */
  116. async getDataByTenderId(idList) {
  117. let result = [];
  118. if (idList.length <= 0) {
  119. return result;
  120. }
  121. let condition = {project_id: {$in: idList},deleteInfo:null};
  122. result = await this.findDataByCondition(condition, null, false);
  123. return result;
  124. }
  125. /**
  126. * 根据建设项目id获取单价文件数据
  127. *
  128. * @param {Number} root_project_id
  129. * @return {Promise}
  130. */
  131. async getDataByRootProject(root_project_id){
  132. let condition = {root_project_id: root_project_id, deleteInfo: null};
  133. return await this.findDataByCondition(condition, null, false);
  134. }
  135. /**
  136. * 根据用户获取对应被删除的单价文件
  137. *
  138. * @param {String} userID
  139. * @return {Promise}
  140. */
  141. async getGCUnitPriceFiles(userID){
  142. let condition = {user_id: userID, 'deleteInfo.deleted': true, '$or': [{'deleteInfo.completeDeleted': false}, {'deleteInfo.completeDeleted': null}]};
  143. let result = await this.findDataByCondition(condition, null, false);
  144. return result;
  145. }
  146. }
  147. module.exports = UnitPriceFileModel;