/**
 * mongodb Helper
 *
 * @author caiaolin
 * @date 2017/5/22.
 */

class MongooseHelper {

    /**
     * mongoose连接对象
     *
     * @var {object}
     */
    connect = null;

    /**
     * mongoose数据模型
     *
     * @var {object}
     */
    model = null;

    /**
     * 查找单一数据
     *
     * @param {object} conditions
     * @param {object} fields
     * @return {Promise}
     */
    findOne(conditions, fields = null) {
        let self = this;
        return new Promise(function (resolve, reject) {
            self.model.findOne(conditions, fields, function (error, data) {
                if (error) {
                    reject(null);
                } else {
                    resolve(data);
                }
            });
        });
    }

    /**
     * 查找数据
     *
     * @param {object} conditions
     * @param {object} fields
     * @param {object} option
     * @return {Promise}
     */
    find(conditions, fields = null, option = null) {
        let self = this;
        return new Promise(function (resolve, reject) {
            self.model.find(conditions, fields, option, function (error, data) {
                if (error) {
                    reject(error);
                } else {
                    resolve(data);
                }
            });
        });
    }

    /**
     * 关联查找数据
     *
     * @param {object} conditions
     * @param {object} fields
     * @param {String|Object} populate
     * @return {Promise}
     */
    findWithPopulate(conditions, fields = null, populate = '') {
        let self = this;
        return new Promise(function (resolve, reject) {
            self.model.find(conditions, fields).populate(populate).exec(function(error, data) {
                if (error) {
                    reject(error);
                } else {
                    resolve(data);
                }
            });
        });
    }

    /**
     * 查找且更新(原子操作)
     *
     * @param {object} update
     * @param {object} condition
     * @param {object} options
     * @return {Promise}
     */
    findAndModify(condition, update, options) {
        let self = this;
        return new Promise(function (resolve, reject) {
            self.model.findOneAndUpdate(condition, update, options, function(error, data) {
                    if (error) {
                        reject(error);
                    } else {
                        resolve(data);
                    }
                });
        });
    }

    /**
     * 新增操作
     *
     * @param {object} data
     * @return {Promise}
     */
    create(data) {
        let self = this;
        return new Promise(function (resolve, reject) {
            self.model.create(data, function(error, data) {
                if (error) {
                    reject(error);
                } else {
                    resolve(data);
                }
            });
        });
    }

    /**
     * 统计数据数量
     *
     * @param {Object} condition
     * @return {Promise}
     */
    count(condition) {
        let self = this;
        return new Promise(function(resolve, reject) {
            self.model.count(condition, function(error, data) {
                if (error) {
                    reject(error);
                } else {
                    resolve(data);
                }
            });
        });
    }

    /**
     * 删除数据
     *
     * @param {Object} condition
     * @return {Promise}
     */
    delete(condition) {
        let self = this;
        return new Promise(function(resolve, reject) {
            self.model.remove(condition, function(error, data) {
                if (error) {
                    reject(error);
                } else {
                    resolve(data);
                }
            });
        });
    }

    /**
     * 更新数据
     *
     * @param {Object} condition
     * @param {Object} updateData
     * @return {Promise}
     */
    update(condition, updateData) {
        let self = this;
        return new Promise(function(resolve, reject) {
            self.model.update(condition, {$set: updateData}, function(error, data) {
                if (error) {
                    reject(error);
                } else {
                    resolve(data);
                }
            });
        });
    }

}

export default MongooseHelper;