devfty
Version:
Devfty is a library for developer building low code factory
130 lines (117 loc) • 3.83 kB
text/typescript
import DB_BASE from './db/base'
import PB_TABLE from './db/pb_table'
import { base64 } from './uid'
class BillAPI {
db: any = null
billMap: any = {}
constructor(db: any, opts: any) {
this.db = db
}
_getApi(billno: string) {
let api = this.billMap[billno]
if (!api) {
api = new PB_TABLE(this.db, `pb_${billno}`, 'id')
this.billMap[billno] = api
}
return api
}
insert(params: any) {
const billno = params?.billno
const data = JSON.parse(params?.data)
const api = this._getApi(billno)
const result = api.insert(data)
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ code: 0, msg: '', data, ...result })
}, 0)
})
}
update(params: any) {
const billno = params?.billno
const data = JSON.parse(params?.data)
const api = this._getApi(billno)
const result = api.update(data)
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ code: 0, msg: '', data, ...result })
}, 0)
})
}
delete(params: any) {
const billno = params?.billno
const data = JSON.parse(params?.data)
const api = this._getApi(billno)
const result = api.delete(data)
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ code: 0, msg: '', data, ...result })
}, 0)
})
}
detail(params: any) {
const billno = params?.billno
const data = JSON.parse(params?.data)
if (!data?.id) {
return Promise.resolve({ code: -1, msg: 'id is empty', data: null })
}
const api = this._getApi(billno)
const result = api.select(data?.id)
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ code: 0, msg: '', data, ...result })
}, 0)
})
}
list(params: any) {
const billno = params?.billno
const data = params?.data
const api = this._getApi(billno)
const result = api.select(data)
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve({ code: 0, msg: '', data, ...result })
}, 0)
})
}
}
export default class Bill {
billAPI: any = null
context: any = {}
constructor(opts: any) {
const db = new DB_BASE(opts.dbName)
Object.assign(this.context, opts)
this.billAPI = new BillAPI(db, opts)
}
save(params: any) {
const data = params?.data
const author = this.context.author
if (!data.id) {
const id = base64.encode(data[this.context.uid || 'code'])
data.id = id
data.creator = author
data.createTime = Date.now()
params.data = JSON.stringify(data)
return this.billAPI.insert(params)
} else {
data.modifor = author
data.modifyTime = Date.now()
params.data = JSON.stringify(data)
return this.billAPI.update(params)
}
}
detail(params: any) {
return this.billAPI.detail(params)
}
list(params: any) {
return this.billAPI.list(params)
}
delete(params: any) {
const author = this.context.author?.getValue()?.code
const data = params?.data
data.isDelete = 1
data.modifor = author
data.modifyTime = Date.now()
params.data = JSON.stringify(data)
return this.billAPI.update(params)
}
}