/**
 * 引导页相关控制器
 *
 * @author CaiAoLin
 * @date 2017/6/12
 * @version
 */
const BaseController = require("../../common/base/base_controller");
const UserModel = require("../models/user_model");
const CompilationModel = require("../models/compilation_model");

class BootController extends BaseController {

    /**
     * 引导页
     *
     * @param {object} request
     * @param {object} response
     * @return {void}
     */
    async boot(request, response) {
        let userModel = new UserModel();
        let sessionUser = request.session.sessionUser;
        let compilationId = request.params.compilation;
        let sessionCompilation = request.session.sessionCompilation;
        let compilationVersion = request.session.compilationVersion;

        // 判断是否有存入编办信息
        if (sessionCompilation === undefined && compilationId !== '') {
            let compilationModel = new CompilationModel();
            let compilationData = await compilationModel.getCompilationById(compilationId);
            // 判断当前用户的是使用免费版还是专业版
            compilationVersion = await userModel.getVersionFromUpgrade(sessionUser.ssoId, compilationId);
            request.session.compilationVersion = compilationVersion.version;
            request.session.sessionUser.compilationLock = compilationVersion.lock;
            request.session.sessionCompilation = compilationData;
            if(sessionUser.latest_used !== compilationId) userModel.updateLatestUsed(sessionUser.id,compilationId);
        }

        // 判断是否已填写信息
        let userData = await userModel.findDataByCondition({_id: sessionUser.id});
        if (userData.company !== '' && userData.real_name !== '') {
            return response.redirect('/pm');
        }

        let renderData = {
            provinceList: userModel.province,
            companyTypeList: userModel.companyType,
            companyScaleList: userModel.companyScale,
            compilation: request.params.compilation,
            compilationName: request.session.sessionCompilation.name,
            versionName: request.session.compilationVersion,
        };
        response.render('users/html/login-infoinput', renderData);
    }

}
module.exports = BootController;
