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

import BaseController from "../../common/base/base_controller";
import mongoose from 'mongoose';
import GljDao from '../models/gljModel';
const gljDao = new GljDao();
const compilationModel = mongoose.model('compilation');
const stdGljLibModel = mongoose.model('std_glj_lib_map');
let config = require("../../../config/config.js");
const fs = require('fs');
import { checkCompilationPermission } from '../../common/base/base_util';

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) {
            checkCompilationPermission(req, res, stdGljLib.compilationId, '/stdGljRepository/main');
            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;
        }
        // await gljDao.copyLib(7,24); //UAT 部颁2018 -> 部颁2018计价标准
        // await gljDao.copyLib(7,25); //PROD 部颁2018 -> 部颁2018计价标准
        // await gljDao.copyLib(7,26); //PROD 部颁2018 -> 河南养护工料机库2022

        // await gljDao.copyLib(7,27); //UAT 部颁2018 -> 湖南养护工料机库(2022)
        // await gljDao.copyLib(7,28); //PROD 部颁2018 -> 湖南养护工料机库(2022)
        // await gljDao.copyLib(28,29); //PROD 湖南养护工料机库(2022) -> 湖南日常养护工料机库(2022)
        // await gljDao.copyLib(7,30); //PROD 部颁2018 -> 内蒙高速公路养护工料机库(2022)

        // await gljDao.copyLib(14,33); //PROD 部颁公路工料机库(2007营改增) -> 广东公路养护人材机库(2010) // 这个是公路PROD2.0的数据库，在hw服务器上，4080端口

        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;
