Blame view

antis-ncc-admin/src/mixins/generator/common.js 5.26 KB
03207d5d   wwk   1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  import { DataModelFieldList } from '@/api/systemData/dataModel'
  import { getDataSourceListAll } from '@/api/systemData/dataSource'
  export default {
    provide() {
      return {
        getFormInfo: this.getFormInfo
      }
    },
    data() {
      return {}
    },
    methods: {
      closeDialog(isRefresh) {
        this.visible = false
        this.$emit('close', isRefresh)
      },
      prve() {
        this.activeStep -= 1
      },
      stepChick(key) {
        if (this.activeStep <= key) return
        this.activeStep = key
      },
      onDbChange() {
        this.tables = []
      },
      openTableBox() {
        if (!this.dataForm.dbLinkId) {
          this.$message({
            message: '请先选择数据库',
            type: 'error',
            duration: 1000
          })
          return
        }
        this.formVisible = true
      },
      async colseForm(data) {
        const type = this.dataForm.type
        let queryType = 0
        if (type == 3 || type == 4 || type == 5) queryType = 1
        let checkList = []
        if (!this.tables.length) {
          for (let i = 0; i < data.length; i++) {
            const e = data[i];
            let relationTable = data[0].table
            let typeId = i == 0 ? "1" : "0"
            let res = await DataModelFieldList(this.dataForm.dbLinkId, e.table, queryType)
            let fields = res.data.list.map(o => ({ field: o.field, fieldName: o.fieldName, dataType: o.dataType }))
            let item = {
              relationField: "",
              relationTable: i == 0 ? '' : relationTable,
              table: e.table,
              tableName: e.tableName,
              tableField: '',
              typeId,
              fields
            }
            checkList.push(item)
          }
          this.relationTable = checkList[0].table
          this.mainTableFields = checkList[0].fields
          this.tables = checkList
        } else {
          for (let i = 0; i < data.length; i++) {
            const e = data[i];
            let boo = this.tables.some(o => o.table == e.table)
            if (!boo) {
              let res = await DataModelFieldList(this.dataForm.dbLinkId, e.table, queryType)
              let fields = res.data.list.map(o => ({ field: o.field, fieldName: o.fieldName, dataType: o.dataType }))
              let item = {
                relationField: "",
                relationTable: this.relationTable,
                table: e.table,
                tableName: e.tableName,
                tableField: '',
                typeId: "0",
                fields
              }
              checkList.push(item)
            }
          }
          this.tables = [...this.tables, ...checkList]
        }
      },
      async updateFields() {
        if (!this.tables.length) return
        this.dataForm.dbLinkId = this.dataForm.dbLinkId || '0'
        const type = this.dataForm.type
        let queryType = 0
        if (type == 3 || type == 4 || type == 5) queryType = 1
        for (let i = 0; i < this.tables.length; i++) {
          let res = await DataModelFieldList(this.dataForm.dbLinkId, this.tables[i].table, queryType)
          let fields = res.data.list.map(o => ({ field: o.field, fieldName: o.fieldName, dataType: o.dataType }))
          this.tables[i].fields = fields
          if (this.tables[i].typeId == '1') {
            this.mainTableFields = this.tables[i].fields
            this.relationTable = this.tables[i].table
          }
        }
      },
      getDbOptions() {
        const defaultItem = {
          fullName: '',
          children: [{
            fullName: '默认数据库',
            id: '0'
          }]
        }
        getDataSourceListAll().then(res => {
          const list = [defaultItem, ...res.data.list]
          this.dbOptions = list.filter(o => o.children && o.children.length)
        }).catch(() => {
          this.dbOptions = [defaultItem]
        })
      },
      delItem(row, index) {
        this.tables.splice(index, 1);
        if (row.typeId == '1' && this.tables.length) {
          this.tables[0].typeId = '1'
          this.tables[0].relationTable = ''
          this.mainTableFields = this.tables[0].fields
          this.relationTable = this.tables[0].table
        }
      },
      changeTable(row) {
        this.relationTable = row.table
        this.mainTableFields = row.fields
        for (let i = 0; i < this.tables.length; i++) {
          this.$set(this.tables[i], "typeId", this.tables[i].table === row.table ? '1' : '0')
          this.$set(this.tables[i], "relationTable", this.tables[i].table === row.table ? '' : this.relationTable)
          this.$set(this.tables[i], "relationField", "")
          this.$set(this.tables[i], "tableField", "")
        }
      },
      getFormInfo() {
        return this.dataForm
      },
      exist() {
        let isOk = true;
        for (let i = 0; i < this.tables.length; i++) {
          const e = this.tables[i];
          if (e.typeId == '0') {
            if (!e.tableField) {
              this.$message({
                showClose: true,
                message: `表${e.table}外键字段不能为空`,
                type: 'warning',
                duration: 1000
              });
              isOk = false
              break
            }
          }
          if (e.typeId == '0') {
            if (!e.relationField) {
              this.$message({
                showClose: true,
                message: `表${e.table}关联主键不能为空`,
                type: 'warning',
                duration: 1000
              });
              isOk = false
              break
            }
          }
        }
        return isOk;
      }
    }
  }