quota_lib.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. const conn = await this.db.beginTransaction(); // 初始化事务
  87. try{
  88. const libResult = await conn.deleteById(id);
  89. const billsResult = await conn.delete('is_quota_bills',{lib_id: id});
  90. await conn.commit(); // 提交事务
  91. return true;
  92. } catch(err) {
  93. console.log(err);
  94. await conn.rollback();
  95. throw err;
  96. return false;
  97. }
  98. }
  99. /**
  100. * 指标源入库
  101. *
  102. * @param {Object} id - 更新的id
  103. * @return {Boolean} - 更新结果
  104. */
  105. async enterLibById(id) {
  106. const updateData = {
  107. status: libConst.status.enter,
  108. enter_time: Date.parse( new Date())/1000,
  109. id,
  110. };
  111. const result = this.db.update(this.tableName, updateData);
  112. return result.affectedRows > 0;
  113. }
  114. /**
  115. * 指标源批量入库
  116. *
  117. * @param {Object} postData - 文件信息数据
  118. * @param {Object} jsonData - json文件数据
  119. * @return {Boolean} - 更新结果
  120. */
  121. async batchAdd(postData, jsonData) {
  122. const conn = await this.db.beginTransaction(); // 初始化事务
  123. try{
  124. const insertData = {
  125. filename: postData.name,
  126. status: libConst.status.pend,
  127. filepath: postData.path,
  128. create_time: postData.create_time,
  129. enter_time: '',
  130. RoadLevel: jsonData.properties.RoadLevel,
  131. StartPegCode: jsonData.properties.StartPegCode,
  132. EndPegCode: jsonData.properties.EndPegCode,
  133. RoadLength: jsonData.properties.RoadLength,
  134. RoadWidth: jsonData.properties.RoadWidth,
  135. };
  136. const libResult = await conn.insert(this.tableName, insertData);
  137. const lib_id = libResult.insertId;
  138. const billsData = jsonData.bills;
  139. for(let i = 0; i < billsData.length; i++) {
  140. billsData[i] = this.ctx.helper.operationJson(billsData[i],'lib_id',lib_id);
  141. }
  142. await this.ctx.service.match.matchBills(billsData);
  143. const billsResult = await conn.insert('is_quota_bills',billsData);
  144. const nodeResult = await this.ctx.service.tenderNode.insertData(this.ctx.service.match.nodes, conn);
  145. const indexResult = await this.ctx.service.tenderIndex.insertData(this.ctx.service.match.indexes, conn);
  146. const paramResult = await this.ctx.service.tenderParam.insertData(this.ctx.service.match.params, conn);
  147. await conn.commit(); // 提交事务
  148. return true;
  149. } catch (err) {
  150. console.log(err);
  151. await conn.rollback();
  152. throw err;
  153. return false;
  154. }
  155. }
  156. }
  157. return QuotaLib;
  158. }