<template>
</template>

<script>
import {convertToTreeData} from 'ap-util/util/DataUtil'
import {getUserConfig} from './../dev/DevUtil'

export default {
    name: 'BackgroundBase',
    data() {
        return {
            selectedTables: [],
            createEntity: 1,
            createEntityFD: 1,
            createEntityTF: 1,
            createController: 1,
            createService: 1,
            createDao: 1,
            createXml: 0,
            backgroundMsg: '',
            userConfig: getUserConfig(),
            backgroundOtherParam: {}, // 前端传给后台的额外参数
        }
    },
    methods: {

        createFile(projectName, list) {
            this.$request({
                url: '/apd/dev/DevGenerateCode/createFile',
                method: 'post',
                data: {
                    list: JSON.stringify(list),
                    projectName: projectName
                }
            }).then(response => {
                this.backgroundMsg = response.data
                this.$message.success('后台代码，生成完成！')
            })
        },
        generateCode() {
            if (this.selectedTables.length < 1) {
                this.$message.error('请选择表！')
                return
            }

            const javaUrl = this.userConfig.fdJavaPath
            if (javaUrl == null || javaUrl == '') {
                this.$message.error('请先配置共通参数。【顶栏】->【配置】')
                return
            }

            this.$confirm(`确定在【${javaUrl}】下生成文件?`, '提示', {
                confirmButtonText: '确定',
                type: 'warning'
            }).then(() => {
                this.doGenerateCode()
            });
        },
        doGenerateCode() {
            const tableList = []
            for (let i = 0; i < this.selectedTables.length; i++) {
                const selectedTable = this.selectedTables[i]
                tableList.push({
                    tableName: selectedTable.tableName,
                    tableSchema: selectedTable.tableSchema
                })
            }

            let tableSchema = tableList[0].tableSchema;
            let javaPath = this.userConfig.fdJavaPath;
            let projectName = javaPath.substring(javaPath.lastIndexOf('\\') + 1);
            // 项目名和数据库不一致，警告
            if (projectName.indexOf(tableSchema) > -1) {
                this.getBackgroundCode(tableList);
                return;
            }
            this.$confirm(`警告：项目【${projectName}】和数据库【${tableSchema}】不匹配！`, '提示', {
                confirmButtonText: '确认生成',
                cancelButtonText: '取消',
                type: 'error'
            }).then(() => {
                this.getBackgroundCode(tableList);
            });
        },
        getBackgroundCode(tableList) {
            let backgroundParam = {
                tableList: JSON.stringify(tableList),
                createEntity: this.createEntity,
                createEntityFD: this.createEntityFD,
                createEntityTF: this.createEntityTF,
                createController: this.createController,
                createService: this.createService,
                createDao: this.createDao,
                createXml: this.createXml,
                javaUrl: this.userConfig.fdJavaPath,
                sourceId: this.userConfig.fdSourceId
            };
            let mergedParam = {...backgroundParam, ...this.backgroundOtherParam};

            this.$request({
                url: '/apd/dev/DevGenerateCode/getBackgroundCode',
                method: 'post',
                data: mergedParam
            }).then(response => {
                // 生成文件
                this.createFile(this.userConfig.fdJavaHref, response.data)
            })
        }

    }
}
</script>

<style scoped>
</style>
