/**
 * Created by Zhong on 2017/9/28.
 */
let mongoose = require('mongoose');
const optionsModel = mongoose.model('options');
class OptionsDao {
    async getOptions(user_id, compilation_id){
        let rst = await optionsModel.find({user_id: user_id, compilation_id: compilation_id});
        rst = rst.length > 0 && typeof rst[0].options !== 'undefined' ? rst[0].options : null;
        return rst;
    }

    async getOptionsByType(user_id, compilation_id, optsType){
        let rst = await optionsModel.findOne({user_id: user_id, compilation_id: compilation_id});
        if(rst){
            return rst.options[optsType] !== undefined && rst.options[optsType] !== null ? rst.options[optsType] : null;
        }
    }

    async saveOptions(user_id, compilation_id, optSetting){
        let optionsData = await optionsModel.find({user_id: user_id, compilation_id: compilation_id});
        if(optionsData.length === 0){
            await optionsModel.create({user_id: user_id, compilation_id: compilation_id, options: optSetting});
        }
        await optionsModel.update({user_id: user_id, compilation_id: compilation_id}, optSetting);
        let rst = await optionsModel.find({user_id: user_id, compilation_id: compilation_id});
        return rst.length > 0 && typeof rst[0].options !== 'undefined' ? rst[0].options : null;
    }
}

export default OptionsDao;