'use strict'; /** * * * @author Mai * @date 2018/6/12 * @version */ const audit = require('../const/audit').revise; module.exports = app => { class LedgerRevise extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'ledger_revise'; } _analysisData(data) { if (!data) return; const datas = data instanceof Array ? data : [data]; for (const d of datas) { if (d.sum) d.sum = JSON.parse(d.sum); } } /** * 获取标段下,修订(分页,且按时间倒序) * @param {Number}tid - 标段id * @returns {Promise<*>} - ledger_change下所有数据,并关联 project_account(读取提交人名称、单位、公司) */ async getReviseList (tid, show_revise_invalid) { const sql = 'SELECT lc.id, lc.tid, lc.corder, lc.in_time, lc.uid, lc.begin_time, lc.end_time, lc.times, lc.status, lc.valid, lc.content, lc.pre_his_id, lc.his_id,' + ' pa.name As user_name, pa.role As user_role, pa.company As user_company' + ' FROM ' + this.tableName + ' As lc' + ' INNER JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON lc.uid = pa.id' + ' WHERE lc.tid = ?' + (show_revise_invalid ? '' : ' AND lc.valid = 1') + ' ORDER BY lc.in_time DESC' + ' LIMIT ?, ?'; const Len = this.app.config.pageSize; const sqlParam = [tid, (this.ctx.page - 1) * Len, Len]; const result = await this.db.query(sql, sqlParam); this._analysisData(result); return result; } /** * 获取全部修订 * @param tid * @returns {Promise<*>} */ async getAllReviseList (tid) { const sql = 'SELECT lc.id, lc.tid, lc.corder, lc.in_time, lc.uid, lc.begin_time, lc.end_time, lc.times, lc.status, lc.valid, lc.pre_his_id, lc.his_id,' + ' pa.name As user_name, pa.role As user_role, pa.company As user_company' + ' FROM ' + this.tableName + ' As lc' + ' INNER JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON lc.uid = pa.id' + ' WHERE lc.tid = ?' + ' ORDER BY lc.in_time DESC'; const sqlParam = [tid]; const result = await this.db.query(sql, sqlParam); this._analysisData(result); return result; } async getLastestRevise(tid, valid = true) { const sql = 'SELECT lc.*,' + ' pa.name As user_name, pa.role As user_role, pa.company As user_company' + ' FROM ' + this.tableName + ' As lc' + ' INNER JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON lc.uid = pa.id' + ' WHERE lc.tid = ? ' + (valid ? ' And lc.valid' : '') + ' ORDER BY lc.in_time DESC' + ' LIMIT 0, 1'; const sqlParam = [tid]; const result = await this.db.queryOne(sql, sqlParam); this._analysisData(result); return result; } async getRevise(tid, rid) { const sql = 'SELECT lc.*,' + ' pa.name As user_name, pa.role As user_role, pa.company As user_company' + ' FROM ' + this.tableName + ' As lc' + ' INNER JOIN ' + this.ctx.service.projectAccount.tableName + ' As pa ON lc.uid = pa.id' + ' WHERE lc.tid = ? and lc.id = ? ' + ' ORDER BY lc.in_time DESC' + ' LIMIT 0, 1'; const sqlParam = [tid, rid]; const result = await this.db.queryOne(sql, sqlParam); this._analysisData(result); return result; } /** * 获取新增修订的序号 * @param {Number}tid - 标段id * @returns {Promise} */ async getNewOrder(tid) { const sql = 'SELECT Max(`corder`) As max_order FROM ' + this.tableName + ' Where `tid` = ? and `valid`'; const sqlParam = [tid]; const result = await this.db.queryOne(sql, sqlParam); return result.max_order || 0; } async _initReviseBills(transaction, tid) { const sql = 'Insert Into ' + this.ctx.service.reviseBills.tableName + ' (id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' + ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' + ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, ccid, tender_id,' + ' sgfh_expr, sjcl_expr, qtcl_expr, check_calc,' + ' ex_memo1, ex_memo2, ex_memo3)' + ' Select id, code, b_code, name, unit, source, remark, ledger_id, ledger_pid, level, `order`, full_path, is_leaf,' + ' quantity, total_price, unit_price, drawing_code, memo, dgn_qty1, dgn_qty2, deal_qty, deal_tp,' + ' sgfh_qty, sgfh_tp, sjcl_qty, sjcl_tp, qtcl_qty, qtcl_tp, node_type, crid, ccid, tender_id,' + ' sgfh_expr, sjcl_expr, qtcl_expr, 0,' + ' ex_memo1, ex_memo2, ex_memo3' + ' From ' + this.ctx.service.ledger.tableName + ' Where `tender_id` = ?'; const sqlParam = [tid]; await transaction.query(sql, sqlParam); } async _initRevisePos(transaction, tid) { const sql = 'Insert Into ' + this.ctx.service.revisePos.tableName + ' (id, tid, lid, name, drawing_code, quantity, add_stage, add_stage_order, add_times, add_user,' + ' sgfh_qty, sjcl_qty, qtcl_qty, crid, ccid, in_time, porder, position,' + ' sgfh_expr, sjcl_expr, qtcl_expr, real_qty,' + ' ex_memo1, ex_memo2, ex_memo3)' + ' Select id, tid, lid, name, drawing_code, quantity, add_stage, add_stage_order, add_times, add_user,' + ' sgfh_qty, sjcl_qty, qtcl_qty, crid, ccid, in_time, porder, position,' + ' sgfh_expr, sjcl_expr, qtcl_expr, real_qty,' + ' ex_memo1, ex_memo2, ex_memo3' + ' From ' + this.ctx.service.pos.tableName + ' Where `tid` = ?'; const sqlParam = [tid]; await transaction.query(sql, sqlParam); } async _initReviseAuditor(transaction, rid, order) { const inTime = new Date(); const preRevise = order > 0 ? await this.getDataByCondition({tid: this.ctx.tender.id, corder: order}) : null; const newAuditors = []; const auditors = preRevise ? await this.ctx.service.reviseAudit.getAuditors(preRevise.id, preRevise.ledger_times) : await this.ctx.service.ledgerAudit.getAuditors(this.ctx.tender.id, this.ctx.tender.data.ledger_times); for (const a of auditors) { if (newAuditors.find(x => { return x.audit_id === a.audit_id; })) continue; newAuditors.push({ tender_id: this.ctx.tender.id, rid: rid, in_time: inTime, audit_order: newAuditors.length + 1, audit_id: a.audit_id, times: 1, status: audit.status.uncheck, }); } if (newAuditors.length > 0) await transaction.insert(this.ctx.service.reviseAudit.tableName, newAuditors); } /** * 新增修订 * @param {Number}tid - 标段id * @param {Number}uid - 提交人id * @returns {Promise} */ async add(tid, uid) { if (!tid && !uid) { throw '数据错误'; } const maxOrder = await this.getNewOrder(tid); const preHisId = await this.ctx.service.ledgerHistory.checkBackupLedgerHistory(tid); if (!preHisId) throw '台账历史数据有误'; const data = { id: this.uuid.v4(), tid: tid, uid: uid, corder: maxOrder + 1, in_time: new Date(), status: audit.status.uncheck, pre_his_id: preHisId, }; const transaction = await this.db.beginTransaction(); try { const result = await transaction.insert(this.tableName, data); if (result.affectedRows !== 1) { throw '新增台账修订失败'; } await transaction.delete(this.ctx.service.reviseBills.tableName, {tender_id: tid}); await this._initReviseBills(transaction, tid, data.id); await transaction.delete(this.ctx.service.revisePos.tableName, {tid: tid}); await this._initRevisePos(transaction, tid, data.id); await this._initReviseAuditor(transaction, data.id, maxOrder); await transaction.commit(); return data; } catch(err) { await transaction.rollback(); throw err; } } /** * 作废修订 * @param revise * @returns {Promise} */ async cancelRevise(revise) { const [his_id, sum] = await this.ctx.service.ledgerHistory.backupReviseLedgerHistory(revise); const transaction = await this.db.beginTransaction(); try { const result = await transaction.update(this.tableName, { id: revise.id, valid: false, end_time: new Date(), his_id, sum: JSON.stringify(sum), }); await transaction.update(this.ctx.service.ledgerHistory.tableName, { id: his_id, valid: 0 }); if (revise.his_id > 0) await transaction.update(this.ctx.service.ledgerHistory.tableName, { id: revise.his_id, valid: 0 }); // 投资进度改变状态 await transaction.update(this.ctx.service.schedule.tableName, { revising: 0 }, { where: { tid: this.ctx.tender.id } }); // 作废单价调整 await transaction.update(this.ctx.service.revisePrice.tableName, { valid: 0 }, { where: { rid: revise.id } }); await transaction.commit(); return result.affectedRows === 1; } catch (err) { await transaction.rollback(); throw err; } } } return LedgerRevise; };