oak-pay-business/es/components/sysAccountMove/create/index.js

146 lines
5.3 KiB
JavaScript

import { uniq } from "oak-domain/lib/utils/lodash";
import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
import { ThousandCont, ToCent, ToYuan } from 'oak-domain/lib/utils/money';
export default OakComponent({
properties: {
systemId: '',
entities: [],
onSuccess: (id) => undefined,
},
data: {
accounts: [],
fromEntity: '',
fromEntityId: '',
toEntity: '',
toEntityId: '',
price: 0,
externalId: '',
remark: '',
max: 0,
},
lifetimes: {
ready() {
this.refreshSysAccount();
}
},
methods: {
async refreshSysAccount() {
const { entities, systemId } = this.props;
const accounts2 = await Promise.all(uniq(['wpAccount', 'offlineAccount'].concat(entities || [])).map(async (entity) => {
const { data: rows } = await this.features.cache.refresh(entity, {
data: entity === 'offlineAccount' ? {
id: 1,
price: 1,
name: 1,
type: 1,
channel: 1,
} : {
id: 1,
price: 1,
},
filter: {
systemId,
}
});
const accounts = rows.map((row) => {
const { id, price, name, type, channel } = row;
return {
id: id,
entity: entity,
priceStr: ThousandCont(ToYuan(price)),
price: price,
label: entity === 'offlineAccount' ? `${this.t(`${entity}:name`)}-${this.t(`offlineAccount:v.type.${type}`)}` : this.t(`${entity}:name`),
name: name || undefined,
};
});
return accounts;
}));
this.setState({
accounts: accounts2.flat(),
});
},
setFromId(id) {
const { accounts } = this.state;
const account = accounts.find(ele => ele.id === id);
this.setState({
fromEntity: account.entity,
fromEntityId: account.id,
max: ToYuan(account.price),
});
},
setToId(id) {
const { accounts } = this.state;
const account = accounts.find(ele => ele.id === id);
this.setState({
toEntity: account.entity,
toEntityId: account.id,
});
},
setPrice(price) {
this.setState({
price,
});
},
setExternalId(externalId) {
this.setState({
externalId,
});
},
setRemark(remark) {
this.setState({
remark,
});
},
async createMove() {
const { fromEntity, fromEntityId, toEntity, toEntityId, price, remark, externalId } = this.state;
const userId = this.features.token.getUserId();
const systemId = this.features.application.getApplication().systemId;
const id = await generateNewIdAsync();
const price2 = ToCent(price);
const { onSuccess } = this.props;
await this.execute(undefined, undefined, undefined, [
{
entity: 'sysAccountMove',
operation: {
id: await generateNewIdAsync(),
action: 'create',
data: {
id,
price: price2,
externalId,
remark,
systemId,
operatorId: userId,
sysAccountOper$sysAccountMove: [
{
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
entity: fromEntity,
entityId: fromEntityId,
delta: -price2,
type: 'moveOut',
},
},
{
id: await generateNewIdAsync(),
action: 'create',
data: {
id: await generateNewIdAsync(),
entity: toEntity,
entityId: toEntityId,
delta: price2,
type: 'moveIn',
},
},
]
}
},
}
]);
onSuccess && onSuccess(id);
}
}
});