'use strict'; /** * * * @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 Filing extends app.BaseService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @param {String} tableName - 表名 * @return {void} */ constructor(ctx) { super(ctx); this.tableName = 'filing'; } get allFilingType () { return filingType.map(x => { return x.value }); } analysisFilingType(filing) { const copy = JSON.parse(JSON.stringify(filing)); const curFilingType = copy.filter(f => { return f.is_fixed; }); const checkChildren = function (parent) { parent.children = curFilingType.filter(x => { return x.tree_pid === parent.id; }); if (parent.children.length > 1) parent.children.sort((x, y) => { return x.tree_order - y.tree_order; }); for (const c of parent.children) { checkChildren(c); } }; const topFiling = curFilingType.filter(x => { return x.tree_level === 1; }); topFiling.sort((x, y) => { return x.tree_order - y.tree_order; }); for (const tp of topFiling) { checkChildren(tp); } const result = []; const getFilingType = function(arr, prefix = '') { for (const a of arr) { if (a.children.length) { getFilingType(a.children, prefix ? prefix + '/' + a.name : a.name); } else { result.push({ value: a.filing_type, name: a.name, parentsName: prefix }); } } }; getFilingType(topFiling); return result; } async getFilingType(spid) { const filing = await ctx.service.filing.getValidFiling(ctx.params.id, ctx.subProject.permission.filing_type); return this.analysisFilingType(filing); } async initFiling(spid, templateId, transaction) { const count = await this.count({ spid }); if (count > 0) return; const templateFiling = await this.ctx.service.filingTemplate.getAllDataByCondition({ where: { temp_id: templateId }, orders: [['tree_level', 'asc']], }); const insertData = []; for (const f of templateFiling) { f.newId = this.uuid.v4(); const parent = f.tree_pid !== rootId ? templateFiling.find(x => { return x.id === f.tree_pid; }) : null; const newData = { id: f.newId, tree_pid : parent ? parent.newId : rootId, tree_level: f.tree_level, tree_order: f.tree_order, spid, add_user_id: this.ctx.session.sessionUser.accountId, is_fixed: f.is_fixed, filing_type: f.filing_type, name: f.name, tips: f.tips, }; insertData.push(newData); } if (transaction) { await transaction.insert(this.tableName, insertData); } else { await this.db.insert(this.tableName, insertData); } } _filterValidFiling(filing, filingType) { const validFiling = filing.filter(x => { return filingType.indexOf(x.filing_type) > -1;}); const checkParent = function(child) { let parent = validFiling.find(x => { return x.id === child.tree_pid; }); if (!parent) { parent = filing.find(x => { return x.id === child.tree_pid; }); validFiling.push(parent); } if (parent.tree_level > 1) checkParent(parent); }; for (const vf of validFiling) { if (vf.tree_level > 1 && vf.is_fixed) checkParent(vf); } return validFiling; } async getValidFiling(spid, filingType) { if (!filingType || filingType.length === 0) return []; const result = await this.getAllDataByCondition({ where: { spid, is_deleted: 0 } }); if (filingType === 'all') return result; return this._filterValidFiling(result, filingType); } async getPosterityData(id){ const result = []; let cur = await this.getAllDataByCondition({ where: { tree_pid: id } }); let iLevel = 1; while (cur.length > 0 && iLevel < 6) { result.push(...cur); cur = await this.getAllDataByCondition({ where: { tree_pid: cur.map(x => { return x.id })} }); iLevel += 1; } return result; } _checkFixed(data) { if (data.is_fixed) throw '固定分类,不可编辑'; } async getNewName(spid, name = '新增文件类别') { const data = await this.db.query(`SELECT * FROM ${this.tableName} WHERE spid = '${spid}' 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(spid) { const max = await this.db.queryOne(`SELECT filing_type FROM ${this.tableName} WHERE spid = '${spid}' ORDER BY filing_type DESC`); return max && max.filing_type ? max.filing_type + 1 : maxFilingType + 1; } async add(data) { const parent = await this.getDataById(data.tree_pid); // 允许管理员添加顶层 // if (!parent) throw '添加数据结构错误'; if (parent && parent.file_count > 0) throw `分类【${parent.name}】下存在文件,不可添加子分类`; const sibling = await this.getAllDataByCondition({ where: { spid: this.ctx.subProject.id, 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(this.ctx.subProject.id); 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(this.ctx.subProject.id); const insertData = { id: this.uuid.v4(), spid: this.ctx.subProject.id, add_user_id: sessionUser.accountId, tree_pid: parent ? parent.id : rootId, tree_level: parent ? parent.tree_level + 1 : 1, tree_order, name, filing_type: filing_type, is_fixed: parent ? 0 : 1 }; 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); // this._checkFixed(filing); const result = await this.db.update(this.tableName, data); if (result.affectedRows > 0) { return data; } else { throw '更新数据失败'; } } async del(data) { const filing = await this.getDataById(data.id); // this._checkFixed(filing); const posterity = await this.getPosterityData(data.id); const delData = posterity.map(x => {return { id: x.id, is_deleted: 1 }; }); delData.push({ id: data.id, is_deleted: 1}); const sibling = await this.getAllDataByCondition({ where: { 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.updateRows(this.tableName, delData); if (updateData.length > 0) conn.updateRows(this.tableName, updateData); await conn.commit(); return { delete: delData.map(x => { return x.id }), update: updateData }; } catch(err) { await conn.rollback(); throw err; } } async move(data) { const filing = await this.getDataById(data.id); if (!filing) throw '移动的分类不存在,请刷新页面后重试'; const parent = await this.getDataById(data.tree_pid); if (!parent) throw '移动后的分类不存在,请刷新页面后重试'; const sibling = await this.getAllDataByCondition({ where: { tree_pid: data.tree_pid, is_deleted: 0 } }); const posterity = await this.getPosterityData(filing.id); const updateData = { id: filing.id, tree_order: data.tree_order, tree_pid: data.tree_pid, tree_level: parent.tree_level + 1 }; const posterityUpdateData = posterity.map(x => { return { id: x.id, tree_level: parent.tree_level + 1 - filing.tree_level + x.tree_level }; }); 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: { tree_pid: filing.tree_pid, is_deleted: 0 } }); 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}); }) } 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(); } catch (err) { await conn.rollback(); throw err; } return { update: [updateData, ...posterityUpdateData, ...siblingUpdateData] }; } async multiUpdate(spid, data) { if (!data || data.length === 0) throw '提交数据格式错误'; const sourceData = await this.getAllDataByCondition({ where: { spid } }); 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.getAllDataByCondition({ where: { spid } }); } async sumFileCount(spid) { const result = await this.db.queryOne(`SELECT SUM(file_count) AS file_count FROM ${this.tableName} WHERE spid = '${spid}' and is_deleted = 0`); return result.file_count; } } return Filing; };