123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343 |
- 'use strict';
- const RoleNames = require("../const").RoleNames;
- const Service = require('egg').Service;
- // const md5 = require('md5-node');
- const Excel = require("exceljs");
- class LogsService extends Service {
- async add( data ){
- let mysql = this.app.mysql;
- //data.add_time = mysql.literals.now;
-
- let {rid, tick, uid, text, result, num, flag} = data;
- let ret = await mysql.insert("train_records_logs", {rid, tick, uid, text, add_time: mysql.literals.now, });
-
- if( result != "0" ) {
- let stateItem = await mysql.query("select id from train_records_result where uid=?, rid=? limit 0,1", [uid, rid]);
- stateItem = stateItem[0];
- //{rid, tick, uid, text, add_time: mysql.literals.now, }
- if( flag == "0" ) //累加
- {
- if( stateItem ) {
- await mysql.query(`update train_records_result set value = value + ${num} where id=${stateItem.id}`);
- } else {
- await mysql.insert(`train_records_result`, {uid, rid, state:result, value:num});
- }
- } else if( flag == "1") {//重置
- if( stateItem ) {
- await mysql.query(`update train_records_result set value = ${num} where id=${stateItem.id}`);
- } else {
- await mysql.insert(`train_records_result`, {uid, rid, state:result, value:num});
- }
- }
- }
- return {id: ret.insertId};
- }
- async flyPosture( data) {
- let mysql = this.app.mysql;
- data.add_time = mysql.literals.now;
- console.log("flyPosture", data);
- let ret = await mysql.insert("train_records_fly", data);
-
- return {id: ret.insertId};
- }
- async flyState(data) {
- let mysql = this.app.mysql;
- data.add_time = mysql.literals.now;
- console.log("flyState", data);
- let ret = await mysql.insert("train_records_fly", data);
- return {id: ret.insertId};
- }
- async adminListLogs( query ) {
- let mysql = this.app.mysql;
- let pageNo = query.pageNo;
- let pageSize = query.pageSize;
-
- pageNo = parseInt( pageNo );
- pageSize = parseInt( pageSize );
- let rid = query.rid;
- let uids = query.uids;
- console.log( "uid", query );
- let ret = {records:[], total: 0, };
- let where = `where rid=${rid}`;
- if( uids != undefined ) {
- let ids = uids.split(",");
-
- let andFilters = [];
-
- ids.forEach(item=>{
- if( item != "" && item != undefined) {
- andFilters.push(`uid=${item}`);
- }
- })
- let f = andFilters.join(" or ");
- where = where + ` and (${f})`;
- }
- let sql = `select id, text, tick, uid, rid, date_format(add_time,'%Y-%m-%d %H:%m:%s') as add_time from train_records_logs ${where}`
-
- let total = await mysql.query(`select count(id) as count from train_records_logs ${where}`);
- ret.total = total[0].count;
-
- let data = await mysql.query( `${sql} limit ${(pageNo-1)*pageSize},${pageSize}`);
- ret.records = data;
- return ret;
- }
- async adminListFlyPosture( query ) {
- let mysql = this.app.mysql;
- let pageNo = query.pageNo;
- let pageSize = query.pageSize;
-
- pageNo = parseInt( pageNo );
- pageSize = parseInt( pageSize );
- let rid = query.rid;
- let uids = query.uids;
-
- let ret = {records:[], total: 0, };
- let where = `where rid=${rid}`;
- if( uids != undefined ) {
- let ids = uids.split(",");
-
- let andFilters = [];
-
- ids.forEach(item=>{
- if( item != "" && item != undefined) {
- andFilters.push(`uid=${item}`);
- }
- })
- let f = andFilters.join(" or ");
- where = where + ` and (${f})`;
- }
- let sql = `select id, tick, uid, rid,longtitude,latitude,pitch,roll,yaw,altitude, date_format(add_time,'%Y-%m-%d %H:%m:%s') as add_time from train_records_fly ${where}`
-
- let total = await mysql.query(`select count(id) as count from train_records_fly ${where}`);
- ret.total = total[0].count;
-
- let data = await mysql.query( `${sql} limit ${(pageNo-1)*pageSize},${pageSize}`);
- ret.records = data;
-
- return ret;
- }
- async adminListMessage( query ) {
- let mysql = this.app.mysql;
- let pageNo = query.pageNo;
- let pageSize = query.pageSize;
-
- pageNo = parseInt( pageNo );
- pageSize = parseInt( pageSize );
- let rid = query.rid;
- let uids = query.uids;
- console.log( "uid", query );
- let ret = {records:[], total: 0, };
- let where = `where rid=${rid}`;
- if( uids != undefined ) {
- let ids = uids.split(",");
-
- let andFilters = [];
-
- ids.forEach(item=>{
- if( item != "" && item != undefined) {
- andFilters.push(`from=${item}`);
- }
- })
- let f = andFilters.join(" or ");
- where = where + ` and (${f})`;
- }
- let sql = `select id, tick, \`from\`, rid, title, auth, approval, level, secret, num, copy, depart, sendDate,template, content, \`cc\`, \`to\`, date_format(add_time,'%Y-%m-%d %H:%m:%s') as add_time from train_records_message ${where}`
-
- let total = await mysql.query(`select count(id) as count from train_records_message ${where}`);
- ret.total = total[0].count;
-
- let data = await mysql.query( `${sql} limit ${(pageNo-1)*pageSize},${pageSize}`);
- data.forEach(item=>{
- let cc = item.cc?item.cc.split(","):[];
- cc = cc.map( c=>{
- return RoleNames[ parseInt(c) ];
- })
- item.cc = cc.join(",");
- let to = item.to?item.to.split(","):[];
- to = to.map( c=>{
- return RoleNames[ parseInt(c) ];
- })
- item.to = to.join(",");
- });
- // console.log("==========", data);
- ret.records = data;
-
- return ret;
- }
- async exportXls(query) {
- console.log("exportXls==>", query );
- let type = query.type;
- let rid = query.rid;
- if( !rid ) throw "rid为空";
- let sql = "";
- if( type == 0 ) {//普通日志
- sql = `select id, tick, text, date_format(add_time,'%Y-%m-%d %H:%m:%s') as add_time from train_records_logs`
- } else if( type == 1) {//飞行日志
- sql = `select id, tick,longtitude,latitude,altitude,pitch,roll,yaw,speed, date_format(add_time,'%Y-%m-%d %H:%m:%s') as add_time from train_records_fly`
- } else if( type == 2) {//作战文书
- sql = `select id, tick, \`from\`, title, \`to\`, date_format(add_time,'%Y-%m-%d %H:%m:%s') as add_time from train_records_message`
- }
- if( query.selections ) { //导出指定的内容,否则导出所有
- sql += ` where r.id in (${query.selections})`;
- } else if(query.uids ) {
- sql += ` where rid=${rid} and uid in(${query.uids})`;
- } else {
- sql += ` where rid=${rid}`;
- }
- 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',
- width:8,
- },{
- style,
- header: '时刻',
- key: 'tick',
- width:10,
- },{
- style,
- width:20,
- header: '时间',
- key: 'add_time',
- }
- ];
- if( type == 0 ) {
- columns.push({
- style,
- width:20,
- header: '内容',
- key: 'text',
- });
- } else if( type == 1) {
- columns.push(
- {
- style,
- width:12,
- header: '经度',
- key: 'longtitude',
- }
- );
- columns.push(
- {
- style,
- width:12,
- header: '纬度',
- key: 'latitude',
- }
- );
- columns.push(
- {
- style,
- width:12,
- header: '高度',
- key: 'altitude',
- }
- );
- columns.push(
- {
- style,
- width:10,
- header: '速度',
- key: 'speed',
- }
- );
- } else {
- columns.push(
- {
- style,
- width:8,
- header: '发送者',
- key: 'from',
- }
- );
- columns.push(
- {
- style,
- width:25,
- header: '标题',
- key: 'title',
- }
- );
- columns.push(
- {
- style,
- width:30,
- header: '接收者',
- key: 'to',
- }
- );
- }
- sheet.columns = columns;
-
- let rows = ret.map(item=>{
- item.tick = Math.ceil(item.tick / 60) + "分钟";
- if( type == 2) {
- item.from = RoleNames[ parseInt(item.from) ];
- let tos = item.to.split(",");
- tos = tos.map(e=>{
- return RoleNames[ parseInt(e) ];
- });
- item.to = tos.join(",");
- }
- return item;
- });
- sheet.addRows( rows );
- return await workbook.xlsx.writeBuffer();
- }
- }
- module.exports = LogsService;
|