project.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. 'use strict';
  2. /**
  3. * 项目数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/16
  7. * @version
  8. */
  9. const imType = require('../const/tender').imType;
  10. const defaultFunRela = {
  11. banOver: false,
  12. imType: imType.zl.value,
  13. };
  14. module.exports = app => {
  15. class Project extends app.BaseService {
  16. /**
  17. * 构造函数
  18. *
  19. * @param {Object} ctx - egg全局变量
  20. * @return {void}
  21. */
  22. constructor(ctx) {
  23. super(ctx);
  24. this.tableName = 'project';
  25. // 状态相关
  26. this.status = {
  27. TRY: 1,
  28. NORMAL: 2,
  29. DISABLE: 3,
  30. };
  31. }
  32. /**
  33. * 数据规则
  34. *
  35. * @param {String} scene - 场景
  36. * @return {Object} - 返回数据规则
  37. */
  38. rule(scene) {
  39. let rule = {};
  40. switch (scene) {
  41. case 'saveInfo':
  42. rule = {
  43. name: { type: 'string', required: true, min: 2 },
  44. };
  45. break;
  46. case 'fun':
  47. rule = {
  48. imType: {type: 'enum', values: [imType.tz.value, imType.zl.value, imType.bb.value, imType.bw.value], required: true},
  49. banOver: {type: 'bool', required: true,}
  50. };
  51. break;
  52. default:
  53. break;
  54. }
  55. return rule;
  56. }
  57. /**
  58. * 根据项目code获取项目数据
  59. *
  60. * @param {String} code - 项目code
  61. * @return {Object} - 返回项目数据
  62. */
  63. async getProjectByCode(code) {
  64. // 获取项目状态为非禁止的项目
  65. this.initSqlBuilder();
  66. this.sqlBuilder.setAndWhere('code', {
  67. value: this.db.escape(code),
  68. operate: '=',
  69. });
  70. this.sqlBuilder.setAndWhere('status', {
  71. value: this.status.DISABLE,
  72. operate: '<',
  73. });
  74. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  75. const projectData = await this.db.queryOne(sql, sqlParam);
  76. return projectData;
  77. }
  78. /**
  79. * 根据项目code获取项目数据
  80. *
  81. * @param {int} prjId - id
  82. * @return {Object} - 返回项目数据
  83. */
  84. async getProjectById(prjId) {
  85. // 获取项目状态为非禁止的项目
  86. this.initSqlBuilder();
  87. this.sqlBuilder.setAndWhere('id', {
  88. value: this.db.escape(prjId),
  89. operate: '=',
  90. });
  91. this.sqlBuilder.setAndWhere('status', {
  92. value: this.status.DISABLE,
  93. operate: '<',
  94. });
  95. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  96. const projectData = await this.db.queryOne(sql, sqlParam);
  97. return projectData;
  98. }
  99. /**
  100. * 切换项目
  101. *
  102. * @param {Number} projectId - 项目id
  103. * @return {Boolean} - 返回切换结果
  104. */
  105. async switchProject(projectId) {
  106. // 获取该用户拥有的项目数据
  107. const sessionUser = this.ctx.session.sessionUser;
  108. const projectInfo = await this.ctx.service.projectAccount.getProjectInfoByAccount(sessionUser.account);
  109. let result = false;
  110. // 判断切换的项目是否属于对应用户
  111. if (projectInfo.length < 0) {
  112. return result;
  113. }
  114. let targetProject = {};
  115. for (const tmp of projectInfo) {
  116. if (tmp.id === projectId) {
  117. result = true;
  118. targetProject = tmp;
  119. }
  120. }
  121. // 成功后更改session
  122. if (result) {
  123. this.ctx.session.sessionProject = {
  124. id: targetProject.id,
  125. name: targetProject.name,
  126. userAccount: targetProject.user_account,
  127. };
  128. }
  129. return result;
  130. }
  131. async getPageshow(id) {
  132. const projectData = await this.db.get(this.tableName, { id });
  133. const pageShow = projectData.page_show ? JSON.parse(projectData.page_show) : null;
  134. if (pageShow) {
  135. if (pageShow.stageExtra === undefined) pageShow.stageExtra = '1';
  136. return pageShow;
  137. } else {
  138. return { stageExtra: '1' };
  139. }
  140. }
  141. /**
  142. * 功能设置
  143. * @param id
  144. * @returns {Promise<null>}
  145. */
  146. async getFunRela(id) {
  147. const projectData = await this.db.get(this.tableName, { id });
  148. const result = projectData.fun_rela ? JSON.parse(projectData.fun_rela) : {};
  149. this.ctx.helper._.defaults(result, defaultFunRela);
  150. return result;
  151. }
  152. async updateFunRela(id, data) {
  153. const result = await this.db.update(this.tableName, {
  154. id: id, fun_rela: JSON.stringify({banOver: data.banOver, imType: data.imType}),
  155. });
  156. return result.affectedRows === 1;
  157. }
  158. }
  159. return Project;
  160. };