Browse Source

NODE 脚本 录入 report

dev
巴林闲侠 1 year ago
parent
commit
977ddae7e0
  1. 17
      scripts/1.3.1/data/1_insert_report_data/.vscode/launch.json
  2. 11
      scripts/1.3.1/data/1_insert_report_data/Dockerfile
  3. BIN
      scripts/1.3.1/data/1_insert_report_data/data/work.xlsx
  4. 124
      scripts/1.3.1/data/1_insert_report_data/index.js
  5. 17
      scripts/1.3.1/data/1_insert_report_data/package.json

17
scripts/1.3.1/data/1_insert_report_data/.vscode/launch.json

@ -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"
}
]
}

11
scripts/1.3.1/data/1_insert_report_data/Dockerfile

@ -0,0 +1,11 @@
FROM repository.anxinyun.cn/base-images/nodejs12:20.10.12.2
COPY . /var/app
WORKDIR /var/app
RUN npm cache clean -f
RUN rm -rf package-lock.json
RUN npm install --registry http://10.8.30.22:7000
CMD ["node", "index.js"]

BIN
scripts/1.3.1/data/1_insert_report_data/data/work.xlsx

Binary file not shown.

124
scripts/1.3.1/data/1_insert_report_data/index.js

@ -0,0 +1,124 @@
try {
const { Pool, Client } = require('pg')
const XLSX = require('xlsx')
const path = require('path')
const fs = require("fs");
// 测试环境
const pool = new Pool({
user: 'postgres',
host: '10.8.30.32',
database: 'highways4good',
password: '123',
port: 5432,
})
const userId = 5
// 商用环境
// const pool = new Pool({
// user: 'highwayadmin',
// host: '10.8.40.223',
// database: 'highway4good',
// password: 'highway123',
// port: 5432,
// })
// const userId = ''
// 生成随机的时间字符串
function generateRandomTimeString () {
// 生成一个在指定时间范围内的随机日期对象
var startDate = new Date('2023-01-01');
var endDate = new Date('2023-07-31');
var randomDate = new Date(startDate.getTime() + Math.random() * (endDate.getTime() - startDate.getTime()));
// 从日期对象中提取年、月和日
var year = randomDate.getFullYear();
var month = String(randomDate.getMonth() + 1).padStart(2, '0');
var day = String(randomDate.getDate()).padStart(2, '0');
// 生成一个在指定时间范围内的随机小时数和分钟数
var hours = String(Math.floor(Math.random() * 10) + 8).padStart(2, '0');
var minutes = String(Math.floor(Math.random() * 60)).padStart(2, '0');
// 组合成时间字符串
var timeString = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':00';
return timeString;
}
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()
try {
await client.query('BEGIN')
console.log(`开始`);
console.warn('user_id 配置了吗?');
await new Promise(resolve => setTimeout(() => {
console.warn('user_id 配置的对吗?');
resolve()
}, 3000));
await new Promise(resolve => setTimeout(() => {
console.warn('user_id 配置的真的对吗?');
resolve()
}, 3000));
await new Promise(resolve => setTimeout(() => {
console.warn('开始喽');
resolve()
}, 1000));
await new Promise(resolve => setTimeout(() => {
console.warn('3');
resolve()
}, 1000));
await new Promise(resolve => setTimeout(() => {
console.warn('2');
resolve()
}, 1000));
await new Promise(resolve => setTimeout(() => {
console.warn('1');
resolve()
}, 1000));
await new Promise(resolve => setTimeout(() => {
console.warn('开始!');
resolve()
}, 1000));
// 读取数据文件
let workbook = XLSX.readFile(path.join(__dirname, './data/work.xlsx'));
let firstSheetName = workbook.SheetNames[0];
let worksheet = workbook.Sheets[firstSheetName];
let res = XLSX.utils.sheet_to_json(worksheet, {
defval: ''
});
for (let r of res) {
console.log(r);
let time = generateRandomTimeString()
console.log(time);
await client.query(
`INSERT INTO report (report_type, project_type, road, time,content,user_id) VALUES($1, $2, $3, $4, $5, $6) `,
['conserve', 'road', r['养护道路'], time, r['养护内容'], userId])
// break
}
// 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)
}

17
scripts/1.3.1/data/1_insert_report_data/package.json

@ -0,0 +1,17 @@
{
"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": {
"crypto-js": "^4.1.1",
"pg": "^7.18.2",
"xlsx": "^0.17.1"
}
}
Loading…
Cancel
Save