base_service.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. * 根据条件查找单条数据
  51. *
  52. * @param {Object} condition - 筛选条件
  53. * @return {Object} - 返回单条数据
  54. */
  55. async getDataByCondition(condition) {
  56. return await this.db.get(this.tableName, condition);
  57. }
  58. /**
  59. * 根据id删除数据
  60. *
  61. * @param {Number} id - 数据库中的id
  62. * @return {boolean} - 返回是否删除成功
  63. */
  64. async deleteById(id) {
  65. const result = await this.db.delete(this.tableName, { id });
  66. return result.affectedRows > 0;
  67. }
  68. /**
  69. * 获取总数
  70. *
  71. * @param {Object} option - 过滤条件(参考select方法中的condition)
  72. * @return {Number} - 返回查找的总数
  73. */
  74. async count(option = {}) {
  75. const result = await this.db.count(this.tableName, option);
  76. return result;
  77. }
  78. /**
  79. * 更新数据
  80. *
  81. * @param {Object} data - 需要更新的数据
  82. * @param {Object} condition - 更新的条件筛选
  83. * @return {Boolean} - 返回更新结果
  84. */
  85. async update(data, condition) {
  86. if (Object.keys(data).length <= 0 || Object.keys(condition).length <= 0) {
  87. return false;
  88. }
  89. const sqlParam = [];
  90. const param = [];
  91. for (const key in data) {
  92. param.push(key + ' = ?');
  93. sqlParam.push(data[key]);
  94. }
  95. const paramString = param.join(',');
  96. const where = [];
  97. for (const key in condition) {
  98. where.push(key + ' = ?');
  99. sqlParam.push(condition[key]);
  100. }
  101. const whereString = where.join(' AND ');
  102. const sql = 'UPDATE ' + this.tableName + ' SET ' + paramString + ' WHERE ' + whereString;
  103. const result = await this.db.query(sql, sqlParam);
  104. return result.affectedRows > 0;
  105. }
  106. /**
  107. * 获取分页数据(SqlBuilder版本)
  108. *
  109. * @return {Array} - 返回分页数据
  110. */
  111. async getListWithBuilder() {
  112. // 由于egg-mysql不提供like、不等于操作,所以需要自己组sql
  113. if (this.sqlBuilder === null) {
  114. return [];
  115. }
  116. // 分页相关
  117. this.sqlBuilder.limit = this.app.config.pageSize;
  118. this.sqlBuilder.offset = this.app.config.pageSize * (this.ctx.page - 1);
  119. // 数据筛选
  120. if (this.ctx.sort !== undefined && this.ctx.sort.length > 0) {
  121. this.sqlBuilder.orderBy = [this.ctx.sort];
  122. }
  123. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  124. const list = await this.db.query(sql, sqlParam);
  125. return list;
  126. }
  127. /**
  128. * 获取分页数据
  129. *
  130. * @param {Object} condition - 搜索条件
  131. * @return {Array} - 返回分页数据
  132. */
  133. async getList(condition) {
  134. const page = this.ctx.page;
  135. // 分页设置
  136. condition.limit = condition.limit === undefined ? this.app.config.pageSize : condition.limit;
  137. condition.offset = condition.offset === undefined ? this.app.config.pageSize * (page - 1) : condition.offset;
  138. if (this.ctx.sort !== undefined && this.ctx.sort.length > 0) {
  139. condition.orders = [this.ctx.sort];
  140. }
  141. const list = await this.db.select(this.tableName, condition);
  142. return list;
  143. }
  144. }
  145. module.exports = BaseService;