tender_cert.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const profileConst = require('../const/profile');
  10. module.exports = app => {
  11. class TenderCert extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'tender_cert';
  21. }
  22. async getListByTid(tid, allCertList) {
  23. const list = await this.getAllDataByCondition({ where: { tid } });
  24. if (list.length > 0) {
  25. const accountList = await this.ctx.service.projectAccount.getAllDataByCondition({ columns: ['id', 'account', 'name', 'role'], where: { id: this._.uniq(this._.map(list, 'uid')) } });
  26. for (const l of list) {
  27. const acInfo = this._.find(allCertList, { id: l.cert_id });
  28. if (acInfo) {
  29. acInfo.edu_json = acInfo.edu_json ? JSON.parse(acInfo.edu_json) : null;
  30. acInfo.eduInfo = acInfo.edu_json && acInfo.edu_json.length > 0 ? acInfo.edu_json[acInfo.edu_json.length - 1] : null;
  31. l.cert_info = acInfo;
  32. }
  33. const accountInfo = this._.find(accountList, { id: l.uid });
  34. if (accountInfo) {
  35. const certs = this._.filter(allCertList, { uid: l.uid });
  36. l.account_info = accountInfo;
  37. l.account_info.certs = certs;
  38. }
  39. }
  40. }
  41. return list;
  42. }
  43. async saveUserCert(tid, list) {
  44. const transaction = await this.db.beginTransaction();
  45. try {
  46. if (list.insertList.length > 0) {
  47. const insertList = [];
  48. for (const i of list.insertList) {
  49. insertList.push({
  50. uid: i.uid,
  51. cert_id: i.cert_id,
  52. tid,
  53. create_time: new Date(),
  54. });
  55. }
  56. await transaction.insert(this.tableName, insertList);
  57. }
  58. if (list.removeList.length > 0) {
  59. await transaction.delete(this.tableName, { id: list.removeList });
  60. }
  61. if (list.updateList.length > 0) {
  62. await transaction.updateRows(this.tableName, list.updateList);
  63. }
  64. await transaction.commit();
  65. const allCertList = await this.ctx.service.accountCert.getAllDataByCondition({ where: { pid: this.ctx.session.sessionProject.id }, orders: [['create_time', 'desc']] });
  66. return await this.getListByTid(tid, allCertList);
  67. } catch (err) {
  68. await transaction.rollback();
  69. throw err;
  70. }
  71. }
  72. async updateOneCert(data) {
  73. const transaction = await this.db.beginTransaction();
  74. try {
  75. const info = await transaction.get(this.tableName, { id: data.id });
  76. if (!info) throw '数据已不存在';
  77. await transaction.update(this.tableName, data);
  78. await transaction.commit();
  79. } catch (err) {
  80. await transaction.rollback();
  81. throw err;
  82. }
  83. }
  84. async updateMoreCert(tid, data) {
  85. const transaction = await this.db.beginTransaction();
  86. try {
  87. const updateData = [];
  88. for (const d of data) {
  89. const info = await transaction.get(this.tableName, { id: d.id });
  90. if (!info) throw '数据已不存在';
  91. updateData.push(d);
  92. }
  93. if (updateData.length > 0) await transaction.updateRows(this.tableName, updateData);
  94. await transaction.commit();
  95. const allCertList = await this.ctx.service.accountCert.getAllDataByCondition({ where: { pid: this.ctx.session.sessionProject.id }, orders: [['create_time', 'desc']] });
  96. return await this.getListByTid(tid, allCertList);
  97. } catch (err) {
  98. await transaction.rollback();
  99. throw err;
  100. }
  101. }
  102. }
  103. return TenderCert;
  104. };