/**
 * Created by Tony on 2017/8/9.
 */

// import mongoose from "mongoose";
// let consts = require('../../../modules/main/models/project_consts');
// let projectConst = consts.projectConst;
let prjMdl = require('../../../modules/pm/models/project_model');
let projectDataMdl = require('../../../modules/main/models/project');
let projectFacade = require('../../../modules/main/facade/project_facade');

module.exports = {
    prepareProjectData: prepareProjectData,
    getBudgetSummayDatas: getBudgetSummayDatas,
    getGLJSummayDatas: getGLJSummayDatas
};

function prepareProjectData(userId, prjId, filter, callback) {
    let rawDataObj = null;
    let basicInformation = prjMdl.project.getBasicInfo(prjId);
    prjMdl.project.getUserProject(userId, prjId, function(err, msg, rst){
        if (!err) {
            rawDataObj = {};
            rawDataObj.prj = rst;
            projectDataMdl.getFilterData(prjId, filter, function (results) {
                if (results) {
                    rawDataObj.prjData = results;
                    basicInformation.then(function(basicRst) {
                        if (basicRst !== null && basicRst !== undefined) {
                            let doc = (rawDataObj.prj._doc)?rawDataObj.prj._doc:rawDataObj.prj;
                            doc.property.basicInformation = basicRst;
                        }
                        callback(false, 'Succeeded!', rawDataObj);
                    });
                    // callback(false, 'Succeeded!', rawDataObj);
                } else {
                    callback(true, 'No data were found!', null);
                }
            });
        } else {
            callback(err, msg, null);
        }
    });
}

async function getBudgetSummayDatas(prjIds) {
    return await projectFacade.getBudgetSummayDatas(prjIds);
}

async function getGLJSummayDatas(prjIds) {
    return await projectFacade.getGLJSummayDatas(prjIds);
}
