/**
 * Created by Mai on 2017/1/18.
 */
let ProjectsData = require('../models/project_model').project;
let projType = require('../models/project_model').projType;

//统一回调函数
let callback = function(req, res, err, message, data){
    res.json({error: err, message: message, data: data});
};



module.exports = {
    checkRight: function (req, res) {
        let data = JSON.parse(req.body.data);
        if (data.user_id) {
            return data.user_id === req.session.sessionUser.ssoId;
        } else {
            return false;
        }
    },
    checkProjectRight: function (userId, projectId, callback) {
        ProjectsData.getProject(projectId).then(function (result) {
            /**
             * result._doc.userID(Number): MongoDB
             * userId(String): Session.userID
             */
            if (result._doc.userID == userId && result._doc.projType === projType.tender) {
                callback(true);
            } else {
                callback(false);
            }
        }).catch(function (err) {
            callback(false);
        });
    },
    getProjects: function(req, res){
        ProjectsData.getUserProjects(req.session.sessionUser.ssoId, function(err, message, projects){
            if (projects) {
                callback(req, res, err, message, projects);
            } else {
                callback(req, res, err, message, null);
            }
        });
    },
    updateProjects: function (req, res) {
        let data = JSON.parse(req.body.data);
        ProjectsData.updateUserProjects(req.session.sessionUser.ssoId, data.updateData, function (err, message, data) {
            if (err === 0) {
                callback(req, res, err, message, data);
            } else {
                callback(req, res, err, message, null);
            }
        });
    },
    copyProjects: function (req, res) {
        let data = JSON.parse(req.body.data);
        ProjectsData.copyUserProjects(req.session.sessionUser.ssoId, data.updateData, function (err, message, data) {
            if (err === 0) {
                callback(req, res, err, message, data);
            } else {
                callback(req, res, err, message, null);
            }
        });
    },
    rename: function (req, res) {
        let data = JSON.parse(req.body.data);
        ProjectsData.rename(req.session.sessionUser.ssoId, data.id, data.newName, function (err, message) {
            callback(req, res, err, message, null);
        });
    },
    getProject: function(req, res){
        let data = JSON.parse(req.body.data);
        ProjectsData.getUserProject(req.session.sessionUser.ssoId, data.proj_id, function(err, message, data){
            if (err === 0) {
                callback(req, res, err, message, data);
            } else {
                callback(req, res, err, message, null);
            }
        });
    },
    beforeOpenProject: function (req, res) {
        let data = JSON.parse(req.body.data);
        ProjectsData.beforeOpenProject(req.session.sessionUser.ssoId, data.proj_id, data.updateData, function (err, message, data) {
            callback(req, res, err, message, data);
        });
    },
    getNewProjectID: function (req, res) {
        let data = JSON.parse(req.body.data);
        ProjectsData.getNewProjectID(data.count, function (err, message, data) {
            callback(req, res, err, message, data);
        });
    }
}