'use strict';

/**
 *
 *  附件
 * @author Ellisran
 * @date 2019/1/11
 * @version
 */

module.exports = app => {
    class StageAtt extends app.BaseService {
        /**
         * 构造函数
         *
         * @param {Object} ctx - egg全局变量
         * @return {void}
         */
        constructor(ctx) {
            super(ctx);
            this.tableName = 'stage_attachment';
        }

        /**
         * 添加附件
         * @param {Object} postData - 表单信息
         * @param {Object} fileData - 文件信息
         * @param {int} uid - 上传者id
         * @return {void}
         */
        async save(postData, fileData, uid) {
            const data = {
                lid: postData.lid,
                uid,
                remark: '',
            };
            Object.assign(data, fileData);
            const result = await this.db.insert(this.tableName, data);
            return result;
        }

        /**
         * 添加附件
         * @param {Object} postData - 表单信息
         * @param {Object} fileData - 文件信息
         * @param {int} uid - 上传者id
         * @return {void}
         */
        async updateByID(postData, fileData) {
            delete postData.size;
            const data = {};
            Object.assign(data, fileData);
            Object.assign(data, postData);
            const result = await this.db.update(this.tableName, data);
            return result.affectedRows === 1;
        }

        /**
         * 获取所有附件
         * @param {int} tid - 标段id
         * @param {int} sid - 当前期数
         * @return {void}
         */
        async getDataByTenderIdAndStageId(tid, sid) {
            const sql = 'SELECT att.id, att.lid, att.uid, att.filename, att.fileext, att.filesize, att.remark, att.in_time,' +
                ' pa.name as `username`, leg.name as `lname`, leg.code as `code`, leg.b_code as `b_code`' +
                ' FROM ?? AS att,?? AS pa,?? AS leg' +
                ' WHERE leg.id = att.lid AND pa.id = att.uid AND att.tid = ? AND att.sid = ? ORDER BY att.in_time DESC';
            const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.ctx.service.ledger.tableName, tid, sid];
            return await this.db.query(sql, sqlParam);
        }

        /**
         * 获取单个附件
         * @param {int} tid - 标段id
         * @param {int} sid - 当前期数
         * @return {void}
         */
        async getDataByFid(id) {
            const sql = 'SELECT att.id, att.lid, att.uid, att.filename, att.fileext, att.filesize, att.remark, att.in_time,' +
                ' pa.name as `username`, leg.name as `lname`, leg.code as `code`, leg.b_code as `b_code`' +
                ' FROM ?? AS att,?? AS pa,?? AS leg' +
                ' WHERE leg.id = att.lid AND pa.id = att.uid AND att.id = ? ORDER BY att.in_time DESC';
            const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, this.ctx.service.ledger.tableName, id];
            return await this.db.queryOne(sql, sqlParam);
        }
    }

    return StageAtt;
};
