tender_info.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/10/30
  7. * @version
  8. */
  9. const infoConst = require('../const/tender_info');
  10. const parseInfo = infoConst.parseInfo;
  11. const defaultInfo = infoConst.defaultInfo;
  12. module.exports = app => {
  13. class TenderInfo extends app.BaseService {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局变量
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. this.tableName = 'tender_info';
  23. }
  24. /**
  25. * 新增 标段相关信息
  26. *
  27. * @param tenderId - 标段Id
  28. * @param projectId - 项目Id
  29. * @param transaction - 事务
  30. * @returns {Promise<void>}
  31. */
  32. async addTenderInfo(tenderId, projectId, transaction) {
  33. const info = JSON.parse(JSON.stringify(defaultInfo));
  34. info.tid = tenderId;
  35. info.pid = projectId;
  36. for (const pi of parseInfo) {
  37. info[pi] = JSON.stringify(info[pi])
  38. }
  39. if (transaction) {
  40. await transaction.insert(this.tableName, info);
  41. } else {
  42. await this.db.insert(this.tableName, info);
  43. }
  44. return info;
  45. }
  46. /**
  47. * 获取标段相关信息
  48. * @param tenderId
  49. * @returns {Promise<void>}
  50. */
  51. async getTenderInfo(tenderId) {
  52. let info = await this.getDataByCondition({tid: tenderId});
  53. // 兼容不存在info的情况
  54. if (!info) {
  55. info = await this.addTenderInfo(tenderId, this.ctx.session.sessionProject.id);
  56. }
  57. for (const pi of parseInfo) {
  58. info[pi] = !info[pi] || info[pi] === '' ? defaultInfo[pi] : JSON.parse(info[pi]);
  59. }
  60. return info;
  61. }
  62. /**
  63. * 保存标段相关信息
  64. *
  65. * @param data
  66. * @returns {Promise<void>}
  67. */
  68. async saveTenderInfo(tenderId, data) {
  69. for (const di in data) {
  70. if (parseInfo.indexOf(di) >= 0) {
  71. data[di] = JSON.stringify(data[di]);
  72. }
  73. }
  74. await this.db.update(this.tableName, data, {where: {tid: tenderId}});
  75. }
  76. async savePrecision(tenderId, newPrecision, oldPrecision, decimal) {
  77. const changePrecision = [];
  78. const units = await this.ctx.service.ledger.getTenderUsedUnits(tenderId);
  79. const defUnits = this._.map(newPrecision, 'units');
  80. const otherUnits = units.filter(function (u) {return defUnits.indexOf(u) === -1});
  81. let changeUnits = [];
  82. for (const prop in newPrecision) {
  83. if (oldPrecision[prop]) {
  84. if (newPrecision[prop].value < oldPrecision[prop].value) {
  85. changePrecision.push(newPrecision[prop]);
  86. }
  87. } else {
  88. changePrecision.push(newPrecision[prop]);
  89. }
  90. }
  91. for (const cp of changePrecision) {
  92. if (cp.unit) {
  93. changeUnits.push(cp.unit);
  94. } else {
  95. changeUnits.push(otherUnits);
  96. }
  97. }
  98. changeUnits = this._.flatten(changeUnits);
  99. if (changeUnits.length > 0) {
  100. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  101. columns: ['id', 'unit', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'],
  102. where: {tender_id: tenderId, unit: changeUnits, is_leaf: true}
  103. });
  104. const pos = changeUnits.length > 0 ? await this.ctx.service.pos.getPosDataByUnits(tenderId, changeUnits) : [];
  105. for (const b of bills) {
  106. const precision = this.ctx.helper.findPrecision(newPrecision, b.unit);
  107. const bPos = this._.filter(pos, {lid: b.id});
  108. if (bPos.length > 0) {
  109. let sgfh_qty = 0, sjcl_qty = 0, qtcl_qty = 0, quantity = 0;
  110. for (const p of bPos) {
  111. this.ctx.helper.checkFieldPrecision(p, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty'], precision.value);
  112. p.quantity = this.ctx.helper.add(this.ctx.helper.add(p.sgfh_qty, p.sjcl_qty), p.qtcl_qty);
  113. sgfh_qty = this.ctx.helper.add(sgfh_qty, p.sgfh_qty);
  114. sjcl_qty = this.ctx.helper.add(sjcl_qty, p.sjcl_qty);
  115. qtcl_qty = this.ctx.helper.add(qtcl_qty, p.qtcl_qty);
  116. quantity = this.ctx.helper.add(quantity, p.quantity);
  117. }
  118. b.sgfh_qty = sgfh_qty;
  119. b.sjcl_qty = sjcl_qty;
  120. b.qtcl_qty = qtcl_qty;
  121. b.quantity = quantity;
  122. } else {
  123. this.ctx.helper.checkFieldPrecision(b, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'], precision.value);
  124. }
  125. b.quantity = this.ctx.helper.add(this.ctx.helper.add(b.sgfh_qty, b.sjcl_qty), b.qtcl_qty);
  126. b.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, b.unit_price, decimal.tp);
  127. b.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, b.unit_price, decimal.tp);
  128. b.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, b.unit_price, decimal.tp);
  129. b.deal_tp = this.ctx.helper.mul(b.deal_qty, b.unit_price, decimal.tp);
  130. b.total_price = this.ctx.helper.mul(b.quantity, b.unit_price, decimal.tp);
  131. }
  132. const transaction = await this.db.beginTransaction();
  133. try {
  134. await transaction.update(this.tableName,
  135. {precision: JSON.stringify(newPrecision)}, {where: {tid: tenderId}});
  136. if (bills.length > 0) await transaction.updateRows(this.ctx.service.ledger.tableName, bills);
  137. if (pos.length > 0) await transaction.updateRows(this.ctx.service.pos.tableName, pos);
  138. await transaction.commit();
  139. } catch (err) {
  140. await transaction.rollback();
  141. throw err;
  142. }
  143. } else {
  144. await this.db.update(this.tableName,
  145. {precision: JSON.stringify(newPrecision)}, {where: {tid: tenderId}});
  146. }
  147. }
  148. async saveDecimal(tenderId, newDecimal, oldDecimal) {
  149. const changeBills = [], calcUp = newDecimal.up < oldDecimal.up, calcTp = newDecimal.tp !== oldDecimal.tp;
  150. if (calcUp || calcTp) {
  151. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  152. columns: ['id', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty', 'quantity'],
  153. where: {tender_id: tenderId, is_leaf: true}
  154. });
  155. for (const b of bills) {
  156. const cb = { id: b.id };
  157. cb.unit_price = calcUp ? this.ctx.helper.round(b.unit_price, newDecimal.up) : b.unit_price;
  158. cb.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, cb.unit_price, newDecimal.tp);
  159. cb.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, cb.unit_price, newDecimal.tp);
  160. cb.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, cb.unit_price, newDecimal.tp);
  161. cb.deal_tp = this.ctx.helper.mul(b.deal_qty, cb.unit_price, newDecimal.tp);
  162. cb.total_price = this.ctx.helper.mul(b.quantity, cb.unit_price, newDecimal.tp);
  163. changeBills.push(cb);
  164. }
  165. }
  166. if (changeBills.length > 0) {
  167. const transaction = await this.db.beginTransaction();
  168. try {
  169. await transaction.update(this.tableName,
  170. {decimal: JSON.stringify(newDecimal)}, { where: { tid: tenderId }});
  171. await transaction.updateRows(this.ctx.service.ledger.tableName, changeBills);
  172. await transaction.commit();
  173. } catch(error) {
  174. await transaction.rollback();
  175. throw error;
  176. }
  177. } else {
  178. await this.db.update(this.tableName,
  179. {decimal: JSON.stringify(newDecimal)}, { where: { tid: tenderId }});
  180. }
  181. }
  182. }
  183. return TenderInfo;
  184. };