'use strict'; /** * Created by EllisRan on 2020/3/3. */ const BaseService = require('../base/base_service'); module.exports = app => { class ContractAudit extends BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'contract_audit'; this.dataId = 'id'; } async getList(options) { const list = options.spid ? await this.ctx.service.subProjPermission.getContractAuditList(options.spid) : await this.db.select(this.tableName, { where: options, 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; l.mobile = accountInfo.mobile; } return list; } async saveAudits(options, accountList, transaction = null) { // 判断是否已存在该用户,存在则不插入 if (options.spid) { const spAudits = await this.ctx.service.subProjPermission.getAllDataByCondition({ where: { spid: options.spid, uid: this._.map(accountList, 'id') } }); const insertData = []; for (const a of spAudits) { if (!a.contract_permission) { insertData.push({ id: a.id, contract_permission: '5', }); } } if (insertData.length > 0) { return transaction ? await transaction.updateRows(this.ctx.service.subProjPermission.tableName, insertData) : await this.db.updateRows(this.ctx.service.subProjPermission.tableName, insertData); } return false; } const pauditList = await this.getAllDataByCondition({ where: options }); const pushData = []; for (const a of this._.uniqBy(accountList, 'id')) { if (this._.findIndex(pauditList, { uid: a.id }) === -1) { const data = { spid: options.spid || null, tid: options.tid || null, uid: a.id, create_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(options, ids) { if (options.spid) { const updateData = []; for (const id of ids) { updateData.push({ id, contract_permission: '', }); } return await this.db.updateRows(this.ctx.service.subProjPermission.tableName, updateData); } return await this.db.delete(this.tableName, { id: ids }); } async updatePermission(options, updateData) { if (!updateData.uid) { return false; } if (options.spid) { const spAudit = await this.ctx.service.subProjPermission.getDataByCondition({ spid: options.spid, uid: updateData.uid }); if (!spAudit) { return false; } const newContractPermission = await this.ctx.service.subProjPermission.getNewContractPermission(spAudit.contract_permission, updateData); return await this.db.update(this.ctx.service.subProjPermission.tableName, { id: spAudit.id, contract_permission: newContractPermission }); } const contractAudit = await this.getDataByCondition({ tid: options.tid, uid: updateData.uid }); if (!contractAudit) { return false; } updateData.id = contractAudit.id; return await this.db.update(this.tableName, updateData); } async getUserPermissionEdit(options, uid) { const cloneOptions = this._.cloneDeep(options); cloneOptions.uid = uid; const info = await this.getDataByCondition(cloneOptions); return info && info.permission_edit; } 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({ spid: null, tid: t.id, uid: user.uid, permission_add: user.permission.add, permission_edit: user.permission.edit, permission_show_unit: user.permission.showUnit, permission_show_node: user.permission.showNode, permission_att: user.permission_att, create_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 ContractAudit; };