base_service.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. 'use strict';
  2. /**
  3. * 业务逻辑基类
  4. *
  5. * @author Mai
  6. * @date 2018/4/19
  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.sqlBuilder = null;
  23. }
  24. /**
  25. * 设置表名
  26. *
  27. * @param {String} table - 表名
  28. * @return {void}
  29. */
  30. set tableName(table) {
  31. this._table = this.app.config.tablePrefix + table;
  32. }
  33. /**
  34. * 获取表名
  35. *
  36. * @return {String} - 返回表名
  37. */
  38. get tableName() {
  39. return this._table;
  40. }
  41. /**
  42. * 初始化sqlBuilder
  43. *
  44. * @return {void}
  45. */
  46. initSqlBuilder() {
  47. if (this.sqlBuilder === null) {
  48. this.sqlBuilder = new SqlBuilder();
  49. }
  50. }
  51. /**
  52. * 根据id查找数据
  53. *
  54. * @param {Number} id - 数据库中的id
  55. * @return {Object} - 返回单条数据
  56. */
  57. async getDataById(id) {
  58. return await this.db.get(this.tableName, { id });
  59. }
  60. /**
  61. * 根据条件查找单条数据
  62. *
  63. * @param {Object} condition - 筛选条件
  64. * @return {Object} - 返回单条数据
  65. */
  66. async getDataByCondition(condition) {
  67. return await this.db.get(this.tableName, condition);
  68. }
  69. /**
  70. * 根据条件查找数据
  71. *
  72. * @param {Object} condition - 筛选条件
  73. * @return {Array} - 返回数据
  74. */
  75. async getAllDataByCondition(condition) {
  76. return await this.db.select(this.tableName, condition);
  77. }
  78. /**
  79. * 根据id删除数据
  80. *
  81. * @param {Number} id - 数据库中的id
  82. * @return {boolean} - 返回是否删除成功
  83. */
  84. async deleteById(id) {
  85. const result = await this.db.delete(this.tableName, { id });
  86. return result.affectedRows > 0;
  87. }
  88. /**
  89. * 获取总数
  90. *
  91. * @param {Object} option - 过滤条件(参考select方法中的condition)
  92. * @return {Number} - 返回查找的总数
  93. */
  94. async count(option = {}) {
  95. const result = await this.db.count(this.tableName, option);
  96. return result;
  97. }
  98. /**
  99. * 获取分页数据
  100. *
  101. * @param {Object} condition - 搜索条件
  102. * @return {Array} - 返回分页数据
  103. */
  104. async getList(condition) {
  105. const page = this.ctx.page;
  106. // 分页设置
  107. condition.limit = condition.limit === undefined ? this.app.config.pageSize : condition.limit;
  108. condition.offset = condition.offset === undefined ? this.app.config.pageSize * (page - 1) : condition.offset;
  109. if (this.ctx.sort !== undefined && this.ctx.sort.length > 0) {
  110. condition.orders = [this.ctx.sort];
  111. }
  112. const list = await this.db.select(this.tableName, condition);
  113. return list;
  114. }
  115. /**
  116. * 在事务中写入数据
  117. *
  118. * @param {Array|Object} datas
  119. * @param transaction - 事务
  120. * @returns {Promise<void>}
  121. */
  122. async insertData(data, transaction) {
  123. const datas = data instanceof Array ? data : [data];
  124. const insertResult = await transaction.insert(this.tableName, datas);
  125. if (insertResult.affectedRows !== datas.length) {
  126. throw '写入错误';
  127. }
  128. }
  129. /**
  130. * 在事务中删除数据
  131. *
  132. * @param {Array|Object} datas
  133. * @param transaction - 事务
  134. * @returns {Promise<void>}
  135. */
  136. async deleteData(data, transaction) {
  137. const count = await this.count(data);
  138. const deleteResult = await transaction.delete(this.tableName, data);
  139. if (deleteResult.affectedRows !== count) {
  140. throw '删除错误';
  141. }
  142. }
  143. }
  144. module.exports = BaseService;