Tony Kang 10 kuukautta sitten
vanhempi
commit
0a05d14d17

+ 34 - 6
app/controller/report_controller.js

@@ -61,6 +61,33 @@ module.exports = app => {
             // throw
         }
 
+        async _getCustFolder(ctx, acc_id, tender_id) {
+            const rst = [];
+            // 因新需求,将增加tender_id作为定制化显示,但考虑到无缝链接,还是保留acc_id的判断:
+            const custFolders1 = await ctx.service.rptTreeNodeCust.getCustFoldersByUserId(acc_id);
+            const custFolders2 = await ctx.service.rptTreeNodeCust.getCustFolderByTenderId(tender_id);
+            // 1. 两种条件都要查询结果,然后:
+            //  1.1. 两种条件都没有,返回空数组
+            //  1.2. 两种条件都有,返回tender_id的,删除acc_id的
+            //  1.3. 只有acc_id的,那么:
+            //   1.3.1 增加新记录,用tender_id作为索引保存(acc_id = -1);
+            //   1.3.2 删除acc_id的这条记录,返回tender_id的这条记录
+            //  1.4. 只有tender_id的,返回这条记录
+            if (custFolders1.length > 0) {
+                if (custFolders2.length > 0) {
+                    rst.push(custFolders2[0]);
+                } else {
+                    await ctx.service.rptTreeNodeCust.createCustNodeByTenderId(tender_id, custFolders1[0].rpt_tpl_items);
+                    rst.push(custFolders1[0]); // 直接拿原先的用了
+                }
+                // 考虑不同的用户会有同一个标段,需要不断地删除用户的自定义设置
+                await ctx.service.rptTreeNodeCust.removeCustFolder(custFolders1[0].id);
+            } else if (custFolders2.length > 0) {
+                rst.push(custFolders2[0]);
+            }
+            return rst;
+        }
+
         async _createNodes(ctx, source_type, pid) {
             const treeNodes = await ctx.service.rptTreeNode.getNodesBySourceType([pid], source_type); // 这个查定制的
             const commonTreeNodes = await ctx.service.rptTreeNode.getNodesByProjectId([-1]); // 这个查通用的
@@ -141,7 +168,8 @@ module.exports = app => {
                 let stage_status = -1;
 
                 const { treeNodes, custCfg, allTreeItems, allIndivTreeItems } = await this._createNodes(ctx, sourceTypeConst.sourceType.tender, tender.data.project_id);
-                const custTreeNodes = await ctx.service.rptTreeNodeCust.getCustFoldersByUserId(this.ctx.session.sessionUser.accountId);
+                // const custTreeFolders = await ctx.service.rptTreeNodeCust.getCustFoldersByUserId(this.ctx.session.sessionUser.accountId);
+                const custTreeFolders = await this._getCustFolder(ctx, this.ctx.session.sessionUser.accountId, tender.id);
                 const stageList = await ctx.service.stage.getValidStagesShort(tender.id);
                 const unitList = await ctx.service.constructionUnit.getAllDataByCondition({ where: { pid: tender.data.project_id } }); // 找公司章用的
                 // 以后还有预付款、材差
@@ -285,8 +313,8 @@ module.exports = app => {
                 }
                 const cust_select_keys = JSON.stringify(['common', 'customize']); // 因其他地方也有可能保存用户报表的显示选择项,因当初设计问题,不好改数据库结构了,但可以调节内部json来满足需求
                 const rpt_tpl_items = { customize: [], common: [] };
-                if (custTreeNodes.length > 0) {
-                    const cust_select_item = JSON.parse(custTreeNodes[0].rpt_tpl_items);
+                if (custTreeFolders.length > 0) {
+                    const cust_select_item = JSON.parse(custTreeFolders[0].rpt_tpl_items);
                     if (cust_select_item.common) rpt_tpl_items.common = cust_select_item.common;
                     if (cust_select_item.customize) rpt_tpl_items.customize = cust_select_item.customize;
                 }
@@ -590,9 +618,9 @@ module.exports = app => {
                 }
                 const cust_select_keys = JSON.stringify(['common_dynamic', 'customize_dynamic']); // 因其他地方也有可能保存用户报表的显示选择项,因当初设计问题,不好改数据库结构了,但可以调节内部json来满足需求
                 const rpt_tpl_items = { customize: [], common: [] };
-                const custTreeNodes = await ctx.service.rptTreeNodeCust.getCustFoldersByUserId(this.ctx.session.sessionUser.accountId);
-                if (custTreeNodes.length > 0) {
-                    const cust_select_item = JSON.parse(custTreeNodes[0].rpt_tpl_items);
+                const custTreeFolders = await ctx.service.rptTreeNodeCust.getCustFoldersByUserId(this.ctx.session.sessionUser.accountId);
+                if (custTreeFolders.length > 0) {
+                    const cust_select_item = JSON.parse(custTreeFolders[0].rpt_tpl_items);
                     if (cust_select_item.common_dynamic) rpt_tpl_items.common = cust_select_item.common_dynamic;
                     if (cust_select_item.customize_dynamic) rpt_tpl_items.customize = cust_select_item.customize_dynamic;
                     if (!cust_select_item.hasOwnProperty('common_dynamic') && !cust_select_item.hasOwnProperty('customize_dynamic')) {

+ 72 - 0
app/service/rpt_tree_node_cust.js

@@ -26,6 +26,19 @@ module.exports = app => {
             this.dataId = 'id';
         }
 
+        async removeCustFolder(id) {
+            this.transaction = await this.db.beginTransaction();
+            try {
+                const rst = await this.transaction.delete(this.tableName, { id });
+                await this.transaction.commit();
+                return rst;
+            } catch (ex) {
+                console.log(ex);
+                // 回滚
+                await this.transaction.rollback();
+            }
+        }
+
         async getCustFoldersByUserId(userId) {
             this.initSqlBuilder();
             this.sqlBuilder.setAndWhere('cust_acc_id', {
@@ -39,12 +52,48 @@ module.exports = app => {
             return list;
         }
 
+        async getCustFolderByTenderId(tenderId) {
+            this.initSqlBuilder();
+            this.sqlBuilder.setAndWhere('tender_id', {
+                value: tenderId,
+                operate: '=',
+            });
+            this.sqlBuilder.columns = ['id', 'cust_acc_id', 'tender_id', 'rpt_tpl_items'];
+            const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
+
+            const list = await this.db.query(sql, sqlParam);
+            return list;
+        }
+
         async createCustNode(cust_acc_id, newRptItems) {
             let rst = null;
             this.transaction = await this.db.beginTransaction();
             try {
                 const data = {
                     cust_acc_id,
+                    tender_id: -1,
+                    rpt_tpl_items: newRptItems,
+                };
+                // console.log('createNode:');
+                // console.log(data);
+                rst = await this.transaction.insert(this.tableName, data);
+                await this.transaction.commit();
+            } catch (ex) {
+                console.log(ex);
+                // 回滚
+                await this.transaction.rollback();
+            } finally {
+                return rst;
+            }
+        }
+
+        async createCustNodeByTenderId(tender_id, newRptItems) {
+            let rst = null;
+            this.transaction = await this.db.beginTransaction();
+            try {
+                const data = {
+                    cust_acc_id: -1,
+                    tender_id,
                     rpt_tpl_items: newRptItems,
                 };
                 // console.log('createNode:');
@@ -60,6 +109,29 @@ module.exports = app => {
             }
         }
 
+        async updateCustNodeByTenderId(tender_id, newNodeItems) {
+            let rst = null;
+            try {
+                const custNode = await this.getCustFolderByTenderId(tender_id);
+                // console.log('query Node:');
+                // console.log(custNode);
+                if (custNode.length > 0) {
+                    this.transaction = await this.db.beginTransaction();
+                    custNode[0].rpt_tpl_items = newNodeItems;
+                    rst = await this.transaction.update(this.tableName, custNode[0]);
+                    this.transaction.commit();
+                } else {
+                    rst = await this.createCustNodeByTenderId(tender_id, newNodeItems);
+                }
+            } catch (ex) {
+                console.log(ex);
+                this.transaction.rollback();
+                rst = null;
+            } finally {
+                return rst;
+            }
+        }
+
         async updateCustNode(cust_acc_id, newNodeItems, custSelectKeys) {
             let rst = null;
             try {

+ 1 - 1
app/view/report/rpt_all_popup.ejs

@@ -1155,7 +1155,7 @@
     }
 
     function updateCustRptCfg(){
-        let params = {};
+        let params = { tender_id: TENDER_ID};
         params.nodeItems = CUST_TREE_NODES;
         params.custSelectKeys = CUST_SELECT_KEYS;
         CommonAjax.postXsrfEx("/tender/report_api/updateCustNode", params, 60000, true, getCookie('csrfToken_j'),

+ 5 - 0
sql/update.sql

@@ -405,3 +405,8 @@ ADD COLUMN `features` varchar(1000) NOT NULL DEFAULT '' COMMENT '项目特征' A
 
 ALTER TABLE zh_change`
 ADD COLUMN `order_site` int(11) NULL DEFAULT NULL COMMENT 'order_by为自定义时插入到清单id值' AFTER `order_by`;
+
+ALTER TABLE `zh_rpt_tree_node_cust` 
+ADD COLUMN `tender_id` INT NULL DEFAULT -1 COMMENT '新需求,跟标段走,不跟客户走' AFTER `cust_acc_id`,
+ADD INDEX `tender` (`tender_id` ASC);
+;