quota_lib.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author EllisRan.
  6. * @date 2018/4/19
  7. * @version
  8. */
  9. const libConst = require('../const/lib');
  10. const _ = require('lodash');
  11. module.exports = app => {
  12. class QuotaLib extends app.BaseService {
  13. /**
  14. * 构造函数
  15. *
  16. * @param {Object} ctx - egg全局变量
  17. * @return {void}
  18. */
  19. constructor(ctx) {
  20. super(ctx);
  21. this.tableName = 'quota_lib';
  22. }
  23. /**
  24. * 获取指标源列表
  25. *
  26. * @param {Object} status - 指标源状态
  27. * @return {Array} - 返回列表数据
  28. */
  29. async getList(status = 0) {
  30. this.initSqlBuilder();
  31. if(status !== 0) {
  32. this.sqlBuilder.setAndWhere('status', {
  33. value: status,
  34. operate: '=',
  35. });
  36. }
  37. this.sqlBuilder.orderBy = [['id', 'desc']];
  38. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  39. return await this.db.query(sql, sqlParam);
  40. }
  41. /**
  42. * 获取指标源各状态数量
  43. *
  44. * @return {Array} - 返回数量列表数据
  45. */
  46. async getStatusNum() {
  47. const pend = await this.db.count(this.tableName, { status: libConst.status.pend });
  48. const enter = await this.db.count(this.tableName, { status: libConst.status.enter });
  49. const data = {
  50. pend,
  51. enter
  52. };
  53. return data;
  54. }
  55. /**
  56. * 新增指标源
  57. *
  58. * @param {Object} postData - 文件上传过来的数据
  59. * @return {Boolean} - 返回新增结果
  60. */
  61. async add(postData) {
  62. const insertData = {
  63. filename: postData.name,
  64. status: libConst.status.pend,
  65. filepath: postData.path,
  66. create_time: postData.create_time,
  67. enter_time: ''
  68. };
  69. const operate = await this.db.insert(this.tableName, insertData);
  70. return operate;
  71. }
  72. /**
  73. * 获取指标源详细页参数
  74. *
  75. * @return {Array} - 返回单条数据
  76. */
  77. async getLibDataById(id) {
  78. return await this.getDataById(id);
  79. }
  80. /**
  81. * 删除指标源
  82. *
  83. * @param {Object} id - 删除的id
  84. * @return {Boolean} - 删除结果
  85. */
  86. async deleteLibById(id) {
  87. const conn = await this.db.beginTransaction(); // 初始化事务
  88. try{
  89. const libResult = await this.ctx.service.quotaLib.deleteData({id: id}, conn);
  90. const billsResult = await this.ctx.service.bills.deleteData({lib_id: id}, conn);
  91. const indexResult = await this.ctx.service.tenderIndex.deleteData({lib_id: id}, conn);
  92. const nodeResult = await this.ctx.service.tenderNode.deleteData({lib_id: id}, conn);
  93. const paramResult = await this.ctx.service.tenderParam.deleteData({lib_id: id}, conn);
  94. await conn.commit(); // 提交事务
  95. return true;
  96. } catch(err) {
  97. console.log(err);
  98. await conn.rollback();
  99. throw err;
  100. return false;
  101. }
  102. }
  103. /**
  104. * 指标源入库
  105. *
  106. * @param {Object} id - 更新的id
  107. * @return {Boolean} - 更新结果
  108. */
  109. async enterLibById(id) {
  110. const updateData = {
  111. status: libConst.status.enter,
  112. enter_time: Date.parse( new Date())/1000,
  113. id,
  114. };
  115. const result = this.db.update(this.tableName, updateData);
  116. return result.affectedRows > 0;
  117. }
  118. /**
  119. * 改变第一次进入detail后状态
  120. *
  121. * @param {Object} id - 更新的id
  122. * @return {Boolean} - 更新结果
  123. */
  124. async updateOpenItemTab(id) {
  125. const updateData = {
  126. openitem_tab: 1,
  127. id,
  128. };
  129. const result = this.db.update(this.tableName, updateData);
  130. return result.affectedRows > 0;
  131. }
  132. /**
  133. * 指标源批量入库
  134. *
  135. * @param {Object} postData - 文件信息数据
  136. * @param {Object} jsonData - json文件数据
  137. * @return {Boolean} - 更新结果
  138. */
  139. async batchAdd(postData, jsonData) {
  140. const conn = await this.db.beginTransaction(); // 初始化事务
  141. try{
  142. const insertData = {
  143. filename: postData.name,
  144. status: libConst.status.pend,
  145. filepath: postData.path,
  146. create_time: postData.create_time,
  147. enter_time: '',
  148. RoadLevel: jsonData.properties.RoadLevel,
  149. StartPegCode: jsonData.properties.StartPegCode,
  150. EndPegCode: jsonData.properties.EndPegCode,
  151. RoadLength: jsonData.properties.RoadLength,
  152. RoadWidth: jsonData.properties.RoadWidth,
  153. };
  154. const libResult = await conn.insert(this.tableName, insertData);
  155. const lib_id = libResult.insertId;
  156. const billsData = jsonData.bills;
  157. for(let i = 0; i < billsData.length; i++) {
  158. billsData[i] = this.ctx.helper.operationJson(billsData[i],'lib_id',lib_id);
  159. }
  160. await this.ctx.service.match.matchBills(billsData);
  161. const billsResult = await conn.insert('is_quota_bills',billsData);
  162. if (this.ctx.service.match.nodes.length > 0) {
  163. const nodeResult = await this.ctx.service.tenderNode.insertData(this.ctx.service.match.nodes, conn);
  164. }
  165. if (this.ctx.service.match.indexes.length > 0) {
  166. const indexResult = await this.ctx.service.tenderIndex.insertData(this.ctx.service.match.indexes, conn);
  167. }
  168. if (this.ctx.service.match.params.length > 0) {
  169. const paramResult = await this.ctx.service.tenderParam.insertData(this.ctx.service.match.params, conn);
  170. }
  171. await conn.commit(); // 提交事务
  172. return true;
  173. } catch (err) {
  174. console.log(err);
  175. await conn.rollback();
  176. throw err;
  177. return false;
  178. }
  179. }
  180. async _setRefreshAllTag() {
  181. const sql = 'Update ' + this.tableName + ' set is_refresh = true';
  182. await this.db.query(sql);
  183. }
  184. async refreshLib(lib) {
  185. //const billsData = await this.ctx.service.bills.getAllDataByCondition({ where: {lib_id: lib.id} });
  186. //const orgGlobalParam = await this.ctx.service.tenderParam.getParam(lib.id, paramConst.globalParamNodeId);
  187. //const orgParam = await this.ctx.service.tenderParam.getParam(lib.id);
  188. //const orgParam = await this.ctx.service.tenderParam.getAllDataByCondition({ where: {lib_id: lib.id} });
  189. await this.ctx.service.match.reMatchBills(lib);
  190. const conn = await this.db.beginTransaction();
  191. try {
  192. await conn.delete(this.ctx.service.tenderNode.tableName, {lib_id: lib.id});
  193. if (this.ctx.service.match.nodes.length > 0) {
  194. const nodeResult = await this.ctx.service.tenderNode.insertData(this.ctx.service.match.nodes, conn);
  195. }
  196. await conn.delete(this.ctx.service.tenderIndex.tableName, {lib_id: lib.id});
  197. if (this.ctx.service.match.indexes.length > 0) {
  198. const indexResult = await this.ctx.service.tenderIndex.insertData(this.ctx.service.match.indexes, conn);
  199. }
  200. await conn.delete(this.ctx.service.tenderParam.tableName, {lib_id: lib.id});
  201. if (this.ctx.service.match.params.length > 0) {
  202. const paramResult = await this.ctx.service.tenderParam.insertData(this.ctx.service.match.params, conn);
  203. }
  204. await conn.update(this.tableName, {id: lib.id, is_refresh: false});
  205. await conn.commit();
  206. } catch(err) {
  207. await conn.rollback();
  208. }
  209. }
  210. async _refreshAll() {
  211. const libs = await this.getAllDataByCondition({ where: { is_refresh: true } });
  212. this.ctx.app.awaitRefresh = _.map(libs, 'id');
  213. }
  214. async refreshAll() {
  215. await this._setRefreshAllTag();
  216. this._refreshAll();
  217. }
  218. }
  219. return QuotaLib;
  220. }