payment_tender_info.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. module.exports = app => {
  13. class PaymentTenderInfo extends app.BaseService {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局变量
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. this.tableName = 'payment_tender_info';
  23. }
  24. get DefaultInfo () {
  25. return infoConst.paymentDefaultInfo;
  26. }
  27. /**
  28. * 新增 标段相关信息
  29. *
  30. * @param tenderId - 标段Id
  31. * @param projectId - 项目Id
  32. * @param transaction - 事务
  33. * @return {Promise<void>}
  34. */
  35. async addTenderInfo(tenderId, projectId, transaction) {
  36. const defaultInfo = this.DefaultInfo;
  37. const info = JSON.parse(JSON.stringify(defaultInfo));
  38. info.id = tenderId;
  39. info.pid = projectId;
  40. for (const pi of parseInfo) {
  41. if (info[pi] === undefined) continue;
  42. info[pi] = JSON.stringify(info[pi]);
  43. }
  44. for (const pi of arrayInfo) {
  45. if (info[pi] === undefined) continue;
  46. info[pi] = JSON.stringify(info[pi]);
  47. }
  48. if (transaction) {
  49. await transaction.insert(this.tableName, info);
  50. } else {
  51. await this.db.insert(this.tableName, info);
  52. }
  53. return info;
  54. }
  55. /**
  56. * 获取标段相关信息
  57. * @param tenderId
  58. * @return {Promise<void>}
  59. */
  60. async getTenderInfo(tenderId, projectId) {
  61. const defaultInfo = this.DefaultInfo;
  62. let info = await this.getDataByCondition({ id: tenderId });
  63. // 兼容不存在info的情况
  64. if (!info) info = await this.addTenderInfo(tenderId, projectId || this.ctx.session.sessionProject.id);
  65. for (const pi of parseInfo) {
  66. if (info[pi] === undefined) continue;
  67. info[pi] = !info[pi] || info[pi] === '' ? defaultInfo[pi] : JSON.parse(info[pi]);
  68. this.ctx.helper._.defaultsDeep(info[pi], defaultInfo[pi]);
  69. }
  70. for (const ai of arrayInfo) {
  71. if (info[ai] === undefined) continue;
  72. info[ai] = !info[ai] || info[ai] === '' ? defaultInfo[ai] : JSON.parse(info[ai]);
  73. }
  74. return info;
  75. }
  76. /**
  77. * 保存标段相关信息
  78. *
  79. * @param data
  80. * @return {Promise<void>}
  81. */
  82. async saveTenderInfo(tenderId, data) {
  83. for (const di in data) {
  84. if (parseInfo.indexOf(di) >= 0 || arrayInfo.indexOf(di) >= 0) {
  85. data[di] = JSON.stringify(data[di]);
  86. }
  87. }
  88. await this.db.update(this.tableName, data, { where: { id: tenderId } });
  89. }
  90. /**
  91. * 拷贝标段数据至当前标段
  92. * @param {number} id - 当前标段id
  93. * @param {number} copy_id - 被拷贝的标段id
  94. */
  95. async copyTenderHandler(id, copy_id) {
  96. const [data] = await this.ctx.service.tenderInfo.getDataByCondition({
  97. where: { id: copy_id },
  98. columns: ['deal_info', 'construction_unit', 'tech_param'],
  99. });
  100. await this.defaultUpdate(data, { id });
  101. }
  102. }
  103. return PaymentTenderInfo;
  104. };