CODE
1 year ago
3 changed files with 132 additions and 0 deletions
@ -0,0 +1,17 @@ |
|||
{ |
|||
// 使用 IntelliSense 了解相关属性。 |
|||
// 悬停以查看现有属性的描述。 |
|||
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 |
|||
"version": "0.2.0", |
|||
"configurations": [ |
|||
{ |
|||
"type": "node", |
|||
"request": "launch", |
|||
"name": "启动程序", |
|||
"skipFiles": [ |
|||
"<node_internals>/**" |
|||
], |
|||
"program": "${workspaceFolder}\\index.js" |
|||
} |
|||
] |
|||
} |
@ -0,0 +1,100 @@ |
|||
try { |
|||
const { Pool, Client } = require('pg') |
|||
const path = require('path') |
|||
const fs = require("fs"); |
|||
|
|||
//! 定义变更数据 数据为想要达到的结果
|
|||
//! 只筛选 管廊 结构物
|
|||
//! projectId 是目标结构物
|
|||
//! points 是要归属到这个结构物下的点位 id
|
|||
const resetBing = [ |
|||
{ |
|||
projectId: 1, |
|||
points: [1, 2, 3] |
|||
} |
|||
] |
|||
|
|||
// 测试环境
|
|||
const pool = new Pool({ |
|||
user: 'FashionAdmin', |
|||
host: '10.8.30.39', |
|||
database: 'Inspection_20231016', |
|||
password: '123456', |
|||
port: 5432, |
|||
}) |
|||
|
|||
// 商用环境
|
|||
// const pool = new Pool({
|
|||
// user: '',
|
|||
// host: '',
|
|||
// database: '',
|
|||
// password: '',
|
|||
// 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(`开始`); |
|||
|
|||
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) |
|||
} |
@ -0,0 +1,15 @@ |
|||
{ |
|||
"name": "appkey-generator", |
|||
"version": "1.0.0", |
|||
"description": "tool", |
|||
"main": "index.js", |
|||
"scripts": { |
|||
"test": "mocha", |
|||
"start": "set NODE_ENV=development&&node index" |
|||
}, |
|||
"author": "liu", |
|||
"license": "ISC", |
|||
"dependencies": { |
|||
"pg": "^7.18.2" |
|||
} |
|||
} |
Loading…
Reference in new issue