base_service.js 6.2 KB

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