project.js 5.8 KB

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