unit_price_model.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /**
  2. * 单价业务模型
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/6/30
  6. * @version
  7. */
  8. import BaseModel from "../../common/base/base_model"
  9. import GLJTypeConst from "../../common/const/glj_type_const"
  10. import CounterModel from "./counter_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. * @param {Number} gljCount
  70. * @return {Promise} 返回数据以及是否新增
  71. */
  72. async addUnitPrice(data, unitPriceFileId, gljCount = 0) {
  73. if (data.code === undefined || data.project_id === undefined || data.name === undefined
  74. || data.market_price === undefined) {
  75. return [null, false];
  76. }
  77. // 先查找是否有同code的单价记录
  78. let unitPriceData = await this.findDataByCondition({code: data.code, unit_price_file_id: unitPriceFileId}, null, false);
  79. // 如果有记录,判断是否存在一样的市场单价,有则直接返回数据
  80. data.market_price = parseFloat(data.market_price);
  81. let unitPriceIndex = this.isPriceIncluded(unitPriceData, data.market_price);
  82. if (unitPriceData && unitPriceIndex >= 0) {
  83. return [unitPriceData[unitPriceIndex], false];
  84. }
  85. // 如果不存在基价单价,则在数据源中获取
  86. if (data.base_price === undefined) {
  87. let firstUnitPrice = unitPriceData[0] !== undefined ? unitPriceData[0] : [];
  88. data.base_price = firstUnitPrice.base_price !== undefined ? firstUnitPrice.base_price : 0;
  89. data.type = firstUnitPrice.type !== undefined ? firstUnitPrice.type : 0;
  90. }
  91. // 更改名称
  92. if (gljCount > 0) {
  93. let regular = /\(\d+\)/;
  94. let changeString = '(' + gljCount + ')';
  95. data.name = regular.test(data.name) ? data.name.replace(regular, changeString) :
  96. data.name + changeString;
  97. }
  98. let insertData = {
  99. code: data.code,
  100. base_price: data.base_price,
  101. market_price: data.market_price,
  102. unit_price_file_id: unitPriceFileId,
  103. name: data.name,
  104. type: data.type,
  105. short_name: data.shortName !== undefined ? data.shortName : ''
  106. };
  107. let addPriceResult = await this.add(insertData);
  108. return [addPriceResult, true];
  109. }
  110. /**
  111. * 新增记录
  112. *
  113. * @param {object} data
  114. * @return {Promise}
  115. */
  116. async add(data) {
  117. let counterModel = new CounterModel();
  118. if (data instanceof Array) {
  119. // 如果是批量新增
  120. for(let tmp in data) {
  121. data[tmp].id = await counterModel.getId(collectionName);
  122. }
  123. } else {
  124. data.id = await counterModel.getId(collectionName);
  125. }
  126. this.setScene('add');
  127. return this.db.model.create(data);
  128. }
  129. /**
  130. * 判断数据中是否包含某个市场价格的记录
  131. *
  132. * @param {Array} data
  133. * @param {Number} price
  134. * @return {Number}
  135. */
  136. isPriceIncluded(data, price) {
  137. let index = -1;
  138. if (data.length <= 0) {
  139. return index;
  140. }
  141. for(let tmp in data) {
  142. if (data[tmp].market_price === price) {
  143. index = tmp;
  144. break;
  145. }
  146. }
  147. return index;
  148. }
  149. /**
  150. * 更新市场单价
  151. *
  152. * @param {Object} condition
  153. * @param {Object} updateData
  154. * @param {String} extend
  155. * @return {Promise}
  156. */
  157. async updatePrice(condition, updateData, extend = '') {
  158. if (Object.keys(condition).length <= 0 || Object.keys(updateData).length <= 0) {
  159. return false;
  160. }
  161. // 首先查找相应的数据判断工料机类型
  162. let unitPriceData = await this.findDataByCondition(condition);
  163. if (!unitPriceData) {
  164. throw '找不到对应的单价数据';
  165. }
  166. // 基价单价的计算
  167. switch (unitPriceData.type) {
  168. // 主材、设备自动赋值基价单价=市场单价
  169. case GLJTypeConst.MAIN_MATERIAL:
  170. case GLJTypeConst.EQUIPMENT:
  171. updateData.base_price = updateData.market_price;
  172. break;
  173. }
  174. // 额外更新数据
  175. if (extend !== '') {
  176. extend = JSON.parse(extend);
  177. for (let code in extend) {
  178. let extendUpdateData = {
  179. market_price: extend[code].market_price,
  180. };
  181. let tmpCondition = {
  182. unit_price_file_id: unitPriceData.unit_price_file_id,
  183. code: code
  184. };
  185. let extendResult = await this.db.update(tmpCondition, extendUpdateData);
  186. if (!extendResult) {
  187. throw '更新额外数据,编码为' + code + '的数据失败!';
  188. }
  189. }
  190. }
  191. let result = await this.db.update(condition, updateData);
  192. return result.ok !== undefined && result.ok === 1;
  193. }
  194. /**
  195. * 复制单价文件数据
  196. *
  197. * @param {Number} currentUnitPriceId
  198. * @param {Number} changeUnitPriceId
  199. * @return {Promise}
  200. */
  201. async copyNotExist(currentUnitPriceId, changeUnitPriceId) {
  202. let result = false;
  203. // 首先查找原单价文件id下的数据
  204. let currentUnitList = await this.findDataByCondition({unit_price_file_id: currentUnitPriceId}, null, false);
  205. if (currentUnitList === null) {
  206. return result;
  207. }
  208. // 过滤mongoose格式
  209. currentUnitList = JSON.stringify(currentUnitList);
  210. currentUnitList = JSON.parse(currentUnitList);
  211. let codeList = [];
  212. for (let tmp of currentUnitList) {
  213. if (codeList.indexOf(tmp.code) >= 0) {
  214. continue;
  215. }
  216. codeList.push(tmp.code);
  217. }
  218. // 查找即将更替的单价文件是否存在对应的工料机数据
  219. let condition = {unit_price_file_id: changeUnitPriceId, code: {"$in": codeList}};
  220. let targetUnitList = await this.findDataByCondition(condition, null, false, 'code');
  221. // 如果没有重叠的数据则原有的数据都复制一份
  222. let insertData = [];
  223. for (let tmp of currentUnitList) {
  224. if (targetUnitList !== null && targetUnitList[tmp.code] !== undefined) {
  225. continue;
  226. }
  227. // 删除原有id信息
  228. delete tmp._id;
  229. delete tmp.id;
  230. tmp.unit_price_file_id = changeUnitPriceId;
  231. insertData.push(tmp);
  232. }
  233. return insertData.length > 0 ? this.add(currentUnitList) : true;
  234. }
  235. }
  236. export default UnitPriceModel;