tender_info.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. return info;
  69. }
  70. /**
  71. * 保存标段相关信息
  72. *
  73. * @param data
  74. * @returns {Promise<void>}
  75. */
  76. async saveTenderInfo(tenderId, data) {
  77. for (const di in data) {
  78. if (parseInfo.indexOf(di) >= 0 || arrayInfo.indexOf(di) >= 0) {
  79. data[di] = JSON.stringify(data[di]);
  80. }
  81. }
  82. await this.db.update(this.tableName, data, {where: {tid: tenderId}});
  83. }
  84. async savePrecision(tenderId, newPrecision, oldPrecision, decimal) {
  85. const changePrecision = [];
  86. const units = await this.ctx.service.ledger.getTenderUsedUnits(tenderId);
  87. const defUnits = this._.map(newPrecision, 'units');
  88. const otherUnits = units.filter(function (u) {return defUnits.indexOf(u) === -1});
  89. let changeUnits = [];
  90. for (const prop in newPrecision) {
  91. if (oldPrecision[prop]) {
  92. if (newPrecision[prop].value < oldPrecision[prop].value) {
  93. changePrecision.push(newPrecision[prop]);
  94. }
  95. } else {
  96. changePrecision.push(newPrecision[prop]);
  97. }
  98. }
  99. for (const cp of changePrecision) {
  100. if (cp.unit) {
  101. changeUnits.push(cp.unit);
  102. } else {
  103. changeUnits.push(otherUnits);
  104. }
  105. }
  106. changeUnits = this._.flatten(changeUnits);
  107. if (changeUnits.length > 0) {
  108. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  109. columns: ['id', 'unit', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'],
  110. where: {tender_id: tenderId, unit: changeUnits, is_leaf: true}
  111. });
  112. const pos = changeUnits.length > 0 ? await this.ctx.service.pos.getPosDataByUnits(tenderId, changeUnits) : [];
  113. for (const b of bills) {
  114. const precision = this.ctx.helper.findPrecision(newPrecision, b.unit);
  115. const bPos = this._.filter(pos, {lid: b.id});
  116. if (bPos.length > 0) {
  117. let sgfh_qty = 0, sjcl_qty = 0, qtcl_qty = 0, quantity = 0;
  118. for (const p of bPos) {
  119. this.ctx.helper.checkFieldPrecision(p, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty'], precision.value);
  120. p.quantity = this.ctx.helper.add(this.ctx.helper.add(p.sgfh_qty, p.sjcl_qty), p.qtcl_qty);
  121. sgfh_qty = this.ctx.helper.add(sgfh_qty, p.sgfh_qty);
  122. sjcl_qty = this.ctx.helper.add(sjcl_qty, p.sjcl_qty);
  123. qtcl_qty = this.ctx.helper.add(qtcl_qty, p.qtcl_qty);
  124. quantity = this.ctx.helper.add(quantity, p.quantity);
  125. }
  126. b.sgfh_qty = sgfh_qty;
  127. b.sjcl_qty = sjcl_qty;
  128. b.qtcl_qty = qtcl_qty;
  129. b.quantity = quantity;
  130. } else {
  131. this.ctx.helper.checkFieldPrecision(b, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'], precision.value);
  132. }
  133. b.quantity = this.ctx.helper.add(this.ctx.helper.add(b.sgfh_qty, b.sjcl_qty), b.qtcl_qty);
  134. b.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, b.unit_price, decimal.tp);
  135. b.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, b.unit_price, decimal.tp);
  136. b.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, b.unit_price, decimal.tp);
  137. b.deal_tp = this.ctx.helper.mul(b.deal_qty, b.unit_price, decimal.tp);
  138. b.total_price = this.ctx.helper.mul(b.quantity, b.unit_price, decimal.tp);
  139. }
  140. const transaction = await this.db.beginTransaction();
  141. try {
  142. await transaction.update(this.tableName,
  143. {precision: JSON.stringify(newPrecision)}, {where: {tid: tenderId}});
  144. if (bills.length > 0) await transaction.updateRows(this.ctx.service.ledger.tableName, bills);
  145. if (pos.length > 0) await transaction.updateRows(this.ctx.service.pos.tableName, pos);
  146. await transaction.commit();
  147. } catch (err) {
  148. await transaction.rollback();
  149. throw err;
  150. }
  151. } else {
  152. await this.db.update(this.tableName,
  153. {precision: JSON.stringify(newPrecision)}, {where: {tid: tenderId}});
  154. }
  155. }
  156. async saveDecimal(tenderId, newDecimal, oldDecimal) {
  157. const changeBills = [], calcUp = newDecimal.up < oldDecimal.up, calcTp = newDecimal.tp !== oldDecimal.tp;
  158. if (calcUp || calcTp) {
  159. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  160. columns: ['id', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty', 'quantity'],
  161. where: {tender_id: tenderId, is_leaf: true}
  162. });
  163. for (const b of bills) {
  164. const cb = { id: b.id };
  165. cb.unit_price = calcUp ? this.ctx.helper.round(b.unit_price, newDecimal.up) : b.unit_price;
  166. cb.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, cb.unit_price, newDecimal.tp);
  167. cb.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, cb.unit_price, newDecimal.tp);
  168. cb.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, cb.unit_price, newDecimal.tp);
  169. cb.deal_tp = this.ctx.helper.mul(b.deal_qty, cb.unit_price, newDecimal.tp);
  170. cb.total_price = this.ctx.helper.mul(b.quantity, cb.unit_price, newDecimal.tp);
  171. changeBills.push(cb);
  172. }
  173. }
  174. if (changeBills.length > 0) {
  175. const transaction = await this.db.beginTransaction();
  176. try {
  177. await transaction.update(this.tableName,
  178. {decimal: JSON.stringify(newDecimal)}, { where: { tid: tenderId }});
  179. await transaction.updateRows(this.ctx.service.ledger.tableName, changeBills);
  180. await transaction.commit();
  181. } catch(error) {
  182. await transaction.rollback();
  183. throw error;
  184. }
  185. } else {
  186. await this.db.update(this.tableName,
  187. {decimal: JSON.stringify(newDecimal)}, { where: { tid: tenderId }});
  188. }
  189. }
  190. }
  191. return TenderInfo;
  192. };