base_service.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. 'use strict';
  2. /**
  3. * 业务逻辑基类
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/10/11
  7. * @version
  8. */
  9. const Service = require('egg').Service;
  10. // sql拼装器
  11. const SqlBuilder = require('../lib/sql_builder');
  12. class BaseService extends Service {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局context
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.db = this.app.mysql;
  22. this.cache = this.app.redis;
  23. this.sqlBuilder = null;
  24. }
  25. /**
  26. * 设置表名
  27. *
  28. * @param {String} table - 表名
  29. * @return {void}
  30. */
  31. set tableName(table) {
  32. this._table = this.app.config.tablePrefix + table;
  33. }
  34. /**
  35. * 获取表名
  36. *
  37. * @return {String} - 返回表名
  38. */
  39. get tableName() {
  40. return this._table;
  41. }
  42. /**
  43. * 初始化sqlBuilder
  44. *
  45. * @return {void}
  46. */
  47. initSqlBuilder() {
  48. if (this.sqlBuilder === null) {
  49. this.sqlBuilder = new SqlBuilder();
  50. }
  51. }
  52. /**
  53. * 根据id查找数据
  54. *
  55. * @param {Number} id - 数据库中的id
  56. * @return {Object} - 返回单条数据
  57. */
  58. async getDataById(id) {
  59. return await this.db.get(this.tableName, { id });
  60. }
  61. /**
  62. * 根据条件查找单条数据
  63. *
  64. * @param {Object} condition - 筛选条件
  65. * @return {Object} - 返回单条数据
  66. */
  67. async getDataByCondition(condition) {
  68. return await this.db.get(this.tableName, condition);
  69. }
  70. /**
  71. * 根据id删除数据
  72. *
  73. * @param {Number} id - 数据库中的id
  74. * @return {boolean} - 返回是否删除成功
  75. */
  76. async deleteById(id) {
  77. const result = await this.db.delete(this.tableName, { id });
  78. return result.affectedRows > 0;
  79. }
  80. /**
  81. * 获取总数
  82. *
  83. * @param {Object} option - 过滤条件(参考select方法中的condition)
  84. * @return {Number} - 返回查找的总数
  85. */
  86. async count(option = {}) {
  87. const result = await this.db.count(this.tableName, option);
  88. return result;
  89. }
  90. /**
  91. * 更新数据
  92. *
  93. * @param {Object} data - 需要更新的数据
  94. * @param {Object} condition - 更新的条件筛选
  95. * @return {Boolean} - 返回更新结果
  96. */
  97. async update(data, condition) {
  98. if (Object.keys(data).length <= 0 || Object.keys(condition).length <= 0) {
  99. return false;
  100. }
  101. const sqlParam = [];
  102. const param = [];
  103. for (const key in data) {
  104. param.push(key + ' = ?');
  105. sqlParam.push(data[key]);
  106. }
  107. const paramString = param.join(',');
  108. const where = [];
  109. for (const key in condition) {
  110. where.push(key + ' = ?');
  111. sqlParam.push(condition[key]);
  112. }
  113. const whereString = where.join(' AND ');
  114. const sql = 'UPDATE ' + this.tableName + ' SET ' + paramString + ' WHERE ' + whereString;
  115. const result = await this.db.query(sql, sqlParam);
  116. return result.affectedRows > 0;
  117. }
  118. /**
  119. * 获取分页数据(SqlBuilder版本)
  120. *
  121. * @return {Array} - 返回分页数据
  122. */
  123. async getListWithBuilder() {
  124. // 由于egg-mysql不提供like、不等于操作,所以需要自己组sql
  125. if (this.sqlBuilder === null) {
  126. return [];
  127. }
  128. // 分页相关
  129. this.sqlBuilder.limit = this.app.config.pageSize;
  130. this.sqlBuilder.offset = this.app.config.pageSize * (this.ctx.page - 1);
  131. // 数据筛选
  132. if (this.ctx.sort !== undefined && this.ctx.sort.length > 0) {
  133. this.sqlBuilder.orderBy = [this.ctx.sort];
  134. }
  135. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  136. const list = await this.db.query(sql, sqlParam);
  137. return list;
  138. }
  139. /**
  140. * 获取分页数据
  141. *
  142. * @param {Object} condition - 搜索条件
  143. * @return {Array} - 返回分页数据
  144. */
  145. async getList(condition) {
  146. const page = this.ctx.page;
  147. // 分页设置
  148. condition.limit = condition.limit === undefined ? this.app.config.pageSize : condition.limit;
  149. condition.offset = condition.offset === undefined ? this.app.config.pageSize * (page - 1) : condition.offset;
  150. if (this.ctx.sort !== undefined && this.ctx.sort.length > 0) {
  151. condition.orders = [this.ctx.sort];
  152. }
  153. const list = await this.db.select(this.tableName, condition);
  154. return list;
  155. }
  156. }
  157. module.exports = BaseService;