change.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 audit = require('../app/const/audit');
  7. const checkChange = async function(change, decimal) {
  8. const changeBills = await querySql('Select * From zh_change_audit_list where cid = ?', [change.cid]);
  9. let p_tp = 0, n_tp = 0;
  10. for (const cb of changeBills) {
  11. cb.tp = ZhCalc.mul(cb.spamount, cb.unit_price, change.tp_decimal || decimal.tp);
  12. if (cb.spamount > 0) {
  13. p_tp = ZhCalc.add(p_tp, cb.tp);
  14. } else if (cb.spamount < 0){
  15. n_tp = ZhCalc.add(n_tp, cb.tp);
  16. }
  17. }
  18. await querySql('Update zh_change Set positive_tp = ?, negative_tp = ? Where cid = ?', [p_tp, n_tp, change.cid]);
  19. console.log(`Update Change ${change.cid}: p_tp(${p_tp}), n_tp(${n_tp})`);
  20. };
  21. const checkStage = async function(stage, decimal, preStage) {
  22. let stageChange;
  23. if (stage.status === 3) {
  24. stageChange = await querySql('Select scf.*, cal.unit_price From zh_stage_change_final scf Left Join zh_change_audit_list cal ON scf.cbid = cal.id where sid = ?', [stage.id]);
  25. } else {
  26. const stageChangeAll = await querySql('SELECT sc.*, cal.unit_price FROM zh_stage_change sc Left Join zh_change_audit_list cal ON sc.cbid = cal.id WHERE sid = ?', [stage.id]);
  27. stageChange = BaseUtil.filterLastestData(stageChangeAll, ['lid', 'pid', 'cbid', 'no_value'], 'stimes', 'sorder');
  28. }
  29. if (stageChange.length === 0) return;
  30. stage.positive_qc_tp = 0;
  31. stage.pre_positive_qc_tp = preStage ? ZhCalc.add(stage.positive_qc_tp, stage.pre_positive_qc_tp) : 0;
  32. stage.negative_qc_tp = 0;
  33. stage.pre_negative_qc_tp = preStage ? ZhCalc.add(stage.negative_qc_tp, stage.pre_negative_qc_tp) : 0;
  34. for (const sc of stageChange) {
  35. if (sc.no_value || !sc.qty) continue;
  36. const tp = ZhCalc.mul(sc.unit_price, sc.qty, decimal.tp);
  37. if (sc.minus) {
  38. stage.negative_qc_tp = ZhCalc.add(tp, stage.negative_qc_tp);
  39. } else {
  40. stage.positive_qc_tp = ZhCalc.add(tp, stage.positive_qc_tp);
  41. }
  42. }
  43. 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]);
  44. 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})`);
  45. };
  46. const doComplete = async function() {
  47. try {
  48. const tender = await querySql('Select * From zh_tender');
  49. for (const t of tender) {
  50. console.log(`Update Tender ${t.id}:`);
  51. const info = await querySql('Select * From zh_tender_info where tid = ?', [t.id]);
  52. const decimal = info[0].decimal ? JSON.parse(info[0].decimal) : defaultInfo.parseInfo.decimal;
  53. const changes = await querySql('Select * From zh_change where tid = ?', [t.id]);
  54. for (const c of changes) {
  55. await checkChange(c, decimal);
  56. }
  57. const stage = await querySql('Select * From zh_stage where tid = ? order by `order` asc', [t.id]);
  58. for (const [i, s] of stage.entries()) {
  59. await checkStage(s, decimal, i > 1 ? stage[i-1] : null);
  60. }
  61. }
  62. } catch (err) {
  63. console.log(err);
  64. }
  65. BaseUtil.closePool();
  66. };
  67. doComplete();