base_service.js 4.9 KB

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