course.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. const Excel = require("exceljs");
  4. const path = require("path");
  5. const MapNames = require("../const").MapNames;
  6. class CourseService extends Service {
  7. async list() {
  8. let mysql = this.app.mysql;
  9. let ret = await mysql.query("select id, subject, title, config from train_templates where state = 0");
  10. return {list:ret};
  11. }
  12. async adminList(query) {
  13. let mysql = this.app.mysql;
  14. let pageNo = query.pageNo;
  15. let pageSize = query.pageSize;
  16. pageNo = parseInt( pageNo );
  17. pageSize = parseInt( pageSize );
  18. let name = query.title;
  19. let ret = {records:[], total: 0, };
  20. let where = name ? `where title like '%${name}%' or subject like '%${name}%' `: "";
  21. let sql = `select id, title, subject, date_format(add_time,'%Y-%m-%d') as add_time, config, state from train_templates ${where}`
  22. let total = await mysql.query(`select count(id) as count from train_templates ${where}`);
  23. ret.total = total[0].count;
  24. let data = await mysql.query( `${sql} limit ${(pageNo-1)*pageSize},${pageSize}`);
  25. ret.records = data;
  26. return ret;
  27. }
  28. async add( data ) {
  29. let mysql = this.app.mysql;
  30. let ret = await mysql.query("insert into train_templates (title, subject, config, add_time) values (?,?,?, now())", [data.title, data.subject, data.config]);
  31. return {id: ret.insertId};
  32. }
  33. async save( data ) {
  34. let id = data.id;
  35. if( !id ) throw "id不为空";
  36. console.log( data );
  37. let config = data.config;
  38. if( typeof config == 'object') {
  39. config = JSON.stringify(config);
  40. }
  41. let mysql = this.app.mysql;
  42. let ret = await mysql.query("update train_templates set title=?, config=?, add_time=? where id=?",[data.title, config, mysql.literals.now, id]);
  43. return ret;
  44. }
  45. async edit( data ) {
  46. let id = data.id;
  47. if( !id ) throw "id不为空";
  48. console.log( data );
  49. let config = data.config;
  50. if( typeof config == 'object') {
  51. config = JSON.stringify(config);
  52. }
  53. let mysql = this.app.mysql;
  54. let ret = await mysql.query("update train_templates set title=?,subject=?, config=?, add_time=? where id=?",[data.title, data.subject, config, mysql.literals.now, id]);
  55. return ret;
  56. }
  57. async delete(id)
  58. {
  59. let mysql = this.app.mysql;
  60. let ret = await mysql.query("delete from train_templates where id=?",[id]);
  61. return ret;
  62. }
  63. async lock( body ) {
  64. let {id, state} = body;
  65. if( !id ) throw "ID为空";
  66. let s = state == 0? 2 : 0;
  67. let mysql = this.app.mysql;
  68. let ret = await mysql.query("update train_templates set state=? where id=?",[s, id]);
  69. ret.state = s;
  70. return ret;
  71. }
  72. async exportXls(query) {
  73. console.log("exportXls==>", query );
  74. let sql = `select id, title, subject, config, date_format(add_time,'%Y-%m-%d') as add_time from train_templates`
  75. if( query.selections ) { //导出指定的内容,否则导出所有
  76. sql += ` where id in (${query.selections})`;
  77. }
  78. let mysql = this.app.mysql;
  79. let ret = await mysql.query( sql );
  80. let workbook = new Excel.Workbook();
  81. let sheet = workbook.addWorksheet("训练科目列表", {
  82. // views: [{xSplit:1, ySplit:1}],
  83. pageSetup:{pageSize:9, orientation:'portrait', fitToPage:true, showGridLines:true, horizontalCentered:true},
  84. headerFooter:{
  85. firstHeader:"舰航仿真模拟训练科目列表",
  86. firstFooter:"舰航仿真模拟训练科目列表",
  87. oddFooter:"第 &P 页,共 &N页"
  88. }
  89. });
  90. let style = {alignment: {vertical:'middle', horizontal:'center'}};
  91. let columns = [
  92. {
  93. style,
  94. header: '#ID',
  95. key: 'id',
  96. },{
  97. style,
  98. header: '训练名称',
  99. key: 'title',
  100. width:20,
  101. },{
  102. style,
  103. width:20,
  104. header: '科目',
  105. key: 'subject',
  106. },{
  107. style,
  108. width:20,
  109. header: '地图',
  110. key: 'map',
  111. },{
  112. style,
  113. width:20,
  114. header: '更新时间',
  115. key: 'add_time',
  116. }
  117. ];
  118. sheet.columns = columns;
  119. let rows = ret.map(item=>{
  120. try {
  121. let cfg = JSON.parse(item.config );
  122. let scene = cfg.scene;
  123. if( scene ) {
  124. item.map = MapNames[parseInt(scene.id)];
  125. }
  126. } catch (error) {
  127. }
  128. delete item.config;
  129. return item;
  130. });
  131. sheet.addRows( rows );
  132. return await workbook.xlsx.writeBuffer();
  133. }
  134. }
  135. module.exports = CourseService;