change_valuation.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // 计算变更令,计价不计价金额数
  2. const defaultInfo = require('../app/const/tender_info');
  3. const BaseUtil = require('./baseUtils');
  4. const _ = require('lodash');
  5. const querySql = BaseUtil.querySql;
  6. const ZhCalc = BaseUtil.ZhCalc;
  7. const checkChange = async function(change, decimal, precision = defaultInfo.defaultInfo.precision) {
  8. const sql = 'Select * From zh_change_audit_list where cid = ?';
  9. const sqlParam = [change.cid];
  10. const changeBills = await querySql(sql, sqlParam);
  11. const unitPriceUnit = change.up_decimal || decimal.up;
  12. const totalPriceUnit = change.tp_decimal || decimal.tp;
  13. let positive_tp = 0;
  14. let negative_tp = 0;
  15. let valuation_tp = 0;
  16. let unvaluation_tp = 0;
  17. let new_tp = 0;
  18. // 先汇总再统计金额
  19. const gclChangeList = _.uniq(_.map(changeBills, 'gcl_id'));
  20. for (const g of gclChangeList) {
  21. if (g) {
  22. const list = _.filter(changeBills, { gcl_id: g });
  23. let spamount = 0;
  24. let valuation_amount = 0;
  25. let unvaluation_amount = 0;
  26. let unitPrice = 0;
  27. for (const c of list) {
  28. if (c.spamount) {
  29. spamount = ZhCalc.add(spamount, ZhCalc.round(c.spamount, findDecimal(c.unit, precision)));
  30. unitPrice = c.unit_price;
  31. if (c.is_valuation) {
  32. valuation_amount = ZhCalc.add(valuation_amount, ZhCalc.round(c.spamount, findDecimal(c.unit, precision)));
  33. } else {
  34. unvaluation_amount = ZhCalc.add(unvaluation_amount, ZhCalc.round(c.spamount, findDecimal(c.unit, precision)));
  35. }
  36. }
  37. }
  38. const price = ZhCalc.round(ZhCalc.mul(spamount, ZhCalc.round(unitPrice, unitPriceUnit), totalPriceUnit), totalPriceUnit);
  39. if (price >= 0) {
  40. positive_tp = ZhCalc.add(positive_tp, price);
  41. } else {
  42. negative_tp = ZhCalc.add(negative_tp, price);
  43. }
  44. const valuation_price = ZhCalc.round(ZhCalc.mul(valuation_amount, ZhCalc.round(unitPrice, unitPriceUnit), totalPriceUnit), totalPriceUnit) || 0;
  45. const unvaluation_price = ZhCalc.round(ZhCalc.mul(unvaluation_amount, ZhCalc.round(unitPrice, unitPriceUnit), totalPriceUnit), totalPriceUnit) || 0;
  46. valuation_tp = ZhCalc.add(valuation_tp, valuation_price);
  47. unvaluation_tp = ZhCalc.add(unvaluation_tp, unvaluation_price);
  48. new_tp = ZhCalc.add(new_tp, price);
  49. } else {
  50. const list = _.filter(changeBills, { gcl_id: g });
  51. for (const c of list) {
  52. if (c.spamount) {
  53. const price = ZhCalc.round(ZhCalc.mul(ZhCalc.round(c.spamount, findDecimal(c.unit, precision)), ZhCalc.round(c.unit_price, unitPriceUnit), totalPriceUnit), totalPriceUnit) || 0;
  54. new_tp = ZhCalc.add(new_tp, price);
  55. if (price >= 0) {
  56. positive_tp = ZhCalc.add(positive_tp, price);
  57. } else {
  58. negative_tp = ZhCalc.add(negative_tp, price);
  59. }
  60. if (c.is_valuation) {
  61. valuation_tp = ZhCalc.add(valuation_tp, price);
  62. } else {
  63. unvaluation_tp = ZhCalc.add(unvaluation_tp, price);
  64. }
  65. }
  66. }
  67. }
  68. }
  69. const updateTpList = {};
  70. let updateFlag = false;
  71. if (positive_tp !== change.positive_tp) {
  72. updateTpList.positive_tp = positive_tp;
  73. updateFlag = true;
  74. }
  75. if (negative_tp !== change.negative_tp) {
  76. updateTpList.negative_tp = negative_tp;
  77. updateFlag = true;
  78. }
  79. if (valuation_tp !== change.valuation_tp) {
  80. updateTpList.valuation_tp = valuation_tp;
  81. updateFlag = true;
  82. }
  83. if (unvaluation_tp !== change.unvaluation_tp) {
  84. updateTpList.unvaluation_tp = unvaluation_tp;
  85. updateFlag = true;
  86. }
  87. if (updateFlag) {
  88. const sql = 'Update zh_change Set ? Where cid = ?';
  89. const sqlParam = [updateTpList, change.cid];
  90. await querySql(sql, sqlParam);
  91. console.log(`Update Change ${change.cid}`);
  92. }
  93. };
  94. const doComplete = async function() {
  95. try {
  96. const tender = await querySql('Select * From zh_tender');
  97. for (const t of tender) {
  98. console.log(`Update Tender ${t.id}:`);
  99. const info = await querySql('Select * From zh_tender_info where tid = ?', [t.id]);
  100. const decimal = info.length > 0 && info[0].decimal ? JSON.parse(info[0].decimal) : defaultInfo.defaultInfo.decimal;
  101. const precision = info.length > 0 && info[0].precision ? JSON.parse(info[0].precision) : defaultInfo.defaultInfo.precision;
  102. const changes = await querySql('Select * From zh_change where tid = ?', [t.id]);
  103. for (const c of changes) {
  104. await checkChange(c, decimal, precision);
  105. }
  106. }
  107. } catch (err) {
  108. console.log(err);
  109. }
  110. BaseUtil.closePool();
  111. };
  112. const doCompleteTest = async function(tid) {
  113. try {
  114. const tender = await querySql('Select * From zh_tender where id = ?', [tid]);
  115. for (const t of tender) {
  116. console.log(`Update Tender ${t.id}:`);
  117. const info = await querySql('Select * From zh_tender_info where tid = ?', [t.id]);
  118. const decimal = info.length > 0 && info[0].decimal ? JSON.parse(info[0].decimal) : defaultInfo.defaultInfo.decimal;
  119. const precision = info.length > 0 && info[0].precision ? JSON.parse(info[0].precision) : defaultInfo.defaultInfo.precision;
  120. const changes = await querySql('Select * From zh_change where tid = ?', [t.id]);
  121. for (const c of changes) {
  122. await checkChange(c, decimal, precision);
  123. }
  124. }
  125. } catch (err) {
  126. console.log(err);
  127. }
  128. BaseUtil.closePool();
  129. };
  130. const tenderId = process.argv[3];
  131. if (tenderId) {
  132. doCompleteTest(tenderId);
  133. } else {
  134. doComplete();
  135. }
  136. function findDecimal(unit, precision) {
  137. let value = precision.other.value;
  138. for (const d in precision) {
  139. if (precision[d].unit !== undefined && precision[d].unit === unit) {
  140. value = precision[d].value;
  141. break;
  142. }
  143. }
  144. return value;
  145. }