123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- '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: '竣工验收文件' },
- ];
- module.exports = app => {
- class Filing extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @param {String} tableName - 表名
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'filing';
- this.filingType = filingType;
- }
- get allFilingType () {
- return filingType.map(x => { return x.value });
- }
- async initFiling(spid, transaction) {
- const count = await this.count({ spid });
- if (count > 0) return;
- const insertData = [];
- for (const [i, f] of filingType.entries()) {
- insertData.push({
- id: this.uuid.v4(), tree_pid: -1, tree_level: 1, tree_order: i + 1,
- spid, add_user_id: this.ctx.session.sessionUser.accountId, is_fixed: 1,
- filing_type: f.value, name: f.name,
- });
- }
- if (transaction) {
- await transaction.insert(this.tableName, insertData);
- } else {
- await this.db.insert(this.tableName, insertData);
- }
- }
- async getValidFiling(spid, filingType) {
- const condition = { spid, is_deleted: 0 };
- if (filingType !== 'all') condition.filing_type = filingType;
- return await this.getAllDataByCondition({ where: condition });
- }
- 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 add(data) {
- const parent = await this.getDataById(data.tree_pid);
- if (!parent) throw '添加数据结构错误';
- if (parent.file_count > 0) throw `分类【${parent.name}】下存在文件,不可添加子分类`;
- const sibling = await this.getAllDataByCondition({ where: { tree_pid: parent.id }, orders: [['tree_order', 'asc']]});
- const preChild = data.tree_pre_id ? sibling.find(x => { x.id === data.tree_pre_id; }) : null;
- 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 insertData = {
- id: this.uuid.v4(), spid: parent.spid, add_user_id: sessionUser.accountId,
- tree_pid: parent.id, tree_level: parent.tree_level + 1, tree_order,
- name: 'xx文件', filing_type: parent.filing_type,
- };
- 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 error;
- }
- }
- }
- return Filing;
- };
|