unit_price_model.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. * 根据标段获取对应单价数据
  25. *
  26. * @param {Number} tenderId
  27. * @return {Promise}
  28. */
  29. async getDataByTenderId(tenderId) {
  30. tenderId = parseInt(tenderId);
  31. if (isNaN(tenderId) || tenderId <= 0) {
  32. return null;
  33. }
  34. let unitPriceList = await this.db.model.find({tender_id: tenderId});
  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('project_id').required(true);
  56. this.model.schema.path('tender_id').required(true);
  57. this.model.schema.path('base_price').required(true);
  58. this.model.schema.path('market_price').required(true);
  59. this.model.schema.path('name').required(true);
  60. this.model.schema.path('code').required(true);
  61. this.model.schema.path('unit').required(true);
  62. this.model.schema.path('type').required(true);
  63. }
  64. }
  65. /**
  66. * 新增单价文件
  67. *
  68. * @param {Object} data
  69. * @return {Promise}
  70. */
  71. async addData(data) {
  72. let result = false;
  73. try {
  74. // 首先查找是否有相同的记录
  75. let unitPriceData = await this.db.model.findOne({code: data.code, market_price: data.market_price});
  76. if (unitPriceData && unitPriceData.id > 0) {
  77. return true;
  78. }
  79. // 没有则新增数据
  80. result = this.db.model.create(data);
  81. } catch (error) {
  82. result = false;
  83. }
  84. return result;
  85. }
  86. /**
  87. * 更新单价文件(定额中修改价格时调用)
  88. *
  89. * @param {Object} data
  90. * @param {Number} unitPriceTenderId
  91. * @return {Promise}
  92. */
  93. async updateUnitPrice(data, unitPriceTenderId) {
  94. if (data.code === undefined || data.tender_id === undefined || data.name === undefined
  95. || data.market_price === undefined) {
  96. return null;
  97. }
  98. let marketPrice = data.market_price !== undefined ? data.market_price : data.base_price;
  99. // 先查找是否有同code的单价记录 @todo 后续可能会加入单位这个字段进一步确定唯一性
  100. let unitPriceData = await this.findDataByCondition({code: data.code, tender_id: unitPriceTenderId}, null, false);
  101. // 如果有记录,判断是否存在一样的市场单价,有则直接返回数据
  102. if (unitPriceData && this.isPriceIncluded(unitPriceData, data.market_price)) {
  103. return unitPriceData;
  104. }
  105. // 如果不存在基价单价,则在数据源中获取
  106. if (data.base_price === undefined) {
  107. let firstUnitPrice = unitPriceData[0] !== undefined ? unitPriceData[0] : [];
  108. data.base_price = firstUnitPrice.base_price !== undefined ? firstUnitPrice.base_price : 0;
  109. data.type = firstUnitPrice.type !== undefined ? firstUnitPrice.type : 0;
  110. data.unit = firstUnitPrice.unit !== undefined ? firstUnitPrice.unit : 0;
  111. }
  112. let insertData = {
  113. code: data.code,
  114. base_price: data.base_price,
  115. market_price: marketPrice,
  116. project_id: data.project_id,
  117. tender_id: unitPriceTenderId,
  118. name: data.name,
  119. type: data.type,
  120. unit: data.unit
  121. };
  122. // 统计当前同code的数量
  123. let sameCount = await this.count({code: data.code});
  124. if (sameCount > 0) {
  125. // 如果存在有别的同code的数据,则新增一条项目工料机,并更改name
  126. let regular = /\(\d\)/;
  127. let changeString = '(' + sameCount + ')';
  128. insertData.name = regular.test(insertData.name) ? insertData.name.replace(regular, changeString) :
  129. insertData.name + changeString;
  130. // 然后再插入一条项目工料机数据
  131. let gljListModel = new GLJListModel();
  132. // 首先先查找原有数据
  133. let originalData = await gljListModel.findDataByCondition({code: data.code, tender_id: data.tender_id}, {_id: 0});
  134. // 查不到数据直接抛出错误
  135. if(!originalData) {
  136. throw '没有找到code为:' + data.code + '的数据';
  137. }
  138. // 新增一条新name的项目工料机
  139. originalData.name = insertData.name;
  140. // 这里由于查出来的数据带有隐藏属性,所以先用json转一下
  141. originalData = JSON.stringify(originalData);
  142. let addGLJResult = await gljListModel.add(JSON.parse(originalData));
  143. if (!addGLJResult) {
  144. throw '新增工料机数据失败!';
  145. }
  146. }
  147. return this.add(insertData);
  148. }
  149. /**
  150. * 新增记录
  151. *
  152. * @param {object} data
  153. * @return {Promise}
  154. */
  155. async add(data) {
  156. let counterModel = new CounterModel();
  157. let unitPriceId = await counterModel.getId(collectionName);
  158. data.id = unitPriceId;
  159. this.setScene('add');
  160. return this.db.model.create(data);
  161. }
  162. /**
  163. * 判断数据中是否包含某个市场价格的记录
  164. *
  165. * @param {Array} data
  166. * @param {Number} price
  167. * @return boolean
  168. */
  169. isPriceIncluded(data, price) {
  170. let result = false;
  171. if (data.length <= 0) {
  172. return result;
  173. }
  174. for(let tmp of data) {
  175. if (tmp.market_price === price) {
  176. return true;
  177. }
  178. }
  179. return result;
  180. }
  181. }
  182. export default UnitPriceModel;