tender_info.js 9.0 KB

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