'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 = [], app = [] } = ctx.request.body 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, } // 已经创建过的 app let existApp = [] // 新增的 let createAppData = [] // url 相同需要更新的 解除 lock 状态 let updateAppData = [] // 没有出现但存在的 叠加 lock 状态 let lockAppData = [] if (bindId_) { if (!existRes) { throw '尚无已绑定的项企项目' } // 修改 await models.ProjectCorrelation.update(storageData, { where: { pepProjectId: pepProjectId }, transaction }) existApp = existRes.projectApps } else { // 新增 if (existRes) { // 但是有之前的数据 if (existRes.del) { // 不过之前的删除了 await models.ProjectCorrelation.update(storageData, { where: { pepProjectId: pepProjectId }, transaction }) existApp = existRes.projectApps } else { // 没有删除 重复添加 throw '当前项企项目已绑定' } } else { storageData.createUser = userId; storageData.createTime = now.format() const createRes = await models.ProjectCorrelation.create(storageData, { transaction }) bindId_ = createRes.id } } app.forEach((a, i, arr) => { if (!a.name || !a.url) { throw `${a.name} ${a.url} 缺少必要参数` } let curUrlArr = a.url.split('://') if (curUrlArr.length < 2) { throw `${a.name} ${a.url} url 地址错误` } curUrlArr.shift() let curUrl = curUrlArr.join('://') // 先判断传过来的数据有没有重复 for (let ii = i + 1; ii < arr.length; ii++) { let curForUrlArr = arr[ii].url.split('://') curForUrlArr.shift() if (curUrl == curForUrlArr.join('://')) { throw `${a.name} ${a.url} 与 ${arr[ii].name} ${arr[ii].url} 地址重复` } } // 再判断和已有的有没有重复 let existSameApp = existApp.find(ea => { let curForUrlArr = ea.url.split('://') curForUrlArr.shift() return curUrl == curForUrlArr.join('://') }) if (existSameApp) { updateAppData.push({ id: existSameApp.id, name: a.name, url: a.url, projectId: bindId_, lock: false, }) existSameApp.addAgain = true } else { createAppData.push({ name: a.name, url: a.url, projectId: bindId_, lock: false, }) } lockAppData = existApp.filter(esa => !esa.addAgain).map(esa => { return { id: esa.id, // name: esa.name, // url: esa.url, // projectId: bindId_, lock: true, } }) }) createAppData.length ? await models.ProjectApp.bulkCreate(createAppData, { transaction }) : null updateAppData.length || lockAppData.length ? await Promise.all(updateAppData.concat(lockAppData).map(ud => { return models.ProjectApp.update(ud, { where: { id: ud.id }, transaction }) })) : null 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: bindId } }) 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 };