/**
 * Created by zhang on 2018/9/3.
 */
import BaseController from "../../common/base/base_controller";
import featureFacade from "../facade/project_feature_facade";
let config = require("../../../config/config.js");
let logger = require('../../../logs/log_helper').logger;
import CompilationModel from '../../users/models/compilation_model';
let mongoose = require('mongoose');
let featureLibModel = mongoose.model('std_project_feature_lib');

class FeatureController extends BaseController{
    async main(request, response) {
        //
        let allLibs = await featureLibModel.find({});
        for (let lib of allLibs) {
            if (lib.name.indexOf('重庆2018') !== -1) {
                await featureLibModel.update({ID: lib.ID},
                    {$set: {compilationId: '5b52b027fd3bb0000b257cf8', compilationName: '重庆定额(2018)'}}, {multi: true});
            } else if (lib.name.indexOf('甘肃2013') !== -1) {
                await featureLibModel.update({ID: lib.ID},
                    {$set: {compilationId: '5b75146cfa63aa000df7ff3c', compilationName: '甘肃定额(2013)'}}, {multi: true});
            } else if (lib.name.indexOf('江西2017') !== -1) {
                await featureLibModel.update({ID: lib.ID},
                    {$set: {compilationId: '5b754aacfa63aa000df80481', compilationName: '江西定额(2017)'}}, {multi: true});
            }else if (lib.name.indexOf('浙江2010') !== -1) {
                await featureLibModel.update({ID: lib.ID},
                    {$set: {compilationId: '5b85218494c4a2000d3d8f5c', compilationName: '浙江定额(2010)'}}, {multi: true});
            }else if (lib.name.indexOf('内蒙古2017') !== -1) {
                await featureLibModel.update({ID: lib.ID},
                    {$set: {compilationId: '5c1760b489ad9a000dd1e78a', compilationName: '内蒙古定额(2017)'}}, {multi: true});
            }else if (lib.name.indexOf('浙江2017') !== -1) {
                await featureLibModel.update({ID: lib.ID},
                    {$set: {compilationId: '5c246ed917f3a0000dbf7111', compilationName: '浙江定额(2018)'}}, {multi: true});
            }else if (lib.name.indexOf('广东') !== -1) {
                await featureLibModel.update({ID: lib.ID},
                    {$set: {compilationId: '5c66649650da2d000d8d37ba', compilationName: '广东定额(2018)'}}, {multi: true});
            }
        }
        //
        let compilationModel = new CompilationModel();
        let compilationList = await compilationModel.getCompilationList({_id: 1, name: 1});
        compilationList.unshift({_id: 'all', name: '所有'});
        let activeCompilation = compilationList.find(compilation => compilation._id.toString() === request.query.filter);
        if (activeCompilation) {
            activeCompilation.active = 'active';
        } else {
            compilationList[0].active = 'active'
        }
        let filter = request.query.filter ? {compilationId: request.query.filter} : {};
        let featureLibs = await featureFacade.findByCondition(filter,{feature:0},false);
        let randerData = {
            title:'工程特征库',
            userAccount: request.session.managerData.username,
            userID: request.session.managerData.userID,
            featureLibs:featureLibs,
            compilationList: compilationList,
            layout: 'maintain/common/html/layout'
        };
        response.render("maintain/project_feature_lib/html/main", randerData);
    }
    async addLib(request, response){
        try {
            await featureFacade.addLib(request.body);
        }catch (error) {
            console.log(error);
        }
        response.redirect(request.headers.referer);
    }
    async findLib(request, response){
        let result={
            error:0
        };
        try {
            let data = request.body.data;
            data = JSON.parse(data);
            let conditions={'ID' : data.ID};
            let resultData = await featureFacade.findByCondition(conditions);
            result.data=resultData;
        }catch (err){
            console.log(err);
            result.error=1;
            result.message = err.message;
        }
        response.json(result);
    }
    async saveLib(request, response){
        let result={
            error:0
        };
        try {
            let data = request.body.data;
            data = JSON.parse(data);
            let resultData= await featureFacade.saveLib(data);
            result.data=resultData;
        }catch (err){
            console.log(err);
            result.error=1;
            result.message = err.message;
        }
        response.json(result);
    }
    async deleteLibByID(request,response){
        logger.info(`delete projectFeatureLib ${request.ip}`);
        let result={
            error:0
        };
        try {
            let data = request.body.data;
            data = JSON.parse(data);
            let resultData= await featureFacade.deleteLibByID(data.ID);
            result.data=resultData;
        }catch (err){
            console.log(err);
            result.error=1;
            result.message = err.message;
        }
        response.json(result);
    }
    async edit(request,response){
        //先取出替换库信息：
        let libID = request.params.libID;
        let featureLib = await featureFacade.findByCondition({'ID':libID});
        if(featureLib){
            let randerData = {
                title:'工程特征库',
                mainURL:'/projectFeature/main',
                libName:featureLib.name,
                userAccount: request.session.managerData.username,
                userID: request.session.managerData.userID,
                featureList:JSON.stringify(featureLib.feature),
                libID:libID,
                LicenseKey:config.getLicenseKey(process.env.NODE_ENV),
                layout: 'maintain/common/html/edit_layout'
            };
            response.render("maintain/project_feature_lib/html/edit", randerData);
        }else {
            response.redirect(request.headers.referer);
        }
    }
}
export default FeatureController;