ttk-app-core
Version:
enterprise develop framework
183 lines (163 loc) • 6.47 kB
JavaScript
import React from 'react'
import { Map, List, fromJS } from 'immutable'
import { action as MetaAction, AppLoader } from 'edf-meta-engine'
import config from './config'
import { FormDecorator, Select } from 'edf-component'
import {fetch} from 'edf-utils'
const Option = Select.Option
class action {
constructor(option) {
this.metaAction = option.metaAction
this.config = config.current
this.webapi = this.config.webapi
this.voucherAction = option.voucherAction
}
onInit = ({ component, injections }) => {
this.voucherAction.onInit({ component, injections })
this.component = component
this.injections = injections
if (this.component.props.setOkListener) {
this.component.props.setOkListener(this.onOk)
}
injections.reduce('init')
this.load()
}
load = async () => {
if(this.component.props.initData) {
this.injections.reduce('load', this.component.props.initData)
}
}
selectChidren = (type) => {
let list = this.metaAction.gf('data.other.customer').toJS()
if(type == '供应商'){
list = this.metaAction.gf('data.other.supplier').toJS()
}
if(list.length == 0) return
return list.map(item => {
return <Option value={item && item.id}>{item && item.name}</Option>
})
}
onFieldChange = (value, index) => {
let list = this.metaAction.gf('data.list').toJS()
let item = list[index]
item.archiveId = value
let selectList = this.metaAction.gf('data.other.customer').toJS()
if(item.archiveType == '供应商') {
selectList = this.metaAction.gf('data.other.supplier').toJS()
}
selectList.filter(o => {
if(o.id == value) {
item.archiveName = o.name
item.ba = o
}
})
this.injections.reduce('load', list)
}
rptTypeChange = async(index, value) => {
//改变性质时如果客户/供应商已经有值,判断对应档案是否存在,不存在时新建对应档案,已选项不清空
let list = this.metaAction.gf('data.list').toJS()
let item = list[index]
item.archiveType = value
if(value == '供应商') {
//item.receiptAndDisbursementDirection = '支出'
let selectList = this.metaAction.gf('data.other.supplier').toJS()
let option = { archiveName: 'ba_supplier' }
if( item.ba ) {
item = await this.getSelectData(selectList, option, 'supplier', item)
}
}else {
//item.receiptAndDisbursementDirection = '收入'
let selectList = this.metaAction.gf('data.other.customer').toJS()
let option = { archiveName: 'ba_customer' }
if( item.ba ) {
item = await this.getSelectData(selectList, option, 'customer', item)
}
}
//通过selectData替换当前选中值 ba为当前选中值所有项
this.injections.reduce('load', list)
}
getSelectData = async(selectList, option, type, item) => {
if(selectList.length == 0) {
let data = await this.webapi.fileList(type)
selectList = data.list || []
}
let selectData = selectList.filter(o => o.name == item.archiveName)
if(selectData.length == 0 ){
let code = await this.webapi.getCode(option)
item.ba.code = code
if(item.ba.ts) delete item.ba.ts
if(item.ba.id) delete item.ba.id
let select = await this.webapi.create(item.ba, type)
selectData.push(select)
let data = await this.webapi.fileList(type)
selectList = data.list
}
item.archiveId = selectData[0].id
item.archiveName = selectData[0].name
this.injections.reduce('updatefile',
{
path: 'data.other.' + type,
value: selectList
}
)
return item
}
addSupplierCustomer = async(type, _rowIndex) => {
let title = '客户', name = 'app-card-customer'
if(type == 'supplier'){
title = '供应商'
name = 'app-card-vendor'
}
const ret = await this.metaAction.modal('show', {
title: title,
width: 700,
children: this.metaAction.loadApp(
name, {
store: this.component.props.store
}
)
})
if (ret) {
if (!ret.isEnable) {
return
}
let res = await this.webapi[type]()
if (res) {
let value = res.list.filter(item => { return item.isEnable == true }), obj = {}
if(type == 'supplier') {
obj['data.other.supplier'] = fromJS(value)
}else{
obj['data.other.customer'] = fromJS(value)
}
let list = this.metaAction.gf('data.list').toJS()
list[_rowIndex].archiveId = ret.id
list[_rowIndex].archiveName = ret.name
// 这个时候是不是要写ba
list[_rowIndex].ba = ret
obj['data.list'] = fromJS(list)
this.injections.reduce('update', obj);
}
}
}
onOk = async () => {
let list = this.metaAction.gf('data.list').toJS()
let option = list.map(item =>{
return {
name: item.name,
archiveName: item.archiveName,
archiveType: item.archiveType,
archiveId: item.archiveId ? item.archiveId : null
}
})
let save = await this.webapi.save(option)
if(save) return true
}
}
export default function creator(option) {
const metaAction = new MetaAction(option),
voucherAction = FormDecorator.actionCreator({ ...option, metaAction }),
o = new action({ ...option, metaAction, voucherAction }),
ret = { ...metaAction, ...voucherAction, ...o }
metaAction.config({ metaHandlers: ret })
return ret
}