'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;