/**
 * Created by Tony on 2017/4/24.
 * 重新构造，不适宜生成多个定额库db，还是得统一在一个表
 */
var mongoose = require('mongoose');
var counter = require('../../../public/counter/counter');
var rationRepository = mongoose.model("std_ration_lib_map");

function createNewLibModel(rationLibObj){
    var rst = {};
    rst.dispName = rationLibObj.dispName;
    rst.appType = rationLibObj.appType?rationLibObj.appType:'construct';
    rst.localeType = rationLibObj.localeType?rationLibObj.localeType:'';
    rst.descr = rationLibObj.descr;
    rst.deleted = false;
    return rst;
}

let rationRepositoryDao = function(){};

rationRepositoryDao.prototype.getRealLibName = function(dispName,callback){
    if (callback) {
        rationRepository.find({"dispName": dispName}, function(err,data){
            if (err) {
                callback('Error', null);
            } else {
                callback(false, data);
            }
        });
    } else {
        var rst = rationRepository.find({"dispName": dispName}).exec();
        return rst;
    }
};

rationRepositoryDao.prototype.getLibIDByName = function(dispName, callback){
    rationRepository.findOne({"dispName": dispName}, function(err,data){
        if (err) {
            callback('Error', null);
        } else {
            callback(false, data.ID);
        }
    });
};

rationRepositoryDao.prototype.getRepositoryById = function(repId,callback){
    if (callback) {
        rationRepository.find({"ID": repId}, function(err,data){
            if (err) {
                callback('Error', null);
            } else {
                callback(false, data);
            }
        });
    } else {
        var rst = rationRepository.find({"ID": repId}).exec();
        return rst;
    }
};

rationRepositoryDao.prototype.addRationRepository = function( rationLibObj,callback){
    counter.counterDAO.getIDAfterCount(counter.moduleName.rationMap, 1, function(err, result){
        var rMap = createNewLibModel(rationLibObj);
        rMap.ID = result.sequence_value;
        new rationRepository(rMap).save(function(err, result) {
            if (err) callback("Error", null)
            else
                callback(false, result);
        });
    });
};

rationRepositoryDao.prototype.getDisplayRationLibs = function(callback) {
    rationRepository.find({"deleted": false}, function(err, data){
        if (err) {
            callback( 'Error', null);
        } else {
            callback( false, data);
        }
    });
};

rationRepositoryDao.prototype.updateName = function(orgName,newName,callback){
    rationRepository.find({"dispName":newName, "deleted": false}, function(err, data){
        if (data.length == 0) {
            rationRepository.update({dispName:orgName},{$set:{dispName:newName}},function(err){
                if(err) callback("err",false);
                else callback(false,"ok")
            })
        } else
            callback("不可重名！",false);
    });
}

rationRepositoryDao.prototype.deleteRationLib = function(rationName,callback){
    rationRepository.find({"dispName":rationName, "deleted": false}, function(err, data){
        if (data.length == 1) {
            rationRepository.update({dispName:rationName},{$set:{deleted: true}},function(err){
                if(err) callback("err",false);
                else callback(false,"ok")
            })
        } else
            callback("删除失败！",false);
    });
}

module.exports = {
    Dao: new rationRepositoryDao(),
    Model: rationRepository
}
