|
@@ -13,6 +13,22 @@ import {default as UnitPriceSchema, collectionName as collectionName} from "./sc
|
|
|
class UnitPriceModel extends BaseModel {
|
|
|
|
|
|
/**
|
|
|
+ * 自动赋值的工料机类型集
|
|
|
+ * (主材、设备)
|
|
|
+ *
|
|
|
+ * @var {Array}
|
|
|
+ */
|
|
|
+ static autoChangeGLJType = [10, 11];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 触发计算混凝土、砂浆、配合比、机械的市场单价
|
|
|
+ * (人工、材料(普通材料))
|
|
|
+ *
|
|
|
+ * @var {Array}
|
|
|
+ */
|
|
|
+ static triggerCalculateGLJType = [2, 5];
|
|
|
+
|
|
|
+ /**
|
|
|
* 构造函数
|
|
|
*
|
|
|
* @return {void}
|
|
@@ -70,18 +86,18 @@ class UnitPriceModel extends BaseModel {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 更新单价数据(定额中修改价格时调用)
|
|
|
+ * 新增单价数据
|
|
|
*
|
|
|
* @param {Object} data
|
|
|
* @param {Number} unitPriceFileId
|
|
|
- * @return {Promise}
|
|
|
+ * @param {Number} gljCount
|
|
|
+ * @return {Promise} 返回数据以及是否新增
|
|
|
*/
|
|
|
- async updateUnitPrice(data, unitPriceFileId) {
|
|
|
+ async addUnitPrice(data, unitPriceFileId, gljCount = 0) {
|
|
|
if (data.code === undefined || data.project_id === undefined || data.name === undefined
|
|
|
|| data.market_price === undefined) {
|
|
|
- return null;
|
|
|
+ return [null, false];
|
|
|
}
|
|
|
- let marketPrice = data.market_price !== undefined ? data.market_price : data.base_price;
|
|
|
|
|
|
// 先查找是否有同code的单价记录 @todo 后续可能会加入单位这个字段进一步确定唯一性
|
|
|
let unitPriceData = await this.findDataByCondition({code: data.code, unit_price_file_id: unitPriceFileId}, null, false);
|
|
@@ -89,7 +105,7 @@ class UnitPriceModel extends BaseModel {
|
|
|
// 如果有记录,判断是否存在一样的市场单价,有则直接返回数据
|
|
|
let unitPriceIndex = this.isPriceIncluded(unitPriceData, data.market_price);
|
|
|
if (unitPriceData && unitPriceIndex >= 0) {
|
|
|
- return unitPriceData[unitPriceIndex];
|
|
|
+ return [unitPriceData[unitPriceIndex], false];
|
|
|
}
|
|
|
|
|
|
// 如果不存在基价单价,则在数据源中获取
|
|
@@ -100,47 +116,26 @@ class UnitPriceModel extends BaseModel {
|
|
|
data.unit = firstUnitPrice.unit !== undefined ? firstUnitPrice.unit : 0;
|
|
|
}
|
|
|
|
|
|
+ // 更改名称
|
|
|
+ if (gljCount > 0) {
|
|
|
+ let regular = /\(\d\)/;
|
|
|
+ let changeString = '(' + gljCount + ')';
|
|
|
+ data.name = regular.test(data.name) ? data.name.replace(regular, changeString) :
|
|
|
+ data.name + changeString;
|
|
|
+ }
|
|
|
+
|
|
|
let insertData = {
|
|
|
code: data.code,
|
|
|
base_price: data.base_price,
|
|
|
- market_price: marketPrice,
|
|
|
+ market_price: data.market_price,
|
|
|
unit_price_file_id: unitPriceFileId,
|
|
|
name: data.name,
|
|
|
type: data.type,
|
|
|
unit: data.unit
|
|
|
};
|
|
|
|
|
|
- // 统计当前同code的数量
|
|
|
- let sameCount = await this.count({code: data.code});
|
|
|
-
|
|
|
- if (sameCount > 0) {
|
|
|
- // 如果存在有别的同code的数据,则新增一条项目工料机,并更改name
|
|
|
- let regular = /\(\d\)/;
|
|
|
- let changeString = '(' + sameCount + ')';
|
|
|
- insertData.name = regular.test(insertData.name) ? insertData.name.replace(regular, changeString) :
|
|
|
- insertData.name + changeString;
|
|
|
-
|
|
|
- // 然后再插入一条项目工料机数据
|
|
|
- let gljListModel = new GLJListModel();
|
|
|
- // 首先先查找原有数据
|
|
|
- let originalData = await gljListModel.findDataByCondition({code: data.code, project_id: data.project_id}, {_id: 0});
|
|
|
-
|
|
|
- // 查不到数据直接抛出错误
|
|
|
- if(!originalData) {
|
|
|
- throw '没有找到code为:' + data.code + '的数据';
|
|
|
- }
|
|
|
-
|
|
|
- // 新增一条新name的项目工料机
|
|
|
- originalData.name = insertData.name;
|
|
|
- // 这里由于查出来的数据带有隐藏属性,所以先用json转一下
|
|
|
- originalData = JSON.stringify(originalData);
|
|
|
- let addGLJResult = await gljListModel.add(JSON.parse(originalData));
|
|
|
- if (!addGLJResult) {
|
|
|
- throw '新增工料机数据失败!';
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return this.add(insertData);
|
|
|
+ let addPriceResult = await this.add(insertData);
|
|
|
+ return [addPriceResult, true];
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -180,6 +175,33 @@ class UnitPriceModel extends BaseModel {
|
|
|
return index;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 更新市场单价
|
|
|
+ *
|
|
|
+ * @param {Number} id
|
|
|
+ * @param {Object} updateData
|
|
|
+ * @return {Promise}
|
|
|
+ */
|
|
|
+ async updatePriceById(id, updateData) {
|
|
|
+ id = parseInt(id);
|
|
|
+ if (isNaN(id) || id <= 0 || Object.keys(updateData).length <= 0) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ // 首先查找相应的数据判断工料机类型
|
|
|
+ let unitPriceData = this.findDataByCondition({id: id});
|
|
|
+ if (!unitPriceData) {
|
|
|
+ throw '找不到对应的单价数据';
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果是主材、设备自动赋值基价单价=市场单价、调整基价=市场单价
|
|
|
+ if (UnitPriceModel.autoChangeGLJType.indexOf(unitPriceData.type) >= 0) {
|
|
|
+ updateData.base_price = updateData.market_price;
|
|
|
+ }
|
|
|
+
|
|
|
+ let result = await this.updateById(id, updateData);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
export default UnitPriceModel;
|