'use strict'; const auditConst = require('../const/audit').advance; module.exports = app => { class Advance extends app.BaseService { constructor(ctx) { super(ctx); this.tableName = 'advance_pay'; } /** * 获取预付款列表 * @param {Number} tid 标段id * @param {Number} type 预付款类型 */ async getAdvanceList(tid, type) { this.initSqlBuilder(); this.sqlBuilder.setAndWhere('tid', { value: tid, operate: '=', }); this.sqlBuilder.setAndWhere('type', { value: type, operate: '=', }); this.sqlBuilder.orderBy = [['order', 'desc']]; const [sql, sqlParam] = this.sqlBuilder.build(this.tableName); const advance = await this.db.query(sql, sqlParam); for (const item of advance) { item.curAuditor = await this.ctx.service.advanceAudit.getAuditorByStatus(item.id, item.status, item.times); if (item.status === auditConst.status.checkNoPre) { item.curAuditor2 = await this.ctx.service.advanceAudit.getAuditorByStatus(item.id, auditConst.status.checking); } } return advance; } /** * 获取预付款最新一期 * @param {Number} tid 标段id * @param {String} type 类型: 开工预付款|材料预付款 (0|1) * @param {Boolean} includeUnCheck 包括未上报的 * @return {Promise<*>} 实例结果集 */ async getLastestAdvance(tid, type, includeUnCheck = false) { this.initSqlBuilder(); this.sqlBuilder.setAndWhere('tid', { value: tid, operate: '=', }); this.sqlBuilder.setAndWhere('type', { value: type, operate: '=', }); if (!includeUnCheck) { this.sqlBuilder.setAndWhere('status', { value: auditConst.status.uncheck, operate: '!=', }); } this.sqlBuilder.orderBy = [['order', 'desc']]; const [sql, sqlParam] = this.sqlBuilder.build(this.tableName); const advance = await this.db.queryOne(sql, sqlParam); return advance; } /** * 创建一条新的记录 * @param {String} type 类型: 开工预付款|材料预付款 (start|material) * @return {Promise<*>} 插入结果集 */ async createRecord(type) { const { ctx } = this; const uid = ctx.session.sessionUser.accountId; const tid = ctx.tender.id; const latestOrder = await this.getLastestAdvance(tid, type); const order = !latestOrder ? 1 : latestOrder.order + 1; const record = await this.db.insert(this.tableName, { type, uid, tid, status: auditConst.status.uncheck, order }); return await this.getDataById(record.insertId); } /** * 获取上一期预付款记录 * @param {Number} tid 标段id * @param {Number} type 预付款类型 */ async getPreviousRecord(tid, type) { this.initSqlBuilder(); this.sqlBuilder.setAndWhere('tid', { value: tid, operate: '=', }); this.sqlBuilder.setAndWhere('type', { value: type, operate: '=', }); this.sqlBuilder.setAndWhere('order', { value: this.ctx.advance.order - 1, operate: '=', }); this.sqlBuilder.orderBy = [['order', 'desc']]; const [sql, sqlParam] = this.sqlBuilder.build(this.tableName); return await this.db.queryOne(sql, sqlParam); } /** * 计算列表进度条所需数据 * @param {Object} latest 最新期的数据 * @param {Number} payTotal 预付款金额总数 * @return {Object} progress所需数据 */ async calcProgress(latest, payTotal) { const { ctx } = this; if (!latest) { latest = { prev_total_amount: 0, prev_amount: 0, cur_amount: 0 }; } const surplus = ctx.helper.sub(payTotal, latest.prev_total_amount || 0); const p_ratio = ctx.helper.mul(ctx.helper.div(latest.prev_amount || 0, payTotal), 100, 2); // 截止上期金额总数 const c_ratio = ctx.helper.mul(ctx.helper.div(latest.cur_amount || 0, payTotal), 100, 0); // 截止本期金额总数 const s_ratio = ctx.helper.mul(ctx.helper.div(surplus, payTotal), 100, 0); // 剩余金额总数 return { p_amount: latest.prev_amount, c_amount: latest.cur_amount, s_amount: surplus, p_ratio, c_ratio, s_ratio, }; } /** * 更新预付款记录 * @param {Object} payload 载荷 * @param {Number} id 预付款id */ async updateAdvance(payload, id) { const { ctx } = this; const prevRecord = await this.getPreviousRecord(ctx.tender.id, ctx.advance.type) || { prev_total_amount: 0 }; const { cur_amount } = payload; console.log(ctx.helper.add(cur_amount, prevRecord.prev_total_amount)); payload.prev_total_amount = ctx.helper.add(cur_amount, prevRecord.prev_total_amount); console.log('payload', payload); return await this.update(payload, { id, }); } } return Advance; };