'use strict'; const moment = require('moment') async function bindAnxin2pep (ctx) { const transaction = await ctx.fs.dc.orm.transaction(); try { const models = ctx.fs.dc.models; const { clickHouse } = ctx.app.fs const { userId, pepUserId } = ctx.fs.api const { bindId, name, pepProjectId, anxinProjectId = [], appId = [] } = ctx.request.body if(name && pepProjectId){ throw '项企项目与自定义项目名称仅可选择一项' } let bindId_ = bindId const now = moment() const existRes = await models.ProjectCorrelation.findOne({ where: { pepProjectId: pepProjectId }, include: { model: models.ProjectApp } }) let storageData = { name, pepProjectId, anxinProjectId, del: false, } if (bindId) { // 修改 if (!existRes) { throw '尚无已绑定的项企项目' } if (name) { } await models.ProjectCorrelation.update(storageData, { where: { pepProjectId: pepProjectId }, transaction }) } else { // 新增 if (existRes) { // 但是有之前的数据 if (existRes.del) { // 不过之前的删除了 await models.ProjectCorrelation.update(storageData, { where: { pepProjectId: pepProjectId }, transaction }) } else { // 没有删除 重复添加 throw '当前项企项目已绑定' } } else { storageData.createUser = userId; storageData.createTime = now.format() const createRes = await models.ProjectCorrelation.create(storageData, { transaction }) bindId_ = createRes.id } } if (bindId) { await models.ProjectApp.destroy({ where: { projectId: bindId }, transaction }) } await models.ProjectApp.bulkCreate(appId.map(aid => { return { projectId: bindId_, appId: aid } }), { transaction }) await transaction.commit(); ctx.status = 204; } catch (error) { await transaction.rollback(); ctx.fs.logger.error(`path: ${ctx.path}, error: ${error}`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } async function del (ctx) { try { const models = ctx.fs.dc.models; const { bindId } = ctx.query await models.ProjectCorrelation.update({ del: true }, { where: { id: { $in: bindId.split(',') } } }) ctx.status = 20; } catch (error) { ctx.fs.logger.error(`path: ${ctx.path}, error: error`); ctx.status = 400; ctx.body = { message: typeof error == 'string' ? error : undefined } } } module.exports = { bindAnxin2pep, del };