unit_price_model.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 {default as UnitPriceSchema, collectionName as collectionName} from "./schemas/unit_price";
  11. class UnitPriceModel extends BaseModel {
  12. /**
  13. * 构造函数
  14. *
  15. * @return {void}
  16. */
  17. constructor() {
  18. let parent = super();
  19. parent.model = UnitPriceSchema;
  20. parent.init();
  21. }
  22. /**
  23. * 根据单价文件id获取单价数据
  24. *
  25. * @param {Number} fileId
  26. * @return {Promise}
  27. */
  28. async getDataByFileId(fileId) {
  29. fileId = parseInt(fileId);
  30. if (isNaN(fileId) || fileId <= 0) {
  31. return null;
  32. }
  33. let unitPriceList = await this.db.model.find({unit_price_file_id: fileId});
  34. if (unitPriceList.length <= 0) {
  35. return null;
  36. }
  37. // 整理数据
  38. let result = {};
  39. for(let tmp of unitPriceList) {
  40. result[tmp.code + tmp.name] = tmp;
  41. }
  42. return result;
  43. }
  44. /**
  45. * 设置场景
  46. *
  47. * @param {string} scene
  48. * @return {void}
  49. */
  50. setScene(scene = '') {
  51. switch (scene) {
  52. // 新增数据的验证规则
  53. case 'add':
  54. this.model.schema.path('base_price').required(true);
  55. this.model.schema.path('market_price').required(true);
  56. this.model.schema.path('name').required(true);
  57. this.model.schema.path('code').required(true);
  58. this.model.schema.path('unit').required(true);
  59. this.model.schema.path('type').required(true);
  60. this.model.schema.path('unit_price_file_id').required(true);
  61. }
  62. }
  63. /**
  64. * 新增单价数据
  65. *
  66. * @param {Object} data
  67. * @param {Number} unitPriceFileId
  68. * @param {Number} gljCount
  69. * @return {Promise} 返回数据以及是否新增
  70. */
  71. async addUnitPrice(data, unitPriceFileId, gljCount = 0) {
  72. if (data.code === undefined || data.project_id === undefined || data.name === undefined
  73. || data.market_price === undefined) {
  74. return [null, false];
  75. }
  76. // 先查找是否有同code的单价记录 @todo 后续可能会加入单位这个字段进一步确定唯一性
  77. let unitPriceData = await this.findDataByCondition({code: data.code, unit_price_file_id: unitPriceFileId}, null, false);
  78. // 如果有记录,判断是否存在一样的市场单价,有则直接返回数据
  79. let unitPriceIndex = this.isPriceIncluded(unitPriceData, data.market_price);
  80. if (unitPriceData && unitPriceIndex >= 0) {
  81. return [unitPriceData[unitPriceIndex], false];
  82. }
  83. // 如果不存在基价单价,则在数据源中获取
  84. if (data.base_price === undefined) {
  85. let firstUnitPrice = unitPriceData[0] !== undefined ? unitPriceData[0] : [];
  86. data.base_price = firstUnitPrice.base_price !== undefined ? firstUnitPrice.base_price : 0;
  87. data.type = firstUnitPrice.type !== undefined ? firstUnitPrice.type : 0;
  88. data.unit = firstUnitPrice.unit !== undefined ? firstUnitPrice.unit : 0;
  89. }
  90. // 更改名称
  91. if (gljCount > 0) {
  92. let regular = /\(\d\)/;
  93. let changeString = '(' + gljCount + ')';
  94. data.name = regular.test(data.name) ? data.name.replace(regular, changeString) :
  95. data.name + changeString;
  96. }
  97. let insertData = {
  98. code: data.code,
  99. base_price: data.base_price,
  100. market_price: data.market_price,
  101. unit_price_file_id: unitPriceFileId,
  102. name: data.name,
  103. type: data.type,
  104. unit: data.unit
  105. };
  106. let addPriceResult = await this.add(insertData);
  107. return [addPriceResult, true];
  108. }
  109. /**
  110. * 新增记录
  111. *
  112. * @param {object} data
  113. * @return {Promise}
  114. */
  115. async add(data) {
  116. let counterModel = new CounterModel();
  117. let unitPriceId = await counterModel.getId(collectionName);
  118. data.id = unitPriceId;
  119. this.setScene('add');
  120. return this.db.model.create(data);
  121. }
  122. /**
  123. * 判断数据中是否包含某个市场价格的记录
  124. *
  125. * @param {Array} data
  126. * @param {Number} price
  127. * @return {Number}
  128. */
  129. isPriceIncluded(data, price) {
  130. let index = -1;
  131. if (data.length <= 0) {
  132. return index;
  133. }
  134. for(let tmp in data) {
  135. if (data[tmp].market_price === price) {
  136. index = tmp;
  137. break;
  138. }
  139. }
  140. return index;
  141. }
  142. /**
  143. * 更新市场单价
  144. *
  145. * @param {Number} id
  146. * @param {Object} updateData
  147. * @return {Promise}
  148. */
  149. async updatePriceById(id, updateData) {
  150. id = parseInt(id);
  151. if (isNaN(id) || id <= 0 || Object.keys(updateData).length <= 0) {
  152. return false;
  153. }
  154. // 首先查找相应的数据判断工料机类型
  155. let unitPriceData = await this.findDataByCondition({id: id});
  156. if (!unitPriceData) {
  157. throw '找不到对应的单价数据';
  158. }
  159. // 基价单价的计算
  160. switch (unitPriceData.type) {
  161. // 主材、设备自动赋值基价单价=市场单价
  162. case 6:
  163. case 7:
  164. updateData.base_price = updateData.market_price;
  165. break;
  166. }
  167. let result = await this.updateById(id, updateData);
  168. return result;
  169. }
  170. }
  171. export default UnitPriceModel;