浏览代码

标段切换项目也弄把用户迁移到权限表里

ellisran 3 月之前
父节点
当前提交
cf4ba2f816
共有 1 个文件被更改,包括 71 次插入65 次删除
  1. 71 65
      app/service/tender.js

+ 71 - 65
app/service/tender.js

@@ -342,6 +342,8 @@ module.exports = app => {
                 if (tender.spid !== postData.spid) {
                     if (postData.spid) await this.ctx.service.subProject.addRelaTender(conn, postData.spid, id);
                     if (tender.spid) await this.ctx.service.subProject.removeRelaTender(conn, tender.spid, id);
+                    const subProject = await this.ctx.service.subProject.getDataById(postData.spid);
+                    await this._addSubProjPermission(conn, subProject, [id]);
                 }
                 rowData.spid = postData.spid || '';
                 const result = await conn.update(this.tableName, rowData);
@@ -708,71 +710,7 @@ module.exports = app => {
                 await conn.updateRows(this.tableName, updateTenders);
                 await conn.update(this.ctx.service.subProject.tableName, { id: subProject.id, rela_tender: this._.uniq(orgRelaTenderId).join(',') });
                 await conn.update(this.ctx.service.budget.tableName, { id: subProject.budget_id, rela_tender: this._.uniq(orgRelaTenderId).join(',') });
-                // 需要把所有审批人也迁移至项目下
-                const subProjPermissionAudits = await this.ctx.service.subProjPermission.getAllDataByCondition({ where: { spid } });
-                const hadUids = this._.map(subProjPermissionAudits, 'uid');
-                const newUids = this._.cloneDeep(hadUids) || [];
-                console.log(tids);
-                console.log(tids.join(','));
-                const stageAuditsSql = `SELECT aid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND aid NOT IN (${newUids.join(',')})` : ''} GROUP BY aid`;
-                const stageAuditParams = [this.ctx.service.stageAudit.tableName];
-                const stageResult = await conn.query(stageAuditsSql, stageAuditParams);
-                newUids.push(...this._.map(stageResult, 'aid'));
-                const ledgerAuditsSql = `SELECT audit_id FROM ?? WHERE tender_id in (${tids.join(',')}) ${newUids.length > 0 ? ` AND audit_id NOT IN (${newUids.join(',')})` : ''} GROUP BY audit_id`;
-                const ledgerAuditParams = [this.ctx.service.ledgerAudit.tableName];
-                const ledgerResult = await conn.query(ledgerAuditsSql, ledgerAuditParams);
-                newUids.push(...this._.map(ledgerResult, 'audit_id'));
-                const auditAssAuditsSql = `SELECT ass_user_id FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND ass_user_id NOT IN (${newUids.join(',')})` : ''} GROUP BY ass_user_id`;
-                const auditAssAuditParams = [this.ctx.service.auditAss.tableName];
-                const auditAssResult = await conn.query(auditAssAuditsSql, auditAssAuditParams);
-                newUids.push(...this._.map(auditAssResult, 'ass_audit_id'));
-                const settleAuditsSql = `SELECT audit_id FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND audit_id NOT IN (${newUids.join(',')})` : ''} GROUP BY audit_id`;
-                const settleAuditParams = [this.ctx.service.settleAudit.tableName];
-                const settleResult = await conn.query(settleAuditsSql, settleAuditParams);
-                newUids.push(...this._.map(settleResult, 'audit_id'));
-                const changeAuditsSql = `SELECT uid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND uid NOT IN (${newUids.join(',')})` : ''} GROUP BY uid`;
-                const changeAuditParams = [this.ctx.service.changeAudit.tableName];
-                const changeResult = await conn.query(changeAuditsSql, changeAuditParams);
-                newUids.push(...this._.map(changeResult, 'uid'));
-                const changeApplyAuditsSql = `SELECT aid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND aid NOT IN (${newUids.join(',')})` : ''} GROUP BY aid`;
-                const changeApplyAuditParams = [this.ctx.service.changeApplyAudit.tableName];
-                const changeApplyResult = await conn.query(changeApplyAuditsSql, changeApplyAuditParams);
-                newUids.push(...this._.map(changeApplyResult, 'aid'));
-                const changeProjectAuditsSql = `SELECT aid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND aid NOT IN (${newUids.join(',')})` : ''} GROUP BY aid`;
-                const changeProjectAuditParams = [this.ctx.service.changeProjectAudit.tableName];
-                const changeProjectResult = await conn.query(changeProjectAuditsSql, changeProjectAuditParams);
-                newUids.push(...this._.map(changeProjectResult, 'aid'));
-                const changeProjectXsAuditsSql = `SELECT aid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND aid NOT IN (${newUids.join(',')})` : ''} GROUP BY aid`;
-                const changeProjectXsAuditParams = [this.ctx.service.changeProjectXsAudit.tableName];
-                const changeProjectXsResult = await conn.query(changeProjectXsAuditsSql, changeProjectXsAuditParams);
-                newUids.push(...this._.map(changeProjectXsResult, 'aid'));
-                const changePlanAuditsSql = `SELECT aid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND aid NOT IN (${newUids.join(',')})` : ''} GROUP BY aid`;
-                const changePlanAuditParams = [this.ctx.service.changePlanAudit.tableName];
-                const changePlanResult = await conn.query(changePlanAuditsSql, changePlanAuditParams);
-                newUids.push(...this._.map(changePlanResult, 'aid'));
-                const reviseAuditsSql = `SELECT audit_id FROM ?? WHERE tender_id in (${tids.join(',')}) ${newUids.length > 0 ? ` AND audit_id NOT IN (${newUids.join(',')})` : ''} GROUP BY audit_id`;
-                const reviseAuditParams = [this.ctx.service.reviseAudit.tableName];
-                const reviseResult = await conn.query(reviseAuditsSql, reviseAuditParams);
-                newUids.push(...this._.map(reviseResult, 'audit_id'));
-                const materialAuditsSql = `SELECT aid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND aid NOT IN (${newUids.join(',')})` : ''} GROUP BY aid`;
-                const materialAuditParams = [this.ctx.service.materialAudit.tableName];
-                const materialResult = await conn.query(materialAuditsSql, materialAuditParams);
-                newUids.push(...this._.map(materialResult, 'aid'));
-                const advanceAuditsSql = `SELECT audit_id FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND audit_id NOT IN (${newUids.join(',')})` : ''} GROUP BY audit_id`;
-                const advanceAuditParams = [this.ctx.service.advanceAudit.tableName];
-                const advanceResult = await conn.query(advanceAuditsSql, advanceAuditParams);
-                newUids.push(...this._.map(advanceResult, 'audit_id'));
-                const tenderTouristSql = `SELECT user_id FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND user_id NOT IN (${newUids.join(',')})` : ''} GROUP BY user_id`;
-                const tenderTouristParams = [this.ctx.service.tenderTourist.tableName];
-                const tenderTouristResult = await conn.query(tenderTouristSql, tenderTouristParams);
-                newUids.push(...this._.map(tenderTouristResult, 'user_id'));
-                const diffUids = this._.difference(newUids, hadUids);
-                if (diffUids.length > 0) {
-                    const insertData = diffUids.map(x => {
-                        return { id: this.uuid.v4(), spid: subProject.id, pid: subProject.project_id, uid: x };
-                    });
-                    await conn.insert(this.ctx.service.subProjPermission.tableName, insertData);
-                }
+                await this._addSubProjPermission(conn, subProject, tids);
                 await conn.commit();
                 return true;
             } catch (error) {
@@ -782,6 +720,74 @@ module.exports = app => {
             // return await this.ctx.subProject.setRelaTender({ id: spid, rela_tender: newTids });
         }
 
+        async _addSubProjPermission(conn, subProject, tids) {
+            // 需要把所有审批人也迁移至项目下
+            const subProjPermissionAudits = await this.ctx.service.subProjPermission.getAllDataByCondition({ where: { spid: subProject.id } });
+            const hadUids = this._.map(subProjPermissionAudits, 'uid');
+            const newUids = this._.cloneDeep(hadUids) || [];
+            console.log(tids);
+            console.log(tids.join(','));
+            const stageAuditsSql = `SELECT aid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND aid NOT IN (${newUids.join(',')})` : ''} GROUP BY aid`;
+            const stageAuditParams = [this.ctx.service.stageAudit.tableName];
+            const stageResult = await conn.query(stageAuditsSql, stageAuditParams);
+            newUids.push(...this._.map(stageResult, 'aid'));
+            const ledgerAuditsSql = `SELECT audit_id FROM ?? WHERE tender_id in (${tids.join(',')}) ${newUids.length > 0 ? ` AND audit_id NOT IN (${newUids.join(',')})` : ''} GROUP BY audit_id`;
+            const ledgerAuditParams = [this.ctx.service.ledgerAudit.tableName];
+            const ledgerResult = await conn.query(ledgerAuditsSql, ledgerAuditParams);
+            newUids.push(...this._.map(ledgerResult, 'audit_id'));
+            const auditAssAuditsSql = `SELECT ass_user_id FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND ass_user_id NOT IN (${newUids.join(',')})` : ''} GROUP BY ass_user_id`;
+            const auditAssAuditParams = [this.ctx.service.auditAss.tableName];
+            const auditAssResult = await conn.query(auditAssAuditsSql, auditAssAuditParams);
+            newUids.push(...this._.map(auditAssResult, 'ass_audit_id'));
+            const settleAuditsSql = `SELECT audit_id FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND audit_id NOT IN (${newUids.join(',')})` : ''} GROUP BY audit_id`;
+            const settleAuditParams = [this.ctx.service.settleAudit.tableName];
+            const settleResult = await conn.query(settleAuditsSql, settleAuditParams);
+            newUids.push(...this._.map(settleResult, 'audit_id'));
+            const changeAuditsSql = `SELECT uid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND uid NOT IN (${newUids.join(',')})` : ''} GROUP BY uid`;
+            const changeAuditParams = [this.ctx.service.changeAudit.tableName];
+            const changeResult = await conn.query(changeAuditsSql, changeAuditParams);
+            newUids.push(...this._.map(changeResult, 'uid'));
+            const changeApplyAuditsSql = `SELECT aid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND aid NOT IN (${newUids.join(',')})` : ''} GROUP BY aid`;
+            const changeApplyAuditParams = [this.ctx.service.changeApplyAudit.tableName];
+            const changeApplyResult = await conn.query(changeApplyAuditsSql, changeApplyAuditParams);
+            newUids.push(...this._.map(changeApplyResult, 'aid'));
+            const changeProjectAuditsSql = `SELECT aid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND aid NOT IN (${newUids.join(',')})` : ''} GROUP BY aid`;
+            const changeProjectAuditParams = [this.ctx.service.changeProjectAudit.tableName];
+            const changeProjectResult = await conn.query(changeProjectAuditsSql, changeProjectAuditParams);
+            newUids.push(...this._.map(changeProjectResult, 'aid'));
+            const changeProjectXsAuditsSql = `SELECT aid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND aid NOT IN (${newUids.join(',')})` : ''} GROUP BY aid`;
+            const changeProjectXsAuditParams = [this.ctx.service.changeProjectXsAudit.tableName];
+            const changeProjectXsResult = await conn.query(changeProjectXsAuditsSql, changeProjectXsAuditParams);
+            newUids.push(...this._.map(changeProjectXsResult, 'aid'));
+            const changePlanAuditsSql = `SELECT aid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND aid NOT IN (${newUids.join(',')})` : ''} GROUP BY aid`;
+            const changePlanAuditParams = [this.ctx.service.changePlanAudit.tableName];
+            const changePlanResult = await conn.query(changePlanAuditsSql, changePlanAuditParams);
+            newUids.push(...this._.map(changePlanResult, 'aid'));
+            const reviseAuditsSql = `SELECT audit_id FROM ?? WHERE tender_id in (${tids.join(',')}) ${newUids.length > 0 ? ` AND audit_id NOT IN (${newUids.join(',')})` : ''} GROUP BY audit_id`;
+            const reviseAuditParams = [this.ctx.service.reviseAudit.tableName];
+            const reviseResult = await conn.query(reviseAuditsSql, reviseAuditParams);
+            newUids.push(...this._.map(reviseResult, 'audit_id'));
+            const materialAuditsSql = `SELECT aid FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND aid NOT IN (${newUids.join(',')})` : ''} GROUP BY aid`;
+            const materialAuditParams = [this.ctx.service.materialAudit.tableName];
+            const materialResult = await conn.query(materialAuditsSql, materialAuditParams);
+            newUids.push(...this._.map(materialResult, 'aid'));
+            const advanceAuditsSql = `SELECT audit_id FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND audit_id NOT IN (${newUids.join(',')})` : ''} GROUP BY audit_id`;
+            const advanceAuditParams = [this.ctx.service.advanceAudit.tableName];
+            const advanceResult = await conn.query(advanceAuditsSql, advanceAuditParams);
+            newUids.push(...this._.map(advanceResult, 'audit_id'));
+            const tenderTouristSql = `SELECT user_id FROM ?? WHERE tid in (${tids.join(',')}) ${newUids.length > 0 ? ` AND user_id NOT IN (${newUids.join(',')})` : ''} GROUP BY user_id`;
+            const tenderTouristParams = [this.ctx.service.tenderTourist.tableName];
+            const tenderTouristResult = await conn.query(tenderTouristSql, tenderTouristParams);
+            newUids.push(...this._.map(tenderTouristResult, 'user_id'));
+            const diffUids = this._.difference(newUids, hadUids);
+            if (diffUids.length > 0) {
+                const insertData = diffUids.map(x => {
+                    return { id: this.uuid.v4(), spid: subProject.id, pid: subProject.project_id, uid: x };
+                });
+                await conn.insert(this.ctx.service.subProjPermission.tableName, insertData);
+            }
+        }
+
         async getNoSpTenders(pid) {
             const list = await this.getAllDataByCondition({ where: { project_id: pid, spid: '' }, orders: [['create_time', 'desc']] });
             const accountList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { project_id: pid } });