tender_info.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 defaultInfo = infoConst.defaultInfo;
  12. module.exports = app => {
  13. class TenderInfo extends app.BaseService {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局变量
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. this.tableName = 'tender_info';
  23. }
  24. /**
  25. * 新增 标段相关信息
  26. *
  27. * @param tenderId - 标段Id
  28. * @param projectId - 项目Id
  29. * @param transaction - 事务
  30. * @returns {Promise<void>}
  31. */
  32. async addTenderInfo(tenderId, projectId, transaction) {
  33. const info = JSON.parse(JSON.stringify(defaultInfo));
  34. info.tid = tenderId;
  35. info.pid = projectId;
  36. for (const pi of parseInfo) {
  37. info[pi] = JSON.stringify(info[pi])
  38. }
  39. if (transaction) {
  40. await transaction.insert(this.tableName, info);
  41. } else {
  42. await this.db.insert(this.tableName, info);
  43. }
  44. return info;
  45. }
  46. /**
  47. * 获取标段相关信息
  48. * @param tenderId
  49. * @returns {Promise<void>}
  50. */
  51. async getTenderInfo(tenderId) {
  52. let info = await this.getDataByCondition({tid: tenderId});
  53. // 兼容不存在info的情况
  54. if (!info) {
  55. info = await this.addTenderInfo(tenderId, this.ctx.session.sessionProject.id);
  56. }
  57. for (const pi of parseInfo) {
  58. info[pi] = !info[pi] || info[pi] === '' ? defaultInfo[pi] : JSON.parse(info[pi]);
  59. }
  60. return info;
  61. }
  62. /**
  63. * 保存标段相关信息
  64. *
  65. * @param data
  66. * @returns {Promise<void>}
  67. */
  68. async saveTenderInfo(tenderId, data) {
  69. for (const di in data) {
  70. if (parseInfo.indexOf(di) >= 0) {
  71. data[di] = JSON.stringify(data[di]);
  72. }
  73. }
  74. await this.db.update(this.tableName, data, {where: {tid: tenderId}});
  75. }
  76. }
  77. return TenderInfo;
  78. };