Skip to content

Commit

Permalink
restruct
Browse files Browse the repository at this point in the history
  • Loading branch information
szuprefix committed Jun 19, 2019
1 parent 1ed2a47 commit 7790d23
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 35 deletions.
21 changes: 19 additions & 2 deletions src/components/layout/table/DataTable.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<el-table :data="_value" ref="table" :span-method="spanMethod">
<el-table :data="_value" ref="table" :span-method="spanMethod" :cell-class-name="options.cellClassName">
<!--<el-table-column :prop="f.name" :column-key="f.name" :label="f.label || f.name"-->
<!--:min-width="f.min_width" :width="f.width" :formater="f.formater"-->
<!--:align="f.align" :class-name="f.type"-->
Expand All @@ -19,11 +19,27 @@
import {percent, toThousandslsFilter} from '../../../utils/filters'
import {sortBy} from 'lodash'
import DataTableColumn from './DataTableColumn.vue'
function flatten(ns, children_field_name){
let r = []
ns.forEach(a => {
let sns = a[children_field_name]
if(sns){
r = r.concat(flatten(sns, children_field_name))
}else{
let n = Object.assign({}, a)
// delete n[children_field_name]
r.push(n)
}
})
return r
}
export default{
props: {
value: Array,
defaultWidget: [Function, Object],
group: false,
options:{type:Object, default:() => {}},
fields: {
type: Array, default: function () {
return [{name: '__str__', label: '名称'}]
Expand Down Expand Up @@ -72,7 +88,8 @@
})
},
fieldNames (){
return this._fields.map(a => a.name)
let fs = flatten(this._fields, 'subColumns')
return fs.map(a => a.name)
},
spanMap () {
let vs = this._value
Expand Down
98 changes: 80 additions & 18 deletions src/components/rest/ModelBatchCreator.vue
Original file line number Diff line number Diff line change
@@ -1,28 +1,39 @@
<template>
<div>
<csv-input v-model="value" :field="{name:'content', type:'string', items: fields, callback:change}" ></csv-input>
</div>
<el-row>
<el-col :span="11">
<csv-input v-model="value"
:field="{name:'content', type:'string', items: csvItems, callback:change}"></csv-input>
</el-col>
<el-col :span="11" :offset="2">
<data-table :fields="tableItems" v-model="records" :group="true" :options="{cellClassName}"></data-table>
</el-col>
</el-row>
</template>
<script>
import BatchCreator from '../../utils/batch_creator'
import CsvInput from '../widgets/CsvInput.vue'
import DataTable from '../layout/table/DataTable.vue'
import queue_limit from '../../utils/async_queue'
export default{
props :{
structure: Object
props: {
structure: Object,
defaults: {
type: Object, default: a => {
}
}
},
data () {
return {
fields: [],
optionsLoading: true,
value: '',
records: []
records: [],
matrix: []
}
},
components: {CsvInput},
components: {CsvInput, DataTable},
created () {
let struct = BatchCreator.getStructure(this.structure)
BatchCreator.batchLoadOptions(struct.getModelNames()).then((r)=> {
this.fields = struct.getTableItems()
})
this.loadModelOptions()
//
// let struct = {models:BatchCreator.getModels(this.structure)}
Expand All @@ -35,13 +46,64 @@
// })
},
methods: {
change(d) {
this.records = d
// console.log(d)
this.$emit('change', d)
batchCreate(){
let st = this._structure
queue_limit(this.matrix, 1, a => st.create(a)).then(data => {
console.log(st.getNotExists())
})
},
change({matrix, records}){
let st = this._structure
let ds = st.genDataList(matrix)
this.records = records
this.matrix = matrix
console.log(JSON.stringify(matrix))
queue_limit(matrix, 1, a => st.checkPk(a)).then(data => {
// console.log(st.dmap)
console.log(st.foreignKeys[0].getNotExists())
})
this.$emit('change', {matrix, records})
},
cellClassName({row, column, rowIndex, columnIndex}){
let series = Object.values(row)
let d = this._structure.get(series)
return columnIndex < 3 ? ( d.id < 0 ? 'warning-row' : 'success-row') : ''
},
loadModelOptions () {
this.optionsLoading = true
BatchCreator.batchLoadOptions(this._structure.getModelNames()).then((r) => {
this.optionsLoading = false
this.$emit('struct', this._structure)
})
}
},
computed: {
_structure () {
return BatchCreator.getStructure(this.structure)
},
tableItems () {
if (this.optionsLoading) {
return []
}
return this._structure.getTableItems()
},
csvItems () {
return BatchCreator.genCsvItems(this.tableItems)
}
},
computed: {}
watched: {
_structure (val) {
this.loadModelOptions()
}
}
}
</script>
<style scoped></style>
<style>
.el-table .warning-row {
background: lightyellow;
}
.el-table .success-row {
background: lightgreen;
}
</style>
112 changes: 97 additions & 15 deletions src/utils/batch_creator.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,25 @@ import axios from '../configs/axios'
import queueLimit from './async_queue'
import {countBy, groupBy, merge, last, uniqueId} from 'lodash'

function flatten(ns, children_field_name){
let r = []
ns.forEach(a => {
let sns = a[children_field_name]
if(sns){
r = r.concat(flatten(sns, children_field_name))
}else{
let n = Object.assign({}, a)
// delete n[children_field_name]
r.push(n)
}
})
return r
}

function ModelAccount(appModelName) {
// let model={}
let model = Register.get(appModelName)
model.loadOptions()
// model.loadOptions()
return {
dmap: {},
count: 0,
Expand Down Expand Up @@ -44,7 +59,7 @@ function ModelAccount(appModelName) {
let a = this.foreignKeys[i]
rels[a.rel] = await a.getPk(d[a.rel])
}
console.log(rels)
// console.log(rels)
if (Object.values(rels).find(a => a < 0)) {
pk = -uniqueId()
this.set(d, pk)
Expand All @@ -57,14 +72,69 @@ function ModelAccount(appModelName) {
this.set(d, pk)
return pk
},
async checkPk(series){
let d = this.get(series)
if(d.id){
return d
}
let rels = {}
let p=0
for (var i in this.foreignKeys) {
let a = this.foreignKeys[i]
let ses = series.slice(p, a.count)
let fd = await a.checkPk(ses)
if(fd.id && fd.id>0){
rels[a.rel] = fd.id
}else{
d.id = -1
return d
}
}
let q = Object.assign({}, d, rels)
let o = await this.getRemote(q).catch(err => {})
d.id = o && o.id ? o.id : -1
return d
},
postObject(d){
return axios.post(this.model.listUrl, d).then(r => r.data)
},
async create(series){
let d = this.get(series)
if(d.id>0){
return d
}
let rels = {}
let p=0
for (var i in this.foreignKeys) {
let a = this.foreignKeys[i]
let ses = series.slice(p, a.count)
let fd = await a.create(ses)
if(fd.id && fd.id>0){
rels[a.rel] = fd.id
}else{
d.id = -1
return d
}
}
let q = Object.assign({}, d, rels)
let o = await this.postObject(q).catch(err => {})
d.id = o && o.id ? o.id : -1
return d

},
set (d, v) {
this.dmap[this.key(d)] = v
},
genData (series) {
let c = this.count
let pfs = this.plainFields
let pfsc = pfs.length
let r = {}
let r = this.get(series)
if(r){
return r
}else{
r= {}
}
let pfds = series.slice(c - pfsc)
pfs.forEach((f, i) => {
r[f] = pfds[i]
Expand All @@ -78,33 +148,35 @@ function ModelAccount(appModelName) {
p += fk.count
})
}

this.set(series, r)
return r
},
genDataList(dataframe){
return dataframe.map(s => this.genData(s))
},
getNotExists(){
return Object.keys(this.dmap).filter(a => this.dmap[a]<0).map(a => JSON.parse(a))
return Object.keys(this.dmap).filter(a => this.dmap[a].id<0).map(a => this.dmap[a])
},
getFieldMap(){
return model.config.rest_options.actions.POST
},
getTableItems() {
let tis = []
let fmap = model.config.rest_options.actions.POST
let r = []
let fmap = this.getFieldMap()
this.foreignKeys.forEach(a => {
let i = fmap[a.rel]
let i = Object.assign( {}, fmap[a.rel])
i.subColumns = a.getTableItems()
i.name=`${model.name}.${a.rel}`
tis.push(i)
i.modelVerboseName = a.model.verboseName
r.push(i)
} )
this.plainFields.forEach(a => {
let i = fmap[a]
let i = Object.assign( {}, fmap[a])
i.name=`${model.name}.${a}`
tis.push(i)
i.modelVerboseName = model.verboseName
r.push(i)
})
return tis

},
getCsvItems() {
return r

},
getModelNames(){
Expand Down Expand Up @@ -151,6 +223,16 @@ export default {
return r
},

genCsvItems(tableItems) {
let ls =flatten(tableItems,'subColumns')
let lcs = countBy(ls, a => a.label)
ls.forEach(a => {
if (lcs[a.label] > 1) {
a.label = a.modelVerboseName + a.label
}
})
return ls
},
getFieldsByStep(d, ls, rel, p) {
if (!ls) {
ls = []
Expand Down

0 comments on commit 7790d23

Please sign in to comment.