financial_transfer.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. 修改脚本,请现在指定项目测试
  3. 例如: node db_script/sub_project local T201711273363
  4. 没有意外再全部执行
  5. 所有修改均应考虑脚本二次执行时的兼容,应检查是否已执行过,避免在生产环境运行时出现问题后需要二次执行,参见自定义分类脚本
  6. */
  7. const BaseUtil = require('./baseUtils');
  8. const querySql = BaseUtil.querySql;
  9. const _ = require('lodash');
  10. const ZhCalc = BaseUtil.ZhCalc;
  11. const calcPreHbTP = async function(subProject) {
  12. const ftList = await querySql('SELECT * FROM zh_financial_transfer where spid = ? ORDER BY id DESC', [subProject.id]);
  13. if (ftList.length > 0) {
  14. const updateFtList = [];
  15. const updateFttList = [];
  16. const fttList = await querySql('SELECT * FROM zh_financial_transfer_tender where spid = ? ORDER BY trid DESC, id ASC', [subProject.id]);
  17. if (fttList.length > 0) {
  18. for (const ftt of fttList) {
  19. const ftts = _.filter(fttList, function(item) {
  20. return item.tid === ftt.tid && item.trid < ftt.trid;
  21. });
  22. let pre_hb_tp = 0;
  23. if (ftts.length > 0) {
  24. pre_hb_tp = ZhCalc.sum(_.map(ftts, 'hb_tp'));
  25. }
  26. if (pre_hb_tp !== ftt.pre_hb_tp) {
  27. ftt.pre_hb_tp = pre_hb_tp;
  28. updateFttList.push({ id: ftt.id, pre_hb_tp });
  29. }
  30. }
  31. for (const ft of ftList) {
  32. const fts = _.filter(ftList, function(item) {
  33. return item.tid === ft.tid && item.id < ft.id;
  34. });
  35. let pre_hb_tp = 0;
  36. if (fts.length > 0) {
  37. pre_hb_tp = ZhCalc.sum(_.map(fts, 'total_price'));
  38. }
  39. if (pre_hb_tp !== ft.pre_hb_tp) {
  40. ft.pre_hb_tp = pre_hb_tp;
  41. updateFtList.push({ id: ft.id, pre_hb_tp });
  42. }
  43. }
  44. }
  45. if (updateFtList.length > 0) {
  46. const sql = 'UPDATE zh_financial_transfer SET pre_hb_tp = ? WHERE id = ?';
  47. for (const ft of updateFtList) {
  48. await querySql(sql, [ft.pre_hb_tp, ft.id]);
  49. }
  50. }
  51. if (updateFttList.length > 0) {
  52. const sql = 'UPDATE zh_financial_transfer_tender SET pre_hb_tp = ? WHERE id = ?';
  53. for (const ftt of updateFttList) {
  54. await querySql(sql, [ftt.pre_hb_tp, ftt.id]);
  55. }
  56. }
  57. }
  58. };
  59. const doComplete = async function(code) {
  60. try {
  61. const filter = code ? ` where code = '${code}'` : '';
  62. const project = await querySql('Select * From zh_project' + filter);
  63. for (const p of project) {
  64. console.log(`Update Project ${p.code}(${p.id}):`);
  65. const subProj = await querySql('SELECT * FROM zh_sub_project where project_id = ? and is_folder = 0 and is_delete = 0;', [p.id]);
  66. for (const sp of subProj) {
  67. await calcPreHbTP(sp);
  68. }
  69. console.log('END Update;');
  70. }
  71. } catch (err) {
  72. console.log(err);
  73. }
  74. BaseUtil.closePool();
  75. };
  76. const projectCode = process.argv[3];
  77. doComplete(projectCode);