'use strict';

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

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

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

        /**
         * 获取 变更令 所有附件
         * @param {uuid} cid - 变更令id
         * @returns {Promise<void>}
         */
        async getChangeAttachment(cid) {
            const sql = 'SELECT ca.*, pa.name As u_name, pa.role As u_role ' +
                '  FROM ?? As ca ' +
                '  Left Join ?? As pa ' +
                '  On ca.uid = pa.id ' +
                '  Where ca.cid = ?';
            const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, cid];
            return await this.db.query(sql, sqlParam);
        }
    }

    return ChangeAtt;
};
