pay_att.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 config = process.argv[2];
  12. console.log(config);
  13. const mysqlOptions = require(`../config/config.${config}`)({ baseDir: __dirname + '/app', root: __dirname, name: 'calc' }).mysql;
  14. console.log(mysqlOptions);
  15. const pool = mysql.createPool(mysqlOptions.client);
  16. const querySql = async function (sql, sqlParam) {
  17. return new Promise(function (resolve, reject) {
  18. pool.getConnection(function (err, conn) {
  19. if (err) {
  20. reject(err);
  21. } else {
  22. conn.query(sql, sqlParam, function (err, rows, fields) {
  23. //释放连接
  24. conn.release();
  25. //传递Promise回调对象
  26. resolve({"err": err, "rows": rows, "fields": fields});
  27. });
  28. }
  29. });
  30. });
  31. };
  32. const doComplete = async function () {
  33. try {
  34. const tenders = await querySql('Select * From zh_tender where ledger_status = ?', [audit.ledger.status.checked]);
  35. for (const t of tenders.rows) {
  36. const stages = await querySql('Select * From zh_stage where tid = ?', [t.id]);
  37. for (const s of stages.rows) {
  38. console.log('处理标段: ' + t.name + ' 期: ' + s.order);
  39. const auditors = await querySql('Select * From zh_stage_audit where sid = ? and times = ? order by `order`', [s.id, s.times ? s.times : 1]);
  40. const curAuditor = auditors.rows.find(x => { return x.status === audit.stage.status.checking});
  41. const order = curAuditor ? curAuditor.order : (auditors.rows.length > 0 ? auditors.rows.length : 0);
  42. const latestStagePay = await querySql('Select * From zh_stage_pay where sid = ? and stimes = ? and sorder = ?', [s.id, s.times ? s.times : 1, order]);
  43. const insertData = [];
  44. for (const lsp of latestStagePay.rows) {
  45. if (!lsp.attachment) continue;
  46. const attachment = JSON.parse(lsp.attachment);
  47. for (const a of attachment) {
  48. insertData.unshift([t.id, s.id, lsp.pid, a.uid, a.filename, a.fileext, a.filesize, a.filepath, a.in_time, a.username || '']);
  49. }
  50. }
  51. const result = await querySql('Insert Into zh_pay_attachment (tid, sid, pid, uid, filename, fileext, filesize, filepath, in_time, username ) Values ?', [insertData]);
  52. }
  53. }
  54. await querySql("Update zh_pay_attachment att Left Join zh_project_account acc On att.uid = acc.id Set att.username = acc.name Where att.username = ''");
  55. pool.end();
  56. } catch(err) {
  57. console.log(err);
  58. }
  59. };
  60. doComplete();