unit_price_model.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * 单价业务模型
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/6/30
  6. * @version
  7. */
  8. import BaseModel from "../../common/base/base_model"
  9. import CounterModel from "./counter_model"
  10. import GLJListModel from "./glj_list_model";
  11. import {default as UnitPriceSchema, collectionName as collectionName} from "./schemas/unit_price";
  12. class UnitPriceModel extends BaseModel {
  13. /**
  14. * 构造函数
  15. *
  16. * @return {void}
  17. */
  18. constructor() {
  19. let parent = super();
  20. parent.model = UnitPriceSchema;
  21. parent.init();
  22. }
  23. /**
  24. * 根据单价文件id获取单价数据
  25. *
  26. * @param {Number} fileId
  27. * @return {Promise}
  28. */
  29. async getDataByFileId(fileId) {
  30. fileId = parseInt(fileId);
  31. if (isNaN(fileId) || fileId <= 0) {
  32. return null;
  33. }
  34. let unitPriceList = await this.db.model.find({unit_price_file_id: fileId});
  35. if (unitPriceList.length <= 0) {
  36. return null;
  37. }
  38. // 整理数据
  39. let result = {};
  40. for(let tmp of unitPriceList) {
  41. result[tmp.code + tmp.name] = tmp;
  42. }
  43. return result;
  44. }
  45. /**
  46. * 设置场景
  47. *
  48. * @param {string} scene
  49. * @return {void}
  50. */
  51. setScene(scene = '') {
  52. switch (scene) {
  53. // 新增数据的验证规则
  54. case 'add':
  55. this.model.schema.path('base_price').required(true);
  56. this.model.schema.path('market_price').required(true);
  57. this.model.schema.path('name').required(true);
  58. this.model.schema.path('code').required(true);
  59. this.model.schema.path('unit').required(true);
  60. this.model.schema.path('type').required(true);
  61. this.model.schema.path('unit_price_file_id').required(true);
  62. }
  63. }
  64. /**
  65. * 更新单价数据(定额中修改价格时调用)
  66. *
  67. * @param {Object} data
  68. * @param {Number} unitPriceFileId
  69. * @return {Promise}
  70. */
  71. async updateUnitPrice(data, unitPriceFileId) {
  72. if (data.code === undefined || data.project_id === undefined || data.name === undefined
  73. || data.market_price === undefined) {
  74. return null;
  75. }
  76. let marketPrice = data.market_price !== undefined ? data.market_price : data.base_price;
  77. // 先查找是否有同code的单价记录 @todo 后续可能会加入单位这个字段进一步确定唯一性
  78. let unitPriceData = await this.findDataByCondition({code: data.code, unit_price_file_id: unitPriceFileId}, null, false);
  79. // 如果有记录,判断是否存在一样的市场单价,有则直接返回数据
  80. let unitPriceIndex = this.isPriceIncluded(unitPriceData, data.market_price);
  81. if (unitPriceData && unitPriceIndex > 0) {
  82. return unitPriceData[unitPriceIndex];
  83. }
  84. // 如果不存在基价单价,则在数据源中获取
  85. if (data.base_price === undefined) {
  86. let firstUnitPrice = unitPriceData[0] !== undefined ? unitPriceData[0] : [];
  87. data.base_price = firstUnitPrice.base_price !== undefined ? firstUnitPrice.base_price : 0;
  88. data.type = firstUnitPrice.type !== undefined ? firstUnitPrice.type : 0;
  89. data.unit = firstUnitPrice.unit !== undefined ? firstUnitPrice.unit : 0;
  90. }
  91. let insertData = {
  92. code: data.code,
  93. base_price: data.base_price,
  94. market_price: marketPrice,
  95. unit_price_file_id: unitPriceFileId,
  96. name: data.name,
  97. type: data.type,
  98. unit: data.unit
  99. };
  100. // 统计当前同code的数量
  101. let sameCount = await this.count({code: data.code});
  102. if (sameCount > 0) {
  103. // 如果存在有别的同code的数据,则新增一条项目工料机,并更改name
  104. let regular = /\(\d\)/;
  105. let changeString = '(' + sameCount + ')';
  106. insertData.name = regular.test(insertData.name) ? insertData.name.replace(regular, changeString) :
  107. insertData.name + changeString;
  108. // 然后再插入一条项目工料机数据
  109. let gljListModel = new GLJListModel();
  110. // 首先先查找原有数据
  111. let originalData = await gljListModel.findDataByCondition({code: data.code, project_id: data.project_id}, {_id: 0});
  112. // 查不到数据直接抛出错误
  113. if(!originalData) {
  114. throw '没有找到code为:' + data.code + '的数据';
  115. }
  116. // 新增一条新name的项目工料机
  117. originalData.name = insertData.name;
  118. // 这里由于查出来的数据带有隐藏属性,所以先用json转一下
  119. originalData = JSON.stringify(originalData);
  120. let addGLJResult = await gljListModel.add(JSON.parse(originalData));
  121. if (!addGLJResult) {
  122. throw '新增工料机数据失败!';
  123. }
  124. }
  125. return this.add(insertData);
  126. }
  127. /**
  128. * 新增记录
  129. *
  130. * @param {object} data
  131. * @return {Promise}
  132. */
  133. async add(data) {
  134. let counterModel = new CounterModel();
  135. let unitPriceId = await counterModel.getId(collectionName);
  136. data.id = unitPriceId;
  137. this.setScene('add');
  138. return this.db.model.create(data);
  139. }
  140. /**
  141. * 判断数据中是否包含某个市场价格的记录
  142. *
  143. * @param {Array} data
  144. * @param {Number} price
  145. * @return {Number}
  146. */
  147. isPriceIncluded(data, price) {
  148. let index = -1;
  149. if (data.length <= 0) {
  150. return index;
  151. }
  152. for(let tmp in data) {
  153. if (data[tmp].market_price === price) {
  154. index = tmp;
  155. break;
  156. }
  157. }
  158. return index;
  159. }
  160. }
  161. export default UnitPriceModel;