quota_lib.js 6.1 KB

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