project.js 6.1 KB

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