'use strict'; /** * * * @author Mai * @date * @version */ const readOnlyFields = ['id', 'spid', 'tree_id', 'tree_pid', 'tree_order', 'tree_level', 'tree_full_path', 'tree_is_leaf']; const defaultData = [ { code: '1', name: '前期阶段', tree_id: 1, tree_pid: -1, tree_level: 1, tree_order: 1, tree_full_path: '1', tree_is_leaf: 1 }, { code: '2', name: '实施阶段', tree_id: 2, tree_pid: -1, tree_level: 1, tree_order: 2, tree_full_path: '2', tree_is_leaf: 1 }, { code: '3', name: '竣(交)工阶段', tree_id: 3, tree_pid: -1, tree_level: 1, tree_order: 3, tree_full_path: '3', tree_is_leaf: 1 }, ]; module.exports = app => { class SubProjProgress extends app.BaseTreeService { /** * 构造函数 * * @param {Object} ctx - egg全局变量 * @param {String} tableName - 表名 * @return {void} */ constructor(ctx) { super(ctx, { mid: 'spid', kid: 'tree_id', pid: 'tree_pid', order: 'tree_order', level: 'tree_level', isLeaf: 'tree_is_leaf', fullPath: 'tree_full_path', cacheKey: false, uuid: true, }); this.tableName = 'sub_project_progress'; } _getDefaultData(data, spid) { data.id = this.uuid.v4(); data.spid = spid; data.add_user_id = this.ctx.session.sessionUser.accountId; data.update_user_id = this.ctx.session.sessionUser.accountId; } async init(subProj) { if (!subProj) throw '阶段进度数据错误'; const insertData = []; for (const b of defaultData) { const bills = JSON.parse(JSON.stringify(b)); this._getDefaultData(bills, subProj.id); insertData.push(bills); } const operate = await this.db.insert(this.tableName, insertData); return operate.affectedRows === insertData.length; } async getData(subProj) { let result = await this.getAllDataByCondition({ where: { spid: subProj.id } }); if (result.length === 0) { await this.init(subProj); result = await this.getAllDataByCondition({ where: { spid: subProj.id } }); } return result; } async addChild(spid, select, count) { const maxId = await this._getMaxLid(spid); const children = await this.getChildrenByParentId(spid, select[this.setting.kid]); const newDatas = []; for (let i = 1; i < count + 1; i++) { const newData = {}; newData[this.setting.kid] = maxId + i; newData[this.setting.pid] = select[this.setting.kid]; newData[this.setting.level] = select[this.setting.level] + 1; newData[this.setting.order] = children.length + i; newData[this.setting.fullPath] = select[this.setting.fullPath] + '-' + newData[this.setting.kid]; newData[this.setting.isLeaf] = true; this._getDefaultData(newData, spid); newDatas.push(newData); } this.transaction = await this.db.beginTransaction(); try { const result = await this.transaction.insert(this.tableName, newDatas); if (children.length === 0) await this.transaction.update(this.tableName, { id: select.id, tree_is_leaf: 0 }); await this.transaction.commit(); } catch(err) { this.transaction.rollback(); throw err; } // 查询应返回的结果 const resultData = {}; resultData.create = await this.getNextsData(spid, select[this.setting.kid], children.length); if (children.length === 0) resultData.update = await this.getDataByKid(spid, select[this.setting.kid]); return resultData; } async addNextSibling(spid, select, count) { const maxId = await this._getMaxLid(spid); const newDatas = []; for (let i = 1; i < count + 1; i++) { const newData = {}; newData[this.setting.kid] = maxId + i; newData[this.setting.pid] = select ? select[this.setting.pid] : this.rootId; newData[this.setting.level] = select ? select[this.setting.level] : 1; newData[this.setting.order] = select ? select[this.setting.order] + i : i; newData[this.setting.fullPath] = newData[this.setting.level] > 1 ? select[this.setting.fullPath].replace('-' + select[this.setting.kid], '-' + newData[this.setting.kid]) : newData[this.setting.kid] + ''; newData[this.setting.isLeaf] = true; this._getDefaultData(newData, spid); newDatas.push(newData); } this.transaction = await this.db.beginTransaction(); try { if (select) await this._updateChildrenOrder(spid, select[this.setting.pid], select[this.setting.order] + 1, count); const insertResult = await this.transaction.insert(this.tableName, newDatas); if (insertResult.affectedRows !== count) throw '新增节点数据错误'; await this.transaction.commit(); } catch (err) { await this.transaction.rollback(); this.transaction = null; throw err; } if (select) { const createData = await this.getChildBetween(spid, select[this.setting.pid], select[this.setting.order], select[this.setting.order] + count + 1); const updateData = await this.getNextsData(spid, select[this.setting.pid], select[this.setting.order] + count); return {create: createData, update: updateData}; } else { const createData = await this.getChildBetween(spid, -1, 0, count + 1); return {create: createData}; } } async addProgressNode(spid, targetId, count) { if (!spid) return null; const select = targetId ? await this.getDataByKid(spid, targetId) : null; if (targetId && !select) throw '新增节点数据错误'; if (select[this.setting.level] === 1) { return await this.addChild(spid, select, count); } else { return await this.addNextSibling(spid, select, count); } } async _deleteRelaData(spid, deleteData) { if (!this.transaction) throw '删除相关数据错误'; await this.transaction.update(this.ctx.service.subProjFile.tableName, {is_deleted: 1}, { where: { spid, type: 'progress', rela_id: deleteData.map(x => { return x.id; })} }); } async updateInfos(spid, data) { if (!spid) throw '数据错误'; const datas = Array.isArray(data) ? data : [data]; const orgDatas = await this.getAllDataByCondition({ where: { id: datas.map(x => { return x.id; })} }); const updateDatas = []; for (const d of datas) { const node = orgDatas.find(x => { return x.id === d.id; }); if (!node || node.spid !== spid) throw '提交数据错误'; d.update_user_id = this.ctx.session.sessionUser.accountId; updateDatas.push(this._filterUpdateInvalidField(node.id, d)); } await this.db.updateRows(this.tableName, updateDatas); const resultData = await this.getDataById(this._.map(datas, 'id')); return { update: resultData }; } } return SubProjProgress; }