tender_info.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. * @return {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. * @return {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. * @return {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. * @return {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,
  147. sjcl_qty = 0,
  148. qtcl_qty = 0,
  149. quantity = 0;
  150. for (const p of bPos) {
  151. this.ctx.helper.checkFieldPrecision(p, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty'], precision.value);
  152. p.quantity = this.ctx.helper.add(this.ctx.helper.add(p.sgfh_qty, p.sjcl_qty), p.qtcl_qty);
  153. sgfh_qty = this.ctx.helper.add(sgfh_qty, p.sgfh_qty);
  154. sjcl_qty = this.ctx.helper.add(sjcl_qty, p.sjcl_qty);
  155. qtcl_qty = this.ctx.helper.add(qtcl_qty, p.qtcl_qty);
  156. quantity = this.ctx.helper.add(quantity, p.quantity);
  157. }
  158. b.sgfh_qty = sgfh_qty;
  159. b.sjcl_qty = sjcl_qty;
  160. b.qtcl_qty = qtcl_qty;
  161. b.quantity = quantity;
  162. } else {
  163. this.ctx.helper.checkFieldPrecision(b, ['sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty'], precision.value);
  164. }
  165. b.quantity = this.ctx.helper.add(this.ctx.helper.add(b.sgfh_qty, b.sjcl_qty), b.qtcl_qty);
  166. b.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, b.unit_price, decimal.tp);
  167. b.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, b.unit_price, decimal.tp);
  168. b.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, b.unit_price, decimal.tp);
  169. b.deal_tp = this.ctx.helper.mul(b.deal_qty, b.unit_price, decimal.tp);
  170. b.total_price = this.ctx.helper.mul(b.quantity, b.unit_price, decimal.tp);
  171. }
  172. const transaction = await this.db.beginTransaction();
  173. try {
  174. await transaction.update(this.tableName,
  175. { precision: JSON.stringify(newPrecision) }, { where: { tid: tenderId } });
  176. if (bills.length > 0) await transaction.updateRows(this.ctx.service.ledger.tableName, bills);
  177. if (pos.length > 0) await transaction.updateRows(this.ctx.service.pos.tableName, pos);
  178. await transaction.commit();
  179. } catch (err) {
  180. await transaction.rollback();
  181. throw err;
  182. }
  183. } else {
  184. await this.db.update(this.tableName,
  185. { precision: JSON.stringify(newPrecision) }, { where: { tid: tenderId } });
  186. }
  187. }
  188. async saveDecimal(tenderId, newDecimal, oldDecimal) {
  189. const changeBills = [],
  190. calcUp = newDecimal.up < oldDecimal.up,
  191. calcTp = newDecimal.tp !== oldDecimal.tp;
  192. if (calcUp || calcTp) {
  193. const bills = await this.ctx.service.ledger.getAllDataByCondition({
  194. columns: ['id', 'unit_price', 'sgfh_qty', 'sjcl_qty', 'qtcl_qty', 'deal_qty', 'quantity'],
  195. where: { tender_id: tenderId, is_leaf: true },
  196. });
  197. for (const b of bills) {
  198. const cb = { id: b.id };
  199. cb.unit_price = calcUp ? this.ctx.helper.round(b.unit_price, newDecimal.up) : b.unit_price;
  200. cb.sgfh_tp = this.ctx.helper.mul(b.sgfh_qty, cb.unit_price, newDecimal.tp);
  201. cb.sjcl_tp = this.ctx.helper.mul(b.sjcl_qty, cb.unit_price, newDecimal.tp);
  202. cb.qtcl_tp = this.ctx.helper.mul(b.qtcl_qty, cb.unit_price, newDecimal.tp);
  203. cb.deal_tp = this.ctx.helper.mul(b.deal_qty, cb.unit_price, newDecimal.tp);
  204. cb.total_price = this.ctx.helper.mul(b.quantity, cb.unit_price, newDecimal.tp);
  205. changeBills.push(cb);
  206. }
  207. }
  208. if (changeBills.length > 0) {
  209. const transaction = await this.db.beginTransaction();
  210. try {
  211. await transaction.update(this.tableName,
  212. { decimal: JSON.stringify(newDecimal) }, { where: { tid: tenderId } });
  213. await transaction.updateRows(this.ctx.service.ledger.tableName, changeBills);
  214. await transaction.commit();
  215. } catch (error) {
  216. await transaction.rollback();
  217. throw error;
  218. }
  219. } else {
  220. await this.db.update(this.tableName,
  221. { decimal: JSON.stringify(newDecimal) }, { where: { tid: tenderId } });
  222. }
  223. }
  224. }
  225. return TenderInfo;
  226. };