tender_info.js 8.9 KB

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