project.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict';
  2. /**
  3. * 项目数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/16
  7. * @version
  8. */
  9. const SqlBuilder = require('../lib/sql_builder');
  10. module.exports = app => {
  11. class Project extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'project';
  21. // 状态相关
  22. this.status = {
  23. TRY: 1,
  24. NORMAL: 2,
  25. DISABLE: 3,
  26. };
  27. }
  28. /**
  29. * 根据项目code获取项目数据
  30. *
  31. * @param {String} code - 项目code
  32. * @return {Object} - 返回项目数据
  33. */
  34. async getProjectByCode(code) {
  35. // 获取项目状态为非禁止的项目
  36. const sqlBuilder = new SqlBuilder();
  37. sqlBuilder.setAndWhere('code', {
  38. value: this.db.escape(code),
  39. operate: '=',
  40. });
  41. sqlBuilder.setAndWhere('status', {
  42. value: this.status.DISABLE,
  43. operate: '<',
  44. });
  45. const [sql, sqlParam] = sqlBuilder.build(this.tableName);
  46. const projectData = await this.db.queryOne(sql, sqlParam);
  47. return projectData;
  48. }
  49. }
  50. return Project;
  51. };