1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- 'use strict';
- const scheduleConst = require('../const/schedule');
- module.exports = app => {
- class ScheduleAudit extends app.BaseService {
- constructor(ctx) {
- super(ctx);
- this.tableName = 'schedule_audit';
- }
- async addAudit(data) {
- const transaction = await this.db.beginTransaction();
- try {
- const insertData = {
- tid: this.ctx.tender.id,
- audit_id: data.audit_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 editAudit(data) {
- const transaction = await this.db.beginTransaction();
- try {
- if (!this._.includes(this._.values(scheduleConst.permission), data.permission)) {
- throw '修改失败';
- }
- await transaction.update(this.tableName, { id: data.id, permission: 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,
- audit_id: user.uid,
- permission: 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 ScheduleAudit;
- };
|