123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- 'use strict';
- /**
- * 项目数据模型
- *
- * @author CaiAoLin
- * @date 2017/11/16
- * @version
- */
- const imType = require('../const/tender').imType;
- const defaultFunRela = {
- banOver: false,
- imType: imType.zl.value,
- };
- const sjsRelaConst = require('../const/setting').sjsRela;
- module.exports = app => {
- class Project extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'project';
- // 状态相关
- this.status = {
- TRY: 1,
- NORMAL: 2,
- DISABLE: 3,
- };
- }
- /**
- * 数据规则
- *
- * @param {String} scene - 场景
- * @return {Object} - 返回数据规则
- */
- rule(scene) {
- let rule = {};
- switch (scene) {
- case 'saveInfo':
- rule = {
- name: { type: 'string', required: true, min: 2 },
- };
- break;
- case 'fun':
- rule = {
- imType: {type: 'enum', values: [imType.tz.value, imType.zl.value, imType.bb.value, imType.bw.value], required: true},
- banOver: {type: 'bool', required: true,}
- };
- break;
- default:
- break;
- }
- return rule;
- }
- /**
- * 根据项目code获取项目数据
- *
- * @param {String} code - 项目code
- * @return {Object} - 返回项目数据
- */
- async getProjectByCode(code) {
- // 获取项目状态为非禁止的项目
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('code', {
- value: this.db.escape(code),
- operate: '=',
- });
- this.sqlBuilder.setAndWhere('status', {
- value: this.status.DISABLE,
- operate: '<',
- });
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
- const projectData = await this.db.queryOne(sql, sqlParam);
- return projectData;
- }
- /**
- * 根据项目code获取项目数据
- *
- * @param {int} prjId - id
- * @return {Object} - 返回项目数据
- */
- async getProjectById(prjId) {
- // 获取项目状态为非禁止的项目
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('id', {
- value: this.db.escape(prjId),
- operate: '=',
- });
- this.sqlBuilder.setAndWhere('status', {
- value: this.status.DISABLE,
- operate: '<',
- });
- const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
- const projectData = await this.db.queryOne(sql, sqlParam);
- return projectData;
- }
- /**
- * 切换项目
- *
- * @param {Number} projectId - 项目id
- * @return {Boolean} - 返回切换结果
- */
- async switchProject(projectId) {
- // 获取该用户拥有的项目数据
- const sessionUser = this.ctx.session.sessionUser;
- const projectInfo = await this.ctx.service.projectAccount.getProjectInfoByAccount(sessionUser.account);
- let result = false;
- // 判断切换的项目是否属于对应用户
- if (projectInfo.length < 0) {
- return result;
- }
- let targetProject = {};
- for (const tmp of projectInfo) {
- if (tmp.id === projectId) {
- result = true;
- targetProject = tmp;
- }
- }
- // 成功后更改session
- if (result) {
- this.ctx.session.sessionProject = {
- id: targetProject.id,
- name: targetProject.name,
- userAccount: targetProject.user_account,
- };
- }
- return result;
- }
- async getPageshow(id) {
- const projectData = await this.db.get(this.tableName, { id });
- const pageShow = projectData.page_show ? JSON.parse(projectData.page_show) : null;
- if (pageShow) {
- if (pageShow.stageExtra === undefined) pageShow.stageExtra = '1';
- return pageShow;
- } else {
- return { stageExtra: '1' };
- }
- }
- /**
- * 功能设置
- * @param id
- * @returns {Promise<null>}
- */
- async getFunRela(id) {
- const projectData = await this.db.get(this.tableName, { id });
- const result = projectData.fun_rela ? JSON.parse(projectData.fun_rela) : {};
- this.ctx.helper._.defaults(result, defaultFunRela);
- return result;
- }
- async updateFunRela(id, data) {
- const result = await this.db.update(this.tableName, {
- id: id, fun_rela: JSON.stringify({banOver: data.banOver, imType: data.imType}),
- });
- return result.affectedRows === 1;
- }
- async getSjsRela(id) {
- const projectData = await this.db.get(this.tableName, { id });
- const result = projectData.sjs_rela ? JSON.parse(projectData.sjs_rela) : {};
- this.ctx.helper._.defaults(result, sjsRelaConst);
- return result;
- }
- async updateSjsRela(id, sub, field, key, value) {
- const sjsRela = await this.getSjsRela(id);
- if (!sjsRela[sub]) throw '数据异常';
- const sjsField = sjsRela[sub].find(x => { return x.field === field; });
- if (!sjsField) throw '数据异常';
- sjsField[key] = value;
- await this.db.update(this.tableName, { id: id, sjs_rela: JSON.stringify(sjsRela) });
- return sjsField;
- }
- }
- return Project;
- };
|