'use strict'; /** * * 2024/3/21 * @author Mai * @date * @version */ const rootId = '-1'; const filingType = [ { value: 1, name: '立项文件' }, { value: 2, name: '招标投标、合同协议文件' }, { value: 3, name: '勘察、设计文件' }, { value: 4, name: '征地、拆迁、移民文件' }, { value: 5, name: '项目管理文件' }, { value: 6, name: '施工文件' }, { value: 7, name: '信息系统开发文件' }, { value: 8, name: '设备文件' }, { value: 9, name: '监理文件' }, { value: 10, name: '科研项目文件' }, { value: 11, name: '生产技术准备、试运行文件' }, { value: 12, name: '竣工验收文件' }, ]; const maxFilingType = 12; module.exports = app => { class FilingTemplate extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'filing_template'; } async initTemplate(transaction, template) { const insertData = []; for (const [i, f] of filingType.entries()) { insertData.push({ id: this.uuid.v4(), temp_id: template.id, tree_pid: -1, tree_level: 1, tree_order: i + 1, add_user_id: this.ctx.session.sessionUser.accountId, is_fixed: true, name: f.name, filing_type: f.value, }); } if (transaction) { await transaction.insert(this.tableName, insertData); } else { await this.db.insert(this.tableName, insertData); } } async getData(templateId) { return await this.getAllDataByCondition({ where: { temp_id: templateId } }) } async getPosterityData(templateId, id){ const result = []; let cur = await this.getAllDataByCondition({ where: { temp_id: templateId, tree_pid: id } }); let iLevel = 1; while (cur.length > 0 && iLevel < 6) { result.push(...cur); cur = await this.getAllDataByCondition({ where: { temp_id: templateId, tree_pid: cur.map(x => { return x.id })} }); iLevel += 1; } return result; } async getNewName(templateId, name = '新增文件类别') { const data = await this.db.query(`SELECT * FROM ${this.tableName} WHERE temp_id = '${templateId}' AND name LIKE '${name}%'`); if (data.length === 0) return name; const _ = this._; const names = data.map(x => { return _.toInteger(x.name.replace(name, '')) }); const filterNames = names.filter(x => { return x > 0 }); const max = filterNames.reduce((pre, cur) => { return Math.max(pre, cur); }, 0); return max >= 0 ? name + (max + 1) : name; } async getNewFilingType(templateId) { const max = await this.db.queryOne(`SELECT filing_type FROM ${this.tableName} WHERE temp_id = '${templateId}' ORDER BY filing_type DESC`); return max && max.filing_type ? max.filing_type + 1 : maxFilingType + 1; } async add(templateId, data) { const parent = await this.getDataById(data.tree_pid); const sibling = await this.getAllDataByCondition({ where: { temp_id: templateId, tree_pid: parent ? parent.id : rootId }, orders: [['tree_order', 'asc']]}); const preChild = data.tree_pre_id ? sibling.find(x => { return x.id === data.tree_pre_id; }) : null; const filing_type = parent ? parent.filing_type : await this.getNewFilingType(templateId); const conn = await this.db.beginTransaction(); try { // 获取当前用户信息 const sessionUser = this.ctx.session.sessionUser; // 获取当前项目信息 const sessionProject = this.ctx.session.sessionProject; const tree_order = preChild ? preChild.tree_order + 1 : (sibling.length > 0 ? sibling[sibling.length - 1].tree_order + 1 : 1); const name = await this.getNewName(templateId); const insertData = { id: this.uuid.v4(), temp_id: templateId, add_user_id: sessionUser.accountId, tree_pid: parent ? parent.id : rootId, tree_level: parent ? parent.tree_level + 1 : 1, tree_order, name, filing_type }; insertData.is_fixed = insertData.tree_level === 1 ? 1 : (preChild ? preChild.is_fixed : 0); const operate = await conn.insert(this.tableName, insertData); if (operate.affectedRows === 0) throw '新增文件夹失败'; const updateData = []; if (preChild) { sibling.forEach(x => { if (x.tree_order >= tree_order) updateData.push({ id: x.id, tree_order: x.tree_order + 1 }); }); } if (updateData.length > 0) await conn.updateRows(this.tableName, updateData); await conn.commit(); return { create: [insertData], update: updateData }; } catch (error) { await conn.rollback(); throw error; } } async save(data) { const filing = await this.getDataById(data.id); if (!filing) throw '分类不存在,请刷新页面后重试'; const result = await this.db.update(this.tableName, { id: data.id, name: data.name }); if (result.affectedRows > 0) { return data; } else { throw '更新数据失败'; } } async del(templateId, data) { const filing = await this.getDataById(data.id); if (!filing) throw '分类不存在,请刷新页面后重试'; const posterity = await this.getPosterityData(templateId, data.id); const delData = posterity.map(x => {return x.id; }); delData.push(data.id); const sibling = await this.getAllDataByCondition({ where: { temp_id: templateId, tree_pid: filing.tree_pid } }); const updateData = []; sibling.forEach(x => { if (x.tree_order > filing.tree_order) updateData.push({ id: x.id, tree_order: x.tree_order - 1}); }); const conn = await this.db.beginTransaction(); try { await conn.delete(this.tableName, { id: delData }); if (updateData.length > 0) conn.updateRows(this.tableName, updateData); await conn.commit(); return { delete: delData, update: updateData }; } catch(err) { await conn.rollback(); throw err; } } async move(templateId, data) { const filing = await this.getDataById(data.id); if (!filing) throw '移动的分类不存在,请刷新页面后重试'; const parent = await this.getDataById(data.tree_pid); const sibling = await this.getAllDataByCondition({ where: { temp_id: templateId, tree_pid: data.tree_pid } }); const filing_type = parent ? parent.filing_type : await this.getNewFilingType(templateId); const posterity = await this.getPosterityData(templateId, filing.id); const result = []; const updateData = { id: filing.id, tree_order: data.tree_order, tree_pid: data.tree_pid, tree_level: parent ? parent.tree_level + 1 : 1, filing_type }; result.push(updateData); const posterityUpdateData = posterity.map(x => { return { id: x.id, filing_type, tree_level: updateData.tree_level - filing.tree_level + x.tree_level }}); result.push(posterityUpdateData); const siblingUpdateData = []; if (data.tree_pid === filing.tree_pid) { if (data.tree_order < filing.tree_order) { sibling.forEach(x => { if (x.id === filing.id) return; if (x.tree_order < data.tree_order) return; if (x.tree_order > filing.tree_order) return; siblingUpdateData.push({id: x.id, tree_order: x.tree_order + 1}); }); } else { sibling.forEach(x => { if (x.id === filing.id) return; if (x.tree_order < filing.tree_order) return; if (x.tree_order > data.tree_order) return; siblingUpdateData.push({id: x.id, tree_order: x.tree_order - 1}); }); } } else { const orgSibling = await this.getAllDataByCondition({ where: { temp_id: templateId, tree_pid: filing.tree_pid } }); orgSibling.forEach(x => { if (x.id === filing.id) return; if (x.tree_order < filing.tree_order) return; siblingUpdateData.push({id: x.id, tree_order: x.tree_order - 1}); }); sibling.forEach(x => { if (x.id === filing.id) return; if (x.tree_order < data.tree_order) return; siblingUpdateData.push({id: x.id, tree_order: x.tree_order + 1}); }) } result.push(...siblingUpdateData); const conn = await this.db.beginTransaction(); try { await conn.update(this.tableName, updateData); if (posterityUpdateData.length > 0) await conn.updateRows(this.tableName, posterityUpdateData); if (siblingUpdateData.length > 0) await conn.updateRows(this.tableName, siblingUpdateData); await conn.commit(); return { update: result }; } catch(err) { await conn.rollback(); throw err; } } async import(templateId, data) { if (!data || data.length === 0) throw '导入数据不存在'; const insertData = []; for (const d of data) { if (d.id === undefined || d.tree_pid === undefined || d.tree_order === undefined || d.tree_level === undefined || d.name === undefined || d.is_fixed === undefined || d.filing_type === undefined) { throw '导入数据格式有误'; } const parent = insertData.find(x => { return x.org_id === d.tree_pid }); insertData.push({ id: this.uuid.v4(), temp_id: templateId, add_user_id: this.ctx.session.sessionUser.accountId, tree_pid: parent ? parent.id : rootId, tree_level: parent ? parent.tree_level + 1 : 1, tree_order: d.tree_order, name: d.name, tips: d.tips, filing_type: d.filing_type, is_fixed: parent ? d.is_fixed : 1, org_id: d.id, }); } insertData.forEach(x => { delete x.org_id; }); const conn = await this.db.beginTransaction(); try { await conn.delete(this.tableName, { temp_id: templateId }); await conn.insert(this.tableName, insertData); await conn.commit(); } catch(err) { await conn.rollback(); throw '导入数据格式有误'; } return await this.getData(templateId) } async multiUpdate(templateId, data) { if (!data || data.length === 0) throw '提交数据格式错误'; const sourceData = await this.getData(templateId); const validFields = ['id', 'is_fixed', 'name', 'filing_type', 'tree_order', 'tips']; const updateData = []; for (const d of data) { if (!d.id) throw '提交数据格式错误'; const sd = sourceData.find(x => { return x.id === d.id; }); if (!sd) throw '提交数据格式错误'; const nd = {}; for (const prop in d) { if (validFields.indexOf(prop) < 0) continue; nd[prop] = d[prop]; } updateData.push(nd); } await this.db.updateRows(this.tableName, updateData); return await this.getData(templateId); } } return FilingTemplate; };