quotaLib.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author EllisRan.
  6. * @date 2018/4/19
  7. * @version
  8. */
  9. const libConst = require('../const/lib');
  10. module.exports = app => {
  11. class QuotaLib extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'quota_lib';
  21. }
  22. /**
  23. * 获取指标源列表
  24. *
  25. * @param {Object} status - 指标源状态
  26. * @return {Array} - 返回列表数据
  27. */
  28. async getList(status = 0) {
  29. this.initSqlBuilder();
  30. if(status !== 0) {
  31. this.sqlBuilder.setAndWhere('status', {
  32. value: status,
  33. operate: '=',
  34. });
  35. }
  36. this.sqlBuilder.orderBy = [['id', 'desc']];
  37. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  38. return await this.db.query(sql, sqlParam);
  39. }
  40. /**
  41. * 获取指标源各状态数量
  42. *
  43. * @return {Array} - 返回数量列表数据
  44. */
  45. async getStatusNum() {
  46. const pend = await this.db.count(this.tableName, { status: libConst.status.pend });
  47. const enter = await this.db.count(this.tableName, { status: libConst.status.enter });
  48. const data = {
  49. pend,
  50. enter
  51. };
  52. return data;
  53. }
  54. /**
  55. * 新增指标源
  56. *
  57. * @param {Object} postData - 文件上传过来的数据
  58. * @return {Boolean} - 返回新增结果
  59. */
  60. async add(postData) {
  61. const insertData = {
  62. filename: postData.name,
  63. status: libConst.status.pend,
  64. filepath: postData.path,
  65. create_time: postData.create_time,
  66. enter_time: ''
  67. };
  68. const operate = await this.db.insert(this.tableName, insertData);
  69. return operate;
  70. }
  71. /**
  72. * 获取指标源详细页参数
  73. *
  74. * @return {Array} - 返回单条数据
  75. */
  76. async getLibDataById(id) {
  77. return await this.getDataById(id);
  78. }
  79. /**
  80. * 删除指标源
  81. *
  82. * @param {Object} id - 删除的id
  83. * @return {Boolean} - 删除结果
  84. */
  85. async deleteLibById(id) {
  86. return await this.deleteById(id);
  87. }
  88. /**
  89. * 指标源入库
  90. *
  91. * @param {Object} id - 更新的id
  92. * @return {Boolean} - 更新结果
  93. */
  94. async enterLibById(id) {
  95. const updateData = {
  96. status: libConst.status.enter,
  97. enter_time: Date.parse( new Date())/1000,
  98. id,
  99. };
  100. const result = this.db.update(this.tableName, updateData);
  101. return result.affectedRows > 0;
  102. }
  103. /**
  104. * 指标源批量入库
  105. *
  106. * @param {Object} postData - 文件信息数据
  107. * @param {Object} jsonData - json文件数据
  108. * @return {Boolean} - 更新结果
  109. */
  110. async batchAdd(postData,jsonData) {
  111. const conn = await this.db.beginTransaction(); // 初始化事务
  112. try{
  113. const insertData = {
  114. filename: postData.name,
  115. status: libConst.status.pend,
  116. filepath: postData.path,
  117. create_time: postData.create_time,
  118. enter_time: '',
  119. RoadLevel: jsonData.properties.RoadLevel,
  120. StartPegCode: jsonData.properties.StartPegCode,
  121. EndPegCode: jsonData.properties.EndPegCode,
  122. RoadLength: jsonData.properties.RoadLength,
  123. RoadWidth: jsonData.properties.RoadWidth,
  124. };
  125. // postData.push(updateData);
  126. const libResult = await conn.insert(this.tableName, insertData);
  127. const lib_id = libResult.insertId;
  128. const billsData = jsonData.bills;
  129. for(let i = 0; i < billsData.length; i++) {
  130. billsData[i] = this.ctx.helper.operationJson(billsData[i],'lib_id',lib_id);
  131. }
  132. const billsResult = await conn.insert('is_quota_bills',billsData);
  133. await conn.commit(); // 提交事务
  134. return true;
  135. } catch (err) {
  136. console.log(err);
  137. await conn.rollback();
  138. throw err;
  139. return false;
  140. }
  141. }
  142. }
  143. return QuotaLib;
  144. }