<template>
  <div>
    <el-button style="margin-top:10px" @click="genEntrty">生成实体类</el-button>
    <el-button style="margin-top:10px" @click="genControler">生成接口类</el-button>
    <el-table
      :data="tableData"
      :header-cell-style="headerStyle"
      border
      stripe
      fit
      style="width: 100%;margin-top: 25px;"
    >
      <el-table-column fixed prop="ColumnName" label="字段名" width="150"></el-table-column>
      <el-table-column prop="Description" min-width="200" label="说明"></el-table-column>
      <el-table-column prop="ColumnType" min-width="120" label="类型"></el-table-column>
      <el-table-column prop="CharMaxLength" min-width="60" label="长度"></el-table-column>
      <el-table-column prop="Nullable" align="center" label="不是 null"></el-table-column>
      <el-table-column prop="Default" min-width="260" label="默认值"></el-table-column>
    </el-table>
  </div>
</template>

<script>
import Vue from "vue";
import ElementUI from "element-ui";
import Axios from "axios";
import "element-ui/lib/theme-chalk/index.css";
import Opt from "../opt";
import { SStringBuilder } from "@persagy-web/base";
Vue.use(ElementUI);

export default {
  data: function() {
    return {
      tableData: [],
      headerStyle: {
        backgroundColor: "#e1e4e5",
        color: "#2b2b2b",
        lineHeight: "30px"
      } // 列表样式
    };
  },
  components: {},
  created: function() {
    this.getData();
  },
  mounted: function() {
    // alert(this.schema);
  },
  props: ["sys", "schema", "table"],
  computed: {},
  methods: {
    getData() {
      Axios.get(
        `${Opt.databaseDoc[this.sys]}/--database-doc--/column-list?schema=${
          this.schema
        }&table=${this.table}`,
        {}
      ).then(res => {
        this.tableData = res.data;
      });
    },
    //生成实体类
    genEntrty() {
      let strBuild = new SStringBuilder();
      strBuild.append(
        `package cn.sagacloud.server.datacenter.models.entities

import cn.sagacloud.server.datacenter.models.entities.base.BaseInfo
import cn.sagacloud.server.datacenter.models.entities.task.SchemeUtils
import com.alibaba.fastjson.annotation.JSONField
import com.sybotan.service.models.annotations.SCascade
import io.swagger.annotations.ApiModel
import io.swagger.annotations.ApiModelProperty
import javax.persistence.Column
import javax.persistence.Id
import javax.persistence.Table

/**
 * 资产信息实体类
 *
 * @author  自动生成
 */
@ApiModel(description = "资产信息实体类")
@Table(name = "${this.schema}.${this.table}")
open class ${this.toHump(this.table)} : BaseInfo(),Comparator<${this.toHump(this.table)}> {
`
      );
      this.tableData.forEach(element => {
        strBuild.append(`    /** ${element.Description} */`);
        if (element.Nullable == "YES") {
          strBuild.append(
            `    @ApiModelProperty(value = "${element.Description}")`
          );
        } else {
          strBuild.append(
            `    @ApiModelProperty(value = "${element.Description}", required = true)`
          );
        }
        strBuild.append(`    @Column(name = "${element.ColumnName}")`);
        strBuild.append(`    var ${this.toHump(element.ColumnName)}: ${this.typeToName(element.ColumnType)}? = null\n`);
      });

      strBuild.append(`} // Class ${this.toHump(this.table)}`);
      this.download(`${this.toHump(this.table)}.kt`,strBuild.toString())
    },
    //生成接口类
    genControler() {
      this.tableData;
    },
    // 数据包类型到实体类的转换
    typeToName(name) {
     const obj = {
        varchar: "String",
        char: "String",
        timestamptz: "Date",
        timestamp: "Date",
        text: "String",
        bool: "Boolean",
        int4: "Int",
        date: "Date",
        int2: "Int",
        json: "HashMap<String, Any?>",
        jsonb:"HashMap<String, Any?>"
      };
      if (name) {
        return obj[name] ? obj[name] : "String";
      } else {
        return "String";
      }
    },
    //转驼峰
    toHump(name){
      return name
    },
    // 下载文件
  download(filename, text) {
  var element = document.createElement('a');
  element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
  element.setAttribute('download', filename);

  element.style.display = 'none';
  document.body.appendChild(element);

  element.click();

  document.body.removeChild(element);
}
  }
};
</script>

<style scoped>
/deep/ table {
  margin: 0;
}
</style>
