c_flight.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. /**
  8. *
  9. * @param departureCity 出发城市
  10. * @param targetCity 目标城市
  11. * @param routeType 航线类型
  12. * @param startUnixTime 起飞时间开始
  13. * @param endUnixTime 起飞时间结束
  14. * @returns {Promise<*>}
  15. */
  16. async function searchFlight(departureCity,targetCity,routeType,startUnixTime,endUnixTime){
  17. let [err,result] = await handle(db_air.flightSearch(...arguments));
  18. if(err){throw err}
  19. return result;
  20. }
  21. /**
  22. * 航班列表
  23. * @param routeType 航班类型
  24. * @returns {Promise<*>}
  25. */
  26. async function flightList(routeType){
  27. let [err,result] = await handle(db_air.flightList(routeType));
  28. if(err){throw err}
  29. return result;
  30. }
  31. /**
  32. * 新增航班
  33. * @param flightName 航班代号
  34. * @param airCode 飞机代号
  35. * @param originalPrice 原始价格
  36. * @param currentPrice 当前价格
  37. * @param seilingTime 出发时间
  38. * @param langdingTime 落地时间
  39. * @param totalVotes 票数量
  40. * @param departureCity 出发城市id
  41. * @param targetCity 目标城市id
  42. * @returns {Promise<*>}
  43. */
  44. async function addFlight(
  45. flightName,airCode,
  46. originalPrice,currentPrice,
  47. seilingTime,langdingTime,
  48. totalVotes,departureCity,targetCity){
  49. let err,result,departCityType,targetCityType,routerType;
  50. // 检查参数
  51. if(!flightName||!airCode||!originalPrice||!currentPrice||!seilingTime||!langdingTime||!totalVotes||!departureCity||!targetCity){
  52. throw {rcode:codeMap.notParam,msg:``}
  53. }
  54. // 判断时间是否合法
  55. if(seilingTime >= langdingTime){
  56. throw {rcode:codeMap.customError,msg:`出发时间晚于到站时间`}
  57. }
  58. // 获取城市类型
  59. [err,departCityType] = await handle(db_area.cityType(departureCity));
  60. [err,targetCityType] = await handle(db_area.cityType(targetCity));
  61. if(!departCityType.length||!targetCityType.length){
  62. console.log('------error------')
  63. console.log(departCityType);
  64. console.log(targetCityType);
  65. throw {rcode:codeMap.customError,msg:`未知的起始城市`}
  66. }
  67. departCityType = departCityType[0].cityType;
  68. targetCityType = targetCityType[0].cityType;
  69. if(departCityType==field.cityType_domestic&&targetCityType==field.cityType_domestic){
  70. routerType=field.routeType_domestic;
  71. }else{
  72. routerType=field.routeType_international;
  73. }
  74. if(err){throw err}
  75. [err,result] = await handle(db_air.addFlight(
  76. flightName,
  77. airCode,
  78. originalPrice,
  79. currentPrice,
  80. seilingTime,
  81. langdingTime,
  82. totalVotes,
  83. routerType,
  84. departureCity,
  85. targetCity
  86. ));
  87. if(err){throw err}
  88. return result;
  89. }
  90. module.exports = {
  91. searchFlight,
  92. flightList,
  93. addFlight,
  94. }