d_air.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 airplaneCode 飞机代码
  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. airplaneCode,
  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,airplaneCode,
  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 flightName
  131. * @param airplaneCode
  132. * @param originalPrice
  133. * @param currentPrice
  134. * @param sailingTime
  135. * @param langdinTime
  136. * @param totalVotes
  137. * @param routeType
  138. * @param departureCity
  139. * @param targetCity
  140. * @returns {Promise<unknown>}
  141. */
  142. function updateFlight(
  143. flightId,
  144. flightName,
  145. airplaneCode,
  146. originalPrice,
  147. currentPrice,
  148. sailingTime,
  149. langdinTime,
  150. totalVotes,
  151. routeType,
  152. departureCity,
  153. targetCity){
  154. let sql=``,values=[];
  155. let _arguments = Array.from(arguments);
  156. // 判断如果所有参数都为空时抛出异常
  157. if(checkArgumentsIsEmpty(Array.from(arguments))){
  158. throw {rcode:code.notParam}
  159. }
  160. sql+=`update area set`
  161. //航班名
  162. if(flightName){
  163. sql+=' flightName = ?'
  164. values.push(flightName)
  165. }
  166. //机票代码
  167. if(airplaneCode){
  168. sql+=' airplaneCode = ?'
  169. values.push(airplaneCode)
  170. }
  171. //原始价格
  172. if(originalPrice){
  173. sql+=' originalPrice = ?'
  174. values.push(originalPrice)
  175. }
  176. // 当前价格
  177. if(currentPrice){
  178. sql+=' currentPrice = ?'
  179. values.push(currentPrice)
  180. }
  181. //起飞时间
  182. if(sailingTime){
  183. sql+=' sailingTime = ?'
  184. values.push(sailingTime)
  185. }
  186. // 登录时间
  187. if(langdinTime){
  188. sql+=' langdinTime = ?'
  189. values.push(langdinTime)
  190. }
  191. //票数
  192. if(totalVotes){
  193. sql+=' totalVotes = ?'
  194. values.push(totalVotes)
  195. }
  196. //航线类型
  197. if(routeType){
  198. sql+=' routeType = ?'
  199. values.push(routeType)
  200. }
  201. //出发城市
  202. if(departureCity){
  203. sql+=' departureCity = ?'
  204. values.push(departureCity)
  205. }
  206. // 目标城市
  207. if(targetCity){
  208. sql+=' targetCity = ?'
  209. values.push(targetCity)
  210. }
  211. sql += ` where id = ?;`
  212. values.push(flightId);
  213. return mysql.pq(sql,values);
  214. }
  215. function recommendFlight(){
  216. }
  217. module.exports = {
  218. flightSearch,
  219. addFlight,
  220. flightTicks,
  221. updateFlight,
  222. flightList,
  223. flightInfo
  224. }