123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- 'use strict';
- /**
- *
- *
- * @author Ellisran
- * @date 2021/4/8
- * @version
- */
- const touristPermissionConst = require('../const/tourist_permission').defaultPermission;
- module.exports = app => {
- class TenderTourist extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'tender_tourist';
- }
- async getTourists(tid) {
- const sql = 'SELECT tt.*,' +
- 'pa.`name` As `user_name`, pa.`role` As `user_role`, pa.`company` As `user_company` ' +
- 'FROM ?? As tt LEFT JOIN ?? As pa ON tt.`user_id` = pa.`id` ' +
- 'WHERE tt.`tid` = ?';
- const sqlParam = [this.tableName, this.ctx.service.projectAccount.tableName, tid];
- return await this.db.query(sql, sqlParam);
- }
- async addAudit(data) {
- const transaction = await this.db.beginTransaction();
- try {
- const insertData = {
- tid: this.ctx.tender.id,
- user_id: data.user_id,
- in_time: new Date(),
- };
- const result = await transaction.insert(this.tableName, insertData);
- await transaction.commit();
- return await this.getDataById(result.insertId);
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async removeAudit(data) {
- const transaction = await this.db.beginTransaction();
- try {
- await transaction.delete(this.tableName, { id: data.id });
- await transaction.commit();
- return true;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async getTouristPermission(info) {
- if (info && info.permission) {
- return JSON.parse(info.permission);
- }
- return touristPermissionConst;
- }
- async setPermission(data) {
- const transaction = await this.db.beginTransaction();
- try {
- await transaction.update(this.tableName, { id: data.id, permission: JSON.stringify(data.permission) });
- await transaction.commit();
- return true;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async setOtherTender(tidList, userList) {
- // 根据标段找出创建人去除,已存在的先删除再插入
- const transaction = await this.db.beginTransaction();
- try {
- const tenderList = await this.ctx.service.tender.getAllDataByCondition({
- columns: ['id', 'user_id'],
- where: { id: tidList.split(',') },
- });
- const oldTouristList = await this.getAllDataByCondition({ where: { tid: tidList.split(',') } });
- const insertData = [];
- const deleteIdData = [];
- for (const user of userList) {
- for (const t of tenderList) {
- const delId = this._.find(oldTouristList, { tid: t.id, user_id: user.uid });
- if (delId) deleteIdData.push(delId.id);
- if (user.uid !== t.user_id) {
- insertData.push({
- tid: t.id,
- user_id: user.uid,
- permission: JSON.stringify(user.permission),
- in_time: new Date(),
- });
- }
- }
- }
- if (deleteIdData.length > 0) await transaction.delete(this.tableName, { id: deleteIdData });
- if (insertData.length > 0) await transaction.insert(this.tableName, insertData);
- await transaction.commit();
- return true;
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- }
- return TenderTourist;
- };
|