123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- const db_air = require('../database/d_air')
- const db_area = require('../database/d_area')
- const db_user = require('../database/d_user')
- const handle = require('../until/handle')
- const field = require('../maps/field')
- const codeMap = require('../maps/rcodeMap')
- const str_action = require('../until/string_action')
- let offsetTime = 7 * 24 * 60 * 60;
- async function searchFlight(departureCity,targetCity,flightState,startUnixTime,endUnixTime){
- let [err,result] = await handle(db_air.flightSearch(...arguments));
- if(err){throw err}
- return result;
- }
- async function flightList(routeType){
- let [err,result] = await handle(db_air.flightList(routeType));
- if(err){throw err}
- return result;
- }
- async function flightInfo(flightId){
- let [err,result] = await handle(db_air.flightInfo(flightId));
- if(err){throw err}
- if(!result.length){
- throw {rcode:codeMap.notFound,msg:'无法找到航班'}
- }
- let flight = result[0];
- flight.maxTotalVotes = parseInt(flight.col) * parseInt(flight.row);
-
- [err,result] = await handle(db_air.flightOrder(flightId));
- if(err){throw err}
- let sold = result.reduce((acc,val)=>{
-
- let ticketNum = parseInt(val.ticketNum);
- let refundTick = 0;
- if(val.refundTick){
- refundTick = val.refundTick.split(',').length;
- }
- return acc+(ticketNum - refundTick)
- },0);
- flight.pay = sold;
- return flight;
- }
- async function seatInfo(flightId){
- let err,result,flight,air,seat = {};
- [err,flight] = await handle(flightInfo(flightId));
- if(err){throw err}
-
- [err,air] = await handle(airInfo(flight.airId));
- if(err){throw err}
-
- seat.row = air.row;
- seat.col = air.col;
-
- [err,result] = await handle(db_user.flightTickSeat(flightId));
- if(err){throw err}
- seat.selecteds = result.map(val=>{
- return {
- id:val.id,
- row: val.row,
- col: val.col,
- }
- })
- return seat;
- }
- async function searchFlights(state,options,page,limie){
- console.log(options);
- let searchItems = {
- departureCity:options.departureCity,
- targetCity:options.targetCity,
- startTime:options.startTime?(options.startTime-0)/1000:((new Date().getTime()-0)/1000),
- endTime:options.endTime,
- flightState: state,
- routeType: options.routeType,
- isLate: options.isLate,
- startPrice: options.startPrice,
- endPrice: options.endPrice,
- }
- let [err,result] = await handle(db_air.searchFlights(searchItems));
- if(err){throw err}
- console.log(result)
- return result;
- }
- async function addFlight(
- flightName,airId,
- originalPrice,currentPrice,
- sailingTime,langdinTime,
- totalVotes,departureCity,targetCity){
- let err,result,departCityType,targetCityType,routerType;
-
- if(!flightName||!airId||!originalPrice||!currentPrice||!sailingTime||!langdinTime||!totalVotes||!departureCity||!targetCity){
- throw {rcode:codeMap.notParam,msg:``}
- }
-
- if(sailingTime >= langdinTime){
- throw {rcode:codeMap.customError,msg:`出发时间晚于到站时间`}
- }
-
- [err,departCityType] = await handle(db_area.cityType(departureCity));
- [err,targetCityType] = await handle(db_area.cityType(targetCity));
- if(!departCityType.length||!targetCityType.length){
- console.log('------error------')
- console.log(departCityType);
- console.log(targetCityType);
- throw {rcode:codeMap.customError,msg:`未知的起始城市`}
- }
- departCityType = departCityType[0].cityType;
- targetCityType = targetCityType[0].cityType;
- if(departCityType==field.cityType_domestic&&targetCityType==field.cityType_domestic){
- routerType=field.routeType_domestic;
- }else{
- routerType=field.routeType_international;
- }
- if(err){throw err}
- [err,result] = await handle(db_air.addFlight(
- flightName,
- airId,
- originalPrice,
- currentPrice,
- sailingTime,
- langdinTime,
- totalVotes,
- routerType,
- departureCity,
- targetCity
- ));
- if(err){throw err}
- return result;
- }
- async function updateFlight(flightId,updateOption){
- let err,result,departCityType,targetCityType,routerType;
- let updateOptions = {}
- console.log(flightId);
- console.log(updateOption);
-
- if(updateOption.departureCity){
- [err,departCityType] = await handle(db_area.cityType(updateOption.departureCity));
- if(err) throw err;
- departCityType = departCityType[0].cityType;
- if(departCityType){
-
- if(departCityType==field.cityType_international){
- routerType = field.routeType_international;
- }
- }
- }
- if(updateOption.targetCity){
- [err,targetCityType] = await handle(db_area.cityType(updateOption.departureCity));
- if(err) throw err;
- targetCityType = targetCityType[0].cityType;
- if(targetCityType) {
-
- if (targetCityType == field.cityType_international) {
- routerType = field.routeType_international;
- }
- }
- }
- if(routerType){
- updateOptions.routeType = routerType;
- }
- updateOptions.flightName = updateOption.flightName;
- updateOptions.airId = updateOption.airId;
- updateOptions.originalPrice = updateOption.originalPrice;
- updateOptions.currentPrice = updateOption.currentPrice;
- updateOptions.sailingTime = updateOption.sailingTime;
- updateOptions.langdinTime = updateOption.langdinTime;
- updateOptions.totalVotes = updateOption.totalVotes;
- updateOptions.departureCity = updateOption.departureCity;
- updateOptions.targetCity = updateOption.targetCity;
- updateOptions.flightState = updateOption.flightState;
- [err,result] = await handle(db_air.updateFlight(flightId,updateOptions));
- if(err) throw err;
- return result
- }
- async function news(nums){
- let result = {},sellFlights,wicketFlights;
- [err,wicketFlights] = await handle(db_air.wicketFlights(nums));
- if(err){throw err}
- [err,sellFlights] = await handle(db_air.sellFlights(nums));
- if(err){throw err}
- result.sellFlights = sellFlights;
- result.wicketFlights = wicketFlights;
- return result;
- }
- async function addAir(airCode,row,col){
- let [err,result] = await handle(db_air.addAir(airCode,row,col));
- if(err){throw err}
- return result;
- }
- async function airs(state){
- let [err,result] = await handle(db_air.airs(state));
- if(err){throw err}
- return result;
- }
- async function airInfo(airId){
- let [err,result] = await handle(db_air.airInfo(airId));
- if(err){throw err}
- if(result.length<1){throw {rcode:codeMap.notFound,msg:'无法找到对应飞机'}}
- return result[0];
- }
- async function updateAir(airId,updateParam){
- let param = {
- airCode:updateParam.airCode,
- col:updateParam.col,
- row:updateParam.row,
- }
- let [err,result] = await handle(db_air.updateAir(airId,param));
- if(err){throw err}
- return result;
- }
- async function setState(flightId,nextState){
- let err,flight,result;
-
- [err,flight] = await handle(flightInfo(flightId));
- if(err){throw err}
- if(flight.flightState != (nextState -1)){
- throw {rcode:codeMap.customError,msg:'不允许跳级修改'}
- }
- [err,result] = await handle(db_air.updateFlight(flightId,{
- flightState:nextState
- }))
- if(err){throw err}
- if(nextState == 2){
-
- console.log(`航班${flightId},开始检票`);
- }else if(nextState == 3){
-
- console.log(`航班${flightId},开始飞行`);
- }else if(nextState == 4){
-
-
- [err,result] = await handle(db_user.okOrder(flightId))
- if(err){throw err}
- }
- return result;
- }
- module.exports = {
- searchFlight,
- flightList,
- addFlight,
- updateFlight,
- flightInfo,
- news,
- searchFlights,
- addAir,
- airs,
- updateAir,
- seatInfo,
- setState
- }
|