base_service.js 5.7 KB

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