change_valuation.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 positive_amount = 0;
  25. let negative_amount = 0;
  26. let valuation_amount = 0;
  27. let unvaluation_amount = 0;
  28. let unitPrice = 0;
  29. for (const c of list) {
  30. if (c.spamount) {
  31. const amount = ZhCalc.round(c.spamount, findDecimal(c.unit, precision));
  32. spamount = ZhCalc.add(spamount, amount);
  33. if (amount >= 0) {
  34. positive_amount = ZhCalc.add(positive_amount, amount);
  35. } else {
  36. negative_amount = ZhCalc.add(negative_amount, amount);
  37. }
  38. unitPrice = c.unit_price;
  39. if (c.is_valuation) {
  40. valuation_amount = ZhCalc.add(valuation_amount, ZhCalc.round(c.spamount, findDecimal(c.unit, precision)));
  41. } else {
  42. unvaluation_amount = ZhCalc.add(unvaluation_amount, ZhCalc.round(c.spamount, findDecimal(c.unit, precision)));
  43. }
  44. }
  45. }
  46. const price = ZhCalc.round(ZhCalc.mul(spamount, ZhCalc.round(unitPrice, unitPriceUnit), totalPriceUnit), totalPriceUnit);
  47. // 正负数量分别汇总后计算金额,避免相互抵消。
  48. const positive_price = ZhCalc.round(ZhCalc.mul(positive_amount, ZhCalc.round(unitPrice, unitPriceUnit), totalPriceUnit), totalPriceUnit) || 0;
  49. const negative_price = ZhCalc.round(ZhCalc.mul(negative_amount, ZhCalc.round(unitPrice, unitPriceUnit), totalPriceUnit), totalPriceUnit) || 0;
  50. positive_tp = ZhCalc.add(positive_tp, positive_price);
  51. negative_tp = ZhCalc.add(negative_tp, negative_price);
  52. const valuation_price = ZhCalc.round(ZhCalc.mul(valuation_amount, ZhCalc.round(unitPrice, unitPriceUnit), totalPriceUnit), totalPriceUnit) || 0;
  53. const unvaluation_price = ZhCalc.round(ZhCalc.mul(unvaluation_amount, ZhCalc.round(unitPrice, unitPriceUnit), totalPriceUnit), totalPriceUnit) || 0;
  54. valuation_tp = ZhCalc.add(valuation_tp, valuation_price);
  55. unvaluation_tp = ZhCalc.add(unvaluation_tp, unvaluation_price);
  56. new_tp = ZhCalc.add(new_tp, price);
  57. } else {
  58. const list = _.filter(changeBills, { gcl_id: g });
  59. for (const c of list) {
  60. if (c.spamount) {
  61. const price = ZhCalc.round(ZhCalc.mul(ZhCalc.round(c.spamount, findDecimal(c.unit, precision)), ZhCalc.round(c.unit_price, unitPriceUnit), totalPriceUnit), totalPriceUnit) || 0;
  62. new_tp = ZhCalc.add(new_tp, price);
  63. if (price >= 0) {
  64. positive_tp = ZhCalc.add(positive_tp, price);
  65. } else {
  66. negative_tp = ZhCalc.add(negative_tp, price);
  67. }
  68. if (c.is_valuation) {
  69. valuation_tp = ZhCalc.add(valuation_tp, price);
  70. } else {
  71. unvaluation_tp = ZhCalc.add(unvaluation_tp, price);
  72. }
  73. }
  74. }
  75. }
  76. }
  77. const updateTpList = {};
  78. let updateFlag = false;
  79. if (positive_tp !== change.positive_tp) {
  80. updateTpList.positive_tp = positive_tp;
  81. updateFlag = true;
  82. }
  83. if (negative_tp !== change.negative_tp) {
  84. updateTpList.negative_tp = negative_tp;
  85. updateFlag = true;
  86. }
  87. if (valuation_tp !== change.valuation_tp) {
  88. updateTpList.valuation_tp = valuation_tp;
  89. updateFlag = true;
  90. }
  91. if (unvaluation_tp !== change.unvaluation_tp) {
  92. updateTpList.unvaluation_tp = unvaluation_tp;
  93. updateFlag = true;
  94. }
  95. if (updateFlag) {
  96. const sql = 'Update zh_change Set ? Where cid = ?';
  97. const sqlParam = [updateTpList, change.cid];
  98. await querySql(sql, sqlParam);
  99. console.log(`Update Change ${change.cid}`);
  100. }
  101. };
  102. const doComplete = async function() {
  103. try {
  104. const tender = await querySql('Select * From zh_tender');
  105. for (const t of tender) {
  106. console.log(`Update Tender ${t.id}:`);
  107. const info = await querySql('Select * From zh_tender_info where tid = ?', [t.id]);
  108. const decimal = info.length > 0 && info[0].decimal ? JSON.parse(info[0].decimal) : defaultInfo.defaultInfo.decimal;
  109. const precision = info.length > 0 && info[0].precision ? JSON.parse(info[0].precision) : defaultInfo.defaultInfo.precision;
  110. const changes = await querySql('Select * From zh_change where tid = ?', [t.id]);
  111. for (const c of changes) {
  112. await checkChange(c, decimal, precision);
  113. }
  114. }
  115. } catch (err) {
  116. console.log(err);
  117. }
  118. BaseUtil.closePool();
  119. };
  120. const doCompleteTest = async function(tid) {
  121. try {
  122. const tender = await querySql('Select * From zh_tender where id = ?', [tid]);
  123. for (const t of tender) {
  124. console.log(`Update Tender ${t.id}:`);
  125. const info = await querySql('Select * From zh_tender_info where tid = ?', [t.id]);
  126. const decimal = info.length > 0 && info[0].decimal ? JSON.parse(info[0].decimal) : defaultInfo.defaultInfo.decimal;
  127. const precision = info.length > 0 && info[0].precision ? JSON.parse(info[0].precision) : defaultInfo.defaultInfo.precision;
  128. const changes = await querySql('Select * From zh_change where tid = ?', [t.id]);
  129. for (const c of changes) {
  130. await checkChange(c, decimal, precision);
  131. }
  132. }
  133. } catch (err) {
  134. console.log(err);
  135. }
  136. BaseUtil.closePool();
  137. };
  138. const tenderId = process.argv[3];
  139. if (tenderId) {
  140. doCompleteTest(tenderId);
  141. } else {
  142. doComplete();
  143. }
  144. function findDecimal(unit, precision) {
  145. let value = precision.other.value;
  146. for (const d in precision) {
  147. if (precision[d].unit !== undefined && precision[d].unit === unit) {
  148. value = precision[d].value;
  149. break;
  150. }
  151. }
  152. return value;
  153. }