unit_price_model.js 6.0 KB

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