lib_controller.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. 'use strict';
  2. /**
  3. * 指标库控制器
  4. *
  5. * @author Mai
  6. * @data 2018/4/19
  7. * @version
  8. */
  9. const fs = require('fs');
  10. const path = require('path');
  11. const awaitWriteStream = require('await-stream-ready').write;
  12. const sendToWormhole = require('stream-wormhole');
  13. const libConst = require('../const/lib');
  14. module.exports = app => {
  15. class LibController extends app.BaseController {
  16. /**
  17. * 指标库页面
  18. *
  19. * @param {object} ctx - egg全局context
  20. * @return {void}
  21. */
  22. async index (ctx) {
  23. let status = ctx.query.status;
  24. const libList = await ctx.service.quotaLib.getList(status);
  25. //获取指标源状态数量
  26. const libStatus = await ctx.service.quotaLib.getStatusNum();
  27. const renderData = {
  28. status,
  29. libList,
  30. libConst,
  31. libStatus
  32. };
  33. await this.layout('lib/index.ejs', renderData, 'lib/modal.ejs');
  34. }
  35. /**
  36. * 上传指标源
  37. *
  38. * @param {object} ctx - egg全局context
  39. * @return {void}
  40. */
  41. async upload(ctx) {
  42. const parts = ctx.multipart({ autoFields: true });
  43. let addData = {};
  44. let stream;
  45. while ((stream = await parts()) != null) {
  46. const filename = stream.filename.toLowerCase();
  47. if(filename === ''){
  48. throw '请上传文件';
  49. ctx.redirect('/lib');
  50. }
  51. const fileInfo = path.parse(filename);
  52. let create_time = Date.parse( new Date())/1000;
  53. if(!fs.existsSync(libConst.quotaLibDir)) {
  54. fs.mkdirSync(libConst.quotaLibDir);
  55. }
  56. let filepath = libConst.quotaLibDir + create_time + fileInfo.ext;
  57. addData = {
  58. name: fileInfo.name,
  59. path: filepath,
  60. create_time: create_time
  61. };
  62. const target = path.join(filepath);
  63. const writeStream = fs.createWriteStream(target);
  64. try {
  65. await awaitWriteStream(stream.pipe(writeStream));
  66. } catch (err) {
  67. await sendToWormhole(stream);
  68. throw err;
  69. }
  70. }
  71. if(addData !== {}){
  72. let fileStram = fs.readFileSync(addData.path, 'utf8');
  73. try {
  74. const fileResult = JSON.parse(fileStram);
  75. //插入数据到数据库
  76. const result = await ctx.service.quotaLib.batchAdd(addData,fileResult);
  77. // const result = await ctx.service.quotaLib.add(addData);
  78. if (!result) {
  79. //插入失败则删除文件;
  80. fs.unlinkSync(addData.path);
  81. throw '新增指标源数据失败';
  82. }
  83. ctx.redirect('/lib');
  84. } catch (err) {
  85. //插入失败则删除文件;
  86. fs.unlinkSync(addData.path);
  87. throw err;
  88. }
  89. }else{
  90. throw '请上传文件';
  91. }
  92. }
  93. /**
  94. * 指标源全局参数详细页面
  95. *
  96. * @param {object} ctx - egg全局context
  97. * @return {void}
  98. */
  99. async detail (ctx) {
  100. let lid = ctx.params.id;
  101. lid = parseInt(lid);
  102. try{
  103. if(isNaN(lid) || lid < 0){
  104. throw '参数有误';
  105. }
  106. //获取指标源数据
  107. const libInfo = await ctx.service.quotaLib.getLibDataById(lid);
  108. // if(libInfo.status === libConst.status.enter){
  109. // throw '指标源已入库';
  110. // }
  111. const renderData = {
  112. libInfo,
  113. libConst
  114. };
  115. await this.layout('lib/detail.ejs', renderData);
  116. } catch (error) {
  117. console.log(error);
  118. ctx.redirect('/lib');
  119. }
  120. }
  121. /**
  122. * 指标源项目节参数详细页面
  123. *
  124. * @param {object} ctx - egg全局context
  125. * @return {void}
  126. */
  127. async detail2 (ctx) {
  128. let lid = ctx.params.id;
  129. lid = parseInt(lid);
  130. try{
  131. if(isNaN(lid) || lid < 0){
  132. throw '参数有误';
  133. }
  134. //获取指标源数据
  135. const libInfo = await ctx.service.quotaLib.getLibDataById(lid);
  136. // if(libInfo.status === libConst.status.enter){
  137. // throw '指标源已入库';
  138. // }
  139. //获取项目节编号
  140. let billsList = {};
  141. if(libInfo){
  142. billsList = await ctx.service.quotaBills.getListByLid(lid);
  143. }
  144. const renderData = {
  145. libInfo,
  146. libConst,
  147. billsList
  148. };
  149. await this.layout('lib/detail2.ejs', renderData, 'lib/detail-modal.ejs');
  150. } catch (error) {
  151. console.log(error);
  152. ctx.redirect('/lib');
  153. }
  154. }
  155. /**
  156. * 删除指标源
  157. *
  158. * @param {Object} ctx -egg全局变量
  159. * @return {void}
  160. */
  161. async delete(ctx) {
  162. let id = ctx.request.body.del_lib_id;
  163. let text = ctx.request.body.del_lib_text;
  164. id = parseInt(id);
  165. try {
  166. if (isNaN(id) || id <= 0) {
  167. throw '参数错误';
  168. }
  169. if(text === undefined || text !== '确认删除'){
  170. throw '参数错误';
  171. }
  172. //获取指标源数据
  173. const libInfo = await ctx.service.quotaLib.getLibDataById(id);
  174. if(!libInfo){
  175. throw '不存在该标段';
  176. }
  177. //删清指标源和清单数据
  178. const result = ctx.service.quotaLib.deleteLibById(id);
  179. if (!result) {
  180. throw '删除标段失败';
  181. }
  182. fs.unlinkSync(libInfo.filepath);
  183. ctx.redirect('/lib');
  184. } catch (error) {
  185. console.log(error);
  186. ctx.redirect(ctx.request.headers.referer);
  187. }
  188. }
  189. /**
  190. * 指标源入库
  191. *
  192. * @param {Object} ctx -egg全局变量
  193. * @return {void}
  194. */
  195. async enter(ctx) {
  196. let id = ctx.request.body.enter_lib_id;
  197. id = parseInt(id);
  198. try {
  199. if (isNaN(id) || id <= 0) {
  200. throw '参数错误';
  201. }
  202. const result = ctx.service.quotaLib.enterLibById(id);
  203. if (!result) {
  204. throw '指标入库失败';
  205. }
  206. ctx.redirect('/lib');
  207. } catch (error) {
  208. console.log(error);
  209. ctx.redirect(ctx.request.headers.referer);
  210. }
  211. }
  212. }
  213. return LibController;
  214. }