/**
 * Created by Tony on 2017/5/31.
 */
var mongoose = require('mongoose');
var dbm = require("../../../config/db/db_manager");
var db = dbm.getCfgConnection("Reports");
var Schema = mongoose.Schema;
var TreeNodeSchema = new Schema({
    ID:Number,
    ParentID:Number,
    NextSiblingID:Number,
    grp_type: Number, //建筑(const : 1)/公路(const 2)/其他 etc...
    node_type: Number,//节点类型：树节点(枝) 或 模板节点(叶)
    tpl_type: Number, //概算、预算、招投标 etc...
    userId: String,   //用户自定义模板用
    ref_ID: Number,   //
    name: String,
    isDeleted: Boolean
});

var TreeNodeModel = db.model("rpt_tpl_tree", TreeNodeSchema, "rpt_tpl_tree");
var counter = require('../../../public/counter/counter');

var RplTplTreeDAO = function(){};

RplTplTreeDAO.prototype.getTplTreeNodes = function(grp_type, userId, tpl_Type, callback) {
    //
};

RplTplTreeDAO.prototype.createTplTreeNode = function(nodeData, lastNodeId, callback) {
    counter.counterDAO.getIDAfterCount(counter.moduleName.report, 1, function(err, result){
        nodeData.ID = result.value.sequence_value;
        var node = new TreeNodeModel(nodeData);
        node.save(function (err, result) {
            if (err) {
                callback("树节点错误！", false);
            } else {
                if (lastNodeId > 0) {
                    TreeNodeModel.update({ID: lastNodeId}, {"NextSiblingID": nodeData.ID}, function(err, rst){
                        if (err) {
                            callback("树节点错误！", false);
                        } else {
                            callback(false, result);
                        }
                    });
                } else callback(false, result);
            }
        });
    });
};

module.exports = new RplTplTreeDAO();
