'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 }, ]; const billsUtils = require('../lib/bills_utils'); 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 budgetStd = subProj.std_id ? await this.ctx.service.budgetStd.getDataById(subProj.std_id) : null; const template_id = budgetStd ? budgetStd.progress_template_id.split(',')[0] : ''; const insertData = []; if (template_id) { const templateData = await this.ctx.service.tenderNodeTemplate.getData(template_id); for (const tmp of templateData) { const bills = { code: tmp.code || '', name: tmp.name || '', tree_id: tmp.template_id, tree_pid: tmp.pid, tree_level: tmp.level, tree_order: tmp.order, tree_full_path: tmp.full_path, tree_is_leaf: tmp.is_leaf, }; this._getDefaultData(bills, subProj.id); insertData.push(bills); } } else { 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 addStdNode(subProj, targetId, stdData) { const findPreData = function(list, a) { if (!list || list.length === 0) { return null; } for (let i = 0, iLen = list.length; i < iLen; i++) { if (billsUtils.compareCode(list[i].code, a.code) > 0) { return i > 0 ? list[i - 1] : null; } } return list[list.length - 1]; }; let parent = await this.getDataByKid(subProj.id, targetId); if (targetId && !parent) throw '新增节点数据错误,请刷新页面重试'; const orgParentId = parent.id; let children = await this.getChildrenByParentId(subProj.id, targetId); const updateParent = children.length === 0; const insertData = []; const maxId = await this._getMaxLid(subProj.id); this.transaction = await this.db.beginTransaction(); try { if (updateParent) { const updateData = { id: parent.id }; updateData[this.setting.isLeaf] = false; this.clearParentingData(updateData); await this.transaction.update(this.tableName, updateData); } // 从最顶层节点依次查询是否存在,否则添加 const addStdData = stdData[stdData.length - 1]; const newData = { code: addStdData.code, name: addStdData.name }; newData[this.setting.kid] = maxId + 1; newData[this.setting.pid] = parent ? parent[this.setting.kid] : this.rootId; newData[this.setting.level] = parent ? parent[this.setting.level] + 1 : 1; newData[this.setting.fullPath] = parent ? `${parent[this.setting.fullPath]}-${newData[this.setting.kid]}` : `${newData[this.setting.kid]}`; const pre = findPreData(children, newData); newData[this.setting.order] = pre ? pre[this.setting.order] + 1 : 1; if (!pre || children.indexOf(pre) < children.length - 1) { await this._updateChildrenOrder(subProj.id, parent ? parent[this.setting.kid] : this.rootId, pre ? pre[this.setting.order] + 1 : 1); } newData[this.setting.isLeaf] = 1; this._getDefaultData(newData, subProj.id); insertData.push(newData); await this.transaction.insert(this.tableName, insertData); await this.transaction.commit(); } catch (err) { await this.transaction.rollback(); throw err; } this._cacheMaxLid(subProj.id, maxId + stdData.length); // 查询应返回的结果 const createData = await this.getDataByFullPath(subProj.id, insertData[0][this.setting.fullPath] + '%'); const updateData = await this.getNextsData(subProj.id, targetId, insertData[0][this.setting.order]); if (updateParent) { updateData.push(await this.getDataByKid(subProj.id, targetId)); } return { create: createData, update: updateData }; } async addStdNodeWithParent(subProj, targetId, stdData) { const findPreData = function(list, a) { if (!list || list.length === 0) { return null; } for (let i = 0, iLen = list.length; i < iLen; i++) { if (billsUtils.compareCode(list[i].code, a.code) > 0) { return i > 0 ? list[i - 1] : null; } } return list[list.length - 1]; }; let parent = await this.getDataByKid(subProj.id, targetId); if (targetId && !parent) throw '新增节点数据错误,请刷新页面重试'; const orgParentId = parent.id; let children = await this.getChildrenByParentId(subProj.id, targetId); const updateParent = children.length === 0; const insertData = []; const maxId = await this._getMaxLid(subProj.id); this.transaction = await this.db.beginTransaction(); try { if (updateParent) { const updateData = { id: parent.id }; updateData[this.setting.isLeaf] = false; this.clearParentingData(updateData); await this.transaction.update(this.tableName, updateData); } // 从最顶层节点依次查询是否存在,否则添加 for (let i = 0, len = stdData.length; i < len; i++) { const newData = { code: stdData[i].code, name: stdData[i].name }; newData[this.setting.kid] = maxId + i + 1; newData[this.setting.pid] = parent ? parent[this.setting.kid] : this.rootId; newData[this.setting.level] = parent ? parent[this.setting.level] + 1 : 1; newData[this.setting.fullPath] = parent ? `${parent[this.setting.fullPath]}-${newData[this.setting.kid]}` : `${newData[this.setting.kid]}`; const pre = findPreData(children, newData); newData[this.setting.order] = pre ? pre[this.setting.order] + 1 : 1; if (!pre || children.indexOf(pre) < children.length - 1) { await this._updateChildrenOrder(subProj.id, parent ? parent[this.setting.kid] : this.rootId, pre ? pre[this.setting.order] + 1 : 1); } newData[this.setting.isLeaf] = (i === len - 1); this._getDefaultData(newData, subProj.id); insertData.push(newData); parent = newData; children = []; } await this.transaction.insert(this.tableName, insertData); await this.transaction.commit(); } catch (err) { await this.transaction.rollback(); throw err; } this._cacheMaxLid(subProj.id, maxId + stdData.length); // 查询应返回的结果 const createData = await this.getDataByFullPath(subProj.id, insertData[0][this.setting.fullPath] + '%'); const updateData = await this.getNextsData(subProj.id, targetId, insertData[0][this.setting.order]); if (updateParent) { updateData.push(await this.getDataByKid(subProj.id, targetId)); } return { create: createData, update: updateData }; } 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; }