/**
 * Created by zhang on 2019/12/3.
 */
/**
 * Created by jimiz on 2017/4/9.
 */
let logger = require("../../../logs/log_helper").logger;
let pm_facade = require('../../pm/facade/pm_facade');
const Project = require('../../main/models/project');
const project_facade = require('../../main/facade/project_facade.js');
let controller = {
    importProject:async function (req){
        let data = req.body;
        return await pm_facade.downLoadProjectFile(data);
    },
    importChongqingProject:async function(req){
        let data = req.body;
        return await pm_facade.importChongqingProject(data);
    },
    importExampleProject:async function(req){
        let data = req.body;
        return await pm_facade.importExampleProject(data);
    },
    copyConstructionProject:async function(req){
      let data = req.body;
      return await pm_facade.copyConstructionProject(data);
    },
    prepareInitialData: async function(req) {
        const data = req.body;
        return await pm_facade.prepareInitialData(data.userID, data.compilationID, data.example);
    },
    exportProject:async function(req){
        let result={
            error:0
        };
        let data = JSON.parse(req.body.dataString);
        result.data = await pm_facade.exportProject(req.body.userID, data);
        return result
    },
    copyProject:async function(req){
        let result={
            error:0
        };
        let data = JSON.parse(req.body.dataString);
        result.data = await pm_facade.accessToCopyProject(req.body.userID,req.body.compilationID,data);
        return result
    },
    async importInterface (req) {
        const result = {
            error: 0
        };
        result.data = await pm_facade.importInterface(req.body.key, req.body.session);
        return result;
    },
    async getDataForInterface (req) {
        const result = {
            error: 0
        };
        result.data = await Project.getDataSync(req.body.project_id);
        return result;
    },
    async loadSEIProjectData (req) {
        const result = {
            error: 0
        };
        result.data = await project_facade.loadSEIProjectData(req.body.projectID);
        return result;
    }
};


module.exports = {
    action:async function(req,res){//自动跳转到URL对应的controller方法
        let result={
            error:0
        };
        try {
            let functionName = req.url.replace(/\//g,"");
            result = controller[functionName]?await controller[functionName](req):"";
        }catch (err){
            logger.err(err);
            if(typeof err == "string"){
                result.error=2;
                result.msg = err;
            }else {
                result.error=1;
                result.msg = "导入失败请检查文件！"
            }
        }
        res.json(result);
    }
};




