Browse Source

自定义分类需求调整第一次提交

MaiXinRong 6 years ago
parent
commit
5fd0a4f7a1

+ 1 - 0
app/base/base_service.js

@@ -25,6 +25,7 @@ class BaseService extends Service {
         this.uuid = this.app.uuid;
         this.uuid = this.app.uuid;
         this.transaction = null;
         this.transaction = null;
         this.sqlBuilder = null;
         this.sqlBuilder = null;
+        this._ = require('lodash');
     }
     }
 
 
     /**
     /**

+ 64 - 11
app/controller/setting_controller.js

@@ -28,7 +28,7 @@ module.exports = app => {
         }
         }
 
 
         /**
         /**
-         * 项目信息页面
+         * 项目信息页面(Get)
          *
          *
          * @param {Object} ctx - egg全局变量
          * @param {Object} ctx - egg全局变量
          * @return {void}
          * @return {void}
@@ -62,6 +62,11 @@ module.exports = app => {
             }
             }
         }
         }
 
 
+        /**
+         * 项目设置 -- 账号设置(Get)
+         * @param ctx
+         * @returns {Promise<void>}
+         */
         async user(ctx) {
         async user(ctx) {
             try {
             try {
                 // 获取项目数据
                 // 获取项目数据
@@ -81,6 +86,12 @@ module.exports = app => {
             }
             }
         }
         }
 
 
+        /**
+         * 项目设置 -- 自定义标段分类(Get)
+         *
+         * @param ctx
+         * @returns {Promise<void>}
+         */
         async category(ctx) {
         async category(ctx) {
             try {
             try {
                 // 获取项目数据
                 // 获取项目数据
@@ -90,11 +101,13 @@ module.exports = app => {
                     throw '没有对应的项目数据';
                     throw '没有对应的项目数据';
                 }
                 }
 
 
-                const categoryData = await ctx.service.category.getAllCategory({ where: { pid: projectId } });
+                const categoryData = await ctx.service.category.getAllCategory(projectId);
+                const tenderData = await ctx.service.tender.getList();
                 const renderData = {
                 const renderData = {
                     projectData,
                     projectData,
                     categoryType: settingConst.cType,
                     categoryType: settingConst.cType,
                     categoryData,
                     categoryData,
+                    tenderData,
                 };
                 };
                 await this.layout('setting/category.ejs', renderData, 'setting/category_modal.ejs');
                 await this.layout('setting/category.ejs', renderData, 'setting/category_modal.ejs');
             } catch (error) {
             } catch (error) {
@@ -103,6 +116,12 @@ module.exports = app => {
             }
             }
         }
         }
 
 
+        /**
+         * 新增分类(Ajax)
+         *
+         * @param ctx
+         * @returns {Promise<void>}
+         */
         async addCategory(ctx) {
         async addCategory(ctx) {
             try {
             try {
                 const projectId = ctx.session.sessionProject.id;
                 const projectId = ctx.session.sessionProject.id;
@@ -123,12 +142,16 @@ module.exports = app => {
             }
             }
         }
         }
 
 
+        /**
+         * 编辑分类(Ajax)
+         *
+         * @param ctx
+         * @returns {Promise<void>}
+         */
         async updateCategory(ctx) {
         async updateCategory(ctx) {
             try {
             try {
                 const projectId = ctx.session.sessionProject.id;
                 const projectId = ctx.session.sessionProject.id;
-                const responseData = {
-                    err: 0, msg: '', data: null,
-                }
+                const responseData = { err: 0, msg: '', data: null, };
 
 
                 const data = JSON.parse(ctx.request.body.data);
                 const data = JSON.parse(ctx.request.body.data);
                 if (!data.id) {
                 if (!data.id) {
@@ -141,15 +164,13 @@ module.exports = app => {
                     }
                     }
                 }
                 }
 
 
-                if (data.value) {
-                    data.value = JSON.stringify(data.value)
-                }
                 const result = await ctx.service.category.update(data, {id: data.id});
                 const result = await ctx.service.category.update(data, {id: data.id});
                 if (!result) {
                 if (!result) {
-                    throw '提交数据失败'
+                    throw '提交数据失败';
                 }
                 }
 
 
-                responseData.data = await ctx.service.category.getCategory({id: data.id});
+                responseData.data = await ctx.service.category.getCategory(data.id);
+
                 ctx.body = responseData;
                 ctx.body = responseData;
             } catch (err) {
             } catch (err) {
                 console.log(err);
                 console.log(err);
@@ -157,6 +178,32 @@ module.exports = app => {
             }
             }
         }
         }
 
 
+        async setCategoryValue(ctx) {
+            try {
+                const responseData = { err: 0, msg: '', data: {}, };
+
+                const data = JSON.parse(ctx.request.body.data);
+                if (!data.id) {
+                    throw '提交数据错误';
+                }
+                await ctx.service.categoryValue.setCategoryValue(data.id, data.value);
+
+                responseData.data.category = await ctx.service.category.getCategory(data.id);
+                responseData.data.tenders = await ctx.service.tender.getList();
+
+                ctx.body = responseData;
+            } catch (err) {
+                console.log(err);
+                ctx.body = {err: 1, msg: err instanceof string ? err : '提交数据失败', data: null};
+            }
+        }
+
+        /**
+         * 删除分类(Ajax)
+         *
+         * @param ctx
+         * @returns {Promise<void>}
+         */
         async deleteCategory(ctx) {
         async deleteCategory(ctx) {
             try {
             try {
                 const projectId = ctx.session.sessionProject.id;
                 const projectId = ctx.session.sessionProject.id;
@@ -181,6 +228,12 @@ module.exports = app => {
             }
             }
         }
         }
 
 
+        /**
+         * 调整分类层次排序(Ajax)
+         *
+         * @param ctx
+         * @returns {Promise<void>}
+         */
         async resetCategoryLevel(ctx) {
         async resetCategoryLevel(ctx) {
             try {
             try {
                 const projectId = ctx.session.sessionProject.id;
                 const projectId = ctx.session.sessionProject.id;
@@ -190,7 +243,7 @@ module.exports = app => {
                 const data = JSON.parse(ctx.request.body.data);
                 const data = JSON.parse(ctx.request.body.data);
 
 
                 await ctx.service.category.resetCategoryLevel(data);
                 await ctx.service.category.resetCategoryLevel(data);
-                responseData.data = await ctx.service.category.getAllCategory({ where: { pid: projectId } });
+                responseData.data = await ctx.service.category.getAllCategory(projectId);
                 ctx.body = responseData;
                 ctx.body = responseData;
             } catch (err) {
             } catch (err) {
                 console.log(err);
                 console.log(err);

+ 1 - 1
app/controller/stage_controller.js

@@ -130,7 +130,7 @@ module.exports = app => {
                     tenderList,
                     tenderList,
                     stage,
                     stage,
                     auditConst: audit.flow,
                     auditConst: audit.flow,
-                }
+                };
                 await this.layout('stage/deal.ejs', renderData, 'stage/deal_modal.ejs');
                 await this.layout('stage/deal.ejs', renderData, 'stage/deal_modal.ejs');
             } catch (err) {
             } catch (err) {
                 console.log(err);
                 console.log(err);

+ 1 - 3
app/controller/tender_controller.js

@@ -32,9 +32,7 @@ module.exports = app => {
         async _list(view, setting, modal = '') {
         async _list(view, setting, modal = '') {
             try {
             try {
                 const tenderList = await this.ctx.service.tender.getList();
                 const tenderList = await this.ctx.service.tender.getList();
-                const categoryData = await this.ctx.service.category.getAllCategory({
-                    where: { pid: this.ctx.session.sessionProject.id }
-                });
+                const categoryData = await this.ctx.service.category.getAllCategory(this.ctx.session.sessionProject.id);
 
 
                 const renderData = {
                 const renderData = {
                     tenderList,
                     tenderList,

+ 40 - 14
app/public/js/category.js

@@ -36,8 +36,12 @@ function getCategoryHtml(category) {
         html.push('<td>', d.name, '</td>');
         html.push('<td>', d.name, '</td>');
         html.push('<td>', d.typeStr, '</td>');
         html.push('<td>', d.typeStr, '</td>');
         html.push('<td>');
         html.push('<td>');
-        for (const v of d.value) {
-            html.push('<span class="h5"><span class="badge badge-secondary">', v, '</span></span>\n');
+        if (d.value && d.value.length > 0) {
+            for (const v of d.value) {
+                html.push('<span class="h5"><span class="badge badge-secondary">', v.value, '</span></span>\n');
+            }
+        } else {
+            html.push('请在右侧添加值');
         }
         }
         html.push('</td>');
         html.push('</td>');
         html.push('<td>');
         html.push('<td>');
@@ -50,11 +54,29 @@ function getCategoryHtml(category) {
     return html.join('');
     return html.join('');
 }
 }
 
 
+function getValueTenderCount(value) {
+    function findTenderCate(tender) {
+        for (const c of tender.category) {
+            if (c.cid == value.cid) {
+                return c;
+            }
+        }
+    }
+    const valueTender = tenders.filter(function (t) {
+        const cate = findTenderCate(t);
+        console.log(cate);
+        return cate ? cate.value == value.id : false;
+    });
+    return valueTender ? valueTender.length : 0;
+}
+
 function getValueHtml(value) {
 function getValueHtml(value) {
     const html = [];
     const html = [];
     for (const v of value) {
     for (const v of value) {
         html.push('<tr name="value">');
         html.push('<tr name="value">');
-        html.push('<td><input class="form-control form-control-sm" name="value" placeholder="请输入值" value="' + v + '"></td>');
+        html.push('<td><input class="form-control form-control-sm" name="value" placeholder="请输入值" value="' + v.value + '" vid ="' + v.id +  '"></td>');
+        console.log(v);
+        html.push('<td>', getValueTenderCount(v) ,'</td>');
         html.push('<td><a href="javasrcipt: void(0);" class="text-danger">删除</a></td>');
         html.push('<td><a href="javasrcipt: void(0);" class="text-danger">删除</a></td>');
         html.push('</tr>');
         html.push('</tr>');
     }
     }
@@ -77,9 +99,9 @@ function bindCategoryControl() {
         $('#add-ok').attr('cid', id);
         $('#add-ok').attr('cid', id);
         if (category) {
         if (category) {
             const list = $('#value-list');
             const list = $('#value-list');
-            list.html(getValueHtml(category.value) + '<tr id="add-value-row"><td colspan="2"><a href="javascript: void(0);">添加新值</a></td></tr>');
+            list.html(getValueHtml(category.value) + '<tr id="add-value-row"><td colspan="3"><a href="javascript: void(0);">添加新值</a></td></tr>');
             $('#add-value-row').click(function () {
             $('#add-value-row').click(function () {
-                $(this).before('<tr><td><input class="form-control form-control-sm" name="value" placeholder="请输入值"></td><td><a href="javasrcipt: void(0);" class="text-danger">删除</a></td></tr>');
+                $(this).before('<tr><td><input class="form-control form-control-sm" name="value" placeholder="请输入值"></td><td>0</td><td><a href="javasrcipt: void(0);" class="text-danger">删除</a></td></tr>');
             });
             });
             $('#add').modal('show');
             $('#add').modal('show');
         }
         }
@@ -132,20 +154,23 @@ $(document).ready(() => {
         const value = $('input[name=value]');
         const value = $('input[name=value]');
         for (const v of value) {
         for (const v of value) {
             if (v.value !== '') {
             if (v.value !== '') {
-                valueArr.push(v.value);
+                valueArr.push({
+                    id: $(v).attr('id'),
+                    value: $(v).val(),
+                });
             }
             }
         }
         }
         const data = {
         const data = {
-            id: $(this).attr('cid'),
+            id: parseInt($(this).attr('cid')),
             value: valueArr,
             value: valueArr,
-        }
-        postData('/setting/category/update', data, function (data) {
-            const category = findCategory(data.id);
-            for (const c in data) {
-                category[c] = data[c];
-            }
+        };
+        postData('/setting/category/value', data, function (data) {
+            tenders = data.tenders;
+            const category = findCategory(data.category.id);
+            category.value = data.category.value;
             InitCategoryData(category);
             InitCategoryData(category);
-            $('tr[cid=' + data.id + ']')[0].outerHTML = getCategoryHtml(category);
+            $('tr[cid=' + data.category.id + ']')[0].outerHTML = getCategoryHtml(category);
+            bindCategoryControl();
             $('#add').modal('hide');
             $('#add').modal('hide');
         }, function () {
         }, function () {
             $('#add').modal('hide');
             $('#add').modal('hide');
@@ -167,6 +192,7 @@ $(document).ready(() => {
             }
             }
             InitCategoryData(category);
             InitCategoryData(category);
             $('tr[cid=' + data.id + ']')[0].outerHTML = getCategoryHtml(category);
             $('tr[cid=' + data.id + ']')[0].outerHTML = getCategoryHtml(category);
+            bindCategoryControl();
             $('#edit-cate').modal('hide');
             $('#edit-cate').modal('hide');
         }, function () {
         }, function () {
             $('#edit-cate').modal('hide');
             $('#edit-cate').modal('hide');

File diff suppressed because it is too large
+ 29 - 0
app/public/js/lodash.js


+ 4 - 23
app/public/js/tender_list.js

@@ -77,7 +77,7 @@ function onDropNode(event, treeId, treeNodes, targetNode, moveType) {
     }
     }
     resetFixNode(1);
     resetFixNode(1);
     resetFixNode(2);
     resetFixNode(2);
-    if (targetNode.lid === 1 && treeNodes[0].children.length !== 0) {
+    if (targetNode.lid === 1 && treeNodes[0].children && treeNodes[0].children.length !== 0) {
         moveChildren(treeNodes[0].children, zTree.getNodeByParam('lid', 1));
         moveChildren(treeNodes[0].children, zTree.getNodeByParam('lid', 1));
     } else if (targetNode.lid !== 1) {
     } else if (targetNode.lid !== 1) {
         if (targetNode.children.length >= 2) {
         if (targetNode.children.length >= 2) {
@@ -174,22 +174,6 @@ function getCategoryHtml() {
     }
     }
     return html.join('');
     return html.join('');
 }
 }
-function reSortTenderNodes (arr) {
-    for (const a of arr) {
-        if (a.children && a.children.length > 0) {
-            reSortTenderNodes(a.children);
-        }
-    }
-    arr.sort(function (a, b) {
-        if (a.cid && !b.cid) {
-            return -1;
-        } else if (!a.cid && b.cid) {
-            return 1;
-        } else {
-            return 0;
-        }
-    })
-}
 // 初始化TenderTree数据
 // 初始化TenderTree数据
 function initTenderTree () {
 function initTenderTree () {
     const levelCategory = category.filter(function (c) {
     const levelCategory = category.filter(function (c) {
@@ -197,7 +181,7 @@ function initTenderTree () {
     });
     });
     function findCategoryNode(cid, value, array) {
     function findCategoryNode(cid, value, array) {
         for (const a of array) {
         for (const a of array) {
-            if (a.cid === cid && a.name === value) {
+            if (a.cid === cid && a.vid === value) {
                 return a;
                 return a;
             }
             }
         }
         }
@@ -208,7 +192,8 @@ function initTenderTree () {
         if (!cate) {
         if (!cate) {
             cate = {
             cate = {
                 cid: category.id,
                 cid: category.id,
-                name: value,
+                vid: value,
+                name: findNode('id', value, category.value).value,
                 children: [],
                 children: [],
                 level: category.level,
                 level: category.level,
             };
             };
@@ -219,8 +204,6 @@ function initTenderTree () {
     function loadTenderCategory (tender) {
     function loadTenderCategory (tender) {
         let tenderCategory = null;
         let tenderCategory = null;
         for (const lc of levelCategory) {
         for (const lc of levelCategory) {
-            console.log(tender.category);
-            console.log(lc);
             const tenderCate = findNode('cid', lc.id, tender.category);
             const tenderCate = findNode('cid', lc.id, tender.category);
             if (tenderCate) {
             if (tenderCate) {
                 tenderCategory = getCategoryNode(lc, tenderCate.value, tenderCategory);
                 tenderCategory = getCategoryNode(lc, tenderCate.value, tenderCategory);
@@ -235,7 +218,6 @@ function initTenderTree () {
         t.valid = true;
         t.valid = true;
         if (t.category && levelCategory.length > 0) {
         if (t.category && levelCategory.length > 0) {
             const parent = loadTenderCategory(t);
             const parent = loadTenderCategory(t);
-            console.log(parent);
             if (parent) {
             if (parent) {
                 t.level = parent.level + 1;
                 t.level = parent.level + 1;
                 parent.children.push(t);
                 parent.children.push(t);
@@ -246,7 +228,6 @@ function initTenderTree () {
             tenderTree.push(t);
             tenderTree.push(t);
         }
         }
     }
     }
-    reSortTenderNodes(tenderTree);
 }
 }
 function recursiveGetTenderNodeHtml (node, arr) {
 function recursiveGetTenderNodeHtml (node, arr) {
     const html = [];
     const html = [];

+ 1 - 1
app/router.js

@@ -35,8 +35,8 @@ module.exports = app => {
     app.get('/setting/category', sessionAuth, 'settingController.category');
     app.get('/setting/category', sessionAuth, 'settingController.category');
     app.post('/setting/category/add', sessionAuth, 'settingController.addCategory');
     app.post('/setting/category/add', sessionAuth, 'settingController.addCategory');
     app.post('/setting/category/del', sessionAuth, 'settingController.deleteCategory');
     app.post('/setting/category/del', sessionAuth, 'settingController.deleteCategory');
-    app.post('/setting/category/del', sessionAuth, 'settingController.deleteCategory');
     app.post('/setting/category/update', sessionAuth, 'settingController.updateCategory');
     app.post('/setting/category/update', sessionAuth, 'settingController.updateCategory');
+    app.post('/setting/category/value', sessionAuth, 'settingController.setCategoryValue');
     app.post('/setting/category/level', sessionAuth, 'settingController.resetCategoryLevel');
     app.post('/setting/category/level', sessionAuth, 'settingController.resetCategoryLevel');
 
 
     // 项目相关
     // 项目相关

+ 15 - 6
app/service/category.js

@@ -45,16 +45,25 @@ module.exports = app => {
             return category;
             return category;
         }
         }
 
 
-        async getCategory(condition) {
-            const data = await this.getDataByCondition(condition);
-            data.value = data.value && data.value !== '' ? JSON.parse(data.value) : [];
+        async getCategory(id) {
+            const data = await this.getDataByCondition({id: id});
+            data.value = await this.ctx.service.categoryValue.getAllDataByCondition({ where: {cid: id} });
             return data;
             return data;
         }
         }
 
 
-        async getAllCategory(condition) {
-            const data = await this.getAllDataByCondition(condition);
+        /**
+         * 获取标段分类数据
+         *
+         * @param {Number} pid - 标段id
+         * @returns {Promise<*>}
+         */
+        async getAllCategory(pid) {
+            const data = await this.getAllDataByCondition({where: {pid: pid}});
+            const values = await this.ctx.service.categoryValue.getAllDataByCondition({ where: {pid: pid} });
             for (const d of data) {
             for (const d of data) {
-                d.value = d.value && d.value !== '' ? JSON.parse(d.value) : [];
+                d.value = values.filter(function (v) {
+                    return v.cid === d.id;
+                });
             }
             }
             return data;
             return data;
         }
         }

+ 110 - 0
app/service/category_value.js

@@ -0,0 +1,110 @@
+'use strict';
+
+/**
+ *
+ *
+ * @author Mai
+ * @date 2018/10/15
+ * @version
+ */
+
+module.exports = app => {
+    class CategoryValue extends app.BaseService {
+        /**
+         * 构造函数
+         *
+         * @param {Object} ctx - egg全局变量
+         * @return {void}
+         */
+        constructor(ctx) {
+            super(ctx);
+            this.tableName = 'category_value';
+        }
+
+        /**
+         * 新增分类值
+         *
+         * @param categoryId - 分类Id
+         * @param value - 值
+         * @returns {Promise<void>}
+         * @private
+         */
+        async _addValue(categoryId, value) {
+            const count = await this.count({cid: categoryId, value: value});
+            if (count > 0) {
+                throw '该分类下存在相同的值';
+            }
+
+            const newValue = {
+                cid: categoryId,
+                value: value,
+            };
+            const result = await this.transaction.insert(this.tableName, newValue);
+            if (result.affectedRows !== 1) {
+                throw '提交数据失败';
+            } else {
+                category.id = result.insertId;
+            }
+
+            category.value = category.value && category.value !== '' ? JSON.parse(category.value) : [];
+            return category;
+        }
+
+        async _deleteAndUpdateValue(orgValue, newValue) {
+            if (!orgValue || orgValue.length === 0) { return }
+            for (const ov of orgValue) {
+                const nv = _.find(newValue, function (v) {
+                    return v.id === orgValue.id;
+                });
+                if (!nv) {
+                    await this.transaction.delete(this.tableName, {id: orgValue.id});
+                } else if (nv.value !== ov.value) {
+                    await this.transaction.update(this.tableName, {id: orgValue.id, value: nv.value});
+                }
+            }
+        }
+
+        async setCategoryValue(cid, value) {
+            const orgValue = await this.getAllDataByCondition({ where: {cid: cid} });
+            const tenders = await this.ctx.service.tender.getList();
+
+            this.transaction = await this.db.beginTransaction();
+            try {
+                // 删除值 & 更新值
+                await this._deleteAndUpdateValue(orgValue, value);
+                // 新增值
+                const newValue = value.filter(function (v) {
+                    return !v.id;
+                });
+                for (const nv of newValue) {
+                    nv.cid = cid;
+                    nv.pid = this.ctx.session.sessionProject.id;
+                    const result = await this.transaction.insert(this.tableName, nv);
+                    nv.id = result.insertId;
+                }
+                // 更新标段属性
+                for (const t of tenders) {
+                    const category = t.category ? t.category : [];
+                    const cate = _.find(t.category, function (c) {
+                        return c.cid === cid;
+                    });
+                    const tenderValue = cate ? _.find(value, function (v) {return v.id === cate.value;}) : null;
+                    if (!tenderValue) {
+                        category.push({cid: cid, value: newValue[0].id});
+                        await this.transaction.update(this.service.tender.tableName, {
+                            id: t.id,
+                            category: JSON.stringify(category),
+                        });
+                    }
+                }
+                await this.transaction.commit();
+            } catch(error) {
+                await this.transaction.rollback();
+                throw error;
+            }
+        }
+
+    }
+
+    return CategoryValue;
+};

+ 1 - 0
app/view/layout/layout.ejs

@@ -25,6 +25,7 @@
     <script src="/public/js/cookies.js"></script>
     <script src="/public/js/cookies.js"></script>
     <script src="/public/js/jquery-contextmenu/jquery.ui.position.min.js"></script>
     <script src="/public/js/jquery-contextmenu/jquery.ui.position.min.js"></script>
     <script src="/public/js/jquery-contextmenu/jquery.contextMenu.min.js"></script>
     <script src="/public/js/jquery-contextmenu/jquery.contextMenu.min.js"></script>
+    <script src="/public/js/lodash.js"></script>
 </head>
 </head>
 
 
 <body>
 <body>

+ 1 - 0
app/view/setting/category.ejs

@@ -27,5 +27,6 @@
 <script>
 <script>
     let cData = JSON.parse('<%- JSON.stringify(categoryData) %>');
     let cData = JSON.parse('<%- JSON.stringify(categoryData) %>');
     const cType = JSON.parse('<%- JSON.stringify(categoryType) %>');
     const cType = JSON.parse('<%- JSON.stringify(categoryType) %>');
+    let tenders = JSON.parse('<%- JSON.stringify(tenderData) %>');
 </script>
 </script>
 <script src="/public/js/category.js"></script>
 <script src="/public/js/category.js"></script>

+ 1 - 1
app/view/setting/category_modal.ejs

@@ -58,7 +58,7 @@
             <div class="modal-body">
             <div class="modal-body">
                 <table class="table table-bordered">
                 <table class="table table-bordered">
                     <thead>
                     <thead>
-                    <tr><th>值</th><th>删除</th></tr>
+                    <tr><th>值</th><th>包含标段</th><th>删除</th></tr>
                     </thead>
                     </thead>
                     <tbody id="value-list">
                     <tbody id="value-list">
                     </tbody>
                     </tbody>

+ 1 - 0
package.json

@@ -15,6 +15,7 @@
     "egg-view-ejs": "^1.1.0",
     "egg-view-ejs": "^1.1.0",
     "gt3-sdk": "^2.0.0",
     "gt3-sdk": "^2.0.0",
     "koa-is-json": "^1.0.0",
     "koa-is-json": "^1.0.0",
+    "lodash": "^4.17.11",
     "moment": "^2.20.1",
     "moment": "^2.20.1",
     "node-uuid": "^1.4.8",
     "node-uuid": "^1.4.8",
     "node-xlsx": "^0.12.0",
     "node-xlsx": "^0.12.0",