|
@@ -487,17 +487,56 @@ module.exports = app => {
|
|
|
*/
|
|
|
async batchInsertChild(tenderId, selectId, data) {
|
|
|
const result = { ledger: {}, pos: null };
|
|
|
- if ((tenderId <= 0) || (selectId <= 0)) {
|
|
|
- return result;
|
|
|
- }
|
|
|
+ if ((tenderId <= 0) || (selectId <= 0)) return result;
|
|
|
const selectData = await this.getDataByNodeId(tenderId, selectId);
|
|
|
- if (!selectData) {
|
|
|
- throw '位置数据错误';
|
|
|
- }
|
|
|
+ if (!selectData) throw '位置数据错误';
|
|
|
|
|
|
+ const info = this.ctx.tender.info;
|
|
|
this.transaction = await this.db.beginTransaction();
|
|
|
- const newIds = [];
|
|
|
const lastChild = await this.getLastChildData(tenderId, selectId);
|
|
|
+ // 计算id
|
|
|
+ const maxId = await this._getMaxLid(tenderId);
|
|
|
+ const insertBillsData = [], insertPosData = [], in_time = new Date();
|
|
|
+ const order = lastChild ? lastChild.order : 0;
|
|
|
+ for (let i = 0, iLen = data.length; i < iLen; i++) {
|
|
|
+ // 合并新增数据
|
|
|
+ const qd = {
|
|
|
+ id: this.uuid.v4(),
|
|
|
+ tender_id: tenderId,
|
|
|
+ ledger_id: maxId + i + 1,
|
|
|
+ ledger_pid: selectData.ledger_id,
|
|
|
+ is_leaf: true,
|
|
|
+ order: order + i + 1,
|
|
|
+ level: selectData.level + 1,
|
|
|
+ b_code: data[i].b_code,
|
|
|
+ name: data[i].name,
|
|
|
+ unit: data[i].unit,
|
|
|
+ unit_price: this.ctx.helper.round(data[i].price, info.decimal.up),
|
|
|
+ };
|
|
|
+ qd.full_path = selectData.full_path + '-' + qd.ledger_id;
|
|
|
+ insertBillsData.push(qd);
|
|
|
+ const precision = this.ctx.helper.findPrecision(info.precision, qd.unit);
|
|
|
+ if (data[i].pos.length > 0) {
|
|
|
+ for (const [i, p] of data[i].pos.entries()) {
|
|
|
+ const inD = {
|
|
|
+ id: this.uuid.v4(), tid: tid, lid: bills.id,
|
|
|
+ add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
|
|
|
+ in_time, porder: i + 1,
|
|
|
+ name: p.name, drawing_code: p.drawing_code,
|
|
|
+ };
|
|
|
+ if (d.quantity) {
|
|
|
+ inD.sgfh_qty = this.round(p.quantity, precision.value);
|
|
|
+ inD.quantity = inD.sgfh_qty;
|
|
|
+ qd.sgfh_qty = this.ctx.helper.add(qd.sgfh_qty, inD.sgfh_qty);
|
|
|
+ }
|
|
|
+ insertPosData.push(inD);
|
|
|
+ await transaction.insert(this.tableName, insertDatas);
|
|
|
+ }
|
|
|
+ qd.sgfh_tp = this.ctx.helper.mul(qd.sgfh_qty, qd.unit_price, info.decimal.tp);
|
|
|
+ qd.quantity = qd.sgfh_qty;
|
|
|
+ qd.total_price = qd.sgfh_tp;
|
|
|
+ }
|
|
|
+ }
|
|
|
try {
|
|
|
// 更新父项isLeaf
|
|
|
if (!lastChild) {
|
|
@@ -505,46 +544,15 @@ module.exports = app => {
|
|
|
id: selectData.id,
|
|
|
is_leaf: false,
|
|
|
unit_price: null,
|
|
|
- sgfh_qty: null,
|
|
|
- sgfh_tp: null,
|
|
|
- sjcl_qty: null,
|
|
|
- sjcl_tp: null,
|
|
|
- qtcl_qty: null,
|
|
|
- qtcl_tp: null,
|
|
|
- quantity: null,
|
|
|
- total_price: null,
|
|
|
- deal_qty: null,
|
|
|
- deal_tp: null,
|
|
|
+ sgfh_qty: null, sgfh_tp: null,
|
|
|
+ sjcl_qty: null, sjcl_tp: null,
|
|
|
+ qtcl_qty: null, qtcl_tp: null,
|
|
|
+ quantity: null, total_price: null,
|
|
|
+ deal_qty: null, deal_tp: null,
|
|
|
});
|
|
|
}
|
|
|
- const order = lastChild ? lastChild.order : 0;
|
|
|
- // 计算id
|
|
|
- const maxId = await this._getMaxLid(tenderId);
|
|
|
-
|
|
|
- // 数据库创建新增节点数据
|
|
|
- for (let i = 0, iLen = data.length; i < iLen; i++) {
|
|
|
- // 合并新增数据
|
|
|
- const qd = {
|
|
|
- id: this.uuid.v4(),
|
|
|
- tender_id: tenderId,
|
|
|
- ledger_id: maxId + i + 1,
|
|
|
- ledger_pid: selectData.ledger_id,
|
|
|
- is_leaf: true,
|
|
|
- order: order + i + 1,
|
|
|
- level: selectData.level + 1,
|
|
|
- b_code: data[i].b_code,
|
|
|
- name: data[i].name,
|
|
|
- unit: data[i].unit,
|
|
|
- unit_price: data[i].price,
|
|
|
- };
|
|
|
- qd.full_path = selectData.full_path + '-' + qd.ledger_id;
|
|
|
- const insertResult = await this.transaction.insert(this.tableName, qd);
|
|
|
- newIds.push(qd.id);
|
|
|
- if (data[i].pos.length > 0) {
|
|
|
- await this.ctx.service.pos.insertLedgerPosData(this.transaction, tenderId, qd, data[i].pos);
|
|
|
- await this.calcNode(qd, this.transaction);
|
|
|
- }
|
|
|
- }
|
|
|
+ await this.transaction.insert(this.tableName, insertBillsData);
|
|
|
+ if (insertPosData.length > 0) await this.transaction.insert(this.ctx.service.pos.tableName, insertPosData);
|
|
|
this._cacheMaxLid(tenderId, maxId + data.length);
|
|
|
await this.transaction.commit();
|
|
|
} catch (err) {
|
|
@@ -553,11 +561,9 @@ module.exports = app => {
|
|
|
}
|
|
|
|
|
|
// 查询应返回的结果
|
|
|
- result.ledger.create = await this.getDataByIds(newIds);
|
|
|
- if (!lastChild) {
|
|
|
- result.ledger.update = await this.getDataByIds([selectData.id]);
|
|
|
- }
|
|
|
- result.pos = await this.ctx.service.pos.getPosData({tid: tenderId, lid: newIds });
|
|
|
+ result.ledger.create = insertBillsData;
|
|
|
+ if (!lastChild) result.ledger.update = await this.getDataByIds([selectData.id]);
|
|
|
+ result.pos = insertPosData;
|
|
|
return result;
|
|
|
}
|
|
|
|
|
@@ -570,50 +576,63 @@ module.exports = app => {
|
|
|
*/
|
|
|
async batchInsertNext(tenderId, selectId, data) {
|
|
|
const result = { ledger: {}, pos: null };
|
|
|
- if ((tenderId <= 0) || (selectId <= 0)) {
|
|
|
- return result;
|
|
|
- }
|
|
|
+ if ((tenderId <= 0) || (selectId <= 0)) return result;
|
|
|
const selectData = await this.getDataByNodeId(tenderId, selectId);
|
|
|
- if (!selectData) {
|
|
|
- throw '位置数据错误';
|
|
|
- }
|
|
|
+ if (!selectData) throw '位置数据错误';
|
|
|
const parentData = await this.getDataByNodeId(tenderId, selectData.ledger_pid);
|
|
|
- if (!parentData) {
|
|
|
- throw '位置数据错误';
|
|
|
- }
|
|
|
+ if (!parentData) throw '位置数据错误';
|
|
|
|
|
|
+ const info = this.ctx.tender.info;
|
|
|
this.transaction = await this.db.beginTransaction();
|
|
|
- const newIds = [];
|
|
|
+
|
|
|
+ const insertBillsData = [], insertPosData = [];
|
|
|
+ const maxId = await this._getMaxLid(tenderId);
|
|
|
+ const order = selectData.order;
|
|
|
+ for (let i = 0, iLen = data.length; i < iLen; i++) {
|
|
|
+ // 合并新增数据
|
|
|
+ const qd = {
|
|
|
+ id: this.uuid.v4(),
|
|
|
+ tender_id: tenderId,
|
|
|
+ ledger_id: maxId + i + 1,
|
|
|
+ ledger_pid: selectData.ledger_id,
|
|
|
+ is_leaf: true,
|
|
|
+ order: order + i + 1,
|
|
|
+ level: selectData.level + 1,
|
|
|
+ b_code: data[i].b_code,
|
|
|
+ name: data[i].name,
|
|
|
+ unit: data[i].unit,
|
|
|
+ unit_price: this.ctx.helper.round(data[i].price, info.decimal.up),
|
|
|
+ };
|
|
|
+ qd.full_path = selectData.full_path + '-' + qd.ledger_id;
|
|
|
+ insertBillsData.push(qd);
|
|
|
+ const precision = this.ctx.helper.findPrecision(info.precision, qd.unit);
|
|
|
+ if (data[i].pos.length > 0) {
|
|
|
+ for (const [i, p] of data[i].pos.entries()) {
|
|
|
+ const inD = {
|
|
|
+ id: this.uuid.v4(), tid: tid, lid: bills.id,
|
|
|
+ add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
|
|
|
+ in_time, porder: i + 1,
|
|
|
+ name: p.name, drawing_code: p.drawing_code,
|
|
|
+ };
|
|
|
+ if (d.quantity) {
|
|
|
+ inD.sgfh_qty = this.round(p.quantity, precision.value);
|
|
|
+ inD.quantity = inD.sgfh_qty;
|
|
|
+ qd.sgfh_qty = this.ctx.helper.add(qd.sgfh_qty, inD.sgfh_qty);
|
|
|
+ }
|
|
|
+ insertPosData.push(inD);
|
|
|
+ await transaction.insert(this.tableName, insertDatas);
|
|
|
+ }
|
|
|
+ qd.sgfh_tp = this.ctx.helper.mul(qd.sgfh_qty, qd.unit_price, info.decimal.tp);
|
|
|
+ qd.quantity = qd.sgfh_qty;
|
|
|
+ qd.total_price = qd.sgfh_tp;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
try {
|
|
|
// 选中节点的所有后兄弟节点,order+粘贴节点个数
|
|
|
await this._updateChildrenOrder(tenderId, selectData.ledger_pid, selectData.order + 1, data.length);
|
|
|
- // 计算id和order
|
|
|
- const maxId = await this._getMaxLid(tenderId);
|
|
|
- const order = selectData.order;
|
|
|
- // 数据库创建新增节点数据
|
|
|
- for (let i = 0, iLen = data.length; i < iLen; i++) {
|
|
|
- // 合并新增数据
|
|
|
- const qd = {
|
|
|
- id: this.uuid.v4(),
|
|
|
- tender_id: tenderId,
|
|
|
- ledger_id: maxId + i + 1,
|
|
|
- ledger_pid: parentData.ledger_id,
|
|
|
- is_leaf: true,
|
|
|
- order: order + i + 1,
|
|
|
- level: parentData.level + 1,
|
|
|
- b_code: data[i].b_code,
|
|
|
- name: data[i].name,
|
|
|
- unit: data[i].unit,
|
|
|
- unit_price: data[i].price,
|
|
|
- };
|
|
|
- qd.full_path = parentData.full_path + '-' + qd.ledger_id;
|
|
|
- const insertResult = await this.transaction.insert(this.tableName, qd);
|
|
|
- newIds.push(qd.id);
|
|
|
- if (data[i].pos.length > 0) {
|
|
|
- await this.ctx.service.pos.insertLedgerPosData(this.transaction, tenderId, qd, data[i].pos);
|
|
|
- await this.calcNode(qd, this.transaction);
|
|
|
- }
|
|
|
- }
|
|
|
+ await this.transaction.insert(this.tableName, insertBillsData);
|
|
|
+ if (insertPosData.length > 0) await this.transaction.insert(this.ctx.service.pos.tableName, insertPosData);
|
|
|
this._cacheMaxLid(tenderId, maxId + data.length);
|
|
|
await this.transaction.commit();
|
|
|
} catch (err) {
|
|
@@ -622,52 +641,12 @@ module.exports = app => {
|
|
|
}
|
|
|
|
|
|
// 查询应返回的结果
|
|
|
- result.ledger.create = await this.getDataByIds(newIds);
|
|
|
+ result.ledger.create = insertBillsData;
|
|
|
result.ledger.update = await this.getNextsData(selectData.tender_id, selectData.ledger_pid, selectData.order + data.length);
|
|
|
- result.pos = await this.ctx.service.pos.getPosData({tid: tenderId, lid: newIds });
|
|
|
+ result.pos = insertPosData;
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- *
|
|
|
- * @param node
|
|
|
- * @param transaction
|
|
|
- * @return {Promise<void>}
|
|
|
- * @private
|
|
|
- */
|
|
|
- async calcNode(node, transaction) {
|
|
|
- const info = this.ctx.tender.info;
|
|
|
- const precision = this.ctx.helper.findPrecision(info.precision, node.unit);
|
|
|
-
|
|
|
- const calcQtySql = 'SELECT SUM(`sgfh_qty`) As `sgfh_qty`, SUM(`sjcl_qty`) As `sjcl_qty`, SUM(`qtcl_qty`) As `qtcl_qty`, SUM(`quantity`) As `quantity` FROM ?? WHERE `lid` = ?';
|
|
|
- const data = await transaction.queryOne(calcQtySql, [this.ctx.service.pos.tableName, node.id]);
|
|
|
- data.id = node.id;
|
|
|
- data.sgfh_qty = this.round(data.sgfh_qty, precision.value);
|
|
|
- data.sjcl_qty = this.round(data.sjcl_qty, precision.value);
|
|
|
- data.qtcl_qty = this.round(data.qtcl_qty, precision.value);
|
|
|
- data.quantity = this.round(data.quantity, precision.value);
|
|
|
- data.sgfh_tp = this.ctx.helper.mul(data.sgfh_qty, node.unit_price, info.decimal.tp);
|
|
|
- data.sjcl_tp = this.ctx.helper.mul(data.sjcl_qty, node.unit_price, info.decimal.tp);
|
|
|
- data.qtcl_tp = this.ctx.helper.mul(data.qtcl_qty, node.unit_price, info.decimal.tp);
|
|
|
- data.total_price = this.ctx.helper.mul(data.quantity, node.unit_price, info.decimal.tp);
|
|
|
- const result = await transaction.update(this.tableName, data);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- *
|
|
|
- * @param {Number} tid - 标段id
|
|
|
- * @param {Number} id - 需要计算的节点的id
|
|
|
- * @param {Object} transaction - 操作所属事务,没有则创建
|
|
|
- * @return {Promise<void>}
|
|
|
- */
|
|
|
- async calc(tid, id, transaction) {
|
|
|
- const node = await transaction.get(this.tableName, { id });
|
|
|
- if (!node) {
|
|
|
- throw '数据错误';
|
|
|
- }
|
|
|
- await this.calcNode(node, transaction);
|
|
|
- }
|
|
|
-
|
|
|
async _importCacheTreeNodes(transaction, nodes) {
|
|
|
const datas = [];
|
|
|
for (const node of nodes) {
|