123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- 'use strict';
- /**
- * Created by EllisRan on 2020/3/3.
- */
- const BaseService = require('../base/base_service');
- module.exports = app => {
- class ConstructionAudit extends BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'construction_audit';
- this.dataId = 'id';
- }
- async getList(tid) {
- const list = await this.db.select(this.tableName, { where: { tid }, orders: [['id', 'desc']] });
- for (const l of list) {
- const accountInfo = await this.ctx.service.projectAccount.getDataById(l.uid);
- l.name = accountInfo.name;
- l.role = accountInfo.role;
- l.company = accountInfo.company;
- }
- return list;
- }
- async saveAudits(tid, accountList, transaction = null) {
- // 判断是否已存在该用户,存在则不插入
- const pauditList = await this.getAllDataByCondition({ where: { tid } });
- const pushData = [];
- for (const a of this._.uniqBy(accountList, 'id')) {
- if (this._.findIndex(pauditList, { uid: a.id }) === -1) {
- const data = {
- tid,
- uid: a.id,
- in_time: new Date(),
- };
- pushData.push(data);
- }
- }
- if (pushData.length > 0) {
- return transaction ? await transaction.insert(this.tableName, pushData) : await this.db.insert(this.tableName, pushData);
- }
- return false;
- }
- async delAudit(id) {
- return await this.db.delete(this.tableName, { id });
- }
- async updateReport(updateData) {
- if (!updateData.id || updateData.is_report === undefined) {
- return false;
- }
- return await this.db.update(this.tableName, updateData);
- }
- async checkPermission(tender, uid) {
- let flag = false;
- const accountInfo = await this.ctx.service.projectAccount.getDataById(uid);
- const permission = accountInfo !== undefined && accountInfo.permission !== ''
- ? JSON.parse(accountInfo.permission) : null;
- if (accountInfo.project_id === tender.project_id && (accountInfo.is_admin || (permission !== null && permission.construction !== undefined && permission.construction.indexOf('1') !== -1))) {
- flag = true;
- } else {
- const info = await this.getDataByCondition({ tid: tender.id, uid });
- if (info) {
- flag = true;
- }
- }
- return flag;
- }
- async getUserList(tid, is_report = null) {
- const reportSql = is_report !== null ? ' AND ca.`is_report` = ' + is_report : '';
- const sql = 'SELECT ca.*, pa.name as user_name FROM ?? AS ca LEFT JOIN ?? AS pa ON ca.`uid` = pa.`id` ' +
- 'WHERE ca.`tid` = ?' + reportSql + ' ORDER BY ca.`id` DESC';
- const params = [this.tableName, this.ctx.service.projectAccount.tableName, tid];
- return await this.db.query(sql, params);
- }
- 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, uid: user.uid });
- if (delId) deleteIdData.push(delId.id);
- if (user.uid !== t.user_id) {
- insertData.push({
- tid: t.id,
- uid: user.uid,
- is_report: user.is_report,
- 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 ConstructionAudit;
- };
|