change.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 checkChange = async function(change, decimal) {
  7. const changeBills = await querySql('Select * From zh_change_audit_list where cid = ?', [change.cid]);
  8. let p_tp = 0, n_tp = 0;
  9. for (const cb of changeBills) {
  10. cb.tp = ZhCalc.mul(cb.spamount, cb.unit_price, change.tp_decimal || decimal.tp);
  11. if (cb.spamount > 0) {
  12. p_tp = ZhCalc.add(p_tp, cb.tp);
  13. } else if (cb.spamount < 0){
  14. n_tp = ZhCalc.add(n_tp, cb.tp);
  15. }
  16. }
  17. await querySql('Update zh_change Set positive_tp = ?, negative_tp = ? Where cid = ?', [p_tp, n_tp, change.cid]);
  18. console.log(`Update Change ${change.cid}: p_tp(${p_tp}), n_tp(${n_tp})`);
  19. };
  20. const checkStage = async function(stage, decimal, preStage) {
  21. const billsTable = 'zh_stage_bills_' + stage.tid % 10;
  22. const stageBills = await querySql(`Select * From ${billsTable} Where sid = ?`, [stage.id]);
  23. const history = stage.tp_history ? JSON.parse(stage.tp_history) : null;
  24. if (history) {
  25. for (const h of history) {
  26. const sbFilter = stageBills.filter(x => {
  27. return x.times < h.times || (x.times === h.times && x.order <= h.order);
  28. });
  29. const sbLatest = BaseUtil.filterLastestData(sbFilter, ['lid']);
  30. history.positive_qc_tp = 0;
  31. history.negative_qc_tp = 0;
  32. for (const sbl of sbLatest) {
  33. stage.positive_qc_tp = ZhCalc.add(sbl.positive_qc_tp, stage.positive_qc_tp);
  34. stage.negative_qc_tp = ZhCalc.add(sbl.negative_qc_tp, stage.negative_qc_tp);
  35. }
  36. }
  37. await querySql('Update zh_stage Set tp_history = ? Where id = ?', [JSON.stringify(history), stage.id]);
  38. }
  39. const stageBillsLatest = BaseUtil.filterLastestData(stageBills, ['lid']);
  40. if (stageBillsLatest.length === 0) return;
  41. stage.positive_qc_tp = 0;
  42. stage.pre_positive_qc_tp = preStage ? ZhCalc.add(preStage.positive_qc_tp, preStage.pre_positive_qc_tp) : 0;
  43. stage.negative_qc_tp = 0;
  44. stage.pre_negative_qc_tp = preStage ? ZhCalc.add(preStage.negative_qc_tp, preStage.pre_negative_qc_tp) : 0;
  45. for (const sbl of stageBillsLatest) {
  46. stage.positive_qc_tp = ZhCalc.add(sbl.positive_qc_tp, stage.positive_qc_tp);
  47. stage.negative_qc_tp = ZhCalc.add(sbl.negative_qc_tp, stage.negative_qc_tp);
  48. }
  49. 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]);
  50. 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})`);
  51. };
  52. const checkStagePos = async function (stage, decimal, preStage) {
  53. const posTable = 'zh_stage_pos_' + stage.tid % 20;
  54. const posFinalTable = 'zh_stage_pos_final_' + stage.tid % 20;
  55. // 计算更新stagePos相关
  56. const stageChange = await querySql('SELECT * FROM zh_stage_change WHERE sid = ?', [stage.id]);
  57. const stagePos = await querySql(`SELECT * From ${posTable} where sid = ?`, [stage.id]);
  58. for (const sp of stagePos) {
  59. const filterTimesOrder = stageChange.filter(x => {
  60. if (x.pid === sp.pid && !x.no_value) {
  61. return x.stimes < sp.times || (x.stimes === sp.times && x.sorder <= sp.order);
  62. } else {
  63. return false;
  64. }
  65. });
  66. const filterLatest = BaseUtil.filterLastestData(filterTimesOrder, ['cbid'], 'stimes', 'sorder');
  67. if (filterLatest.length === 0) continue;
  68. sp.positive_qc_qty = 0;
  69. sp.negative_qc_qty = 0;
  70. for (const fl of filterLatest) {
  71. if (fl.minus) {
  72. sp.negative_qc_qty = ZhCalc.add(sp.negative_qc_qty, fl.qty);
  73. } else {
  74. sp.positive_qc_qty = ZhCalc.add(sp.positive_qc_qty, fl.qty);
  75. }
  76. }
  77. if (sp.positive_qc_qty || sp.negative_qc_qty)
  78. await querySql(`Update ${posTable} Set positive_qc_qty = ?, negative_qc_qty = ? Where id = ?`, [sp.positive_qc_qty, sp.negative_qc_qty, sp.id]);
  79. }
  80. console.log(`Update StagePos ${stage.order}: ${stagePos.length}`);
  81. // 计算更新stagePosFinal
  82. const stagePosLatest = BaseUtil.filterLastestData(stagePos, ['pid']);
  83. const stagePosLatestIndex = {};
  84. stagePosLatest.forEach(x => { stagePosLatestIndex[x.pid] = x; });
  85. const preStagePosFinal = preStage ? await querySql(`Select * From ${posFinalTable} Where sid = ?`, [preStage.id]) : [];
  86. const preStagePosFinalIndex = {};
  87. preStagePosFinal.forEach(x => { preStagePosFinalIndex[x.pid] = x; });
  88. const stagePosFinal = await querySql(`Select * From ${posFinalTable} Where sid = ?`, [stage.id]);
  89. for (const spf of stagePosFinal) {
  90. const curSp = stagePosLatestIndex[spf.pid];
  91. const preSp = preStagePosFinalIndex[spf.pid];
  92. if (preSp) {
  93. if (curSp) {
  94. spf.positive_qc_qty = ZhCalc.add(preSp.positive_qc_qty, curSp.positive_qc_qty);
  95. spf.negative_qc_qty = ZhCalc.add(preSp.negative_qc_qty, curSp.negative_qc_qty);
  96. } else {
  97. spf.positive_qc_qty = preSp.positive_qc_qty;
  98. spf.negative_qc_qty = preSp.negative_qc_qty;
  99. }
  100. } else if (curSp) {
  101. spf.positive_qc_qty = curSp.positive_qc_qty;
  102. spf.negative_qc_qty = curSp.negative_qc_qty;
  103. }
  104. if (spf.positive_qc_qty || spf.negative_qc_qty)
  105. await querySql(`Update ${posFinalTable} Set positive_qc_qty = ?, negative_qc_qty = ? Where id = ?`, [spf.positive_qc_qty, spf.negative_qc_qty, spf.id]);
  106. }
  107. console.log(`Update StagePosFinal ${stage.order}: ${stagePosFinal.length}`);
  108. };
  109. const checkStageBills = async function (stage, decimal, preStage) {
  110. const billsTable = 'zh_stage_bills_' + stage.tid % 10;
  111. const billsFinalTable = 'zh_stage_bills_final_' + stage.tid % 10;
  112. // 计算更新stagePos相关
  113. const stageChange = 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]);
  114. const stageBills = await querySql(`SELECT * From ${billsTable} where sid = ?`, [stage.id]);
  115. for (const sb of stageBills) {
  116. const filterTimesOrder = stageChange.filter(x => {
  117. if (x.no_value) return false;
  118. if (x.lid !== sb.lid) return false;
  119. return x.stimes < sb.times || (x.stimes === sb.times && x.sorder <= sb.order);
  120. });
  121. const filterLatest = BaseUtil.filterLastestData(filterTimesOrder, ['pid', 'cbid'], 'stimes', 'sorder');
  122. if (filterLatest.length === 0) continue;
  123. sb.positive_qc_qty = 0;
  124. sb.negative_qc_qty = 0;
  125. for (const fl of filterLatest) {
  126. if (!sb.unit_price) sb.unit_price = fl.unit_price;
  127. if (fl.minus) {
  128. sb.negative_qc_qty = ZhCalc.add(sb.negative_qc_qty, fl.qty);
  129. } else {
  130. sb.positive_qc_qty = ZhCalc.add(sb.positive_qc_qty, fl.qty);
  131. }
  132. }
  133. if (sb.positive_qc_qty || sb.negative_qc_qty) {
  134. sb.positive_qc_tp = ZhCalc.mul(sb.unit_price, sb.positive_qc_qty, decimal.tp);
  135. sb.negative_qc_tp = ZhCalc.mul(sb.unit_price, sb.negative_qc_qty, decimal.tp);
  136. await querySql(`Update ${billsTable} Set positive_qc_qty = ?, positive_qc_tp = ?, negative_qc_qty = ?, negative_qc_tp = ? Where id = ?`,
  137. [sb.positive_qc_qty, sb.positive_qc_tp, sb.negative_qc_qty, sb.negative_qc_tp, sb.id]);
  138. }
  139. }
  140. console.log(`Update StageBills ${stage.order}: ${stageBills.length}`);
  141. // 计算更新stagePosFinal
  142. const stageBillsLatest = BaseUtil.filterLastestData(stageBills, ['lid']);
  143. const stageBillsLatestIndex = {};
  144. stageBillsLatest.forEach(x => { stageBillsLatestIndex[x.lid] = x; });
  145. const preStageBillsFinal = preStage ? await querySql(`Select * From ${billsFinalTable} Where sid = ?`, [preStage.id]) : [];
  146. const preStageBillsFinalIndex = {};
  147. preStageBillsFinal.forEach(x => { preStageBillsFinalIndex[x.lid] = x; });
  148. const stageBillsFinal = await querySql(`Select * From ${billsFinalTable} Where sid = ?`, [stage.id]);
  149. for (const sbf of stageBillsFinal) {
  150. const curSb = stageBillsLatestIndex[sbf.lid];
  151. const preSb = preStageBillsFinalIndex[sbf.lid];
  152. if (preSb) {
  153. if (curSb) {
  154. sbf.positive_qc_qty = ZhCalc.add(preSb.positive_qc_qty, curSb.positive_qc_qty);
  155. sbf.positive_qc_tp = ZhCalc.add(preSb.positive_qc_tp, curSb.positive_qc_tp);
  156. sbf.negative_qc_qty = ZhCalc.add(preSb.negative_qc_qty, curSb.negative_qc_qty);
  157. sbf.negative_qc_tp = ZhCalc.add(preSb.negative_qc_tp, curSb.negative_qc_tp);
  158. } else {
  159. sbf.positive_qc_qty = preSb.positive_qc_qty;
  160. sbf.positive_qc_tp = preSb.positive_qc_tp;
  161. sbf.negative_qc_qty = preSb.negative_qc_qty;
  162. sbf.negative_qc_tp = preSb.negative_qc_tp;
  163. }
  164. } else if (curSb) {
  165. sbf.positive_qc_qty = curSb.positive_qc_qty;
  166. sbf.positive_qc_tp = curSb.positive_qc_tp;
  167. sbf.negative_qc_qty = curSb.negative_qc_qty;
  168. sbf.negative_qc_tp = curSb.negative_qc_tp;
  169. }
  170. if (sbf.positive_qc_qty || sbf.negative_qc_qty)
  171. await querySql(`Update ${billsFinalTable} Set positive_qc_qty = ?, positive_qc_tp = ?, negative_qc_qty = ?, negative_qc_tp = ? Where id = ?`,
  172. [sbf.positive_qc_qty, sbf.positive_qc_tp, sbf.negative_qc_qty, sbf.negative_qc_tp, sbf.id]);
  173. }
  174. console.log(`Update StageBillsFinal ${stage.order}: ${stageBillsFinal.length}`);
  175. };
  176. const doComplete = async function() {
  177. try {
  178. const tender = await querySql('Select * From zh_tender Where id >= 11405');
  179. for (const t of tender) {
  180. console.log(`Update Tender ${t.id}:`);
  181. const info = await querySql('Select * From zh_tender_info where tid = ?', [t.id]);
  182. const decimal = info.length > 0 && info[0].decimal ? JSON.parse(info[0].decimal) : defaultInfo.defaultInfo.decimal;
  183. const changes = await querySql('Select * From zh_change where tid = ?', [t.id]);
  184. for (const c of changes) {
  185. await checkChange(c, decimal);
  186. }
  187. const stage = await querySql('Select * From zh_stage where tid = ? order by `order` asc', [t.id]);
  188. for (const [i, s] of stage.entries()) {
  189. await checkStagePos(s, decimal, i > 0 ? stage[i-1] : null);
  190. await checkStageBills(s, decimal, i > 0 ? stage[i-1] : null);
  191. await checkStage(s, decimal, i > 0 ? stage[i-1] : null);
  192. }
  193. }
  194. } catch (err) {
  195. console.log(err);
  196. }
  197. BaseUtil.closePool();
  198. };
  199. const doCompleteTest = async function(tid) {
  200. try {
  201. const tender = await querySql('Select * From zh_tender where id = ?', [tid]);
  202. for (const t of tender) {
  203. console.log(`Update Tender ${t.id}:`);
  204. const info = await querySql('Select * From zh_tender_info where tid = ?', [t.id]);
  205. const decimal = info.length > 0 && info[0].decimal ? JSON.parse(info[0].decimal) : defaultInfo.defaultInfo.decimal;
  206. const changes = await querySql('Select * From zh_change where tid = ?', [t.id]);
  207. for (const c of changes) {
  208. await checkChange(c, decimal);
  209. }
  210. const stage = await querySql('Select * From zh_stage where tid = ? order by `order` asc', [t.id]);
  211. for (const [i, s] of stage.entries()) {
  212. await checkStagePos(s, decimal, i > 0 ? stage[i-1] : null);
  213. await checkStageBills(s, decimal, i > 0 ? stage[i-1] : null);
  214. await checkStage(s, decimal, i > 0 ? stage[i-1] : null);
  215. }
  216. }
  217. } catch (err) {
  218. console.log(err);
  219. }
  220. BaseUtil.closePool();
  221. };
  222. const tenderId = process.argv[3];
  223. if (tenderId) {
  224. doCompleteTest(tenderId);
  225. } else {
  226. doComplete()
  227. }