123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- const profileConst = require('../const/profile');
- module.exports = app => {
- class TenderCert extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'tender_cert';
- }
- async getListByTid(tid, allCertList) {
- const list = await this.getAllDataByCondition({ where: { tid } });
- if (list.length > 0) {
- const accountList = await this.ctx.service.projectAccount.getAllDataByCondition({ columns: ['id', 'account', 'name', 'role'], where: { id: this._.uniq(this._.map(list, 'uid')) } });
- for (const l of list) {
- const acInfo = this._.find(allCertList, { id: l.cert_id });
- if (acInfo) {
- acInfo.edu_json = acInfo.edu_json ? JSON.parse(acInfo.edu_json) : null;
- acInfo.eduInfo = acInfo.edu_json && acInfo.edu_json.length > 0 ? acInfo.edu_json[acInfo.edu_json.length - 1] : null;
- l.cert_info = acInfo;
- }
- const accountInfo = this._.find(accountList, { id: l.uid });
- if (accountInfo) {
- const certs = this._.filter(allCertList, { uid: l.uid });
- l.account_info = accountInfo;
- l.account_info.certs = certs;
- }
- }
- }
- return list;
- }
- async saveUserCert(tid, list) {
- const transaction = await this.db.beginTransaction();
- try {
- if (list.insertList.length > 0) {
- const insertList = [];
- for (const i of list.insertList) {
- insertList.push({
- uid: i.uid,
- cert_id: i.cert_id,
- tid,
- create_time: new Date(),
- });
- }
- await transaction.insert(this.tableName, insertList);
- }
- if (list.removeList.length > 0) {
- await transaction.delete(this.tableName, { id: list.removeList });
- }
- if (list.updateList.length > 0) {
- await transaction.updateRows(this.tableName, list.updateList);
- }
- await transaction.commit();
- const allCertList = await this.ctx.service.accountCert.getAllDataByCondition({ where: { pid: this.ctx.session.sessionProject.id }, orders: [['create_time', 'desc']] });
- return await this.getListByTid(tid, allCertList);
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async updateOneCert(data) {
- const transaction = await this.db.beginTransaction();
- try {
- const info = await transaction.get(this.tableName, { id: data.id });
- if (!info) throw '数据已不存在';
- await transaction.update(this.tableName, data);
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async updateMoreCert(tid, data) {
- const transaction = await this.db.beginTransaction();
- try {
- const updateData = [];
- for (const d of data) {
- const info = await transaction.get(this.tableName, { id: d.id });
- if (!info) throw '数据已不存在';
- updateData.push(d);
- }
- if (updateData.length > 0) await transaction.updateRows(this.tableName, updateData);
- await transaction.commit();
- const allCertList = await this.ctx.service.accountCert.getAllDataByCondition({ where: { pid: this.ctx.session.sessionProject.id }, orders: [['create_time', 'desc']] });
- return await this.getListByTid(tid, allCertList);
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- }
- return TenderCert;
- };
|