base_service.js 4.8 KB

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