'use strict';

/**
 *
 *
 * @author Mai
 * @date
 * @version
 */

module.exports = app => {
    class RevisePos extends app.BaseService {

        /**
         * 构造函数
         *
         * @param {Object} ctx - egg全局变量
         * @return {void}
         */
        constructor(ctx) {
            super(ctx);
            this.tableName = 'revise_pos';
        }

        /**
         * 获取 修订 清单数据
         * @param {Number}tid - 标段id
         * @param {uuid}rid - 修订id
         * @returns {Promise<void>}
         */
        async getData(tid) {
            return await this.db.select(this.tableName, {
                where: {tid: tid}
            });
        }

        async getDataByLid(tid, lid) {
            return await this.db.select(this.tableName, {
                where: {tid: tid, lid: lid}
            });
        }

        async insertLedgerPosData(transaction, tid, rid, bills, data) {
            const precision = this.ctx.helper.findPrecision(this.ctx.tender.info.precision, bills.unit);
            const insertDatas = [];
            for (const d of data) {
                const inD = {
                    id: this.uuid.v4(), tid: tid, lid: bills.id, crid: rid,
                    add_stage: 0, add_times: 0, add_user: this.ctx.session.sessionUser.accountId,
                    name: d.name, drawing_code: d.drawing_code,
                };
                if (d.quantity) {
                    inD.sgfh_qty = this.round(d.quantity, precision.value);
                    inD.quantity = inD.sgfh_qty;
                }
                insertDatas.push(inD);
            }
            await transaction.insert(this.tableName, insertDatas);
        }
    }

    return RevisePos;
};