d_air.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. const field = require('../maps/field');
  2. const mysql = require('./mysql');
  3. const until_time = require('../until/time');
  4. const {getUnixTimeStamp} = require("../until/time");
  5. const code = require("../maps/rcodeMap");
  6. const checkArgumentsIsEmpty = require("../until/checkArgumentsIsEmpty");
  7. // 查询指定城市航班
  8. /**
  9. * 查询相关航班信息
  10. * @param departureCity 除非城市id
  11. * @param targetCity 目标城市id
  12. * @param [routeType] 航班类型,国内或者国际
  13. * @param [startUnixTime] 出发时间开始,某个时间点之前
  14. * @param [endUnixTime] 出发时间截止,某个时间段内的
  15. * @returns {Promise | Promise<unknown>}
  16. */
  17. function flightSearch(departureCity,targetCity,routeType,startUnixTime,endUnixTime){
  18. let sql=``,values=[];
  19. sql = `select f.id,f.originalPrice,f.currentPrice,f.sailingTime,f.langdinTime ,dep.cityname as departureCityName,tar.cityname as targetCityName
  20. from
  21. flight as f
  22. LEFT JOIN (select id,cityname from area ) as dep on dep.id = f.departureCity
  23. LEFT JOIN (select id,cityname from area ) as tar on tar.id = f.targetCity;
  24. where f.departureCity = ? and f.targetCity = ?`;
  25. values.push(departureCity,targetCity);
  26. if(routeType){
  27. sql += ` and f.routerType = ?`;
  28. values.push(routeType);
  29. }
  30. if(endUnixTime){
  31. // 如果有结束时间,没有开始时间则默认添加
  32. if (!startUnixTime) startUnixTime=getUnixTimeStamp();
  33. sql += ` and f.sailingTime <= ?`;
  34. values.push(endUnixTime);
  35. }
  36. if(startUnixTime){
  37. sql += ` and f.sailingTime >= ?`;
  38. values.push(startUnixTime);
  39. }
  40. sql+=`;`
  41. return mysql.pq(sql,values);
  42. }
  43. /**
  44. * 显示所有航班列表,管理员级别
  45. * @param routeType
  46. * @returns {Promise | Promise<unknown>}
  47. */
  48. function flightList(){
  49. let sql=``,values=[];
  50. sql=`select f.* ,dep.cityname as departureCityName,tar.cityname as targetCityName
  51. from
  52. flight as f
  53. LEFT JOIN (select id,cityName from area ) as dep on dep.id = f.departureCity
  54. LEFT JOIN (select id,cityName from area ) as tar on tar.id = f.targetCity;`;
  55. return mysql.pq(sql,values);
  56. }
  57. /**
  58. * 航班具体信息
  59. * @param flightId
  60. * @returns {Promise<unknown>}
  61. */
  62. function flightInfo(flightId){
  63. let sql=``,values=[];
  64. sql+=`select ff.*,count(flightId = ? or null) as pay
  65. from
  66. (select * from flight where id = ?) as ff,
  67. airTickets;`
  68. values.push(flightId,flightId)
  69. return mysql.pq(sql,values);
  70. }
  71. /**
  72. * 获取航班票数
  73. * @param flightId
  74. * @returns {Promise<unknown>}
  75. */
  76. function flightTicks(flightId){
  77. let sql=``,values=[];
  78. sql=`select ff.*,count(t.flightId = ? or null) as pay
  79. from
  80. (select totalVotes from flight where id = ?) as ff,
  81. airTickets as t`;
  82. values.push(flightId,flightId);
  83. return mysql.pq(sql,values);
  84. }
  85. /**
  86. * 添加航班
  87. * @param flightName 航班名
  88. * @param airCode 飞机代码
  89. * @param originalPrice 原始价格
  90. * @param currentPrice 当前价格
  91. * @param sailingTime 起飞时间
  92. * @param langdinTime 到站时间
  93. * @param totalVotes 机票数量
  94. * @param routeType 线路类型,国际or国内
  95. * @param departureCity 出发城市
  96. * @param targetCity 目标城市
  97. * @returns {Promise<unknown>}
  98. */
  99. function addFlight(flightName,
  100. airCode,
  101. originalPrice,
  102. currentPrice,
  103. sailingTime,
  104. langdinTime,
  105. totalVotes,
  106. routeType,
  107. departureCity,
  108. targetCity){
  109. let sql = ``,values = [];
  110. sql = `insert into flight
  111. (
  112. flightName,airCode,
  113. originalPrice,currentPrice,
  114. sailingTime,langdinTime,
  115. totalVotes,routeType,
  116. departureCity,targetCity
  117. ) values(
  118. ?,?,
  119. ?,?,
  120. ?,?,
  121. ?,?
  122. )`;
  123. values.push(...arguments);
  124. sql += ';'
  125. return mysql.pq(sql,values);
  126. }
  127. /**
  128. * 修改航班信息
  129. * @param flightId
  130. * @param updateOptions
  131. * @returns {Promise<unknown>}
  132. */
  133. function updateFlight(flightId,updateOptions){
  134. let sql=``,values=[];
  135. let _arguments = Array.from(arguments);
  136. // 判断如果所有参数都为空时抛出异常
  137. sql+=`update from flight set`
  138. let keys = Object.keys(updateOptions);
  139. if(keys.length < 1){
  140. throw {rcode:code.notParam,msg:'没有修改项'}
  141. }
  142. for(let i = 0;i<keys.length;i++){
  143. let field = keys[i];
  144. if(i>0){sql+=','}
  145. sql+=`${field} = ?`
  146. values.push(updateOptions[field])
  147. }
  148. sql += ` where id = ?;`
  149. values.push(flightId);
  150. return mysql.pq(sql,values);
  151. }
  152. function recommendFlight(){
  153. }
  154. module.exports = {
  155. flightSearch,
  156. addFlight,
  157. flightTicks,
  158. updateFlight,
  159. flightList,
  160. flightInfo
  161. }