123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- '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({ spid: this.ctx.subProject.id, folder_id: parentId });
- if (tenderCount > 0) {
- throw '文件夹下存在标段无法创建子文件夹';
- }
- let level = 1;
- let parent_path = '';
- if (parentId !== 0) {
- const parentFolderInfo = await this.getDataByCondition({ spid: this.ctx.subProject.id, folder_id: 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.folder_id : parentFolderInfo.folder_id;
- }
- const childrenCount = await transaction.select(this.tableName, { where: { spid: this.ctx.subProject.id, parent_id: parentId }, orders: [['order', 'desc']] });
- const order = childrenCount && childrenCount.length > 0 ? childrenCount[0].order + 1 : 1;
- const maxFolderId = await this.getMaxId(this.ctx.subProject.id);
- const insertData = {
- id: this.uuid.v4(),
- pid: projectId,
- spid: this.ctx.subProject.id,
- uid,
- name,
- folder_id: maxFolderId + 1,
- 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.folder_id];
- if (info.parent_id === 0) {
- const childList = await this.getAllDataByCondition({ where: { spid: this.ctx.subProject.id, parent_id: info.folder_id } });
- if (childList.length > 0) {
- ids = [...ids, ...this._.map(childList, 'folder_id')];
- for (const c of childList) {
- const deleteData = await this.getDataByParentPath(this.tableName, c.parent_path + '-' + c.folder_id + '%', transaction);
- if (deleteData.length > 0) {
- const delids = this._.map(deleteData, 'folder_id');
- ids = [...ids, ...delids];
- }
- }
- }
- } else if (info.parent_path) {
- const deleteData = await this.getDataByParentPath(this.tableName, info.parent_path + '-' + info.folder_id + '%', transaction);
- if (deleteData.length > 0) {
- const delids = this._.map(deleteData, 'folder_id');
- ids = [...ids, ...delids];
- }
- // 父节点可能变成is_leaf为0
- const parentFolderInfo = await this.getDataByCondition({ spid: this.ctx.subProject.id, folder_id: info.parent_id });
- const childrenCount = await this.getAllDataByCondition({ where: { spid: this.ctx.subProject.id, parent_id: parentFolderInfo.folder_id } });
- if (childrenCount.length === 1) {
- await transaction.update(this.tableName, { id: parentFolderInfo.id, is_leaf: 1 });
- }
- }
- // 判断是否存在标段,有则无法删除目录
- const tenderCount = await this.ctx.service.paymentTender.count({ spid: this.ctx.subProject.id, folder_id: ids });
- if (tenderCount > 0) {
- throw '文件夹下存在标段,无法删除';
- }
- await transaction.delete(this.tableName, { spid: this.ctx.subProject.id, folder_id: ids });
- await transaction.commit();
- } catch (err) {
- await transaction.rollback();
- throw err;
- }
- }
- async getDataByParentPath(tableName, parent_path, transaction = null) {
- this.initSqlBuilder();
- this.sqlBuilder.setAndWhere('spid', {
- value: this.db.escape(this.ctx.subProject.id),
- operate: '=',
- });
- 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: { spid: this.ctx.subProject.id } });
- } else {
- folderList = await this.getAllDataByCondition({ where: { spid: this.ctx.subProject.id, uid } });
- // 再找出标段对应的目录及自建的目录下的子目录
- if (tenderList.length > 0) {
- for (const t of tenderList) {
- if (this._.findIndex(folderList, { folder_id: t.folder_id }) === -1) {
- const folderInfo = await this.getDataByCondition({ spid: this.ctx.subProject.id, folder_id: 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: { spid: this.ctx.subProject.id, folder_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 getNoSpList(pid) {
- // 获取所有项目参与者
- const accountList = await this.ctx.service.projectAccount.getAllDataByCondition({
- where: { project_id: this.ctx.session.sessionProject.id, enable: 1 },
- columns: ['id', 'name'],
- });
- const folderList = await this.getAllDataByCondition({ where: { pid, spid: '' } });
- 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 getMaxId(spid, needFlag = false) {
- const result = await this.db.queryOne(`select max(folder_id) as max_id from ${this.tableName} where spid = ?`, [spid]);
- const maxId = result ? result.max_id : 0;
- if (needFlag) {
- const oldResult = await this.db.queryOne(`select max(folder_id) as max_id from ${this.tableName} where pid = ? AND spid = ?`, [this.ctx.session.sessionProject.id, '']);
- const oldMaxId = oldResult ? oldResult.max_id : 0;
- return maxId > oldMaxId ? maxId : oldMaxId;
- }
- return maxId;
- }
- // async getChildrenByParentId(parentId, transaction = null) {
- // const list = await this.getAllDataByCondition({ where: { parent_id: parentId } });
- // }
- }
- return paymentFolder;
- };
|