help.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. const Maps = ["舟山","博鳌机场","昌都机场"]
  4. const Weathers = ["晴天","雨天"];
  5. const Times = ["白天","黑夜"];
  6. const RoleNames = require("../const").RoleNames;
  7. const VehicleIds = {z8:0,z9:1,z10:2,m171:3,y12:4};
  8. const VehicleNames = ["直8","直9","直10","米171","运12"]
  9. const StatisName = require("../const").StatisName;
  10. class HelpService extends Service {
  11. async bigScreen(query) {
  12. let id = query.id;
  13. let mysql = this.app.mysql;
  14. let where = `where r.id=${id}`;
  15. let sql = `select r.id, r.title,r.state, date_format(r.start_time,'%Y-%m-%d %H:%m:%s') as start_time,
  16. date_format(r.end_time,'%Y-%m-%d %H:%m:%s') as end_time, r.config, users.name as teacher_name, r.teacher_id, r.duration, r.playback, t.subject from train_records as r left join users on users.id=r.teacher_id left join train_templates as t on t.id = r.template_id ${where}`
  17. let ret = await mysql.query( sql );
  18. let info = ret[0];
  19. info.duration = Math.floor( info.duration / 60 );
  20. let config = JSON.parse( info.config );
  21. // {
  22. // return {pilots: [], comm, teacher, others: otherIds, fixer, task: [...taskTimes, ...TaskSumCount]};
  23. // }
  24. // info.config = null;
  25. info.scene = config?config.scene.id:-1;
  26. let role = config?config.role:null;
  27. let urs = [];
  28. if( role ) {
  29. let n = role.length;
  30. let users = [];
  31. for(let i=0; i<n; i++) {
  32. if(role[i].user == -1) continue;
  33. users.push({index:i, user:role[i].user});
  34. // {
  35. // "role": 20,
  36. // "team": 1,
  37. // "group": 2,
  38. // "num": 2,
  39. // "vehicleid": 1,
  40. // "user": -1
  41. // }
  42. }
  43. if( users.length > 0 ) {
  44. let ids = users.map(item=>item.user);
  45. ids = ids.join(",");
  46. let userInfo = await mysql.query(`select id, realname,name,code from users where id in (${ids})`);
  47. let usermap = {};
  48. userInfo.forEach( item=>{
  49. usermap[item.id] = item;
  50. });
  51. users.forEach(item=>{
  52. let id = item.user;
  53. let uinfo = usermap[id];
  54. let uinfo2 = role[item.index];
  55. let r = {realname: uinfo.realname, code: uinfo.code, role: RoleNames[uinfo2.role], team:uinfo2.team, group: uinfo2.group, id: uinfo.id};
  56. if( uinfo2.vehicleid > -1) {
  57. r.vehicleid = VehicleNames[uinfo2.vehicleid];
  58. }
  59. urs.push(r);
  60. })
  61. }
  62. }
  63. info.users = urs;
  64. let comm = info;
  65. let teacher = {
  66. faultTotal:2,
  67. faultOk: 0,
  68. logs:["日志1","日志2"]
  69. };
  70. teacher.users = urs;
  71. let others = [{name: '战勤',logs:['日志1'], id:1}];
  72. let otherIds = [];
  73. const OtherRoleNames = ["总参谋长","支队长","融合力量","参谋终端","航管员","气象员","战勤","导调"];
  74. let n = urs.length;
  75. for( let i=0; i<n; i++) {
  76. if( OtherRoleNames.indexOf(urs[i].role) > -1 ) {
  77. otherIds.push({name:urs[i].role, logs: [], id: urs[i].id})
  78. }
  79. }
  80. //获取其他终端的日志
  81. n = otherIds.length;
  82. for( let i=0; i<n; i++) {
  83. otherIds[i].logs = await this.getLogs(id, otherIds[i].id)
  84. }
  85. //获取导调的飞行日志
  86. let url = `select text from train_records_logs where rid=${id} and uid=${info.teacher_id} order by tick limit 0, 10`;
  87. let logs = await mysql.query( url );
  88. teacher.logs = logs.map(item=>item.text);
  89. //获取飞行员的日志
  90. n = urs.length;
  91. let pilotsUsers = [];
  92. for( let i=0; i<n; i++) {
  93. if( urs[i].role == '飞行员' ) {
  94. let data = [];
  95. data.push({name:"编号", value:urs[i].code});
  96. data.push({name:"所属分队", value:`小队${urs[i].team}`});
  97. data.push({name:"所属分组", value:`小队${urs[i].group}`});
  98. data.push({name:"飞机型号", value:`${urs[i].vehicleid}`});
  99. data.push({name:"机务", value:`${urs[i].vehicleid}`});
  100. let crash = await mysql.query("select value from train_records_result where rid=? and uid=? and state=?", [id, urs[i].id, StatisName.indexOf("坠毁次数")]);
  101. crash = crash[0]?crash[0].value:0;
  102. data.push({name:"坠毁次数", value: crash});
  103. let logCount = await mysql.query("select count(id) as count from train_records_logs where rid=? and uid=?", [id, urs[i].id]);
  104. logCount = logCount[0]?logCount[0].count:0;
  105. data.push({name:"日志数量", value: logCount});
  106. let maxS = await mysql.query("select max(speed) as speed, max(altitude) as height from train_records_fly where rid=? and uid=?", [id, urs[i].id]);
  107. let h = maxS[0]?maxS[0].height:0;
  108. let sped = maxS[0]?maxS[0].speed:0;
  109. data.push({name:"最大高度", value: h});
  110. data.push({name:"最大速度", value: sped});
  111. pilotsUsers.push({
  112. name:urs[i].role,
  113. data:data,
  114. logs: await this.getLogs(id, urs[i].id),
  115. id: urs[i].id});
  116. }
  117. }
  118. //获取机务终端数据
  119. // let url = `select `
  120. let fixerNames = ["接收故障总数量", "成功排查故障次数"];
  121. n = fixerNames.length;
  122. let fixer = [];
  123. for( let i=0; i<n; i++) {
  124. let index = StatisName.indexOf( fixerNames[i] );
  125. let sql = `select sum(value) as value from train_records_result where rid=${id} and state=${index}`;
  126. let v = await mysql.query(sql);
  127. let vl = 0;
  128. if( v[0] && v[0].value ) vl = v[0].value;
  129. fixer.push({name: fixerNames[i], value: vl});
  130. }
  131. fixer.push({name:"失败排查故障次数",value: fixer[0].value - fixer[1].value});
  132. teacher.faultTotal = fixer[0].value;
  133. teacher.faultOk = fixer[1].value;
  134. //任务时间
  135. let TaskNames = ["灭火任务完成时间","舱门射击任务完成时间", "搜索任务完成时间", "抢险救援完成时间"];
  136. n = TaskNames.length;
  137. let taskStates = TaskNames.map(item=>{ return StatisName.indexOf(item) });
  138. let taskTimes = await mysql.query(`select state, max(value) as value from train_records_result where rid=${id} and state in(${taskStates.join(",")}) group by state`);
  139. taskTimes = taskTimes.map(item=>{
  140. let v = Math.ceil(item.value / 60) + "分钟";
  141. return `${StatisName[item.state]}:${v}`;
  142. });
  143. //任务次数
  144. let TaskSumCount = ["空中投送成功次数","悬停索降成功次数", "悬停索降失败次数", "机降成功次数","机降失败次数"];
  145. taskStates = TaskSumCount.map(item=>{ return StatisName.indexOf(item) });
  146. let taskT = await mysql.query(`select state, sum(value) as value from train_records_result where rid=${id} and state in(${taskStates.join(",")}) group by state`);
  147. TaskSumCount = [];
  148. if( taskT.length > 0 ) {
  149. TaskSumCount = taskT.map(item=>{
  150. let v = item.value;
  151. return `${StatisName[item.state]}:${v}`;
  152. });
  153. }
  154. return {pilots: pilotsUsers, comm, teacher, others: otherIds, fixer, task: [...taskTimes, ...TaskSumCount]};
  155. }
  156. async getLogs(rid, uid) {
  157. let mysql = this.app.mysql;
  158. let url = `select text from train_records_logs where rid=${rid} and uid=${uid} order by tick limit 0, 10`;
  159. let logs = await mysql.query( url );
  160. return logs.map(item=>item.text);
  161. }
  162. async getTwoLines(query) {
  163. let uid = query.uid;
  164. let rid = query.rid;
  165. if( !uid || !rid || uid == 'undefined') throw "ID为空!";
  166. let mysql = this.app.mysql;
  167. let url = `select tick, speed from train_records_fly where rid=${rid} and uid=${uid} order by tick limit 0, 2000`;
  168. let speeds = await mysql.query( url );
  169. url = `select tick, altitude from train_records_fly where rid=${rid} and uid=${uid} order by tick limit 0, 2000`;
  170. let heights = await mysql.query( url );
  171. return {speeds, heights};
  172. }
  173. async getNaviConfigLine(query) {
  174. let rid = query.rid;
  175. let mysql = this.app.mysql;
  176. let url = `select navi_config from train_records where id=${rid}`;
  177. let ret = await mysql.query( url );
  178. let config = ret[0].navi_config;
  179. if( config ) {
  180. try {
  181. config = JSON.parse( config );
  182. } catch (error) {
  183. console.error( error );
  184. config = {lines:[] }
  185. }
  186. } else {
  187. config = {lines:[] }
  188. }
  189. return config;
  190. }
  191. async getUserNaviLines(query) {
  192. let uid = query.uid;
  193. let rid = query.rid;
  194. if( !uid || !rid || uid == 'undefined') throw "ID为空!";
  195. let mysql = this.app.mysql;
  196. let url = `select tick, longtitude, latitude from train_records_fly where rid=${rid} and uid=${uid} order by tick limit 0, 5000`;
  197. let ret = await mysql.query( url );
  198. ret.map(item=>{
  199. return {lon: item.longtitude, lat: item.latitude}
  200. })
  201. return {lines: [ret] };
  202. }
  203. }
  204. module.exports = HelpService;