123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- 'use strict';
- /**
- *
- *
- * @author EllisRan.
- * @date 2018/4/19
- * @version
- */
- const libConst = require('../const/lib');
- module.exports = app => {
- class QuotaLib extends app.BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'quota_lib';
- }
- /**
- * 获取指标源列表
- *
- * @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;
- }
|