|
@@ -0,0 +1,80 @@
|
|
|
+'use strict';
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ *
|
|
|
+ * @author Mai
|
|
|
+ * @date
|
|
|
+ * @version
|
|
|
+ */
|
|
|
+
|
|
|
+const fs = require('fs');
|
|
|
+const path = require('path');
|
|
|
+var util = require('util');
|
|
|
+var logPath = path.join(__dirname, 'update_ledger_his.log');
|
|
|
+var logFile = fs.createWriteStream(logPath, { flags: 'a' });
|
|
|
+console.log = function() {
|
|
|
+ logFile.write(util.format.apply(null, arguments) + '\n');
|
|
|
+ process.stdout.write(util.format.apply(null, arguments) + '\n');
|
|
|
+};
|
|
|
+
|
|
|
+const mysql = require('mysql');
|
|
|
+const oss = require('ali-oss');
|
|
|
+const config = process.argv.splice(2)[0];
|
|
|
+if (['local', 'uat', 'default'].indexOf(config) < 0) throw `参数错误: ${config}`;
|
|
|
+const options = require(`../config/config.${config}`)({ baseDir: __dirname + '/app', root: __dirname, name: 'calc' });
|
|
|
+
|
|
|
+const pool = mysql.createPool(options.mysql.client);
|
|
|
+const ossOption = {
|
|
|
+ bucket: options.oss.clients.his.bucket,
|
|
|
+ accessKeyId: options.oss.default.accessKeyId,
|
|
|
+ accessKeySecret: options.oss.default.accessKeySecret,
|
|
|
+ endpoint: options.oss.default.endpoint,
|
|
|
+ timeout: options.oss.default.timeout,
|
|
|
+};
|
|
|
+const ossClient = new oss(ossOption);
|
|
|
+
|
|
|
+const querySql = async function(sql, sqlParam) {
|
|
|
+ return new Promise(function(resolve, reject) {
|
|
|
+ pool.getConnection(function(err, conn) {
|
|
|
+ if (err) {
|
|
|
+ if (err) console.log(err);
|
|
|
+ reject(err);
|
|
|
+ } else {
|
|
|
+ conn.query(sql, sqlParam, function(err, rows, fields) {
|
|
|
+ if (err) console.log(err);
|
|
|
+ // 释放连接
|
|
|
+ conn.release();
|
|
|
+ // 传递Promise回调对象
|
|
|
+ resolve(rows);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+};
|
|
|
+
|
|
|
+const loadDataFromOss = async function(url) {
|
|
|
+ const File = await ossClient.get('undefined' + url);
|
|
|
+ if (File.res.status !== 200) return '获取修订台账有误';
|
|
|
+ return File.content;
|
|
|
+};
|
|
|
+
|
|
|
+const doCompleteStashStash = async function(stash) {
|
|
|
+ const data = await loadDataFromOss(stash.filepath);
|
|
|
+ await ossClient.put(options.stashOssPath + stash.filepath, data);
|
|
|
+};
|
|
|
+
|
|
|
+const doComplete = async function() {
|
|
|
+ try {
|
|
|
+ const stash = await querySql('Select * From zh_stage_stash');
|
|
|
+ for (const s of stash) {
|
|
|
+ console.log(s);
|
|
|
+ await doCompleteStashStash(s);
|
|
|
+ }
|
|
|
+ } catch (err) {
|
|
|
+ console.log(err);
|
|
|
+ }
|
|
|
+ pool.end();
|
|
|
+};
|
|
|
+doComplete();
|
|
|
+
|