Browse Source

指标库 no.5 up

laiguoran 7 years atrás
parent
commit
cc9d70fca8

+ 16 - 1
app/controller/lib_controller.js

@@ -54,6 +54,9 @@ module.exports = app => {
                 }
                 const fileInfo = path.parse(filename);
                 let create_time = Date.parse( new Date())/1000;
+                if(!fs.existsSync(libConst.quotaLibDir)) {
+                    fs.mkdirSync(libConst.quotaLibDir);
+                }
                 let filepath = libConst.quotaLibDir + create_time + fileInfo.ext;
 
                 addData = {
@@ -72,14 +75,23 @@ module.exports = app => {
             }
             if(addData !== {}){
 
+                let fileStram = fs.readFileSync(addData.path, 'utf8');
                 try {
+                    const fileResult = JSON.parse(fileStram);
+
                     //插入数据到数据库
-                    const result = await ctx.service.quotaLib.add(addData);
+                    const result = await ctx.service.quotaLib.batchAdd(addData,fileResult);
+
+                    // const result = await ctx.service.quotaLib.add(addData);
                     if (!result) {
+                        //插入失败则删除文件;
+                        fs.unlinkSync(addData.path);
                         throw '新增指标源数据失败';
                     }
                     ctx.redirect('/lib');
                 } catch (err) {
+                    //插入失败则删除文件;
+                    fs.unlinkSync(addData.path);
                     throw err;
                 }
             }else{
@@ -106,6 +118,9 @@ module.exports = app => {
                 // if(libInfo.status === libConst.status.enter){
                 //     throw '指标源已入库';
                 // }
+
+                //获取项目节编号
+                // const billsList = await ctx.service.bills.getListById(lid);
                 const renderData = {
                     libInfo,
                     libConst

+ 18 - 2
app/extend/helper.js

@@ -191,5 +191,21 @@ module.exports = {
     loadJsonFile(fileName) {
         const sJson = fs.readFileSync(fileName);
         return JSON.parse(sJson);
-    }
-}
+    },
+
+    /**
+     * 增删改单条json数据
+     * @param {string} json 单条json值
+     * @param {String} key 属性
+     * @param {String} value 值
+     * @returns {string}
+     */
+    operationJson(json,key, value) {
+        if(typeof value === "undefined") {
+            delete json[key];
+        } else {
+            json[key] = value;
+        }
+        return json;
+    },
+};

+ 163 - 0
app/service/bills.js

@@ -0,0 +1,163 @@
+'use strict';
+
+/**
+ *
+ *
+ * @author EllisRan.
+ * @date 2018/4/25
+ * @version
+ */
+
+module.exports = app => {
+
+    class Bills extends app.BaseService {
+
+        /**
+         * 构造函数
+         *
+         * @param {Object} ctx - egg全局变量
+         * @return {void}
+         */
+        constructor(ctx) {
+            super(ctx);
+            this.tableName = 'quota_bills';
+        }
+
+        /**
+         * 获取指标源列表
+         *
+         * @param {Object} status - 指标源状态
+         * @return {Array} - 返回列表数据
+         */
+        async getList(status = 0) {
+
+            this.initSqlBuilder();
+
+            if(status !== 0) {
+                this.sqlBuilder.setAndWhere('status', {
+                    value: status,
+                    operate: '=',
+                });
+            }
+            this.sqlBuilder.orderBy = [['id', 'desc']];
+            const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
+
+            return await this.db.query(sql, sqlParam);
+        }
+
+        /**
+         * 获取指标源各状态数量
+         *
+         * @return {Array} - 返回数量列表数据
+         */
+        async getStatusNum() {
+            const pend = await this.db.count(this.tableName, { status: libConst.status.pend });
+            const enter = await this.db.count(this.tableName, { status: libConst.status.enter });
+            const data = {
+                pend,
+                enter
+            };
+            return data;
+        }
+
+        /**
+         * 新增指标源
+         *
+         * @param {Object} postData - 文件上传过来的数据
+         * @return {Boolean} - 返回新增结果
+         */
+        async add(postData) {
+            const insertData = {
+                filename: postData.name,
+                status: libConst.status.pend,
+                filepath: postData.path,
+                create_time: postData.create_time,
+                enter_time: ''
+            };
+            const operate = await this.db.insert(this.tableName, insertData);
+            return operate;
+        }
+
+        /**
+         * 获取指标源详细页参数
+         *
+         * @return {Array} - 返回单条数据
+         */
+        async getLibDataById(id) {
+            return await this.getDataById(id);
+        }
+
+        /**
+         * 删除指标源
+         *
+         * @param {Object} id - 删除的id
+         * @return {Boolean} - 删除结果
+         */
+        async deleteLibById(id) {
+            return await this.deleteById(id);
+        }
+
+        /**
+         * 指标源入库
+         *
+         * @param {Object} id - 更新的id
+         * @return {Boolean} - 更新结果
+         */
+        async enterLibById(id) {
+
+            const updateData = {
+                status: libConst.status.enter,
+                enter_time: Date.parse( new Date())/1000,
+                id,
+            };
+
+            const result = this.db.update(this.tableName, updateData);
+
+            return result.affectedRows > 0;
+        }
+
+        /**
+         * 指标源批量入库
+         *
+         * @param {Object} postData - 文件信息数据
+         * @param {Object} jsonData - json文件数据
+         * @return {Boolean} - 更新结果
+         */
+        async batchAdd(postData,jsonData) {
+            const conn = await this.db.beginTransaction(); // 初始化事务
+            try{
+                const insertData = {
+                    filename: postData.name,
+                    status: libConst.status.pend,
+                    filepath: postData.path,
+                    create_time: postData.create_time,
+                    enter_time: '',
+                    RoadLevel: jsonData.properties.RoadLevel,
+                    StartPegCode: jsonData.properties.StartPegCode,
+                    EndPegCode: jsonData.properties.EndPegCode,
+                    RoadLength: jsonData.properties.RoadLength,
+                    RoadWidth: jsonData.properties.RoadWidth,
+                };
+                // postData.push(updateData);
+                const libResult = await conn.insert(this.tableName, insertData);
+                const lib_id = libResult.insertId;
+                const billsData = jsonData.bills;
+                for(let i = 0; i < billsData.length; i++) {
+                    billsData[i] = this.ctx.helper.operationJson(billsData[i],'lib_id',lib_id);
+                }
+                const billsResult = await conn.insert('is_quota_bills',billsData);
+                await conn.commit(); // 提交事务
+                return true;
+            } catch (err) {
+                console.log(err);
+                await conn.rollback();
+                throw err;
+                return false;
+            }
+        }
+
+
+    }
+
+    return QuotaLib;
+}

+ 40 - 0
app/service/quotaLib.js

@@ -116,6 +116,46 @@ module.exports = app => {
             return result.affectedRows > 0;
         }
 
+        /**
+         * 指标源批量入库
+         *
+         * @param {Object} postData - 文件信息数据
+         * @param {Object} jsonData - json文件数据
+         * @return {Boolean} - 更新结果
+         */
+        async batchAdd(postData,jsonData) {
+            const conn = await this.db.beginTransaction(); // 初始化事务
+            try{
+                const insertData = {
+                    filename: postData.name,
+                    status: libConst.status.pend,
+                    filepath: postData.path,
+                    create_time: postData.create_time,
+                    enter_time: '',
+                    RoadLevel: jsonData.properties.RoadLevel,
+                    StartPegCode: jsonData.properties.StartPegCode,
+                    EndPegCode: jsonData.properties.EndPegCode,
+                    RoadLength: jsonData.properties.RoadLength,
+                    RoadWidth: jsonData.properties.RoadWidth,
+                };
+                // postData.push(updateData);
+                const libResult = await conn.insert(this.tableName, insertData);
+                const lib_id = libResult.insertId;
+                const billsData = jsonData.bills;
+                for(let i = 0; i < billsData.length; i++) {
+                    billsData[i] = this.ctx.helper.operationJson(billsData[i],'lib_id',lib_id);
+                }
+                const billsResult = await conn.insert('is_quota_bills',billsData);
+                await conn.commit(); // 提交事务
+                return true;
+            } catch (err) {
+                console.log(err);
+                await conn.rollback();
+                throw err;
+                return false;
+            }
+        }
+
 
     }
 

+ 1 - 1
app/view/lib/modal.ejs

@@ -10,7 +10,7 @@
                 <div class="modal-body">
                     <div class="form-group">
                         <label for="exampleFormControlFile1">上传指标源文件</label>
-                        <div class="form-control"><input class="form-control-file" id="exampleFormControlFile1" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" name="file" type="file"></div>
+                        <div class="form-control"><input class="form-control-file" id="exampleFormControlFile1" accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel, .json" name="file" type="file"></div>
                     </div>
                 </div>
                 <div class="modal-footer">

+ 1 - 1
config/config.default.js

@@ -52,7 +52,7 @@ module.exports = appInfo => {
 
     // 上传设置
     config.multipart = {
-        whitelist: ['.xls', '.xlsx'],
+        whitelist: ['.xls', '.xlsx', '.json'],
         fileSize: '10mb',
     };
 

+ 1 - 1
config/config.qa.js

@@ -52,7 +52,7 @@ module.exports = appInfo => {
 
     // 上传设置
     config.multipart = {
-        whitelist: ['.xls', '.xlsx'],
+        whitelist: ['.xls', '.xlsx', '.json'],
         fileSize: '10mb',
     };
 

+ 6 - 0
test/app/extend/helper.test.js

@@ -42,4 +42,10 @@ describe('test/app/extend/helper.test.js', () => {
         assert(vJ.bills);
         assert(vJ.properties);
     });
+    it('operationJson test', function () {
+        const ctx = app.mockContext();
+        let testjson = {name: 'hello'};
+        const result = ctx.helper.operationJson(testjson,'ok','world');
+        assert(result.ok === 'world');
+    });
 });