-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
380 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<template> | ||
<div> | ||
<template v-for="f in foreignKeys"> | ||
<batch-creator :structure="f" :value="getForeignKeyData(f)" | ||
:appModelName="f.name" @optionLoaded="onForeignKeyLoaded"></batch-creator> | ||
|
||
</template> | ||
|
||
<h4>{{structure.label}}</h4> | ||
<data-table :value="value" :fields="structure.tableItems" v-if="structure.tableItems && value && value.length>0" :topActions="[]" :options="{elTable:{maxHeight:400}}"></data-table> | ||
</div> | ||
</template> | ||
<script> | ||
import DataTable from '../table/DataTable.vue' | ||
import {pick, last, uniqWith, isEqual} from 'lodash' | ||
import ModelView from '../../mixins/model_view' | ||
export default{ | ||
name: 'BatchCreator', | ||
mixins: [ModelView], | ||
props: { | ||
structure: Object, | ||
value: Array | ||
}, | ||
data () { | ||
return { | ||
activeList: [], | ||
loaded: false | ||
} | ||
}, | ||
components: {DataTable}, | ||
created(){ | ||
this.modelInit() | ||
this.modelLoadOptions().then(() => { | ||
this.structure.label = this.modelConfig.verbose_name | ||
if (!this.structure.rel) { | ||
this.structure.rel = last(this.structure.name.split('.')) | ||
} | ||
this.loaded = true | ||
this.checkStatus() | ||
}) | ||
// console.log(this.plainFieldData) | ||
}, | ||
methods: { | ||
onForeignKeyLoaded(context){ | ||
let st = context.structure | ||
Object.assign(this.structure.foreignKeys.find(fk => fk.name === st.name ), st) | ||
this.activeList.push(st) | ||
this.checkStatus() | ||
}, | ||
checkStatus() { | ||
if(this.activeList.length=== this.foreignKeys.length && this.loaded){ | ||
this.structure = Object.assign({}, this.structure, { | ||
tableItems:this.tableItems, | ||
count: this.activeList.map(a => a.count).reduce((a,b) => a+b, this.plainFields.length) | ||
}) | ||
// console.log(this.structure) | ||
this.$emit('optionLoaded', {structure: this.structure}) | ||
} | ||
// console.log([this.activeList.length, this.foreignKeys.length, this.loaded]) | ||
}, | ||
normalizeStructure(){ | ||
let r = Object.assign({}, this.structure) | ||
r.insertMode = r.insertMode || 'ignore' | ||
if (s.foreignKeys) { | ||
s.foreignKeys.forEach(fk => { | ||
let st = this.getStructure(fk) | ||
r.count += st.count | ||
r.foreignKeys.push(st) | ||
}) | ||
} | ||
r.count += s.plainFields.length | ||
if (!r.rel) { | ||
r.rel = last(s.name.split('.')) | ||
} | ||
return r | ||
}, | ||
getForeignKeyData(f){ | ||
// console.log(f) | ||
if(!f.tableItems){ | ||
return [] | ||
} | ||
let fns = [] | ||
let pairs = [] | ||
f.tableItems.forEach(ti => { | ||
let ofn = `${f.rel}.${ti.name}` | ||
fns.push(ofn) | ||
pairs.push([ofn, ti.name]) | ||
}) | ||
let data = this.value.map(a =>pick(a, fns)) | ||
data.forEach(d => { | ||
pairs.forEach(p => { | ||
d[p[1]] = d[p[0]] | ||
delete d[p[0]] | ||
}) | ||
}) | ||
console.log([pairs, fns, f]) | ||
return uniqWith(data, isEqual) | ||
} | ||
}, | ||
computed: { | ||
plainFields(){ | ||
return this.structure.plainFields || [] | ||
}, | ||
foreignKeys() { | ||
return this.structure.foreignKeys || [] | ||
}, | ||
tableItems(){ | ||
let r = [] | ||
let fcs = this.modelFieldConfigs | ||
let pfs = this.plainFields && this.plainFields.map(pf => fcs[pf]) | ||
let fks = this.foreignKeys && this.foreignKeys.map(fk => fcs[fk]) | ||
this.activeList.forEach(fk => { | ||
fk.tableItems.forEach(ti => { | ||
let label = fk.count>1 ?`${fk.label}.${ti.label}`:`${fk.label}` | ||
let synonyms = fk.count>1 ? []: fk.synonyms | ||
let nti = Object.assign({}, ti, {name:`${fk.rel}.${ti.name}`, label, synonyms}) | ||
// console.log(nti) | ||
r.push(nti) | ||
}) | ||
}) | ||
r=r.concat(pfs) | ||
return r | ||
}, | ||
plainFieldData(){ | ||
return this.value.map(a => pick(a, this.plainFields)) | ||
} | ||
} | ||
} | ||
</script> | ||
<style scoped></style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<template> | ||
|
||
<div v-loading="loading" :element-loading-text="loading"> | ||
<el-steps :active="step" finish-status="success"> | ||
<el-step title="上传Excel"></el-step> | ||
<el-step title="整理数据"></el-step> | ||
<el-step title="绑定字段"></el-step> | ||
<!--<el-step title="创建关联数据"></el-step>--> | ||
</el-steps> | ||
|
||
<el-upload :action="actionUrl" v-if="step===0" | ||
:show-file-list="false" :with-credentials="true" :auto-upload="false" | ||
:on-change="submitUpload"> | ||
<el-button size="small" type="primary">点击上传</el-button> | ||
<div slot="tip" class="el-upload__tip">只能上传excel文件,且不超过5MB</div> | ||
</el-upload> | ||
<template v-if="this.excelData && step===1"> | ||
<el-button @click="step --">上一步</el-button> | ||
<el-button @click="genFieldMap(); step ++; ">下一步</el-button> | ||
<book v-model="this.excelData"></book> | ||
</template> | ||
<div v-if="step===2"> | ||
<el-button @click="step --">上一步</el-button> | ||
<el-button @click="change(); step ++; ">确认</el-button> | ||
<column-bind v-model="fieldMap" :choices="bindChoices"></column-bind> | ||
</div> | ||
<!--<div v-if="step===3">--> | ||
<!--<el-button @click="step --">上一步</el-button>--> | ||
<!--<el-button @click="change">确认</el-button>--> | ||
<!--<!–<data-table v-model="importData" :fields="fields" group></data-table>–>--> | ||
<!--</div>--> | ||
</div> | ||
</template> | ||
<script> | ||
import ColumnBind from '../table/ColumnBind.vue' | ||
import DataTable from '../table/DataTable.vue' | ||
import Book from './Book.vue' | ||
import EditableLabel from '../widgets/EditableLabel.vue' | ||
import {SheetUtil, ColumnUtil} from '../../utils/sheets' | ||
import {uniqWith, isEqual, pick, zip} from 'lodash' | ||
export default{ | ||
props: { | ||
value: Object, | ||
binding: Object | ||
}, | ||
data () { | ||
return { | ||
fieldMap: {}, | ||
step: 0, | ||
excelData: null, | ||
importData: null | ||
} | ||
}, | ||
components: {Book, EditableLabel, ColumnBind, DataTable}, | ||
created () { | ||
}, | ||
methods: { | ||
genFieldMap(){ | ||
let d = {} | ||
this.binding.forEach(f => { | ||
let cs = [f.name, f.label].concat(f.synonyms) | ||
d[f.label] = this.fieldNames.find(n => cs.includes(n)) | ||
}) | ||
this.fieldMap = Object.assign(d) | ||
}, | ||
genData() { | ||
let data = uniqWith(this.block.data, isEqual) | ||
this.importData = data | ||
this.change() | ||
// this.$emit('data', {data}) | ||
}, | ||
onUploadSuccess(res, file) { | ||
this.excelData = file.response | ||
}, | ||
submitUpload (file, fileList){ | ||
this.loading = '上传中' | ||
let formData = new FormData() | ||
formData.append('file', file.raw) | ||
formData.append('orient', 'blocks') | ||
let config = { | ||
headers: { | ||
'Content-Type': 'multipart/form-data' | ||
} | ||
} | ||
this.$http.post('/common/excel/read/', formData, config).then(({data}) => { | ||
this.loading = false | ||
SheetUtil.normalize(data) | ||
this.excelData = data | ||
this.step++ | ||
}).catch(this.onServerResponseError) | ||
}, | ||
change(){ | ||
let pairs = [] | ||
let d = this.fieldMap | ||
this.binding.forEach(f => { | ||
pairs.push([d[f.label], f.name]) | ||
}) | ||
// console.log([pairs, this.importData]) | ||
ColumnUtil.rename(this.block, pairs) | ||
this.$emit('input', this.block) | ||
} | ||
}, | ||
computed: { | ||
block () { | ||
return this.excelData.sheets[0].blocks[0] | ||
}, | ||
fields(){ | ||
return this.block.fields | ||
}, | ||
fieldNames() { | ||
return this.fields.map(f => f.name) | ||
}, | ||
bindChoices() { | ||
return this.fields.map(f => { | ||
return {value: f.name, display_name: f.label} | ||
}) | ||
} | ||
} | ||
} | ||
</script> | ||
<style scoped></style> |
Oops, something went wrong.