weapon.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. const Excel = require("exceljs");
  4. const path = require("path");
  5. const Names = {id:"ID",name_cn:"中文名字",name_en:"英文名字",x:"机型宽度",y:"机型长度",z:"机型高度", made_country:"生产国家", image:"图片"};
  6. class WeaponService extends Service {
  7. async list(query) {
  8. let name = query.name;
  9. let where = name ? `where (name_cn like '%${name}%' or name_en like '%${name}%' ) `: "";
  10. let type = query.type;
  11. if( type != undefined ) {
  12. where = where?where + `and (\`type\`='${type}' )`: `where \`type\`='${type}'`;
  13. }
  14. let ctx = this.ctx;
  15. let ret = await ctx.pageSelect(query, 'weapons', `*`, where);
  16. ret.records.forEach(e => {
  17. try {
  18. e.otherProps = JSON.parse(e.props);
  19. } catch (error) {
  20. e.otherProps = [];
  21. }
  22. });
  23. return ret;
  24. }
  25. async add( data ) {
  26. let mysql = this.app.mysql;
  27. data.add_time = mysql.literals.now;
  28. let props = data.otherProps;
  29. data.props = JSON.stringify(props);
  30. delete data['otherProps'];
  31. let ret = await mysql.insert("weapons", data);
  32. return {id: ret.insertId};
  33. }
  34. async edit( data ) {
  35. let id = data.id;
  36. if( !id ) throw "id不为空";
  37. let mysql = this.app.mysql;
  38. data.update_time = mysql.literals.now;
  39. let props = data.otherProps;
  40. data.props = JSON.stringify(props);
  41. delete data['otherProps'];
  42. delete data["add_time"];
  43. let ret = await mysql.update("weapons", data, {where:{id: data.id}});
  44. return ret;
  45. }
  46. async delete(data)
  47. {
  48. let mysql = this.app.mysql;
  49. let ret = await mysql.query("delete from weapons where id=?",[data.id]);
  50. return ret;
  51. }
  52. //app相关接口
  53. async listInApp(query) {
  54. let type = query.type;
  55. let where = type ? `where \`type\`='${type}' `: "";
  56. let ctx = this.ctx;
  57. let ret = await ctx.pageSelect(query, 'weapons', `*`, where);
  58. return ret;
  59. }
  60. parseWeaponProps( obj ) {
  61. let ret = [];
  62. const e = obj;
  63. let keys = Object.keys( e );
  64. let n = keys.length;
  65. let ctx = this.ctx;
  66. for( let m=0; m<n; m++) {
  67. let k = keys[m];
  68. if( k!='props') {
  69. let v = k == 'image' ?ctx.getStaticUrl(e[k]) : e[k];
  70. ret.push({name: Names[k], code: k, value: v} );
  71. }else {
  72. try {
  73. let props = JSON.parse( e[k] );
  74. props.forEach(p=>{
  75. ret.push({name:p.name, value:p.value, code:p.name});
  76. });
  77. } catch (error) {
  78. }
  79. }
  80. }
  81. return ret;
  82. }
  83. async supportedPlane() {
  84. let mysql = this.app.mysql;
  85. let sql = `select id,name_cn, name_en,x,y,z,made_country,props,image from weapons where category_id=1`;
  86. let planes = await mysql.query(sql);
  87. let ret = [];
  88. planes.forEach(e =>{
  89. let props = this.parseWeaponProps(e);
  90. ret.push({id: e.id, props});
  91. });
  92. return {list:ret};
  93. }
  94. async getPlaneInfo( query ) {
  95. if( query.supportId == undefined ) throw "supportId为空";
  96. let mysql = this.app.mysql;
  97. let sql = `select id, name_cn, name_en,x,y,z,made_country,props,image from weapons where support_id=${query.supportId}`;
  98. let ret = await mysql.query(sql);
  99. let retArr = [];
  100. if( ret.length == 1) {
  101. retArr = this.parseWeaponProps(ret[0]);
  102. }
  103. return {list: retArr}
  104. }
  105. async exportXls(query) {
  106. console.log("exportXls==>", query );
  107. let sql = `select id, name_cn, name_en,x,y,z,made_country,support_id from weapons`;
  108. if( query.selections ) { //导出指定的内容,否则导出所有
  109. sql += ` where id in (${query.selections})`;
  110. }
  111. let mysql = this.app.mysql;
  112. let ret = await mysql.query( sql );
  113. let workbook = new Excel.Workbook();
  114. let sheet = workbook.addWorksheet("装备列表", {
  115. // views: [{xSplit:1, ySplit:1}],
  116. pageSetup:{pageSize:9, orientation:'portrait', fitToPage:true, showGridLines:true, horizontalCentered:true},
  117. headerFooter:{
  118. firstHeader:"舰航仿真模拟训练装备列表",
  119. firstFooter:"舰航仿真模拟训练装备列表",
  120. oddFooter:"第 &P 页,共 &N页"
  121. }
  122. });
  123. let style = {alignment: {vertical:'middle', horizontal:'center'}};
  124. let columns = [
  125. {
  126. style,
  127. header: '#ID',
  128. key: 'id',
  129. },{
  130. style,
  131. header: '中文名',
  132. key: 'name_cn',
  133. width:15,
  134. },{
  135. style,
  136. width:15,
  137. header: '英文',
  138. key: 'name_en',
  139. },{
  140. style,
  141. width:10,
  142. header: '生产地',
  143. key: 'made_country',
  144. },{
  145. style,
  146. width:10,
  147. header: '长度',
  148. key: 'x',
  149. },{
  150. style,
  151. width:10,
  152. header: '宽度',
  153. key: 'y',
  154. },{
  155. style,
  156. width:10,
  157. header: '高度',
  158. key: 'z',
  159. }
  160. ];
  161. sheet.columns = columns;
  162. let rows = ret.map(item=>{
  163. return item;
  164. });
  165. sheet.addRows( rows );
  166. return await workbook.xlsx.writeBuffer();
  167. }
  168. }
  169. module.exports = WeaponService;