try { const { Pool, Client } = require('pg') const path = require('path') const fs = require("fs"); //! 定义变更数据 数据为想要达到的结果 //! 只筛选 管廊 结构物 //! projectId 是目标结构物 //! points 是要归属到这个结构物下的点位 id const resetBing = [ { projectId: 20, points: [192, 187, 196, 188, 189, 186, 190, 191, 193, 194, 195, 197, 182, 178, 179, 180, 181, 183, 184 ] }, { projectId: 21, points: [138] }, { projectId: 15, points: [135] }, { projectId: 17, points: [137] }, { projectId: 22, points: [121, 122, 123, 124, 217, 125, 126, 127, 128, 130, 131, 132, 120, 119, 212, 213, 216, 208, 215 ] }, { projectId: 19, points: [159, 169, 165, 160, 161, 166, 162, 163, 164, 167, 168, 172, 170, 175, 171, 173, 174, 176, 177 ] }, { projectId: 16, points: [136] }, { projectId: 14, points: [133] }, { projectId: 18, points: [108, 106, 112, 142, 105, 145, 146, 148, 153, 149, 150, 151, 152, 156, 154, 155, 157, 158, 109 ] }, { projectId: 13, points: [139] } ] // 测试环境 // const pool = new Pool({ // user: 'FashionAdmin', // host: '10.8.30.39', // database: 'inspection_231027_bak', // password: '123456', // port: 5432, // }) // 商用环境 const pool = new Pool({ user: 'insAdmin', host: '10.8.40.223', database: 'inspection', password: 'insAdmin123', port: 5432, }) const fun = async () => { // note: we don't try/catch this because if connecting throws an exception // we don't need to dispose of the client (it will be undefined) const client = await pool.connect() client.on('error', err => { console.error(err); }) try { await client.query('BEGIN') console.log(`开始`); // 查所有管廊结构物 const projects = (await client.query(`SELECT * FROM "project" WHERE type='管廊'`)).rows // console.log(projects); // 查所有管廊结构物下的点位 const points = projects.length ? (await client.query(`SELECT * FROM point WHERE project_id IN (${projects.map(item => `'${item.id}'`).join(',')}, -1)`)).rows : [] // console.log(points); // 查所有结构物相关的巡检计划 const plans = projects.length ? (await client.query(`SELECT * FROM patrol_plan WHERE structure_id IN (${projects.map(item => `'${item.id}'`).join(',')}, -1)`)).rows : [] if (plans.length) // 删除计划相关人员 await client.query(`DELETE FROM patrol_plan_user WHERE patrol_plan_id IN (${plans.map(item => `'${item.id}'`).join(',')},-1)`) // 查所有巡检记录 const records = plans.length ? (await client.query(`SELECT * FROM patrol_record WHERE patrol_plan_id IN (${plans.map(item => `'${item.id}'`).join(',')},-1)`)).rows : [] if (records.length) { // 删除所有巡检问题 await client.query(`DELETE FROM patrol_record_issue_handle WHERE patrol_record_id IN (${records.map(item => `'${item.id}'`).join(',')},-1)`) // 删除所有巡检记录 await client.query(`DELETE FROM patrol_record WHERE patrol_plan_id IN (${plans.map(item => `'${item.id}'`).join(',')},-1)`) } if (plans.length) // 删除所有巡检计划 await client.query(`DELETE FROM patrol_plan WHERE structure_id IN (${projects.map(item => `'${item.id}'`).join(',')}, -1)`) let index = 0 for (let r of resetBing) { if (!r.projectId || !r.points || !r.points.length) { throw `参数错误 index=${index}`; } for (let i = index + 1; i < resetBing.length; i++) { if (resetBing[i].projectId == r.projectId) { throw `重复项目 index= ${index} / ${i} `; } for (let j = 0; j < r.points.length; j++) { if (resetBing[i].points.some(item => item == r.points[j])) { throw `重复点位 pointIndex= ${j} / project ${i} `; } } } const targetProject = (await client.query(` SELECT * FROM "project" WHERE id=$1; `, [r.projectId] )).rows[0] if (!targetProject) { throw `目标项目不存在 index= ${index} `; } if (targetProject.type != '管廊') { throw `目标项目不是管廊 index= ${index} `; } console.log(targetProject); let updatePointSql = `UPDATE point SET project_id=${r.projectId} WHERE id IN (${r.points.join(',')},-1);` console.log(updatePointSql); // 变更点位 await client.query(updatePointSql) // index++ } // await client.query('ROLLBACK') await client.query('COMMIT') console.log('执行完毕~') } catch (e) { await client.query('ROLLBACK') console.log('执行错误~') throw e } finally { client.release(); } } fun() } catch (error) { console.error(error) }