const Service = require("egg").Service const {v4: uuid} = require("uuid"); class DbService extends Service { async addOrUpdate(table, form) { let result if (!form.id) {//创建 form.id = uuid() result = await this.app.mysql.insert(table, form) } else {//更新 result = await this.app.mysql.update(table, form) } return result === 1 ? {code: 0, data: form.id} : {code: 1, data: null} } async delete(table, id) { await this.app.mysql.delete(table, {id}) return {code: 0, data: null} } async detail(table, id) { const data = await this.app.mysql.get(table, {id}) return {code: 0, data} } async list(table, params = {}) { let {current = 0, size = 10} = params delete params.size delete params.current delete params.total const records = await this.app.mysql.select(table, { where: params, limit: size || 10, // 返回数据量 offset: Math.max(--current, 0), // 数据偏移量 }) const total = (await this.app.mysql.select(table, {where: params}))?.length || 0 return {code: 0, data: {records, total}} } } module.exports = DbService