stage-change-final.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const audit = require('../app/const/audit');
  10. const mysql = require('mysql');
  11. const pool = mysql.createPool({
  12. // host
  13. host: '192.168.1.76',
  14. // 端口号
  15. port: '3306',
  16. // 用户名
  17. user: 'zh_dev',
  18. // 密码
  19. password: 'zongheng2019',
  20. // 数据库名
  21. database: 'calculation',
  22. // database: 'calc_copy_pro',
  23. });
  24. const querySql = async function (sql, sqlParam) {
  25. return new Promise(function (resolve, reject) {
  26. pool.getConnection(function (err, conn) {
  27. if (err) {
  28. reject(err);
  29. } else {
  30. conn.query(sql, sqlParam, function (err, rows, fields) {
  31. //释放连接
  32. conn.release();
  33. //传递Promise回调对象
  34. resolve({"err": err, "rows": rows, "fields": fields});
  35. });
  36. }
  37. });
  38. });
  39. };
  40. const timesLen = 100;
  41. const filterLastestData = function(data, keyFields) {
  42. const dataIndex = {};
  43. for (const d of data) {
  44. let key = 'd';
  45. for (const kf of keyFields) {
  46. key = key + '.' + (d[kf] || '');
  47. }
  48. const di = dataIndex[key];
  49. if (di) {
  50. if ((di.times * timesLen + di.order) < (d.times * timesLen + d.order)) dataIndex[key] = d;
  51. } else {
  52. dataIndex[key] = d;
  53. }
  54. }
  55. const result = [];
  56. for (const prop in dataIndex) {
  57. result.push(dataIndex[prop]);
  58. }
  59. return result;
  60. };
  61. const doComplete = async function () {
  62. const tenders = await querySql('Select * From zh_tender where ledger_status = ?', [audit.ledger.status.checked]);
  63. for (const t of tenders.rows) {
  64. const stages = await querySql('Select * From zh_stage where tid = ? and status = ?', [t.id, audit.stage.status.checked]);
  65. for (const s of stages.rows) {
  66. const stageChange = await querySql('Select * From zh_stage_change where sid = ?', [s.id]);
  67. const validStageChange = filterLastestData(stageChange.rows, ['lid', 'pid', 'cid', 'cbid']);
  68. const stageChangeFinal = [];
  69. for (const vsc of validStageChange) {
  70. if (!vsc.qty) continue;
  71. stageChangeFinal.push([
  72. vsc.tid, vsc.sid,
  73. vsc.lid, vsc.pid, vsc.cid, vsc.cbid,
  74. vsc.qty,
  75. ]);
  76. }
  77. const result = await querySql('Insert Into zh_stage_change_final(tid, sid, lid, pid, cid, cbid, qty) Values ?', [stageChangeFinal]);
  78. }
  79. }
  80. pool.end();
  81. };
  82. doComplete();