'use strict'; /** * 项目数据模型 * * @author CaiAoLin * @date 2017/11/16 * @version */ const imType = require('../const/tender').imType; const defaultFunRela = { banOver: true, hintOver: true, banMinusChangeBills: true, minusNoValue: true, lockPayExpr: false, showMinusCol: true, imType: imType.zl.value, needGcl: false, }; const funSet = require('../const/fun_set'); const defaultFunSet = funSet.defaultInfo; const sjsRelaConst = require('../const/setting').sjsRela; const infoConst = require('../const/tender_info'); const parseInfo = infoConst.parseInfo; 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,}, hintOver: {type: 'bool', required: true,}, banMinusChangeBills: {type: 'bool', required: true,}, minusNoValue: {type: 'bool', required: true,}, lockPayExpr: {type: 'bool', required: true,}, showMinusCol: {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; } /** * 功能设置 * @param id * @returns {Promise} */ 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, hintOver: data.hintOver, banMinusChangeBills: data.banMinusChangeBills, imType: data.imType, needGcl: data.needGcl, minusNoValue: data.minusNoValue, lockPayExpr: data.lockPayExpr, showMinusCol: data.showMinusCol, }), }); 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 getTenderSjsRela(id, show) { 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); if (!show) result.ledgerCol.forEach(x => { x.show = 0 }); 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; } async updatePageshow(id) { const result = await this.db.update(this.tableName, { id, page_show: JSON.stringify(this.ctx.session.sessionProject.page_show), }); return result.affectedRows === 1; } /** * 校验项目是否存在(项目管理) * @param {String} token - token * @return {Promise} Promise */ verifyManagementProject(token) { return new Promise((resolve, reject) => { this.ctx.curl(`${app.config.managementProxyPath}/api/external/jl/calibration`, { method: 'POST', // dateType: 'json', encoding: 'utf8', data: { token, }, // timeout: 2000, }).then(({ status, data }) => { if (status === 200) { const result = JSON.parse(data.toString()).data; return resolve(result); } return reject(new Error('校验失败')); }).catch(err => { console.log(err); return reject(err); }); }); } /** * 创建项目和账号(项目管理) * @return {Promise} Promise */ async addProjectFromManagement() { const token = this.ctx.helper.createJWT({ account: this.ctx.session.sessionUser.account, code: this.ctx.session.sessionProject.code }); return new Promise((resolve, reject) => { this.ctx.curl(`${app.config.managementProxyPath}/api/external/jl/project/add`, { method: 'POST', encoding: 'utf8', data: { token }, }).then(({ status, data }) => { if (status === 200) { const result = JSON.parse(data.toString()); return resolve(result); } return reject(new Error('添加失败')); }).catch(err => { console.log(err); return reject(err); }); }); } async setPmDealCache(pid, data) { const pmDeal = data.filter(x => { return x.selected; }).map(x => { return x.id; }); await this.cache.set('pmDeal-' + pid, pmDeal.join(','), 'EX', this.ctx.app.config.cacheTime); } async getPmDealCache(pid) { const result = await this.cache.get('pmDeal-' + pid); return result ? result.split(',') : []; } async getFunSet(fun_set = null) { const result = fun_set ? JSON.parse(fun_set) : {}; this.ctx.helper._.defaults(result, defaultFunSet); return result; } async updateFunSet(id, funSet) { const result = await this.db.update(this.tableName, { id, fun_set: JSON.stringify(funSet), }); return result.affectedRows === 1; } async updateProjectSet(id, field, datas) { const updateData = { id, }; updateData[field] = JSON.stringify(datas); const result = await this.db.update(this.tableName, updateData); return result.affectedRows === 1; } async saveCommonJson(id, field, datas) { const projectData = await this.getDataById(id); projectData.common_json = projectData.common_json ? JSON.parse(projectData.common_json) : {}; const updateData = { id, }; projectData.common_json[field] = datas; updateData.common_json = JSON.stringify(projectData.common_json); const result = await this.db.update(this.tableName, updateData); return result.affectedRows === 1; } } return Project; };