/**
 * 工料机数据源模块
 *
 * @author CaiAoLin
 * @date 2017/6/23
 * @version
 */
import BaseModel from "../../common/base/base_model";
import GLJRepositorySchema from "./schemas/glj_repository";

class GLJRepositoryModel extends BaseModel {

    /**
     * 构造函数
     *
     * @return {void}
     */
    constructor() {
        let parent = super();
        parent.model = GLJRepositorySchema;
        parent.init();
    }

    /**
     * 新增数据
     *
     * @param {object} data
     * @return {Promise} boolean
     */
    async add(data) {
        let result = false;
        try {
            result = await this.db.create(data);
        } catch (error) {
            if (error.name !== null && error.name === 'ValidationError') {
                // 这里是数据验证失败
                console.log('数据验证失败!');
            }
            result = false;
        }

        return result;
    }

    /**
     * 根据条件获取对应工料机数据
     *
     * @param {Object} condition
     * @return {Promise} Array
     */
    async getDataByCondition(condition) {
        let gljData = [];
        try {
            if (Object.keys(condition).length <= 0) {
                throw '筛选条件有误';
            }

            gljData = await this.db.find(condition);

        } catch (error) {
            console.log(error);
            gljData = [];
        }

        return gljData;
    }

}

export default GLJRepositoryModel;
