123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- module.exports = app => {
- class ReviseBills extends app.BaseBillsService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx, {
- mid: 'tender_id',
- kid: 'ledger_id',
- pid: 'ledger_pid',
- order: 'order',
- level: 'level',
- isLeaf: 'is_leaf',
- fullPath: 'full_path',
- keyPre: 'revise_bills_maxLid:',
- uuid: true,
- });
- this.tableName = 'revise_bills';
- }
- async _deleteRelaData (mid, deleteData) {
- await this.ctx.service.revisePos.deletePosData(this.transaction, mid, this._.map(deleteData, 'id'));
- }
- /**
- * 新增节点
- * @param {Number} tid - 台账id
- * @param {uuid} rid - 修订id
- * @param {Number} lid - 清单节点id
- * @returns {Promise<void>}
- */
- async addReviseNode(tid, rid, lid) {
- if (!rid) return null;
- return await this.addNode(tid, lid, {crid: rid});
- }
- /**
- * 批量插入子项
- * @param {uuid} rid - 修订id
- * @param {Number} lid - 节点id
- * @param {Object} data - 批量插入数据
- * @return {Promise<void>}
- */
- async batchInsertChild(tid, rid, lid, data) {
- const result = { ledger: {}, pos: null };
- if (!tid || !rid || !lid) return result;
- const select = await this.getDataByLid(tid, lid);
- if (!select) {
- throw '位置数据错误';
- }
- // 计算id和order
- const maxId = await this._getMaxLid(tid);
- const lastChild = await this.getLastChildData(tid, lid);
- const order = lastChild ? lastChild.order : 0;
- // 整理数据
- const bills = [], pos = [], newIds = [];
- for (let i = 0, iLen = data.length; i < iLen; i++) {
- // 合并新增数据
- const qd = {
- crid: rid,
- id: this.uuid.v4(),
- tender_id: tid,
- ledger_id: maxId + i + 1,
- ledger_pid: select.ledger_id,
- is_leaf: true,
- order: order + i + 1,
- level: select.level + 1,
- b_code: data[i].b_code,
- name: data[i].name,
- unit: data[i].unit,
- unit_price: data[i].price,
- };
- const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
- qd.sgfh_qty = 0;
- for (const p of data[i].pos) {
- const inP = {
- id: this.uuid.v4(), tid: tid, lid: qd.id, crid: rid,
- add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
- name: p.name, drawing_code: p.drawing_code,
- };
- if (p.quantity) {
- inP.sgfh_qty = this.round(p.quantity, precision.value);
- inP.quantity = inP.sgfh_qty;
- }
- qd.sgfh_qty = this.ctx.helper.add(qd.sgfh_qty, inP.sgfh_qty);
- pos.push(inP);
- }
- qd.full_path = select.full_path + '-' + qd.ledger_id;
- qd.sgfh_tp = this.ctx.helper.mul(qd.sgfh_qty, qd.unit_price, this.ctx.tender.info.decimal.tp);
- qd.quantity = qd.sgfh_qty;
- qd.total_price = qd.sgfh_tp;
- bills.push(qd);
- newIds.push(qd.id);
- }
- this.transaction = await this.db.beginTransaction();
- try {
- // 更新父项isLeaf
- if (!lastChild) {
- await this.transaction.update(this.tableName, {
- is_leaf: false,
- unit_price: null,
- quantity: null,
- total_price: null,
- deal_qty: null,
- deal_tp: null,
- }, {tender_id: rid, id: select.id});
- }
- // 数据库创建新增节点数据
- await this.transaction.insert(this.tableName, bills);
- await this.transaction.insert(this.ctx.service.revisePos.tableName, pos);
- this._cacheMaxLid(tid, maxId + data.length);
- await this.transaction.commit();
- } catch (err) {
- await this.transaction.rollback();
- throw err;
- }
- // 查询应返回的结果
- result.ledger.create = await this.getDataById(newIds);
- if (!lastChild) {
- result.ledger.update = await this.getDataByLid(select.id);
- }
- result.pos = await this.ctx.service.revisePos.getDataByLid(tid, newIds);
- return result;
- }
- /**
- * 批量插入后项
- * @param {Number} tid - 台账id
- * @param {uuid} rid - 修订id
- * @param {Number} lid - 节点id
- * @param {Object} data - 批量插入数据
- * @return {Promise<void>}
- */
- async batchInsertNext(tid, rid, lid, data) {
- const result = { ledger: {}, pos: null };
- if (!tid || !rid || !lid) return result;
- const select = await this.getDataByLid(tid, lid);
- if (!select) {
- throw '位置数据错误';
- }
- const parentData = await this.getDataByLid(tid, select.ledger_pid);
- if (!parentData) {
- throw '位置数据错误';
- }
- // 计算id和order
- const maxId = await this._getMaxLid(tid);
- const order = select.order;
- // 整理数据
- const bills = [], pos = [], newIds = [];
- for (let i = 0, iLen = data.length; i < iLen; i++) {
- // 合并新增数据
- const qd = {
- crid: rid,
- id: this.uuid.v4(),
- tender_id: tid,
- 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,
- };
- const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
- qd.sgfh_qty = 0;
- for (const p of data[i].pos) {
- const inP = {
- id: this.uuid.v4(), tid: tid, lid: qd.id, crid: rid,
- add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
- name: p.name, drawing_code: p.drawing_code,
- };
- if (p.quantity) {
- inP.sgfh_qty = this.round(p.quantity, precision.value);
- inP.quantity = inP.sgfh_qty;
- }
- qd.sgfh_qty = this.ctx.helper.add(qd.sgfh_qty, inP.sgfh_qty);
- pos.push(inP);
- }
- qd.full_path = parentData.full_path + '-' + qd.ledger_id;
- qd.sgfh_tp = this.ctx.helper.mul(qd.sgfh_qty, qd.unit_price, this.ctx.tender.info.decimal.tp);
- qd.quantity = qd.sgfh_qty;
- qd.total_price = qd.sgfh_tp;
- bills.push(qd);
- newIds.push(qd.id);
- }
- this.transaction = await this.db.beginTransaction();
- try {
- // 选中节点的所有后兄弟节点,order+粘贴节点个数
- await this._updateChildrenOrder(tid, select.ledger_pid, select.order+1);
- // 数据库创建新增节点数据
- await this.transaction.insert(this.tableName, bills);
- await this.transaction.insert(this.ctx.service.revisePos.tableName, pos);
- this._cacheMaxLid(tid, maxId + data.length);
- await this.transaction.commit();
- } catch (err) {
- await this.transaction.rollback();
- throw err;
- }
- // 查询应返回的结果
- result.ledger.create = await this.getDataById(newIds);
- result.ledger.update = await this.getNextsData(select.tender_id, select.ledger_pid, select.order + data.length);
- result.pos = await this.ctx.service.revisePos.getDataByLid(tid, newIds);
- return result;
- }
- }
- return ReviseBills;
- };
|