logs.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. 'use strict';
  2. const RoleNames = require("../const").RoleNames;
  3. const Service = require('egg').Service;
  4. // const md5 = require('md5-node');
  5. const Excel = require("exceljs");
  6. class LogsService extends Service {
  7. async add( data ){
  8. let mysql = this.app.mysql;
  9. //data.add_time = mysql.literals.now;
  10. let {rid, tick, uid, text, result, num, flag} = data;
  11. let ret = await mysql.insert("train_records_logs", {rid, tick, uid, text, add_time: mysql.literals.now, });
  12. if( result != "0" ) {
  13. let stateItem = await mysql.query("select id from train_records_result where uid=?, rid=? limit 0,1", [uid, rid]);
  14. stateItem = stateItem[0];
  15. //{rid, tick, uid, text, add_time: mysql.literals.now, }
  16. if( flag == "0" ) //累加
  17. {
  18. if( stateItem ) {
  19. await mysql.query(`update train_records_result set value = value + ${num} where id=${stateItem.id}`);
  20. } else {
  21. await mysql.insert(`train_records_result`, {uid, rid, state:result, value:num});
  22. }
  23. } else if( flag == "1") {//重置
  24. if( stateItem ) {
  25. await mysql.query(`update train_records_result set value = ${num} where id=${stateItem.id}`);
  26. } else {
  27. await mysql.insert(`train_records_result`, {uid, rid, state:result, value:num});
  28. }
  29. }
  30. }
  31. return {id: ret.insertId};
  32. }
  33. async flyPosture( data) {
  34. let mysql = this.app.mysql;
  35. data.add_time = mysql.literals.now;
  36. console.log("flyPosture", data);
  37. let ret = await mysql.insert("train_records_fly", data);
  38. return {id: ret.insertId};
  39. }
  40. async flyState(data) {
  41. let mysql = this.app.mysql;
  42. data.add_time = mysql.literals.now;
  43. console.log("flyState", data);
  44. let ret = await mysql.insert("train_records_fly", data);
  45. return {id: ret.insertId};
  46. }
  47. async adminListLogs( query ) {
  48. let mysql = this.app.mysql;
  49. let pageNo = query.pageNo;
  50. let pageSize = query.pageSize;
  51. pageNo = parseInt( pageNo );
  52. pageSize = parseInt( pageSize );
  53. let rid = query.rid;
  54. let uids = query.uids;
  55. console.log( "uid", query );
  56. let ret = {records:[], total: 0, };
  57. let where = `where rid=${rid}`;
  58. if( uids != undefined ) {
  59. let ids = uids.split(",");
  60. let andFilters = [];
  61. ids.forEach(item=>{
  62. if( item != "" && item != undefined) {
  63. andFilters.push(`uid=${item}`);
  64. }
  65. })
  66. let f = andFilters.join(" or ");
  67. where = where + ` and (${f})`;
  68. }
  69. 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}`
  70. let total = await mysql.query(`select count(id) as count from train_records_logs ${where}`);
  71. ret.total = total[0].count;
  72. let data = await mysql.query( `${sql} limit ${(pageNo-1)*pageSize},${pageSize}`);
  73. ret.records = data;
  74. return ret;
  75. }
  76. async adminListFlyPosture( query ) {
  77. let mysql = this.app.mysql;
  78. let pageNo = query.pageNo;
  79. let pageSize = query.pageSize;
  80. pageNo = parseInt( pageNo );
  81. pageSize = parseInt( pageSize );
  82. let rid = query.rid;
  83. let uids = query.uids;
  84. let ret = {records:[], total: 0, };
  85. let where = `where rid=${rid}`;
  86. if( uids != undefined ) {
  87. let ids = uids.split(",");
  88. let andFilters = [];
  89. ids.forEach(item=>{
  90. if( item != "" && item != undefined) {
  91. andFilters.push(`uid=${item}`);
  92. }
  93. })
  94. let f = andFilters.join(" or ");
  95. where = where + ` and (${f})`;
  96. }
  97. 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}`
  98. let total = await mysql.query(`select count(id) as count from train_records_fly ${where}`);
  99. ret.total = total[0].count;
  100. let data = await mysql.query( `${sql} limit ${(pageNo-1)*pageSize},${pageSize}`);
  101. ret.records = data;
  102. return ret;
  103. }
  104. async adminListMessage( query ) {
  105. let mysql = this.app.mysql;
  106. let pageNo = query.pageNo;
  107. let pageSize = query.pageSize;
  108. pageNo = parseInt( pageNo );
  109. pageSize = parseInt( pageSize );
  110. let rid = query.rid;
  111. let uids = query.uids;
  112. console.log( "uid", query );
  113. let ret = {records:[], total: 0, };
  114. let where = `where rid=${rid}`;
  115. if( uids != undefined ) {
  116. let ids = uids.split(",");
  117. let andFilters = [];
  118. ids.forEach(item=>{
  119. if( item != "" && item != undefined) {
  120. andFilters.push(`from=${item}`);
  121. }
  122. })
  123. let f = andFilters.join(" or ");
  124. where = where + ` and (${f})`;
  125. }
  126. 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}`
  127. let total = await mysql.query(`select count(id) as count from train_records_message ${where}`);
  128. ret.total = total[0].count;
  129. let data = await mysql.query( `${sql} limit ${(pageNo-1)*pageSize},${pageSize}`);
  130. data.forEach(item=>{
  131. let cc = item.cc?item.cc.split(","):[];
  132. cc = cc.map( c=>{
  133. return RoleNames[ parseInt(c) ];
  134. })
  135. item.cc = cc.join(",");
  136. let to = item.to?item.to.split(","):[];
  137. to = to.map( c=>{
  138. return RoleNames[ parseInt(c) ];
  139. })
  140. item.to = to.join(",");
  141. });
  142. // console.log("==========", data);
  143. ret.records = data;
  144. return ret;
  145. }
  146. async exportXls(query) {
  147. console.log("exportXls==>", query );
  148. let type = query.type;
  149. let rid = query.rid;
  150. if( !rid ) throw "rid为空";
  151. let sql = "";
  152. if( type == 0 ) {//普通日志
  153. sql = `select id, tick, text, date_format(add_time,'%Y-%m-%d %H:%m:%s') as add_time from train_records_logs`
  154. } else if( type == 1) {//飞行日志
  155. 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`
  156. } else if( type == 2) {//作战文书
  157. sql = `select id, tick, \`from\`, title, \`to\`, date_format(add_time,'%Y-%m-%d %H:%m:%s') as add_time from train_records_message`
  158. }
  159. if( query.selections ) { //导出指定的内容,否则导出所有
  160. sql += ` where r.id in (${query.selections})`;
  161. } else if(query.uids ) {
  162. sql += ` where rid=${rid} and uid in(${query.uids})`;
  163. } else {
  164. sql += ` where rid=${rid}`;
  165. }
  166. let mysql = this.app.mysql;
  167. let ret = await mysql.query( sql );
  168. let workbook = new Excel.Workbook();
  169. let sheet = workbook.addWorksheet("训练日志列表", {
  170. // views: [{xSplit:1, ySplit:1}],
  171. pageSetup:{pageSize:9, orientation:'portrait', fitToPage:true, showGridLines:true, horizontalCentered:true},
  172. headerFooter:{
  173. firstHeader:"舰航仿真模拟训练日志列表",
  174. firstFooter:"舰航仿真模拟训练日志列表",
  175. oddFooter:"第 &P 页,共 &N页"
  176. }
  177. });
  178. let style = {alignment: {vertical:'middle', horizontal:'center'}};
  179. let columns = [
  180. {
  181. style,
  182. header: '#ID',
  183. key: 'id',
  184. width:8,
  185. },{
  186. style,
  187. header: '时刻',
  188. key: 'tick',
  189. width:10,
  190. },{
  191. style,
  192. width:20,
  193. header: '时间',
  194. key: 'add_time',
  195. }
  196. ];
  197. if( type == 0 ) {
  198. columns.push({
  199. style,
  200. width:20,
  201. header: '内容',
  202. key: 'text',
  203. });
  204. } else if( type == 1) {
  205. columns.push(
  206. {
  207. style,
  208. width:12,
  209. header: '经度',
  210. key: 'longtitude',
  211. }
  212. );
  213. columns.push(
  214. {
  215. style,
  216. width:12,
  217. header: '纬度',
  218. key: 'latitude',
  219. }
  220. );
  221. columns.push(
  222. {
  223. style,
  224. width:12,
  225. header: '高度',
  226. key: 'altitude',
  227. }
  228. );
  229. columns.push(
  230. {
  231. style,
  232. width:10,
  233. header: '速度',
  234. key: 'speed',
  235. }
  236. );
  237. } else {
  238. columns.push(
  239. {
  240. style,
  241. width:8,
  242. header: '发送者',
  243. key: 'from',
  244. }
  245. );
  246. columns.push(
  247. {
  248. style,
  249. width:25,
  250. header: '标题',
  251. key: 'title',
  252. }
  253. );
  254. columns.push(
  255. {
  256. style,
  257. width:30,
  258. header: '接收者',
  259. key: 'to',
  260. }
  261. );
  262. }
  263. sheet.columns = columns;
  264. let rows = ret.map(item=>{
  265. item.tick = Math.ceil(item.tick / 60) + "分钟";
  266. if( type == 2) {
  267. item.from = RoleNames[ parseInt(item.from) ];
  268. let tos = item.to.split(",");
  269. tos = tos.map(e=>{
  270. return RoleNames[ parseInt(e) ];
  271. });
  272. item.to = tos.join(",");
  273. }
  274. return item;
  275. });
  276. sheet.addRows( rows );
  277. return await workbook.xlsx.writeBuffer();
  278. }
  279. }
  280. module.exports = LogsService;