tender_info.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. * @param tenderId
  76. * @returns {Promise<void>}
  77. */
  78. async getTenderInfoEx(tenderId) {
  79. const sql = 'select t2.name, t1.* from zh_tender_info t1 inner join zh_tender t2 on t2.id = t1.tid where t1.tid = ?';
  80. // console.log('getTenderInfoEx: ' + tenderId);
  81. const sqlParam = [tenderId];
  82. const list = await this.db.query(sql, sqlParam);
  83. const info = list[0];
  84. const len = info.deal_info.length;
  85. info.deal_info = info.deal_info.slice(0, len - 1) + ',"name":"' + info.name + '"' + info.deal_info.slice(len - 1);
  86. // console.log(info);
  87. for (const pi of parseInfo) {
  88. info[pi] = !info[pi] || info[pi] === '' ? defaultInfo[pi] : JSON.parse(info[pi]);
  89. this.ctx.helper._.defaults(info[pi], defaultInfo[pi]);
  90. }
  91. for (const ai of arrayInfo) {
  92. info[ai] = !info[ai] || info[ai] === '' ? defaultInfo[ai] : JSON.parse(info[ai]);
  93. }
  94. if (info.decimal) {
  95. info.decimal._pay_tp = info.decimal.pay ? info.decimal.payTp : info.decimal.tp;
  96. }
  97. return info;
  98. }
  99. /**
  100. * 保存标段相关信息
  101. *
  102. * @param data
  103. * @returns {Promise<void>}
  104. */
  105. async saveTenderInfo(tenderId, data) {
  106. for (const di in data) {
  107. if (parseInfo.indexOf(di) >= 0 || arrayInfo.indexOf(di) >= 0) {
  108. data[di] = JSON.stringify(data[di]);
  109. }
  110. }
  111. await this.db.update(this.tableName, data, {where: {tid: tenderId}});
  112. }
  113. async savePrecision(tenderId, newPrecision, oldPrecision, decimal) {
  114. const changePrecision = [];
  115. const units = await this.ctx.service.ledger.getTenderUsedUnits(tenderId);
  116. const defUnits = this._.map(newPrecision, 'units');
  117. const otherUnits = units.filter(function (u) {return defUnits.indexOf(u) === -1});
  118. let changeUnits = [];
  119. for (const prop in newPrecision) {
  120. if (oldPrecision[prop]) {
  121. if (newPrecision[prop].value < oldPrecision[prop].value) {
  122. changePrecision.push(newPrecision[prop]);
  123. }
  124. } else {
  125. changePrecision.push(newPrecision[prop]);
  126. }
  127. }
  128. for (const cp of changePrecision) {
  129. if (cp.unit) {
  130. changeUnits.push(cp.unit);
  131. } else {
  132. changeUnits.push(otherUnits);
  133. }
  134. }
  135. changeUnits = this._.flatten(changeUnits);
  136. if (changeUnits.length > 0) {
  137. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  138. columns: ['id', 'unit', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'],
  139. where: {tender_id: tenderId, unit: changeUnits, is_leaf: true}
  140. });
  141. const pos = changeUnits.length > 0 ? await this.ctx.service.pos.getPosDataByUnits(tenderId, changeUnits) : [];
  142. for (const b of bills) {
  143. const precision = this.ctx.helper.findPrecision(newPrecision, b.unit);
  144. const bPos = this._.filter(pos, {lid: b.id});
  145. if (bPos.length > 0) {
  146. let sgfh_qty = 0, sjcl_qty = 0, qtcl_qty = 0, quantity = 0;
  147. for (const p of bPos) {
  148. this.ctx.helper.checkFieldPrecision(p, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty'], precision.value);
  149. p.quantity = this.ctx.helper.add(this.ctx.helper.add(p.sgfh_qty, p.sjcl_qty), p.qtcl_qty);
  150. sgfh_qty = this.ctx.helper.add(sgfh_qty, p.sgfh_qty);
  151. sjcl_qty = this.ctx.helper.add(sjcl_qty, p.sjcl_qty);
  152. qtcl_qty = this.ctx.helper.add(qtcl_qty, p.qtcl_qty);
  153. quantity = this.ctx.helper.add(quantity, p.quantity);
  154. }
  155. b.sgfh_qty = sgfh_qty;
  156. b.sjcl_qty = sjcl_qty;
  157. b.qtcl_qty = qtcl_qty;
  158. b.quantity = quantity;
  159. } else {
  160. this.ctx.helper.checkFieldPrecision(b, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'], precision.value);
  161. }
  162. b.quantity = this.ctx.helper.add(this.ctx.helper.add(b.sgfh_qty, b.sjcl_qty), b.qtcl_qty);
  163. b.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, b.unit_price, decimal.tp);
  164. b.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, b.unit_price, decimal.tp);
  165. b.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, b.unit_price, decimal.tp);
  166. b.deal_tp = this.ctx.helper.mul(b.deal_qty, b.unit_price, decimal.tp);
  167. b.total_price = this.ctx.helper.mul(b.quantity, b.unit_price, decimal.tp);
  168. }
  169. const transaction = await this.db.beginTransaction();
  170. try {
  171. await transaction.update(this.tableName,
  172. {precision: JSON.stringify(newPrecision)}, {where: {tid: tenderId}});
  173. if (bills.length > 0) await transaction.updateRows(this.ctx.service.ledger.tableName, bills);
  174. if (pos.length > 0) await transaction.updateRows(this.ctx.service.pos.tableName, pos);
  175. await transaction.commit();
  176. } catch (err) {
  177. await transaction.rollback();
  178. throw err;
  179. }
  180. } else {
  181. await this.db.update(this.tableName,
  182. {precision: JSON.stringify(newPrecision)}, {where: {tid: tenderId}});
  183. }
  184. }
  185. async saveDecimal(tenderId, newDecimal, oldDecimal) {
  186. const changeBills = [], calcUp = newDecimal.up < oldDecimal.up, calcTp = newDecimal.tp !== oldDecimal.tp;
  187. if (calcUp || calcTp) {
  188. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  189. columns: ['id', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty', 'quantity'],
  190. where: {tender_id: tenderId, is_leaf: true}
  191. });
  192. for (const b of bills) {
  193. const cb = { id: b.id };
  194. cb.unit_price = calcUp ? this.ctx.helper.round(b.unit_price, newDecimal.up) : b.unit_price;
  195. cb.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, cb.unit_price, newDecimal.tp);
  196. cb.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, cb.unit_price, newDecimal.tp);
  197. cb.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, cb.unit_price, newDecimal.tp);
  198. cb.deal_tp = this.ctx.helper.mul(b.deal_qty, cb.unit_price, newDecimal.tp);
  199. cb.total_price = this.ctx.helper.mul(b.quantity, cb.unit_price, newDecimal.tp);
  200. changeBills.push(cb);
  201. }
  202. }
  203. if (changeBills.length > 0) {
  204. const transaction = await this.db.beginTransaction();
  205. try {
  206. await transaction.update(this.tableName,
  207. {decimal: JSON.stringify(newDecimal)}, { where: { tid: tenderId }});
  208. await transaction.updateRows(this.ctx.service.ledger.tableName, changeBills);
  209. await transaction.commit();
  210. } catch(error) {
  211. await transaction.rollback();
  212. throw error;
  213. }
  214. } else {
  215. await this.db.update(this.tableName,
  216. {decimal: JSON.stringify(newDecimal)}, { where: { tid: tenderId }});
  217. }
  218. }
  219. }
  220. return TenderInfo;
  221. };