base_service.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict';
  2. /**
  3. * 业务逻辑基类
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/11
  7. * @version
  8. */
  9. const Service = require('egg').Service;
  10. // 数据模型基类
  11. class BaseService extends Service {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局context
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.db = this.app.mysql;
  21. this.cache = this.app.redis;
  22. }
  23. /**
  24. * 设置表名
  25. *
  26. * @param {String} table - 表名
  27. * @return {void}
  28. */
  29. set tableName(table) {
  30. this._table = this.app.config.tablePrefix + table;
  31. }
  32. /**
  33. * 获取表名
  34. *
  35. * @return {String} - 返回表名
  36. */
  37. get tableName() {
  38. return this._table;
  39. }
  40. /**
  41. * 根据id查找数据
  42. *
  43. * @param {Number} id - 数据库中的id
  44. * @return {Object} - 返回单条数据
  45. */
  46. async getDataById(id) {
  47. return await this.db.get(this.tableName, { id });
  48. }
  49. /**
  50. * 根据id删除数据
  51. *
  52. * @param {Number} id - 数据库中的id
  53. * @return {boolean} - 返回是否删除成功
  54. */
  55. async deleteById(id) {
  56. const result = await this.db.delete(this.tableName, { id });
  57. return result.affectedRows > 0;
  58. }
  59. /**
  60. * 获取总数
  61. *
  62. * @param {Object} option - 过滤条件(参考select方法中的condition)
  63. * @return {Number} - 返回查找的总数
  64. */
  65. async count(option = {}) {
  66. const result = await this.db.count(this.tableName, option);
  67. return result;
  68. }
  69. /**
  70. * 获取分页数据(SqlBuilder版本)
  71. *
  72. * @return {Array} - 返回分页数据
  73. */
  74. async getListWithBuilder() {
  75. // 由于egg-mysql不提供like、不等于操作,所以需要自己组sql
  76. if (this.sqlBuilder === null) {
  77. return [];
  78. }
  79. // 分页相关
  80. this.sqlBuilder.limit = this.app.config.pageSize;
  81. this.sqlBuilder.offset = this.app.config.pageSize * (this.ctx.page - 1);
  82. // 数据筛选
  83. if (this.ctx.sort !== undefined && this.ctx.sort.length > 0) {
  84. this.sqlBuilder.orderBy = [this.ctx.sort];
  85. }
  86. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  87. const list = await this.db.query(sql, sqlParam);
  88. return list;
  89. }
  90. /**
  91. * 获取分页数据
  92. *
  93. * @param {Object} condition - 搜索条件
  94. * @return {Array} - 返回分页数据
  95. */
  96. async getList(condition) {
  97. const page = this.ctx.page;
  98. // 分页设置
  99. condition.limit = condition.limit === undefined ? this.app.config.pageSize : condition.limit;
  100. condition.offset = condition.offset === undefined ? this.app.config.pageSize * (page - 1) : condition.offset;
  101. if (this.ctx.sort !== undefined && this.ctx.sort.length > 0) {
  102. condition.orders = [this.ctx.sort];
  103. }
  104. const list = await this.db.select(this.tableName, condition);
  105. return list;
  106. }
  107. }
  108. module.exports = BaseService;