c_flight.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // 管理航班
  2. const db_air = require('../database/d_air')
  3. const db_area = require('../database/d_area')
  4. const handle = require('../until/handle')
  5. const field = require('../maps/field')
  6. const codeMap = require('../maps/rcodeMap')
  7. const str_action = require('../until/string_action')
  8. let offsetTime = 7 * 24 * 60 * 60;
  9. /**
  10. * 用户查询指定出发时间的航班
  11. * @param departureCity 出发城市
  12. * @param targetCity 目标城市
  13. * @param flightState 航线类型
  14. * @param startUnixTime 起飞时间开始
  15. * @param endUnixTime 起飞时间结束
  16. * @returns {Promise<*>}
  17. */
  18. async function searchFlight(departureCity,targetCity,flightState,startUnixTime,endUnixTime){
  19. let [err,result] = await handle(db_air.flightSearch(...arguments));
  20. if(err){throw err}
  21. return result;
  22. }
  23. /**
  24. * 航班列表,所有航班列表
  25. * @param routeType 航班类型
  26. * @returns {Promise<*>}
  27. */
  28. async function flightList(routeType){
  29. let [err,result] = await handle(db_air.flightList(routeType));
  30. if(err){throw err}
  31. return result;
  32. }
  33. /**
  34. * 航班具体信息,用来给用户直接查看航班
  35. * @param flightId
  36. * @returns {Promise<void>}
  37. */
  38. async function flightInfo(flightId){
  39. let [err,result] = await handle(db_air.flightInfo(flightId));
  40. if(err){throw err}
  41. if(!result.length){
  42. throw {rcode:codeMap.notFound,msg:'无法找到航班'}
  43. }
  44. return result[0];
  45. }
  46. async function searchFlights(state,options,page,limie){
  47. console.log(options);
  48. let searchItems = {
  49. departureCity:options.departureCity,
  50. targetCity:options.targetCity,
  51. startTime:options.startTime?(options.startTime-0)/1000:((new Date().getTime()-0)/1000),
  52. endTime:options.endTime?(options.endTime-0)/1000:((new Date().getTime()-0)/1000)+offsetTime,
  53. flightState: state,
  54. routeType: options.routeType,
  55. isLate: options.isLate,
  56. startPrice: options.startPrice,
  57. endPrice: options.endPrice,
  58. }
  59. let [err,result] = await handle(db_air.searchFlights(searchItems));
  60. if(err){throw err}
  61. console.log(result)
  62. return result;
  63. }
  64. /**
  65. * 新增航班
  66. * @param flightName 航班代号
  67. * @param airCode 飞机代号
  68. * @param originalPrice 原始价格
  69. * @param currentPrice 当前价格
  70. * @param sailingTime 出发时间
  71. * @param langdinTime 落地时间
  72. * @param totalVotes 票数量
  73. * @param departureCity 出发城市id
  74. * @param targetCity 目标城市id
  75. * @returns {Promise<*>}
  76. */
  77. async function addFlight(
  78. flightName,airCode,
  79. originalPrice,currentPrice,
  80. sailingTime,langdinTime,
  81. totalVotes,departureCity,targetCity){
  82. let err,result,departCityType,targetCityType,routerType;
  83. // 检查参数
  84. if(!flightName||!airCode||!originalPrice||!currentPrice||!sailingTime||!langdinTime||!totalVotes||!departureCity||!targetCity){
  85. throw {rcode:codeMap.notParam,msg:``}
  86. }
  87. // 判断时间是否合法
  88. if(sailingTime >= langdinTime){
  89. throw {rcode:codeMap.customError,msg:`出发时间晚于到站时间`}
  90. }
  91. // 获取城市类型
  92. [err,departCityType] = await handle(db_area.cityType(departureCity));
  93. [err,targetCityType] = await handle(db_area.cityType(targetCity));
  94. if(!departCityType.length||!targetCityType.length){
  95. console.log('------error------')
  96. console.log(departCityType);
  97. console.log(targetCityType);
  98. throw {rcode:codeMap.customError,msg:`未知的起始城市`}
  99. }
  100. departCityType = departCityType[0].cityType;
  101. targetCityType = targetCityType[0].cityType;
  102. if(departCityType==field.cityType_domestic&&targetCityType==field.cityType_domestic){
  103. routerType=field.routeType_domestic;
  104. }else{
  105. routerType=field.routeType_international;
  106. }
  107. if(err){throw err}
  108. [err,result] = await handle(db_air.addFlight(
  109. flightName,
  110. airCode,
  111. originalPrice,
  112. currentPrice,
  113. sailingTime,
  114. langdinTime,
  115. totalVotes,
  116. routerType,
  117. departureCity,
  118. targetCity
  119. ));
  120. if(err){throw err}
  121. return result;
  122. }
  123. /**
  124. * 修改航班信息
  125. * @param flightId
  126. * @param updateOption
  127. * @returns {Promise<void>}
  128. */
  129. async function updateFlight(flightId,updateOption){
  130. let err,result,departCityType,targetCityType,routerType;
  131. let updateOptions = {}
  132. console.log(flightId);
  133. console.log(updateOption);
  134. // 如果修改了城市,那么直接修改对应的航班类型
  135. if(updateOption.departureCity){
  136. [err,departCityType] = await handle(db_area.cityType(updateOption.departureCity));
  137. if(err) throw err;
  138. departCityType = departCityType[0].cityType;
  139. if(departCityType){
  140. // 判断是否为国内城市
  141. if(departCityType==field.cityType_international){
  142. routerType = field.routeType_international;
  143. }
  144. }
  145. }
  146. if(updateOption.targetCity){
  147. [err,targetCityType] = await handle(db_area.cityType(updateOption.departureCity));
  148. if(err) throw err;
  149. targetCityType = targetCityType[0].cityType;
  150. if(targetCityType) {
  151. // 判断是否为国内城市
  152. if (targetCityType == field.cityType_international) {
  153. routerType = field.routeType_international;
  154. }
  155. }
  156. }
  157. if(routerType){
  158. updateOptions.routeType = routerType;
  159. }
  160. updateOptions.flightName = updateOption.flightName;
  161. updateOptions.airCode = updateOption.airCode;
  162. updateOptions.originalPrice = updateOption.originalPrice;
  163. updateOptions.currentPrice = updateOption.currentPrice;
  164. updateOptions.sailingTime = updateOption.sailingTime;
  165. updateOptions.langdinTime = updateOption.langdinTime;
  166. updateOptions.totalVotes = updateOption.totalVotes;
  167. updateOptions.departureCity = updateOption.departureCity;
  168. updateOptions.targetCity = updateOption.targetCity;
  169. updateOptions.flightState = updateOption.flightState;
  170. [err,result] = await handle(db_air.updateFlight(flightId,updateOptions));
  171. if(err) throw err;
  172. return result
  173. }
  174. /**
  175. * 航班相关新闻
  176. * @param nums 每种类型的数量
  177. * @returns {Promise<void>}
  178. */
  179. async function news(nums){
  180. let result = {},sellFlights,wicketFlights;
  181. [err,wicketFlights] = await handle(db_air.wicketFlights(nums));
  182. if(err){throw err}
  183. [err,sellFlights] = await handle(db_air.sellFlights(nums));
  184. if(err){throw err}
  185. result.sellFlights = sellFlights;
  186. result.wicketFlights = wicketFlights;
  187. return result;
  188. }
  189. module.exports = {
  190. searchFlight,
  191. flightList,
  192. addFlight,
  193. updateFlight,
  194. flightInfo,
  195. news,
  196. searchFlights
  197. }