Browse Source

期审批通过时,根据是否有新增清单部位等,生成台账历史

MaiXinRong 3 years ago
parent
commit
0b9b23e463
4 changed files with 93 additions and 3 deletions
  1. 4 0
      app/service/ledger_history.js
  2. 4 3
      app/service/stage_audit.js
  3. 82 0
      db_script/ledger_his_count.js
  4. 3 0
      sql/update.sql

+ 4 - 0
app/service/ledger_history.js

@@ -58,6 +58,7 @@ module.exports = app => {
                 pid: this.ctx.session.sessionProject.id, tid: tender.id,
                 pid: this.ctx.session.sessionProject.id, tid: tender.id,
                 in_time: now,
                 in_time: now,
                 bills_file: billsHis, pos_file: posHis,
                 bills_file: billsHis, pos_file: posHis,
+                bills_count: bills.length, pos_count: pos.length,
             });
             });
 
 
             return result.insertId;
             return result.insertId;
@@ -85,6 +86,7 @@ module.exports = app => {
                 pid: this.ctx.session.sessionProject.id, tid: stage.tid, sid: stage.id,
                 pid: this.ctx.session.sessionProject.id, tid: stage.tid, sid: stage.id,
                 in_time: now,
                 in_time: now,
                 bills_file: billsHis, pos_file: posHis,
                 bills_file: billsHis, pos_file: posHis,
+                bills_count: bills.length, pos_count: pos.length,
             });
             });
 
 
             return result.insertId;
             return result.insertId;
@@ -113,6 +115,7 @@ module.exports = app => {
                 rid: revise.id, rorder: revise.corder,
                 rid: revise.id, rorder: revise.corder,
                 in_time: now,
                 in_time: now,
                 bills_file: billsHis, pos_file: posHis,
                 bills_file: billsHis, pos_file: posHis,
+                bills_count: bills.length, pos_count: pos.length,
             });
             });
 
 
             return result.insertId;
             return result.insertId;
@@ -147,6 +150,7 @@ module.exports = app => {
                 cid: change.cid,
                 cid: change.cid,
                 in_time: now,
                 in_time: now,
                 bills_file: billsHis, pos_file: posHis,
                 bills_file: billsHis, pos_file: posHis,
+                bills_count: bills.length, pos_count: pos.length,
             });
             });
 
 
             return result.insertId;
             return result.insertId;

+ 4 - 3
app/service/stage_audit.js

@@ -475,11 +475,12 @@ module.exports = app => {
                         const ledgerHis = await this.ctx.service.ledgerHistory.getLatestHistory(this.ctx.tender.id);
                         const ledgerHis = await this.ctx.service.ledgerHistory.getLatestHistory(this.ctx.tender.id);
                         his_id = ledgerHis.id;
                         his_id = ledgerHis.id;
                     } else {
                     } else {
-                        const sapCount = await this.ctx.service.pos.count({ add_stage: this.ctx.stage.id });
-                        if (sapCount > 0) {
+                        const sbCount = await this.ctx.service.ledger.count({ tender_id: this.ctx.stage.tid });
+                        const spCount = await this.ctx.service.pos.count({ tid: this.ctx.stage.tid });
+                        const ledgerHis = await this.ctx.service.ledgerHistory.getLatestHistory(this.ctx.tender.id);
+                        if (sbCount > ledgerHis.bills_count || spCount > ledgerHis.pos_count) {
                             his_id = await this.ctx.service.ledgerHistory.backupStageLedgerHistory(this.ctx.stage);
                             his_id = await this.ctx.service.ledgerHistory.backupStageLedgerHistory(this.ctx.stage);
                         } else {
                         } else {
-                            const ledgerHis = await this.ctx.service.ledgerHistory.getLatestHistory(this.ctx.tender.id);
                             his_id = ledgerHis.id;
                             his_id = ledgerHis.id;
                         }
                         }
                     }
                     }

+ 82 - 0
db_script/ledger_his_count.js

@@ -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();
+

+ 3 - 0
sql/update.sql

@@ -0,0 +1,3 @@
+ALTER TABLE `zh_ledger_history`
+ADD COLUMN `bills_count`  int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '清单条数' AFTER `valid`,
+ADD COLUMN `pos_count`  int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '计量单元数量' AFTER `bills_count`;