diff --git a/package.json b/package.json index 75fe54b..939ed1f 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "lodash": "^4.17.11", "log4js": "^3.0.5", "mkdirp": "^0.5.1", + "moment": "^2.24.0", "mongodb": "^3.1.10", "mysql2": "^1.6.1", "require-dir": "^1.0.0", @@ -60,6 +61,7 @@ "@types/koa-static": "^4.0.0", "@types/lodash": "^4.14.116", "@types/mkdirp": "^0.5.2", + "@types/moment": "^2.13.0", "@types/mongodb": "^3.1.18", "@types/mysql2": "types/mysql2", "@types/node": "^10.9.4", diff --git a/src/db/baseDao.ts b/src/db/baseDao.ts index 6b9d743..852ddef 100644 --- a/src/db/baseDao.ts +++ b/src/db/baseDao.ts @@ -2,10 +2,11 @@ let dialect: string = G.CONFIGS.db_dialect let Dao = require(`./${dialect}Dao`).default import TransElement from '../common/transElement' import IDao from './idao' +import * as moment from 'moment' export default class BaseDao { table: string - static dao: IDao + static dao: IDao constructor(table?: string) { this.table = table || '' if (!BaseDao.dao) @@ -22,22 +23,28 @@ export default class BaseDao { if (rs.status === G.STCODES.SUCCESS && (!rs.data || rs.data.length === 0)) return G.jsResponse(G.STCODES.QUERYEMPTY, 'data query empty.', rs) else - return rs + return processDatum(rs) } - async create(params = {}, fields = [], session = {userid: ''}): Promise { + async create(params = {}, fields = [], session = { userid: '' }): Promise { let keys = Object.keys(params) if (keys.length === 0 || params['id'] !== undefined && keys.length <= 1) return G.jsResponse(G.STCODES.PRAMAERR, 'params is error.') else { - let rs + let rs, id = params['id'] try { - rs = await BaseDao.dao.insert(this.table, params) + if (!id && params['uuid']) { + id = G.tools.uuid() + delete params['uuid'] + } else if (params['uuid']) { + delete params['uuid'] + } + rs = await BaseDao.dao.insert(this.table, Object.assign(params, id ? { id } : {})) } catch (err) { err.message = `data insert fail: ${err.message}` return err } - let {affectedRows} = rs - return G.jsResponse(G.STCODES.SUCCESS, 'data insert success.', {affectedRows, id: rs.insertId}) + let { affectedRows } = rs + return G.jsResponse(G.STCODES.SUCCESS, 'data insert success.', { affectedRows, id: id || rs.insertId }) } } async update(params, fields = [], session = { userid: '' }): Promise { @@ -58,7 +65,7 @@ export default class BaseDao { return G.jsResponse(G.STCODES.SUCCESS, 'data update success.', { affectedRows, id }) } } - async delete(params = {}, fields = [], session = {userid: ''}): Promise { + async delete(params = {}, fields = [], session = { userid: '' }): Promise { if (params['id'] === undefined) return G.jsResponse(G.STCODES.PRAMAERR, 'params is error.') else { @@ -70,22 +77,22 @@ export default class BaseDao { err.message = `data delete fail: ${err.message}` return err } - let {affectedRows} = rs + let { affectedRows } = rs return G.jsResponse(G.STCODES.SUCCESS, 'data delete success.', { affectedRows, id }) } } async querySql(sql: string, values = [], params = {}, fields = []): Promise { let rs try { - rs = await BaseDao.dao.querySql (sql, values, params, fields) + rs = await BaseDao.dao.querySql(sql, values, params, fields) } catch (err) { err.message = `data querySql fail: ${err.message}` return err } - if (rs.length === 0) - return G.jsResponse( G.STCODES.QUERYEMPTY, 'data querySql empty.', rs) + if (rs.status === G.STCODES.SUCCESS && (!rs.data || rs.data.length === 0)) + return G.jsResponse(G.STCODES.QUERYEMPTY, 'data query empty.', rs) else - return G.jsResponse( G.STCODES.SUCCESS, 'data querySql success.', rs) + return processDatum(rs) } async execSql(sql: string, values = []): Promise { let rs @@ -95,8 +102,8 @@ export default class BaseDao { err.message = `data execSql fail: ${err.message}` return err } - let {affectedRows} = rs - return G.jsResponse(G.STCODES.SUCCESS, 'data execSql success.', {affectedRows}) + let { affectedRows } = rs + return G.jsResponse(G.STCODES.SUCCESS, 'data execSql success.', { affectedRows }) } async insertBatch(tablename: string, elements = []): Promise { let rs @@ -106,8 +113,8 @@ export default class BaseDao { err.message = `data batch fail: ${err.message}` return err } - let {affectedRows} = rs - return G.jsResponse(G.STCODES.SUCCESS, 'data batch success.', {affectedRows}) + let { affectedRows } = rs + return G.jsResponse(G.STCODES.SUCCESS, 'data batch success.', { affectedRows }) } async transGo(elements: Array, isAsync = true): Promise { let rs @@ -117,7 +124,23 @@ export default class BaseDao { err.message = `data trans fail: ${err.message}` return err } - let {affectedRows} = rs - return G.jsResponse(G.STCODES.SUCCESS, 'data trans success.', {affectedRows}) + let { affectedRows } = rs + return G.jsResponse(G.STCODES.SUCCESS, 'data trans success.', { affectedRows }) } +} + +function processDatum(rs) { + rs.data.forEach(element => { + let vs = Object.entries(element) + for (let [key, value] of vs) { + if (G.L.endsWith(key, '_time') && value) { + element[key] = moment(value).format('YYYY-MM-DD hh:mm:ss') + } else if (G.L.endsWith(key, '_json')) { + if (value && value.toString().trim().length === 0) { + element[key] = null + } + } + } + }) + return rs } \ No newline at end of file