|
@@ -0,0 +1,82 @@
|
|
|
|
+'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 loadLedgerDataFromOss = async function(url) {
|
|
|
|
+ const File = await ossClient.get(options.hisOssPath + url);
|
|
|
|
+ if (File.res.status !== 200) return '获取修订台账有误';
|
|
|
|
+ const result = JSON.parse(File.content);
|
|
|
|
+ return result.length;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+const doCompleteLedgerHis = async function(l) {
|
|
|
|
+ const bills_count = await loadLedgerDataFromOss(l.bills_file);
|
|
|
|
+ const pos_count = await loadLedgerDataFromOss(l.pos_file);
|
|
|
|
+ await querySql('Update zh_ledger_history Set bills_count = ?, pos_count = ? Where id = ?', [bills_count, pos_count, l.id]);
|
|
|
|
+ console.log(`ledgerHis-${l.id}(${l.tid}): billsCount(${bills_count}), posCount(${pos_count})`);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+const doComplete = async function() {
|
|
|
|
+ try {
|
|
|
|
+ const ledgerHis = await querySql('Select * From zh_ledger_history');
|
|
|
|
+ for (const l of ledgerHis) {
|
|
|
|
+ await doCompleteLedgerHis(l);
|
|
|
|
+ }
|
|
|
|
+ } catch (err) {
|
|
|
|
+ console.log(err);
|
|
|
|
+ }
|
|
|
|
+ pool.end();
|
|
|
|
+};
|
|
|
|
+doComplete();
|
|
|
|
+
|