unit_price_file_model.js 3.8 KB

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