d_air.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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.* ,dep.cityname as departureCityName,tar.cityname as targetCityName from
  20. flight as f
  21. LEFT JOIN (select id,cityname from area ) as dep on dep.id = f.departureCity
  22. LEFT JOIN (select id,cityname from area ) as tar on tar.id = f.targetCity
  23. where f.departureCity = ? and f.targetCity = ?`;
  24. values.push(departureCity,targetCity);
  25. if(routeType){
  26. sql += ` and f.routerType = ?`;
  27. values.push(routeType);
  28. }
  29. if(endUnixTime){
  30. // 如果有结束时间,没有开始时间则默认添加
  31. if (!startUnixTime) startUnixTime=getUnixTimeStamp();
  32. sql += ` and f.sailingTime <= ?`;
  33. values.push(endUnixTime);
  34. }
  35. if(startUnixTime){
  36. sql += ` and f.sailingTime >= ?`;
  37. values.push(startUnixTime);
  38. }
  39. sql+=`;`
  40. return mysql.pq(sql,values);
  41. }
  42. /**
  43. * 获取航班票数
  44. * @param flightId
  45. * @returns {Promise<unknown>}
  46. */
  47. function flightTicks(flightId){
  48. let sql=``,values=[];
  49. sql=`select ff.*,count(t.flightId = ? or null) as pay
  50. from
  51. (select totalVotes from flight where id = ?) as ff,
  52. airTickets as t`;
  53. values.push(flightId,flightId);
  54. return mysql.pq(sql,values);
  55. }
  56. /**
  57. * 添加航班
  58. * @param flightName 航班名
  59. * @param airplaneCode 飞机代码
  60. * @param originalPrice 原始价格
  61. * @param currentPrice 当前价格
  62. * @param sailingTime 起飞时间
  63. * @param langdinTime 到站时间
  64. * @param totalVotes 机票数量
  65. * @param routeType 线路类型,国际or国内
  66. * @param departureCity 出发城市
  67. * @param targetCity 目标城市
  68. * @returns {Promise<unknown>}
  69. */
  70. function addFlight(flightName,
  71. airplaneCode,
  72. originalPrice,
  73. currentPrice,
  74. sailingTime,
  75. langdinTime,
  76. totalVotes,
  77. routeType,
  78. departureCity,
  79. targetCity){
  80. let sql = ``,values = [];
  81. sql = `insert into flight
  82. (
  83. flightName,airplaneCode,
  84. originalPrice,currentPrice,
  85. sailingTime,langdinTime,
  86. totalVotes,routeType,
  87. departureCity,targetCity
  88. ) values(
  89. ?,?,
  90. ?,?,
  91. ?,?,
  92. ?,?
  93. )`;
  94. values.push(...arguments);
  95. sql += ';'
  96. return mysql.pq(sql,values);
  97. }
  98. /**
  99. * 修改航班信息
  100. * @param flightId
  101. * @param flightName
  102. * @param airplaneCode
  103. * @param originalPrice
  104. * @param currentPrice
  105. * @param sailingTime
  106. * @param langdinTime
  107. * @param totalVotes
  108. * @param routeType
  109. * @param departureCity
  110. * @param targetCity
  111. * @returns {Promise<unknown>}
  112. */
  113. function updateFlight(
  114. flightId,
  115. flightName,
  116. airplaneCode,
  117. originalPrice,
  118. currentPrice,
  119. sailingTime,
  120. langdinTime,
  121. totalVotes,
  122. routeType,
  123. departureCity,
  124. targetCity){
  125. let sql=``,values=[];
  126. let _arguments = Array.from(arguments);
  127. // 判断如果所有参数都为空时抛出异常
  128. if(checkArgumentsIsEmpty(Array.from(arguments))){
  129. throw {rcode:code.notParam}
  130. }
  131. sql+=`update area set`
  132. //航班名
  133. if(flightName){
  134. sql+=' flightName = ?'
  135. values.push(flightName)
  136. }
  137. //机票代码
  138. if(airplaneCode){
  139. sql+=' airplaneCode = ?'
  140. values.push(airplaneCode)
  141. }
  142. //原始价格
  143. if(originalPrice){
  144. sql+=' originalPrice = ?'
  145. values.push(originalPrice)
  146. }
  147. // 当前价格
  148. if(currentPrice){
  149. sql+=' currentPrice = ?'
  150. values.push(currentPrice)
  151. }
  152. //起飞时间
  153. if(sailingTime){
  154. sql+=' sailingTime = ?'
  155. values.push(sailingTime)
  156. }
  157. // 登录时间
  158. if(langdinTime){
  159. sql+=' langdinTime = ?'
  160. values.push(langdinTime)
  161. }
  162. //票数
  163. if(totalVotes){
  164. sql+=' totalVotes = ?'
  165. values.push(totalVotes)
  166. }
  167. //航线类型
  168. if(routeType){
  169. sql+=' routeType = ?'
  170. values.push(routeType)
  171. }
  172. //出发城市
  173. if(departureCity){
  174. sql+=' departureCity = ?'
  175. values.push(departureCity)
  176. }
  177. // 目标城市
  178. if(targetCity){
  179. sql+=' targetCity = ?'
  180. values.push(targetCity)
  181. }
  182. sql += ` where id = ?;`
  183. values.push(flightId);
  184. return mysql.pq(sql,values);
  185. }
  186. function recommendFlight(){
  187. }