/**
 * Created by Zhong on 2017/8/11.
 * 标准工料机库页面控制器
 */

import BaseController from "../../common/base/base_controller";
import mongoose from 'mongoose';
const compilationModel = mongoose.model('compilation');
const stdGljLibModel = mongoose.model('std_glj_lib_map');
let config = require("../../../config/config.js");
const fs = require('fs');
class ViewsController extends BaseController{
    redirectMain(req, res){
        res.render('maintain/std_glj_lib/html/main.html',
        {
            userAccount: req.session.managerData.username
        });
    }
    async redirectGlj(req, res){
        let overWriteUrl = null;
        let stdGljLib = await stdGljLibModel.findOne({ID: req.query.gljLibId, deleted: false});
        let priceProperties = [],
            consumeAmtProperties = [];
        if(stdGljLib){
           let compilation = await compilationModel.findOne({_id: mongoose.Types.ObjectId(stdGljLib.compilationId)});
           priceProperties = compilation.priceProperties ? compilation.priceProperties : [];
           consumeAmtProperties = compilation.consumeAmtProperties ? compilation.consumeAmtProperties : [];
           let absoluteUrl = compilation.overWriteUrl ? req.app.locals.rootDir + compilation.overWriteUrl : req.app.locals.rootDir;
           overWriteUrl = fs.existsSync(absoluteUrl) && fs.statSync(absoluteUrl).isFile()? compilation.overWriteUrl : null;
        }
        res.render('maintain/std_glj_lib/html/gongliao.html',
        {
            userAccount: req.session.managerData.username,
            LicenseKey:config.getLicenseKey(process.env.NODE_ENV),
            priceProperties: JSON.stringify(priceProperties),
            consumeAmtProperties: JSON.stringify(consumeAmtProperties),
            overWriteUrl: overWriteUrl
        });
    }
}

export default ViewsController;
