repair1027.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // 计算变更令,正负变更数
  2. const defaultInfo = require('../app/const/tender_info');
  3. const BaseUtil = require('./baseUtils');
  4. const querySql = BaseUtil.querySql;
  5. const ZhCalc = BaseUtil.ZhCalc;
  6. const checkStage = async function(stage, decimal, preStage) {
  7. const billsTable = 'zh_stage_bills_' + stage.tid % 10;
  8. const stageBills = await querySql(`Select * From ${billsTable} Where sid = ?`, [stage.id]);
  9. const history = stage.tp_history ? JSON.parse(stage.tp_history) : null;
  10. if (history) {
  11. for (const h of history) {
  12. const sbFilter = stageBills.filter(x => {
  13. return x.times < h.times || (x.times === h.times && x.order <= h.order);
  14. });
  15. const sbLatest = BaseUtil.filterLastestData(sbFilter, ['lid']);
  16. history.positive_qc_tp = 0;
  17. history.negative_qc_tp = 0;
  18. for (const sbl of sbLatest) {
  19. stage.positive_qc_tp = ZhCalc.add(sbl.positive_qc_tp, stage.positive_qc_tp);
  20. stage.negative_qc_tp = ZhCalc.add(sbl.negative_qc_tp, stage.negative_qc_tp);
  21. }
  22. }
  23. await querySql('Update zh_stage Set tp_history = ? Where id = ?', [JSON.stringify(history), stage.id]);
  24. }
  25. const stageBillsLatest = BaseUtil.filterLastestData(stageBills, ['lid']);
  26. if (stageBillsLatest.length === 0) return;
  27. stage.positive_qc_tp = 0;
  28. stage.pre_positive_qc_tp = preStage ? ZhCalc.add(preStage.positive_qc_tp, preStage.pre_positive_qc_tp) : 0;
  29. stage.negative_qc_tp = 0;
  30. stage.pre_negative_qc_tp = preStage ? ZhCalc.add(preStage.negative_qc_tp, preStage.pre_negative_qc_tp) : 0;
  31. for (const sbl of stageBillsLatest) {
  32. stage.positive_qc_tp = ZhCalc.add(sbl.positive_qc_tp, stage.positive_qc_tp);
  33. stage.negative_qc_tp = ZhCalc.add(sbl.negative_qc_tp, stage.negative_qc_tp);
  34. }
  35. await querySql('Update zh_stage Set positive_qc_tp = ?, pre_positive_qc_tp = ?, negative_qc_tp = ?, pre_negative_qc_tp = ? Where id = ?', [stage.positive_qc_tp, stage.pre_positive_qc_tp, stage.negative_qc_tp, stage.pre_negative_qc_tp, stage.id]);
  36. console.log(`Update Stage ${stage.order}: p_tp(${stage.positive_qc_tp}), pre_p_tp(${stage.pre_positive_qc_tp}), n_tp(${stage.negative_qc_tp}), pre_n_tp(${stage.pre_negative_qc_tp})`);
  37. };
  38. const checkStageBills = async function (stage, decimal, preStage, reCalcBills) {
  39. const billsTable = 'zh_stage_bills_' + stage.tid % 10;
  40. const billsFinalTable = 'zh_stage_bills_final_' + stage.tid % 10;
  41. const ledgerTable = 'zh_ledger_' + stage.tid % 10;
  42. // 计算更新stagePos相关
  43. const stageChange = await querySql(`SELECT sc.*, l.unit_price FROM zh_stage_change sc Left Join ${ledgerTable} l ON sc.lid = l.id WHERE sid = ?`, [stage.id]);
  44. const stageBills = await querySql(`SELECT * From ${billsTable} where sid = ?`, [stage.id]);
  45. if (reCalcBills) {
  46. for (const sb of stageBills) {
  47. const filterTimesOrder = stageChange.filter(x => {
  48. if (x.no_value) return false;
  49. if (x.lid !== sb.lid) return false;
  50. return x.stimes < sb.times || (x.stimes === sb.times && x.sorder <= sb.order);
  51. });
  52. const filterLatest = BaseUtil.filterLastestData(filterTimesOrder, ['pid', 'cbid'], 'stimes', 'sorder');
  53. if (filterLatest.length === 0) continue;
  54. sb.positive_qc_qty = 0;
  55. sb.negative_qc_qty = 0;
  56. for (const fl of filterLatest) {
  57. if (!sb.unit_price) sb.unit_price = fl.unit_price;
  58. if (fl.minus) {
  59. sb.negative_qc_qty = ZhCalc.add(sb.negative_qc_qty, fl.qty);
  60. } else {
  61. sb.positive_qc_qty = ZhCalc.add(sb.positive_qc_qty, fl.qty);
  62. }
  63. }
  64. if (sb.positive_qc_qty || sb.negative_qc_qty) {
  65. sb.positive_qc_tp = ZhCalc.mul(sb.unit_price, sb.positive_qc_qty, decimal.tp);
  66. sb.negative_qc_tp = ZhCalc.mul(sb.unit_price, sb.negative_qc_qty, decimal.tp);
  67. await querySql(`Update ${billsTable} Set positive_qc_qty = ?, positive_qc_tp = ?, negative_qc_qty = ?, negative_qc_tp = ? Where id = ?`,
  68. [sb.positive_qc_qty, sb.positive_qc_tp, sb.negative_qc_qty, sb.negative_qc_tp, sb.id]);
  69. }
  70. }
  71. console.log(`Update StageBills ${stage.order}: ${stageBills.length}`);
  72. }
  73. // 计算更新stagePosFinal
  74. const stageBillsLatest = BaseUtil.filterLastestData(stageBills, ['lid']);
  75. const stageBillsLatestIndex = {};
  76. stageBillsLatest.forEach(x => { stageBillsLatestIndex[x.lid] = x; });
  77. const preStageBillsFinal = preStage ? await querySql(`Select * From ${billsFinalTable} Where sid = ?`, [preStage.id]) : [];
  78. const preStageBillsFinalIndex = {};
  79. preStageBillsFinal.forEach(x => { preStageBillsFinalIndex[x.lid] = x; });
  80. const stageBillsFinal = await querySql(`Select * From ${billsFinalTable} Where sid = ?`, [stage.id]);
  81. for (const sbf of stageBillsFinal) {
  82. const curSb = stageBillsLatestIndex[sbf.lid];
  83. const preSb = preStageBillsFinalIndex[sbf.lid];
  84. if (preSb) {
  85. if (curSb) {
  86. sbf.positive_qc_qty = ZhCalc.add(preSb.positive_qc_qty, curSb.positive_qc_qty);
  87. sbf.positive_qc_tp = ZhCalc.add(preSb.positive_qc_tp, curSb.positive_qc_tp);
  88. sbf.negative_qc_qty = ZhCalc.add(preSb.negative_qc_qty, curSb.negative_qc_qty);
  89. sbf.negative_qc_tp = ZhCalc.add(preSb.negative_qc_tp, curSb.negative_qc_tp);
  90. } else {
  91. sbf.positive_qc_qty = preSb.positive_qc_qty;
  92. sbf.positive_qc_tp = preSb.positive_qc_tp;
  93. sbf.negative_qc_qty = preSb.negative_qc_qty;
  94. sbf.negative_qc_tp = preSb.negative_qc_tp;
  95. }
  96. } else if (curSb) {
  97. sbf.positive_qc_qty = curSb.positive_qc_qty;
  98. sbf.positive_qc_tp = curSb.positive_qc_tp;
  99. sbf.negative_qc_qty = curSb.negative_qc_qty;
  100. sbf.negative_qc_tp = curSb.negative_qc_tp;
  101. }
  102. if (sbf.positive_qc_qty || sbf.negative_qc_qty)
  103. await querySql(`Update ${billsFinalTable} Set positive_qc_qty = ?, positive_qc_tp = ?, negative_qc_qty = ?, negative_qc_tp = ? Where id = ?`,
  104. [sbf.positive_qc_qty, sbf.positive_qc_tp, sbf.negative_qc_qty, sbf.negative_qc_tp, sbf.id]);
  105. }
  106. console.log(`Update StageBillsFinal ${stage.order}: ${stageBillsFinal.length}`);
  107. };
  108. const doRepair1027 = async function(tid, sorder, reCalcBills) {
  109. try {
  110. const tender = await querySql('Select * From zh_tender where id = ?', [tid]);
  111. for (const t of tender) {
  112. console.log(`Update Tender ${t.id}:`);
  113. const info = await querySql('Select * From zh_tender_info where tid = ?', [t.id]);
  114. const decimal = info.length > 0 && info[0].decimal ? JSON.parse(info[0].decimal) : defaultInfo.defaultInfo.decimal;
  115. const stage = await querySql('Select * From zh_stage where tid = ? and `order` = ?', [t.id, sorder]);
  116. const preStage = sorder > 1 ? await querySql('Select * From zh_stage where tid = ? and `order` = ?', [t.id, sorder - 1]) : [];
  117. await checkStageBills(stage[0], decimal, preStage[0], reCalcBills);
  118. await checkStage(stage[0], decimal, preStage[0], reCalcBills);
  119. }
  120. } catch (err) {
  121. console.log(err);
  122. }
  123. BaseUtil.closePool();
  124. };
  125. const tenderId = parseInt(process.argv[3]);
  126. const stageOrder = parseInt(process.argv[4]);
  127. const reCalcBills = parseInt(process.argv[5]);
  128. doRepair1027(tenderId, stageOrder, reCalcBills);