d_air.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 fields = require("../maps/field");
  7. const checkArgumentsIsEmpty = require("../until/checkArgumentsIsEmpty");
  8. const {loadFlights} = require("./d_recommend");
  9. // 查询指定城市航班
  10. /**
  11. * 获取飞机列表
  12. * @param [state] 飞机状态,默认为
  13. * @returns {Promise | Promise<unknown>}
  14. */
  15. function airs(state){
  16. let sql=``,values=[];
  17. sql = `select * from air`;
  18. if(state == 1 || state == 2){
  19. sql+= ` where state = ?`
  20. values.push(state)
  21. }
  22. sql+=';';
  23. return mysql.pq(sql,values);
  24. }
  25. /**
  26. * 新增飞机
  27. * @param airCode
  28. * @param row
  29. * @param col
  30. * @returns {Promise | Promise<unknown>}
  31. */
  32. function addAir(airCode,row,col){
  33. let sql=``,values=[];
  34. sql = `insert into air(airCode,\`row\`,col) values(?,?,?)`;
  35. sql+=';';
  36. values.push(...arguments);
  37. return mysql.pq(sql,values);
  38. }
  39. /**
  40. * 更新飞机
  41. * @param airId 飞机id
  42. * @param params 要修改的飞机信息
  43. * @returns {Promise | Promise<unknown>}
  44. */
  45. function updateAir(airId,params){
  46. let sql=`update air set`,values=[];
  47. let fields = Object.keys(params);
  48. fields = fields.filter(field=>params[field])
  49. if(fields.length<1){
  50. throw {rcode:code.notParam}
  51. }
  52. for(let field of fields) {
  53. if (!params[field]) {
  54. continue;
  55. }
  56. if(values.length>0){sql+=','}
  57. sql+=` \`${field}\` = ?`
  58. values.push(params[field])
  59. }
  60. sql+=` where id=?`;
  61. values.push(airId)
  62. return mysql.pq(sql,values);
  63. }
  64. /**
  65. * 查询相关航班信息
  66. * @param departureCity 除非城市id
  67. * @param targetCity 目标城市id
  68. * @param [flightState] 航班类型,国内或者国际
  69. * @param [startUnixTime] 出发时间开始,某个时间点之前
  70. * @param [endUnixTime] 出发时间截止,某个时间段内的
  71. * @returns {Promise | Promise<unknown>}
  72. */
  73. function flightSearch(departureCity,targetCity,flightState,startUnixTime,endUnixTime){
  74. let sql=``,values=[];
  75. sql = `select
  76. f.id,f.originalPrice,f.currentPrice,f.sailingTime,f.langdinTime ,
  77. air.airCode,air.row,air.col
  78. dep.cityname as departureCityName,tar.cityname as targetCityName
  79. from
  80. flight as f
  81. LEFT JOIN (select * from air ) as air on air.id = f.airId
  82. LEFT JOIN (select id,cityname from area ) as dep on dep.id = f.departureCity
  83. LEFT JOIN (select id,cityname from area ) as tar on tar.id = f.targetCity;
  84. where f.departureCity = ? and f.targetCity = ?`;
  85. values.push(departureCity,targetCity);
  86. if(flightState){
  87. sql += ` and f.flightState = ?`;
  88. values.push(flightState);
  89. }
  90. if(endUnixTime){
  91. // 如果有结束时间,没有开始时间则默认添加
  92. if (!startUnixTime) startUnixTime=getUnixTimeStamp();
  93. sql += ` and f.sailingTime <= ?`;
  94. values.push(endUnixTime);
  95. }
  96. if(startUnixTime){
  97. sql += ` and f.sailingTime >= ?`;
  98. values.push(startUnixTime);
  99. }
  100. sql+=`;`
  101. return mysql.pq(sql,values);
  102. }
  103. /**
  104. * 显示所有航班列表,管理员级别
  105. * @param routeType
  106. * @returns {Promise | Promise<unknown>}
  107. */
  108. function flightList(){
  109. let sql=``,values=[];
  110. sql=`select f.* ,air.airCode,air.row,air.col,dep.cityname as departureCityName,tar.cityname as targetCityName
  111. from
  112. flight as f
  113. LEFT JOIN (select * from air ) as air on air.id = f.airId
  114. LEFT JOIN (select id,cityName from area ) as dep on dep.id = f.departureCity
  115. LEFT JOIN (select id,cityName from area ) as tar on tar.id = f.targetCity;`;
  116. return mysql.pq(sql,values);
  117. }
  118. /**
  119. * 查询航班对应的订单,仅限新创建以及已经支付的,以及部分退票
  120. * @param flightId 航班id
  121. * @returns {Promise | Promise<unknown>}
  122. */
  123. function flightOrder(flightId){
  124. let sql=``,values=[];
  125. sql+=`select * from orders where flightId = ?
  126. and (payState = ? or payState = ? or payState = ?)`
  127. values.push(
  128. flightId,
  129. fields.payState_pay,
  130. fields.payState_create,
  131. fields.payState_rebates
  132. );
  133. return mysql.pq(sql,values);
  134. }
  135. /**
  136. * 航班具体信息
  137. * @param flightId
  138. * @returns {Promise<unknown>}
  139. */
  140. function flightInfo(flightId){
  141. let sql=``,values=[];
  142. sql+=`select f.*,
  143. air.airCode,air.row,air.col,
  144. dep.cityname as departureCityName,tar.cityname as targetCityName
  145. from
  146. (select * from flight where id = ?) as f
  147. LEFT JOIN (select * from air ) as air on air.id = f.airId
  148. LEFT JOIN (select id,cityName from area ) as dep on dep.id = f.departureCity
  149. LEFT JOIN (select id,cityName from area ) as tar on tar.id = f.targetCity`
  150. values.push(flightId)
  151. return mysql.pq(sql,values);
  152. }
  153. /**
  154. * 获取航班票数
  155. * @param flightId
  156. * @returns {Promise<unknown>}
  157. */
  158. function flightTicks(flightId){
  159. let sql=``,values=[];
  160. sql=`select ff.*,count(t.flightId = ? or null) as pay
  161. from
  162. (select totalVotes from flight where id = ?) as ff,
  163. airTickets as t
  164. where t.payState != '1' and t.payState != '4'`;
  165. values.push(flightId,flightId);
  166. return mysql.pq(sql,values);
  167. }
  168. /**
  169. * 添加航班
  170. * @param flightName 航班名
  171. * @param airCode 飞机代码
  172. * @param originalPrice 原始价格
  173. * @param currentPrice 当前价格
  174. * @param sailingTime 起飞时间
  175. * @param langdinTime 到站时间
  176. * @param totalVotes 机票数量
  177. * @param routeType 线路类型,国际or国内
  178. * @param departureCity 出发城市
  179. * @param targetCity 目标城市
  180. * @returns {Promise<unknown>}
  181. */
  182. function addFlight(flightName,
  183. airId,
  184. originalPrice,
  185. currentPrice,
  186. sailingTime,
  187. langdinTime,
  188. totalVotes,
  189. routeType,
  190. departureCity,
  191. targetCity){
  192. let sql = ``,values = [];
  193. sql = `insert into flight
  194. (flightName,airId,originalPrice,currentPrice,
  195. sailingTime,langdinTime,
  196. totalVotes,routeType,
  197. departureCity,targetCity
  198. ) values(?,?,?,?,?,?,?,?,?,?)`;
  199. values.push(...arguments);
  200. sql += ';'
  201. return mysql.pq(sql,values);
  202. }
  203. /**
  204. * 修改航班信息
  205. * @param flightId
  206. * @param updateOptions
  207. * @returns {Promise<unknown>}
  208. */
  209. function updateFlight(flightId,updateOptions){
  210. let sql=``,values=[];
  211. let _arguments = Array.from(arguments);
  212. // 判断如果所有参数都为空时抛出异常
  213. sql+=`update flight set `
  214. let keys = Object.keys(updateOptions);
  215. if(keys.length < 1){
  216. throw {rcode:code.notParam,msg:'没有修改项'}
  217. }
  218. for(let i = 0;i<keys.length;i++){
  219. let field = keys[i];
  220. if(!updateOptions[field]){continue;}
  221. console.log(i);
  222. if(values.length > 0){sql+=','}
  223. sql+=`${field} = ?`
  224. values.push(updateOptions[field])
  225. }
  226. sql += ` where id = ?;`
  227. values.push(flightId);
  228. return mysql.pq(sql,values);
  229. }
  230. function recommendFlight(){
  231. }
  232. /**
  233. *
  234. * @param searchItems
  235. * @param cityName
  236. * @param page
  237. * @param limit
  238. * @returns {Promise | Promise<unknown>}
  239. */
  240. function searchFlights(searchItems,cityName = true,page = 1,limit){
  241. let sql=`select`,values=[];
  242. if(cityName){
  243. sql+=' f.*,dep.cityname as departureCityName,tar.cityname as targetCityName'
  244. sql+=` from
  245. flight as f
  246. LEFT JOIN (select id,cityName from area ) as dep on dep.id = f.departureCity
  247. LEFT JOIN (select id,cityName from area ) as tar on tar.id = f.targetCity`
  248. }else{
  249. sql+=' f.*';
  250. sql+=` from
  251. flight as f`;
  252. }
  253. sql+=' where '
  254. // 根据类型
  255. let fields = Object.keys(searchItems);
  256. fields = fields.filter(field=>searchItems[field])
  257. if(fields.length<1){
  258. throw {rcode:code.notParam}
  259. }
  260. for(let field of fields){
  261. // console.log(`${field} : ${searchItems[field]}`)
  262. if(!searchItems[field]){
  263. continue;
  264. }
  265. if(values.length>=1)sql += ' and';
  266. if(field === 'startTime'){
  267. sql+=` f.sailingTime >= ?`
  268. }else if(field === 'endTime'){
  269. sql+=` f.sailingTime <= ?`
  270. }else if(field === 'startPrice'){
  271. sql+=` f.currentPrice >= ?`
  272. }else if(field === 'endPrice'){
  273. sql+=` f.currentPrice <= ?`
  274. }else{
  275. sql+=` f.${field} ${searchItems[field].action||'='} ?`
  276. }
  277. values.push(searchItems[field]);
  278. }
  279. sql+=' order by createTime desc'
  280. if(limit){
  281. sql+=` limit ?,?;`
  282. values.push((page-1)*limit,limit)
  283. }
  284. sql+=';'
  285. return mysql.pq(sql,values);
  286. }
  287. /**
  288. * 售票的航班信息
  289. * @param num
  290. * @returns {Promise<unknown>}
  291. */
  292. function sellFlights(num = 5){
  293. let sql=``,values=[];
  294. // 判断状态为
  295. sql+=`select f.id,f.currentPrice,f.sailingTime,f.langdinTime,f.flightState,dep.cityname as departureCityName,tar.cityname as targetCityName
  296. from
  297. flight as f
  298. LEFT JOIN (select id,cityName from area ) as dep on dep.id = f.departureCity
  299. LEFT JOIN (select id,cityName from area ) as tar on tar.id = f.targetCity
  300. where flightState = ? ORDER BY f.createTime desc limit 0,?;`
  301. values.push(fields.flightState_sail,num);
  302. return mysql.pq(sql,values);
  303. }
  304. /**
  305. * 检票中的航班信息
  306. * @param num
  307. * @returns {Promise<unknown>}
  308. */
  309. function wicketFlights(num = 5){
  310. let sql=``,values=[];
  311. // 判断状态为
  312. sql+=`select f.id,f.currentPrice,f.sailingTime,f.langdinTime,f.flightState,f.createTime,dep.cityname as departureCityName,tar.cityname as targetCityName
  313. from
  314. flight as f
  315. LEFT JOIN (select id,cityName from area ) as dep on dep.id = f.departureCity
  316. LEFT JOIN (select id,cityName from area ) as tar on tar.id = f.targetCity
  317. where flightState = ? ORDER BY f.sailingTime desc limit 0,?;`
  318. values.push(fields.flightState_wicket,num);
  319. return mysql.pq(sql,values);
  320. }
  321. module.exports = {
  322. flightSearch,
  323. addFlight,
  324. flightTicks,
  325. updateFlight,
  326. flightList,
  327. flightInfo,
  328. wicketFlights,
  329. searchFlights,
  330. sellFlights,
  331. flightOrder,
  332. airs,
  333. addAir,
  334. updateAir
  335. }