/**
 * Created by Tony on 2017/3/21.
 */
var mongoose = require('mongoose');
var counterModel = mongoose.model("counters");

// All counter Must predefine in DB
const COUNTER_MODULE_NAME = {
    project: 'projects',
    user: 'users',
    bills: 'bills',
    rations: 'rations',
    GLJ: 'glj',
    rationMap: 'rationMaps',
    rationTree: 'rationChapterTrees',
    report: 'rptTemplates',
    fee: 'fees',
    unitPriceFile: 'unitPriceFile',
    unitPriceGLJ: 'unitPriceGLJ',
    template_bills: 'temp_bills',
    billsLib: 'billsLib',
    coeList: 'coeList',
    complementaryCoeList: 'complementary_coe_list',
    glj_list: 'glj_list',
    mix_ratio: 'mix_ratio',
    unit_price_file: 'unit_price_file',
    unit_price: 'unit_price'
}
/*const PROJECT_COUNTER = 'projects', USER_COUNTER = 'users', BILL_COUNTER = 'bills', RATION_COUNTER = 'rations',
    REPORT_COUNTER = 'rptTemplates', FEE_COUNTER = 'fees'*/

var counterDAO = function(){};

/*
 * callback = function (err, result) {
 *     result.value.sequence_value Ϊ�޸ĺ��id
 * }
 */
counterDAO.prototype.getIDAfterCount =async function(moduleName, stepCount, callback) {
    var sc = stepCount;
    if (isNaN(stepCount) || (stepCount < 0)) {
        sc = 1;
    } else if (!(/^-?\d+$/.test(stepCount))) {
        sc = Math.round(stepCount + 0.5);
    }
    if (callback === null) {
        let result = await counterModel.findOneAndUpdate({_id: moduleName}, { $inc: { sequence_value: sc } }, {'new':true});
        return await checkIDResult(result,moduleName,null)
    } else {
        counterModel.findOneAndUpdate({_id: moduleName}, { $inc: { sequence_value: sc } }, {'new':true}, function (err,result) {
            checkIDResult(result,moduleName,callback,err)
        });
    }
}

counterDAO.prototype.getIDAfterCountSync = async function (moduleName, stepCount) {
    let sc = stepCount;
    if (isNaN(stepCount) || (stepCount < 0)) {
        sc = 1;
    } else if (!(/^-?\d+$/.test(stepCount))) {
        sc = Math.round(stepCount + 0.5);
    }
    let result = await counterModel.findOneAndUpdate({_id: moduleName}, { $inc: { sequence_value: sc } }, {'new':true});
    return await checkIDResult(result,moduleName,null)
};

async function checkIDResult(result,moduleName,callback,err){
    if(result){
        return callback?callback(err,result):result;
    }else {
        return callback?counterModel.create({_id: moduleName,sequence_value:1},callback): await counterModel.create({_id: moduleName,sequence_value:1});
    }

}

counterDAO.prototype.getCurrentID = function(moduleName, callback) {
    if (callback) {
        counterModel.findOne({_id: moduleName}).exec()
        .then(function(result, err) {
            if (callback) {
                callback(result, err);
            }
        }
        );
        return null;
    } else {
        var rst = counterModel.findOne({_id: moduleName}).exec() ;
        return rst;
    }
}

module.exports = {
    counterDAO: new counterDAO(),
    moduleName: COUNTER_MODULE_NAME
};
