'use strict'; /** * 决策大屏用户查看权限-数据模型 * * @author ellisran * @date 2021/09/23 * @version */ const accountGroup = require('../const/account_group').group; const paymentConst = require('../const/payment'); module.exports = app => { class paymentFolder extends app.BaseService { constructor(ctx) { super(ctx); this.tableName = 'payment_folder'; } async addFolder(projectId, uid, parentId = 0, name) { const transaction = await this.db.beginTransaction(); try { const tenderCount = await this.ctx.service.paymentTender.count({ folder_id: parentId }); if (tenderCount > 0) { throw '文件夹下存在标段无法创建子文件夹'; } let level = 1; let parent_path = ''; if (parentId !== 0) { const parentFolderInfo = await this.getDataById(parentId); if (!parentFolderInfo) throw '父文件夹不存在'; level = parentFolderInfo.level + level; if (parentFolderInfo.is_leaf) { await transaction.update(this.tableName, { id: parentFolderInfo.id, is_leaf: 0 }); } parent_path = parentFolderInfo.parent_path ? parentFolderInfo.parent_path + '-' + parentFolderInfo.id : parentFolderInfo.id; } const childrenCount = await transaction.count(this.tableName, { parent_id: parentId }); const order = childrenCount + 1; const insertData = { pid: projectId, uid, name, parent_id: parentId, parent_path, level, order, is_leaf: 1, in_time: new Date(), }; await transaction.insert(this.tableName, insertData); await transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } } async deleteFolder(id) { // 删除所有底下的目录 const transaction = await this.db.beginTransaction(); try { const info = await this.getDataById(id); if (info.uid !== this.ctx.session.sessionUser.accountId && !this.ctx.session.sessionUser.is_admin) { throw '您没有权限删除此文件夹'; } let ids = [info.id]; if (info.parent_id === 0) { const childList = await this.getAllDataByCondition({ where: { parent_id: info.id } }); if (childList.length > 0) { ids = [...ids, ...this._.map(childList, 'id')]; for (const c of childList) { const deleteData = await this.getDataByParentPath(this.tableName, c.parent_path + '-' + c.id + '%', transaction); if (deleteData.length > 0) { const delids = this._.map(deleteData, 'id'); ids = [...ids, ...delids]; } } } } else if (info.parent_path) { const deleteData = await this.getDataByParentPath(this.tableName, info.parent_path + '-' + info.id + '%', transaction); if (deleteData.length > 0) { const delids = this._.map(deleteData, 'id'); ids = [...ids, ...delids]; } } // 判断是否存在标段,有则无法删除目录 const tenderCount = await this.ctx.service.paymentTender.count({ folder_id: ids }); if (tenderCount > 0) { throw '文件夹下存在标段,无法删除'; } await transaction.delete(this.tableName, { id: ids }); await transaction.commit(); } catch (err) { await transaction.rollback(); throw err; } } async getDataByParentPath(tableName, parent_path, transaction = null) { this.initSqlBuilder(); this.sqlBuilder.setAndWhere('parent_path', { value: this.db.escape(parent_path), operate: 'Like', }); const [sql, sqlParam] = this.sqlBuilder.build(tableName); const resultData = transaction ? await transaction.query(sql, sqlParam) : await this.db.query(sql, sqlParam); return resultData; } async getList(uid, tenderList, auditPermission) { // 获取所有项目参与者 const accountList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { project_id: this.ctx.session.sessionProject.id, enable: 1 }, columns: ['id', 'name'], }); let folderList = []; if (auditPermission.view_all) { folderList = await this.getAllDataByCondition({ where: { pid: this.ctx.session.sessionProject.id } }); } else { folderList = await this.getAllDataByCondition({ where: { uid } }); // 再找出标段对应的目录及自建的目录下的子目录 if (tenderList.length > 0) { for (const t of tenderList) { if (this._.findIndex(folderList, { id: t.folder_id }) === -1) { const folderInfo = await this.getDataById(t.folder_id); folderList.push(folderInfo); } } } if (folderList.length > 0) { const leafFolderList = this._.filter(folderList, { is_leaf: 1 }); const parentFolderIdList = this._.map(this._.filter(folderList, { is_leaf: 0 }), 'id'); const allNotExistFolderIds = []; for (const lf of leafFolderList) { let parentPathArray = lf.parent_path !== '' ? lf.parent_path.split('-') : []; if (parentPathArray.length > 0) { parentPathArray = parentPathArray.map(function(data) { return +data; });// 字符串数组转整型数组 } const notExistFolderIds = this._.difference(parentPathArray, parentFolderIdList); if (notExistFolderIds.length > 0) { for (const id of notExistFolderIds) { if (!this._.includes(allNotExistFolderIds, id)) { allNotExistFolderIds.push(id); } } } } if (allNotExistFolderIds.length > 0) { const newFolderList = await this.getAllDataByCondition({ where: { id: allNotExistFolderIds } }); folderList = [...folderList, ...newFolderList]; } } } if (folderList.length > 0) { // folderList = this._.uniqBy(folderList, 'id'); for (const f of folderList) { const userInfo = this._.find(accountList, { id: f.uid }); f.user_name = userInfo ? userInfo.name : ''; } } return folderList; } // async getChildrenByParentId(parentId, transaction = null) { // const list = await this.getAllDataByCondition({ where: { parent_id: parentId } }); // } } return paymentFolder; };