'use strict';
/**
 * 标准库基类
 *
 * @author Mai
 * @date 2018/3/13
 * @version
 */

const BaseService = require('../base/base_service');

class StandardLib extends BaseService {

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


    /**
     * 获取数据
     *
     * @param {Number} listId - 项目节列表id
     * @param {Number} level - 小于此层级的全部不显示
     * @return {Array} - 返回对应数据
     */
    async getData(listId, level = 2) {
        this.initSqlBuilder();
        this.sqlBuilder.setAndWhere('list_id', {
            operate: '=',
            value: listId,
        });

        this.sqlBuilder.setAndWhere('level', {
            operate: '<=',
            value: level,
        });
        const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
        const list = await this.db.query(sql, sqlParam);

        return list;
    }


    /**
     * 实例中具体的dataId使用字段不相同，统一赋值到source下
     * @param {Object|Array} data
     */
    setSourceData(data) {
        if (!data) { return; }
        const datas = data instanceof Array ? data : [data];
        for (const d of datas) {
            if (d) {
                d.source = this.stdType + '-' + d.list_id + '-' + d[this.dataId];
            }
        }
    }
    /**
     * 根据dataId和listId获取指定标准节点
     * @param {Number} listId - 标准库id
     * @param {Number} dataId - dataId(chapter_id|bill_id)
     * @returns {Promise<*>}
     */
    async getDataByDataId(listId, dataId) {
        this.initSqlBuilder();
        this.sqlBuilder.setAndWhere('list_id', {
            value: listId,
            operate: '='
        });
        this.sqlBuilder.setAndWhere(this.dataId, {
            value: dataId,
            operate: '='
        });
        const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
        const node = await this.db.queryOne(sql, sqlParam);
        this.setSourceData(node);

        return node;
    }
    /**
     * 根据full_path获取数据 full_path Like ‘1.2.3%’（传参full_path = '1.2.3%'）
     * @param {Number} listId - 标准库id
     * @param {String} full_path - 路径
     * @return {Promise<void>}
     */
    async getDataByFullPath(listId, full_path) {
        this.initSqlBuilder();
        this.sqlBuilder.setAndWhere('list_id', {
            value: listId,
            operate: '=',
        });
        this.sqlBuilder.setAndWhere('full_path', {
            value: this.db.escape(full_path),
            operate: 'Like',
        });
        const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
        const resultData = await this.db.query(sql, sqlParam);
        this.setSourceData(resultData);
        return resultData;
    }
    /**
     * 根据full_path检索自己及所有父项
     * @param {Number} listId - 标准库id
     * @param {Array|String} fullPath - 节点完整路径
     * @returns {Promise<*>}
     * @private
     */
    async getFullLevelDataByFullPath(listId, fullPath) {
        const explodePath = this.ctx.helper.explodePath(fullPath);

        this.initSqlBuilder();
        this.sqlBuilder.setAndWhere('list_id', {
            value: listId,
            operate: '=',
        });
        this.sqlBuilder.setAndWhere('full_path', {
            value: explodePath,
            operate: 'in'
        });

        const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
        const data = await this.db.query(sql, sqlParam);
        this.setSourceData(data);
        return data;
    };
}

module.exports = StandardLib;