revise_price.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. module.exports = app => {
  10. class RevisePrice extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'revise_price';
  20. }
  21. async _addDatas(data) {
  22. const datas = data instanceof Array ? data : [data];
  23. const insertData = [];
  24. const count = await this.db.count(this.tableName, { rid: this.ctx.revise.id });
  25. for (const [i, d] of datas.entries()) {
  26. if (!d.b_code) throw '新增单价调整,提交的数据错误';
  27. const nd = {
  28. pid: this.ctx.session.sessionProject.id,
  29. tid: this.ctx.tender.id,
  30. rid: this.ctx.revise.id,
  31. order: count + i + 1,
  32. b_code: d.b_code,
  33. name: d.name,
  34. unit: d.unit,
  35. org_price: d.unit_price,
  36. new_price: d.unit_price,
  37. valid: 1,
  38. memo: d.memo || '',
  39. };
  40. insertData.push(nd);
  41. }
  42. const result = await this.db.insert(this.tableName, insertData);
  43. for (const [i, d] of insertData.entries()) {
  44. d.id = result.insertId + i;
  45. }
  46. return insertData;
  47. }
  48. async _delDatas(data) {
  49. const datas = data instanceof Array ? data : [data];
  50. await this.db.delete(this.tableName, { id: datas });
  51. return datas;
  52. }
  53. async _updateDatas(data) {
  54. const datas = data instanceof Array ? data : [data];
  55. const uDatas = [];
  56. for (const d of datas) {
  57. const nd = { id: d.id };
  58. if (d.order !== undefined) nd.order = d.order;
  59. if (d.new_price !== undefined) {
  60. nd.new_price = this.ctx.helper.round(d.new_price, this.ctx.tender.info.decimal.up);
  61. nd.valid = !this.ctx.helper.checkZero(this.ctx.helper.sub(nd.new_price, d.org_price));
  62. }
  63. if (d.memo !== undefined) nd.memo = d.memo;
  64. if (d.rela_lid !== undefined) nd.rela_lid = d.rela_lid;
  65. if (d.rela_cid !== undefined) nd.rela_cid = d.rela_cid;
  66. uDatas.push(nd);
  67. }
  68. if (uDatas.length > 0) {
  69. await this.db.updateRows(this.tableName, uDatas);
  70. return uDatas;
  71. } else {
  72. return [];
  73. }
  74. }
  75. async updateDatas(data) {
  76. const result = { add: [], del: [], update: [] };
  77. try {
  78. if (data.add) {
  79. result.add = await this._addDatas(data.add);
  80. }
  81. if (data.update) {
  82. result.update = await this._updateDatas(data.update);
  83. }
  84. if (data.del) {
  85. result.del = await this._delDatas(data.del);
  86. }
  87. return result;
  88. } catch (err) {
  89. throw err;
  90. }
  91. }
  92. async doPriceUsed(stage, transaction) {
  93. const sql = `Update ${this.tableName} Set use_stage = ${stage.id}, use_stage_order = ${stage.order}` +
  94. ' Where tid = ? and use_stage = 0';
  95. await transaction.query(sql, [stage.tid]);
  96. }
  97. async cancelPriceUsed(stage, transaction) {
  98. const sql = `Update ${this.tableName} Set use_stage = 0, use_stage_order = 0` +
  99. ' Where tid = ? and use_stage = ?';
  100. await transaction.query(sql, [stage.tid, stage.id]);
  101. }
  102. }
  103. return RevisePrice;
  104. };