base_service.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. async defaultUpdate(data, options) {
  103. const result = await this.db.update(this.tableName, data, options);
  104. return result;
  105. }
  106. /**
  107. * 更新数据
  108. *
  109. * @param {Object} data - 需要更新的数据
  110. * @param {Object} condition - 更新的条件筛选
  111. * @return {Boolean} - 返回更新结果
  112. */
  113. async update(data, condition) {
  114. if (Object.keys(data).length <= 0 || Object.keys(condition).length <= 0) {
  115. return false;
  116. }
  117. const sqlParam = [];
  118. const param = [];
  119. for (const key in data) {
  120. param.push(key + ' = ?');
  121. sqlParam.push(data[key]);
  122. }
  123. const paramString = param.join(',');
  124. const where = [];
  125. for (const key in condition) {
  126. where.push(key + ' = ?');
  127. sqlParam.push(condition[key]);
  128. }
  129. const whereString = where.join(' AND ');
  130. const sql = 'UPDATE ' + this.tableName + ' SET ' + paramString + ' WHERE ' + whereString;
  131. const result = await this.db.query(sql, sqlParam);
  132. return result.affectedRows > 0;
  133. }
  134. /**
  135. * 获取分页数据(SqlBuilder版本)
  136. *
  137. * @return {Array} - 返回分页数据
  138. */
  139. async getListWithBuilder() {
  140. // 由于egg-mysql不提供like、不等于操作,所以需要自己组sql
  141. if (this.sqlBuilder === null) {
  142. return [];
  143. }
  144. // 分页相关
  145. this.sqlBuilder.limit = this.app.config.pageSize;
  146. this.sqlBuilder.offset = this.app.config.pageSize * (this.ctx.page - 1);
  147. // 数据筛选
  148. if (this.ctx.sort !== undefined && this.ctx.sort.length > 0) {
  149. this.sqlBuilder.orderBy = [this.ctx.sort];
  150. }
  151. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  152. console.log(sql, sqlParam);
  153. const list = await this.db.query(sql, sqlParam);
  154. return list;
  155. }
  156. /**
  157. * 获取分页数据
  158. *
  159. * @param {Object} condition - 搜索条件
  160. * @return {Array} - 返回分页数据
  161. */
  162. async getList(condition) {
  163. const page = this.ctx.page;
  164. // 分页设置
  165. condition.limit = condition.limit === undefined ? this.app.config.pageSize : condition.limit;
  166. condition.offset = condition.offset === undefined ? this.app.config.pageSize * (page - 1) : condition.offset;
  167. if (this.ctx.sort !== undefined && this.ctx.sort.length > 0) {
  168. condition.orders = [this.ctx.sort];
  169. }
  170. const list = await this.db.select(this.tableName, condition);
  171. return list;
  172. }
  173. round(value, decimal) {
  174. return this.ctx.helper.round(value, this._.isNumber(decimal) ? decimal : 8);
  175. }
  176. }
  177. module.exports = BaseService;