123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- 'use strict';
- const Service = require('egg').Service;
- const Excel = require("exceljs");
- const path = require("path");
- const Names = {id:"ID",name_cn:"中文名字",name_en:"英文名字",x:"机型宽度",y:"机型长度",z:"机型高度", made_country:"生产国家", image:"图片"};
- class WeaponService extends Service {
- async list(query) {
- let name = query.name;
- let where = name ? `where (name_cn like '%${name}%' or name_en like '%${name}%' ) `: "";
- let type = query.type;
- if( type != undefined ) {
- where = where?where + `and (\`type\`='${type}' )`: `where \`type\`='${type}'`;
- }
- let ctx = this.ctx;
- let ret = await ctx.pageSelect(query, 'weapons', `*`, where);
- ret.records.forEach(e => {
- try {
- e.otherProps = JSON.parse(e.props);
- } catch (error) {
- e.otherProps = [];
- }
- });
- return ret;
- }
- async add( data ) {
- let mysql = this.app.mysql;
- data.add_time = mysql.literals.now;
- let props = data.otherProps;
- data.props = JSON.stringify(props);
- delete data['otherProps'];
- let ret = await mysql.insert("weapons", data);
- return {id: ret.insertId};
- }
- async edit( data ) {
- let id = data.id;
- if( !id ) throw "id不为空";
- let mysql = this.app.mysql;
- data.update_time = mysql.literals.now;
- let props = data.otherProps;
- data.props = JSON.stringify(props);
- delete data['otherProps'];
- delete data["add_time"];
- let ret = await mysql.update("weapons", data, {where:{id: data.id}});
- return ret;
- }
- async delete(data)
- {
- let mysql = this.app.mysql;
- let ret = await mysql.query("delete from weapons where id=?",[data.id]);
- return ret;
- }
- //app相关接口
- async listInApp(query) {
- let type = query.type;
- let where = type ? `where \`type\`='${type}' `: "";
- let ctx = this.ctx;
- let ret = await ctx.pageSelect(query, 'weapons', `*`, where);
- return ret;
- }
- parseWeaponProps( obj ) {
- let ret = [];
- const e = obj;
- let keys = Object.keys( e );
- let n = keys.length;
- let ctx = this.ctx;
-
- for( let m=0; m<n; m++) {
- let k = keys[m];
- if( k!='props') {
- let v = k == 'image' ?ctx.getStaticUrl(e[k]) : e[k];
- ret.push({name: Names[k], code: k, value: v} );
- }else {
- try {
- let props = JSON.parse( e[k] );
- props.forEach(p=>{
- ret.push({name:p.name, value:p.value, code:p.name});
- });
- } catch (error) {
- }
- }
- }
- return ret;
- }
- async supportedPlane() {
- let mysql = this.app.mysql;
- let sql = `select id,name_cn, name_en,x,y,z,made_country,props,image from weapons where category_id=1`;
- let planes = await mysql.query(sql);
-
- let ret = [];
- planes.forEach(e =>{
- let props = this.parseWeaponProps(e);
- ret.push({id: e.id, props});
- });
- return {list:ret};
- }
- async getPlaneInfo( query ) {
- if( query.supportId == undefined ) throw "supportId为空";
- let mysql = this.app.mysql;
- let sql = `select id, name_cn, name_en,x,y,z,made_country,props,image from weapons where support_id=${query.supportId}`;
- let ret = await mysql.query(sql);
-
- let retArr = [];
- if( ret.length == 1) {
- retArr = this.parseWeaponProps(ret[0]);
- }
- return {list: retArr}
- }
- async exportXls(query) {
- console.log("exportXls==>", query );
-
- let sql = `select id, name_cn, name_en,x,y,z,made_country,support_id from weapons`;
-
- if( query.selections ) { //导出指定的内容,否则导出所有
- sql += ` where id in (${query.selections})`;
- }
- let mysql = this.app.mysql;
- let ret = await mysql.query( sql );
-
- let workbook = new Excel.Workbook();
- let sheet = workbook.addWorksheet("装备列表", {
- // views: [{xSplit:1, ySplit:1}],
- pageSetup:{pageSize:9, orientation:'portrait', fitToPage:true, showGridLines:true, horizontalCentered:true},
- headerFooter:{
- firstHeader:"舰航仿真模拟训练装备列表",
- firstFooter:"舰航仿真模拟训练装备列表",
- oddFooter:"第 &P 页,共 &N页"
- }
- });
-
- let style = {alignment: {vertical:'middle', horizontal:'center'}};
- let columns = [
- {
- style,
- header: '#ID',
- key: 'id',
- },{
- style,
- header: '中文名',
- key: 'name_cn',
- width:15,
- },{
- style,
- width:15,
- header: '英文',
- key: 'name_en',
- },{
- style,
- width:10,
- header: '生产地',
- key: 'made_country',
- },{
- style,
- width:10,
- header: '长度',
- key: 'x',
- },{
- style,
- width:10,
- header: '宽度',
- key: 'y',
- },{
- style,
- width:10,
- header: '高度',
- key: 'z',
- }
- ];
- sheet.columns = columns;
- let rows = ret.map(item=>{
- return item;
- });
- sheet.addRows( rows );
- return await workbook.xlsx.writeBuffer();
- }
- }
- module.exports = WeaponService;
|