/**
 * Created by Tony on 2017/4/21.
 */

var mongoose = require("mongoose");
var async = require("async");
var rationChapterTreeModel = mongoose.model("std_ration_lib_ration_chapter_trees");
var counter = require('../../../public/counter/counter');

var rationChapterTreeDAO = function(){};

rationChapterTreeDAO.prototype.getRationChapterTree = function(rationLibId,callback){
    rationChapterTreeModel.find({"rationRepId": rationLibId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
        if(data.length) callback(false,data);
        else  if(err) callback("获取定额树错误！",false)
        else callback(false,false);
    })
}

rationChapterTreeDAO.prototype.getRationChapterTreeByRepId = function(repId,callback){
    rationChapterTreeModel.find({"rationRepId": repId, "$or": [{"isDeleted": null}, {"isDeleted": false} ]},function(err,data){
        if(data.length) callback(false,data);
        else  if(err) callback("获取定额树错误！",false)
        else callback(false,false);
    })
}

rationChapterTreeDAO.prototype.createNewNode = function(libId, lastNodeId, nodeData,callback){
    counter.counterDAO.getIDAfterCount(counter.moduleName.rationTree, 1, function(err, result){
        nodeData.rationRepId = libId;
        nodeData.ID = result.sequence_value;
        var node = new rationChapterTreeModel(nodeData);
        node.save(function (err, result) {
            if (err) {
                callback("章节树ID错误！", false);
            } else {
                if (lastNodeId > 0) {
                    rationChapterTreeModel.update({ID: lastNodeId}, {"NextSiblingID": nodeData.ID}, function(err, rst){
                        if (err) {
                            callback("章节树ID错误！", false);
                        } else {
                            callback(false, result);
                        }
                    });
                } else callback(false, result);
            }
        });
    });
},

rationChapterTreeDAO.prototype.removeNodes = function(nodeIds, preNodeId, preNodeNextId, callback){
    var functions = [];
    if (preNodeId != -1) {
        functions.push((function(nodeId, nextId) {
            return function(cb) {
                rationChapterTreeModel.update({ID: nodeId}, {"NextSiblingID": nextId}, cb);
            };
        })(preNodeId, preNodeNextId));
    }
    for (var i=0; i < nodeIds.length; i++) {
        functions.push((function(nodeId) {
            return function(cb) {
                rationChapterTreeModel.update({ID: nodeId}, {"isDeleted": true}, cb);
            };
        })(nodeIds[i]));
    }
    async.parallel(functions, function(err, results) {
        callback(err, results);
    });
}

rationChapterTreeDAO.prototype.updateNodes = function(nodes,callback){
    var functions = [];
    for (var i=0; i < nodes.length; i++) {
        //var md = new rationChapterTreeModel(nodes[i]);
        //md.isNew = false;
        functions.push((function(doc) {
            return function(cb) {
                rationChapterTreeModel.update({ID: doc.ID}, doc, cb);
            };
        })(nodes[i]));
    }
    async.parallel(functions, function(err, results) {
        callback(err, results);
    });
};

rationChapterTreeDAO.prototype.getRationChapter = function (repId, chapterID, callback) {
    if (callback) {
        rationChapterTreeModel.findOne({rationRepId: repId, ID: chapterID}, '-_id').exec()
            .then(function (result, err) {
                if (err) {
                    callback(1, '找不到定额章节' , null);
                } else {
                    callback(0, '', result);
                }
            });
        return null;
    } else {
        return rationChapterTreeModel.findOne({rationRepId: repId, ID: chapterID}, '-_id').exec();
    }
}

module.exports = {
    Model: rationChapterTreeModel,
    Dao: new rationChapterTreeDAO()
};
//module.exports = new rationChapterTreeDAO()