123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- '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.getAllCertByPid(this.ctx.session.sessionProject.id);
- 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.getAllCertByPid(this.ctx.session.sessionProject.id);
- return await this.getListByTid(tid, allCertList);
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async getAllCert(tenderId) {
- const sql = 'SELECT tc.id, tc.uid, tc.uid, tc.cert_id, tc.department, tc.job_time, tc.remark,' +
- ' ac.type, ac.name as ac_name, ac.code, ac.reg_unit, ac.job_title, ac.file_name, ac.file_path, ac.edu_json,' +
- ' pa.account, pa.name, pa.role, pa.company' +
- ` FROM ${this.tableName} tc LEFT JOIN ${this.ctx.service.accountCert.tableName} ac ON tc.cert_id = ac.id` +
- ` LEFT JOIN ${this.ctx.service.projectAccount.tableName} pa ON tc.uid = pa.id` +
- ' WHERE tc.tid = ?';
- const result = await this.db.query(sql, [tenderId]);
- result.forEach(x => {
- x.edu_json = x.edu_json ? JSON.parse(x.edu_json) : [];
- const eduInfo = x.edu_json.length > 0 ? x.edu_json[x.edu_json.length - 1] : null;
- for (const prop in eduInfo) {
- x['edu_info_' + prop] = eduInfo[prop];
- }
- [x.ac_type_str, x.ac_name_str] = profileConst.cert.getCertName(x.type, x.ac_name);
- });
- return result;
- }
- }
- return TenderCert;
- };
|